|
| 1 | +<!DOCTYPE html> |
| 2 | +<html lang="en"> |
| 3 | + <head> |
| 4 | + <title>three.js webgpu - MeshBlend</title> |
| 5 | + <meta charset="utf-8"> |
| 6 | + <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0"> |
| 7 | + <link type="text/css" rel="stylesheet" href="example.css"> |
| 8 | + </head> |
| 9 | + <body> |
| 10 | + |
| 11 | + <div id="info"> |
| 12 | + <a href="https://threejs.org/" target="_blank" rel="noopener" class="logo-link"></a> |
| 13 | + |
| 14 | + <div class="title-wrapper"> |
| 15 | + <a href="https://threejs.org/" target="_blank" rel="noopener">three.js</a><span>MeshBlend</span> |
| 16 | + </div> |
| 17 | + |
| 18 | + <small>Mesh Blend. Screen space effect to blend objects.</small> |
| 19 | + </div> |
| 20 | + |
| 21 | + <script type="importmap"> |
| 22 | + { |
| 23 | + "imports": { |
| 24 | + "three": "../build/three.webgpu.js", |
| 25 | + "three/webgpu": "../build/three.webgpu.js", |
| 26 | + "three/tsl": "../build/three.tsl.js", |
| 27 | + "three/addons/": "./jsm/" |
| 28 | + } |
| 29 | + } |
| 30 | + </script> |
| 31 | + |
| 32 | + <script type="module"> |
| 33 | + |
| 34 | + import * as THREE from 'three/webgpu'; |
| 35 | + import { pass, mrt, output, normalView, diffuseColor, velocity, add, vec3, vec4, directionToColor, colorToDirection, sample, depth } from 'three/tsl'; |
| 36 | + import { meshblend } from 'three/addons/tsl/display/MeshBlendNode.js'; |
| 37 | + |
| 38 | + import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; |
| 39 | + |
| 40 | + import { Inspector } from 'three/addons/inspector/Inspector.js'; |
| 41 | + |
| 42 | + let camera, scene, renderer, postProcessing, controls, textureLoader, sphere1, sphere2, box, floor; |
| 43 | + |
| 44 | + init(); |
| 45 | + |
| 46 | + async function init() { |
| 47 | + |
| 48 | + camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 0.1, 100 ); |
| 49 | + camera.position.set( 0, 10, 30 ); |
| 50 | + |
| 51 | + scene = new THREE.Scene(); |
| 52 | + scene.background = new THREE.Color("#33334C"); |
| 53 | + |
| 54 | + renderer = new THREE.WebGPURenderer(); |
| 55 | + //renderer.setPixelRatio( window.devicePixelRatio ); // probably too costly for most hardware |
| 56 | + renderer.setSize( window.innerWidth, window.innerHeight ); |
| 57 | + renderer.setAnimationLoop( animate ); |
| 58 | + renderer.shadowMap.enabled = true; |
| 59 | + renderer.inspector = new Inspector(); |
| 60 | + document.body.appendChild( renderer.domElement ); |
| 61 | + |
| 62 | + // |
| 63 | + |
| 64 | + controls = new OrbitControls( camera, renderer.domElement ); |
| 65 | + controls.target.set( 0, 7, 0 ); |
| 66 | + controls.enablePan = true; |
| 67 | + controls.minDistance = 1; |
| 68 | + controls.maxDistance = 100; |
| 69 | + controls.update(); |
| 70 | + |
| 71 | + var directionalLight = new THREE.DirectionalLight(0xffffff); |
| 72 | + directionalLight.position.set(1, 1, 1); |
| 73 | + scene.add(directionalLight); |
| 74 | + // |
| 75 | + |
| 76 | + postProcessing = new THREE.PostProcessing( renderer ); |
| 77 | + |
| 78 | + const scenePass = pass( scene, camera ); |
| 79 | + scenePass.setMRT( mrt( { |
| 80 | + output: output, |
| 81 | + diffuseColor: diffuseColor, |
| 82 | + normal: directionToColor( normalView ), |
| 83 | + velocity: velocity |
| 84 | + } ) ); |
| 85 | + |
| 86 | + const scenePassColor = scenePass.getTextureNode( 'output' ); |
| 87 | + const scenePassDiffuse = scenePass.getTextureNode( 'diffuseColor' ).toInspector( 'Diffuse Color' ); |
| 88 | + const scenePassDepth = scenePass.getTextureNode( 'depth' ).toInspector( 'Depth', () => { |
| 89 | + |
| 90 | + return scenePass.getLinearDepthNode(); |
| 91 | + |
| 92 | + } ); |
| 93 | + |
| 94 | + const scenePassNormal = scenePass.getTextureNode( 'normal' ).toInspector( 'Normal' ); |
| 95 | + const scenePassVelocity = scenePass.getTextureNode( 'velocity' ).toInspector( 'Velocity' ); |
| 96 | + |
| 97 | + // bandwidth optimization |
| 98 | + |
| 99 | + const diffuseTexture = scenePass.getTexture( 'diffuseColor' ); |
| 100 | + diffuseTexture.type = THREE.UnsignedByteType; |
| 101 | + |
| 102 | + const normalTexture = scenePass.getTexture( 'normal' ); |
| 103 | + normalTexture.type = THREE.UnsignedByteType; |
| 104 | + |
| 105 | + const sceneNormal = sample( ( uv ) => { |
| 106 | + |
| 107 | + return colorToDirection( scenePassNormal.sample( uv ) ); |
| 108 | + |
| 109 | + } ); |
| 110 | + |
| 111 | + // Ambient light |
| 112 | + const ambientLight = new THREE.AmbientLight(0x404040, 2); |
| 113 | + scene.add( ambientLight ); |
| 114 | + |
| 115 | + textureLoader = new THREE.TextureLoader(); |
| 116 | + const texture1 = textureLoader.load( 'textures/brick_diffuse.jpg' ); |
| 117 | + texture1.colorSpace = THREE.SRGBColorSpace; |
| 118 | + const texture2 = textureLoader.load( 'textures/floors/FloorsCheckerboard_S_Diffuse.jpg' ); |
| 119 | + texture2.colorSpace = THREE.SRGBColorSpace; |
| 120 | + |
| 121 | + sphere1 = new THREE.Mesh(new THREE.SphereGeometry(3), new THREE.MeshPhongMaterial({ map: texture1 })); |
| 122 | + sphere1.position.set(5, 5, 0); |
| 123 | + scene.add(sphere1); |
| 124 | + |
| 125 | + sphere2 = new THREE.Mesh(new THREE.SphereGeometry(3), new THREE.MeshPhongMaterial({ color: "orange"})); |
| 126 | + sphere2.position.set(0, 5, 0); |
| 127 | + scene.add(sphere2); |
| 128 | + |
| 129 | + floor = new THREE.Mesh(new THREE.BoxGeometry(50, 1, 50), new THREE.MeshPhongMaterial({ map: texture2 })); |
| 130 | + scene.add(floor); |
| 131 | + floor.position.set(0, 2.5, 0); |
| 132 | + |
| 133 | + window.addEventListener( 'resize', onWindowResize ); |
| 134 | + |
| 135 | + const meshBlend = meshblend( scenePassColor, scenePassDepth, camera, scene ); |
| 136 | + postProcessing.outputNode = meshBlend; |
| 137 | + |
| 138 | + const gui = renderer.inspector.createParameters( 'MeshBlend settings' ); |
| 139 | + gui.add( meshBlend.kernelSize, 'value', 1, 20 ).step( 1 ).name( 'kernel size' ).onChange(() => postProcessing.needsUpdate = true); |
| 140 | + gui.add( meshBlend.blendFactor, 'value', 0.1, 10 ).step( 0.01 ).name( 'blend factor' ).onChange(() => { |
| 141 | + meshBlend.kernelRadius.value = 0.01 * meshBlend.blendFactor.value; |
| 142 | + meshBlend.depthFalloff.value = 0.0001 * meshBlend.blendFactor.value; |
| 143 | + postProcessing.needsUpdate = true; |
| 144 | + }); |
| 145 | + // gui.add( meshBlend.kernelRadius, 'value', 0.01, 1 ).step( 0.01 ).name( 'kernel radius' ).onChange(() => postProcessing.needsUpdate = true); |
| 146 | + // gui.add( meshBlend.depthFalloff, 'value', 0.001, 0.1 ).step( 0.01 ).name( 'depth falloff' ).onChange(() => postProcessing.needsUpdate = true); |
| 147 | + |
| 148 | + postProcessing.needsUpdate = true; |
| 149 | + } |
| 150 | + |
| 151 | + function onWindowResize() { |
| 152 | + |
| 153 | + const width = window.innerWidth; |
| 154 | + const height = window.innerHeight; |
| 155 | + |
| 156 | + camera.aspect = width / height; |
| 157 | + camera.updateProjectionMatrix(); |
| 158 | + |
| 159 | + renderer.setSize( width, height ); |
| 160 | + |
| 161 | + } |
| 162 | + |
| 163 | + function animate() { |
| 164 | + |
| 165 | + controls.update(); |
| 166 | + |
| 167 | + sphere2.position.x = Math.sin( Date.now() * 0.001 ) * 3; |
| 168 | + sphere1.position.y = 5 + Math.sin( Date.now() * 0.001 ) * 3; |
| 169 | + postProcessing.render(); |
| 170 | + |
| 171 | + } |
| 172 | + |
| 173 | + </script> |
| 174 | + </body> |
| 175 | +</html> |
0 commit comments