You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

215 lines
6.7 KiB

<!DOCTYPE html>
<html lang="en">
<head>
<title>three.js webgpu - cubemap adjustments</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
<link type="text/css" rel="stylesheet" href="main.css">
</head>
<body>
<div id="info">
<a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - Env. Adjustments<br />
Battle Damaged Sci-fi Helmet by
<a href="https://sketchfab.com/theblueturtle_" target="_blank" rel="noopener">theblueturtle_</a><br />
</div>
<!-- Import maps polyfill -->
<!-- Remove this when import maps will be widely supported -->
<script async src="https://unpkg.com/es-module-shims@1.3.6/dist/es-module-shims.js"></script>
<script type="importmap">
{
"imports": {
"three": "../build/three.module.js",
"three/addons/": "./jsm/",
"three/nodes": "./jsm/nodes/Nodes.js"
}
}
</script>
<script type="module">
import * as THREE from 'three';
import * as Nodes from 'three/nodes';
import { uniform, mix, cubeTexture, mul, reference, add, positionWorld, normalWorld, modelWorldMatrix, transformDirection, clamp, saturation, hue, reflectVector, context } from 'three/nodes';
import WebGPU from 'three/addons/capabilities/WebGPU.js';
import WebGPURenderer from 'three/addons/renderers/webgpu/WebGPURenderer.js';
import { RGBMLoader } from 'three/addons/loaders/RGBMLoader.js';
import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
let camera, scene, renderer;
init();
function init() {
if ( WebGPU.isAvailable() === false ) {
document.body.appendChild( WebGPU.getErrorMessage() );
throw new Error( 'No WebGPU support' );
}
const container = document.createElement( 'div' );
document.body.appendChild( container );
const initialDistance = 2;
camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 0.25, 20 );
camera.position.set( - 1.8 * initialDistance, 0.6 * initialDistance, 2.7 * initialDistance );
scene = new THREE.Scene();
// cube textures
const rgbmUrls = [ 'px.png', 'nx.png', 'py.png', 'ny.png', 'pz.png', 'nz.png' ];
const cube1Texture = new RGBMLoader()
.setMaxRange( 16 )
.setPath( './textures/cube/pisaRGBM16/' )
.loadCubemap( rgbmUrls );
cube1Texture.generateMipmaps = true;
cube1Texture.minFilter = THREE.LinearMipmapLinearFilter;
const cube2Urls = [ 'posx.jpg', 'negx.jpg', 'posy.jpg', 'negy.jpg', 'posz.jpg', 'negz.jpg' ];
const cube2Texture = new THREE.CubeTextureLoader()
.setPath( './textures/cube/Park2/' )
.load( cube2Urls );
cube2Texture.generateMipmaps = true;
cube2Texture.minFilter = THREE.LinearMipmapLinearFilter;
// nodes and environment
const adjustments = {
mix: 0,
procedural: 0,
intensity: 1,
hue: 0,
saturation: 1
};
const mixNode = reference( 'mix', 'float', adjustments );
const proceduralNode = reference( 'procedural', 'float', adjustments );
const intensityNode = reference( 'intensity', 'float', adjustments );
const hueNode = reference( 'hue', 'float', adjustments );
const saturationNode = reference( 'saturation', 'float', adjustments );
const rotateY1Matrix = new THREE.Matrix4();
const rotateY2Matrix = new THREE.Matrix4();
const getEnvironmentNode = ( reflectNode ) => {
const custom1UV = mul( reflectNode.xyz, uniform( rotateY1Matrix ) );
const custom2UV = mul( reflectNode.xyz, uniform( rotateY2Matrix ) );
const mixCubeMaps = mix( cubeTexture( cube1Texture, custom1UV ), cubeTexture( cube2Texture, custom2UV ), clamp( add( positionWorld.y, mixNode ) ) );
const proceduralEnv = mix( mixCubeMaps, normalWorld, proceduralNode );
const intensityFilter = mul( proceduralEnv, intensityNode );
const hueFilter = hue( intensityFilter, hueNode );
return saturation( hueFilter, saturationNode );
};
const blurNode = uniform( 0 );
scene.environmentNode = getEnvironmentNode( reflectVector );
scene.backgroundNode = context( getEnvironmentNode( transformDirection( positionWorld, modelWorldMatrix ) ), {
levelNode: blurNode // @TODO: currently it uses mipmaps value, I think it should be replaced for [0,1]
} );
// scene objects
const loader = new GLTFLoader().setPath( 'models/gltf/DamagedHelmet/glTF/' );
loader.load( 'DamagedHelmet.gltf', function ( gltf ) {
scene.add( gltf.scene );
} );
const sphereGeometry = new THREE.SphereGeometry( .5, 64, 32 );
const sphereRightView = new THREE.Mesh( sphereGeometry, new THREE.MeshStandardMaterial( { roughness: 0, metalness: 1 } ) );
sphereRightView.position.x += 2;
const sphereLeftView = new THREE.Mesh( sphereGeometry, new THREE.MeshStandardMaterial( { roughness: 1, metalness: 1 } ) );
sphereLeftView.position.x -= 2;
scene.add( sphereLeftView );
scene.add( sphereRightView );
// renderer and controls
renderer = new WebGPURenderer();
renderer.setPixelRatio( window.devicePixelRatio );
renderer.setSize( window.innerWidth, window.innerHeight );
renderer.toneMappingNode = new Nodes.ToneMappingNode( THREE.LinearToneMapping, 1 );
renderer.outputEncoding = THREE.sRGBEncoding;
renderer.setAnimationLoop( render );
container.appendChild( renderer.domElement );
const controls = new OrbitControls( camera, renderer.domElement );
controls.minDistance = 2;
controls.maxDistance = 10;
window.addEventListener( 'resize', onWindowResize );
// gui
const gui = new GUI();
gui.add( { blurBackground: blurNode.value }, 'blurBackground', 0, 10, 0.01 ).onChange( value => {
blurNode.value = value;
} );
gui.add( { offsetCube1: 0 }, 'offsetCube1', 0, Math.PI * 2, 0.01 ).onChange( value => {
rotateY1Matrix.makeRotationY( value );
} );
gui.add( { offsetCube2: 0 }, 'offsetCube2', 0, Math.PI * 2, 0.01 ).onChange( value => {
rotateY2Matrix.makeRotationY( value );
} );
gui.add( adjustments, 'mix', - 1, 2, 0.01 );
gui.add( adjustments, 'procedural', 0, 1, 0.01 );
gui.add( adjustments, 'intensity', 0, 5, 0.01 );
gui.add( adjustments, 'hue', 0, Math.PI * 2, 0.01 );
gui.add( adjustments, 'saturation', 0, 2, 0.01 );
}
function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize( window.innerWidth, window.innerHeight );
}
//
function render() {
renderer.render( scene, camera );
}
</script>
</body>
</html>