webgpu_caustics.html 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgpu - caustics</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. <meta property="og:title" content="three.js webgpu - caustics">
  8. <meta property="og:type" content="website">
  9. <meta property="og:url" content="https://threejs.org/examples/webgpu_caustics.html">
  10. <meta property="og:image" content="https://threejs.org/examples/screenshots/webgpu_caustics.jpg">
  11. <link type="text/css" rel="stylesheet" href="example.css">
  12. </head>
  13. <body>
  14. <div id="info">
  15. <a href="https://threejs.org/" target="_blank" rel="noopener" class="logo-link"></a>
  16. <div class="title-wrapper">
  17. <a href="https://threejs.org/" target="_blank" rel="noopener">three.js</a><span>Caustics</span>
  18. </div>
  19. <small>Realtime caustics effect.</small>
  20. </div>
  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. <script type="module">
  32. import * as THREE from 'three/webgpu';
  33. import { uniform, refract, div, positionViewDirection, positionLocal, normalView, texture, Fn, vec2, vec3, vec4 } from 'three/tsl';
  34. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  35. import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
  36. import { DRACOLoader } from 'three/addons/loaders/DRACOLoader.js';
  37. import { Inspector } from 'three/addons/inspector/Inspector.js';
  38. let camera, scene, renderer, controls;
  39. let gltf;
  40. init();
  41. async function init() {
  42. camera = new THREE.PerspectiveCamera( 25, window.innerWidth / window.innerHeight, 0.025, 5 );
  43. camera.position.set( - 0.5, 0.35, 0.2 );
  44. scene = new THREE.Scene();
  45. // light
  46. const spotLight = new THREE.SpotLight( 0xffffff, 1 );
  47. spotLight.position.set( .2, .3, .2 );
  48. spotLight.castShadow = true;
  49. spotLight.angle = Math.PI / 6;
  50. spotLight.penumbra = 1;
  51. spotLight.decay = 2;
  52. spotLight.distance = 0;
  53. spotLight.shadow.mapType = THREE.HalfFloatType; // For HDR Caustics
  54. spotLight.shadow.mapSize.width = 1024;
  55. spotLight.shadow.mapSize.height = 1024;
  56. spotLight.shadow.camera.near = .1;
  57. spotLight.shadow.camera.far = 1;
  58. spotLight.shadow.intensity = .95;
  59. scene.add( spotLight );
  60. // model / textures
  61. const dracoLoader = new DRACOLoader();
  62. gltf = ( await new GLTFLoader().setDRACOLoader( dracoLoader ).loadAsync( './models/gltf/duck.glb' ) ).scene;
  63. gltf.scale.setScalar( .5 );
  64. scene.add( gltf );
  65. const causticMap = new THREE.TextureLoader().load( './textures/opengameart/Caustic_Free.jpg' );
  66. causticMap.wrapS = causticMap.wrapT = THREE.RepeatWrapping;
  67. causticMap.colorSpace = THREE.SRGBColorSpace;
  68. // objects / material
  69. const duck = gltf.children[ 0 ];
  70. duck.material = new THREE.MeshPhysicalNodeMaterial();
  71. duck.material.side = THREE.DoubleSide;
  72. duck.material.transparent = true;
  73. duck.material.color = new THREE.Color( 0xFFD700 );
  74. duck.material.transmission = 1;
  75. duck.material.thickness = .25;
  76. duck.material.ior = 1.5;
  77. duck.material.metalness = 0;
  78. duck.material.roughness = .1;
  79. duck.castShadow = true;
  80. // tsl shader
  81. const causticOcclusion = uniform( 20 );
  82. duck.material.castShadowPositionNode = Fn( () => {
  83. // optional: add some distortion to the geometry shadow position if needed
  84. return positionLocal;
  85. } )();
  86. duck.material.castShadowNode = Fn( () => {
  87. const refractionVector = refract( positionViewDirection.negate(), normalView, div( 1.0, duck.material.ior ) ).normalize();
  88. const viewZ = normalView.z.pow( causticOcclusion );
  89. const textureUV = refractionVector.xy.mul( .6 );
  90. const causticColor = uniform( duck.material.color );
  91. const chromaticAberrationOffset = normalView.z.pow( - .9 ).mul( .004 );
  92. const causticProjection = vec3(
  93. texture( causticMap, textureUV.add( vec2( chromaticAberrationOffset.negate(), 0 ) ) ).r,
  94. texture( causticMap, textureUV.add( vec2( 0, chromaticAberrationOffset.negate() ) ) ).g,
  95. texture( causticMap, textureUV.add( vec2( chromaticAberrationOffset, chromaticAberrationOffset ) ) ).b
  96. );
  97. return causticProjection.mul( viewZ.mul( 25 ) ).add( viewZ ).mul( causticColor );
  98. } )();
  99. //
  100. const textureLoader = new THREE.TextureLoader();
  101. // glass
  102. const colorMap = textureLoader.load( 'textures/colors.png' );
  103. colorMap.wrapS = colorMap.wrapT = THREE.RepeatWrapping;
  104. colorMap.colorSpace = THREE.SRGBColorSpace;
  105. const glassMaterial = new THREE.MeshPhysicalNodeMaterial();
  106. glassMaterial.map = colorMap;
  107. glassMaterial.side = THREE.DoubleSide;
  108. glassMaterial.transparent = true;
  109. glassMaterial.color = new THREE.Color( 0xffffff );
  110. glassMaterial.transmission = 1;
  111. glassMaterial.ior = 1.5;
  112. glassMaterial.metalness = 0;
  113. glassMaterial.roughness = .1;
  114. glassMaterial.castShadowNode = vec4( texture( colorMap ).rgb, .8 );
  115. const glass = new THREE.Mesh( new THREE.PlaneGeometry( .2, .2 ), glassMaterial );
  116. glass.position.y = .1;
  117. glass.castShadow = true;
  118. glass.visible = false;
  119. scene.add( glass );
  120. // ground
  121. const map = textureLoader.load( 'textures/hardwood2_diffuse.jpg' );
  122. map.wrapS = map.wrapT = THREE.RepeatWrapping;
  123. map.repeat.set( 10, 10 );
  124. const geometry = new THREE.PlaneGeometry( 2, 2 );
  125. const material = new THREE.MeshStandardMaterial( { color: 0x999999, map } );
  126. const ground = new THREE.Mesh( geometry, material );
  127. ground.rotation.x = - Math.PI / 2;
  128. ground.receiveShadow = true;
  129. scene.add( ground );
  130. // renderer
  131. renderer = new THREE.WebGPURenderer( { antialias: true } );
  132. renderer.setPixelRatio( window.devicePixelRatio );
  133. renderer.setSize( window.innerWidth, window.innerHeight );
  134. renderer.setAnimationLoop( animate );
  135. renderer.shadowMap.enabled = true;
  136. renderer.shadowMap.transmitted = true;
  137. renderer.inspector = new Inspector();
  138. document.body.appendChild( renderer.domElement );
  139. // gui
  140. const gui = renderer.inspector.createParameters( 'Settings' );
  141. gui.add( causticOcclusion, 'value', 0, 20 ).name( 'caustic occlusion' );
  142. gui.addColor( duck.material, 'color' ).name( 'material color' );
  143. gui.add( { model: 'duck' }, 'model', [
  144. 'duck',
  145. 'glass'
  146. ] ).onChange( model => {
  147. duck.visible = glass.visible = false;
  148. if ( model === 'duck' ) {
  149. duck.visible = true;
  150. } else if ( model === 'glass' ) {
  151. glass.visible = true;
  152. }
  153. } );
  154. // controls
  155. controls = new OrbitControls( camera, renderer.domElement );
  156. controls.maxDistance = 3;
  157. controls.maxPolarAngle = Math.PI / 2;
  158. window.addEventListener( 'resize', onWindowResize );
  159. }
  160. function onWindowResize() {
  161. camera.aspect = window.innerWidth / window.innerHeight;
  162. camera.updateProjectionMatrix();
  163. renderer.setSize( window.innerWidth, window.innerHeight );
  164. }
  165. function animate() {
  166. for ( const mesh of gltf.children ) {
  167. mesh.rotation.y -= .01;
  168. }
  169. controls.update();
  170. renderer.render( scene, camera );
  171. }
  172. </script>
  173. </body>
  174. </html>
粤ICP备19079148号