webgpu_caustics.html 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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. dracoLoader.setDecoderPath( 'jsm/libs/draco/' );
  63. dracoLoader.setDecoderConfig( { type: 'js' } );
  64. gltf = ( await new GLTFLoader().setDRACOLoader( dracoLoader ).loadAsync( './models/gltf/duck.glb' ) ).scene;
  65. gltf.scale.setScalar( .5 );
  66. scene.add( gltf );
  67. const causticMap = new THREE.TextureLoader().load( './textures/opengameart/Caustic_Free.jpg' );
  68. causticMap.wrapS = causticMap.wrapT = THREE.RepeatWrapping;
  69. causticMap.colorSpace = THREE.SRGBColorSpace;
  70. // objects / material
  71. const duck = gltf.children[ 0 ];
  72. duck.material = new THREE.MeshPhysicalNodeMaterial();
  73. duck.material.side = THREE.DoubleSide;
  74. duck.material.transparent = true;
  75. duck.material.color = new THREE.Color( 0xFFD700 );
  76. duck.material.transmission = 1;
  77. duck.material.thickness = .25;
  78. duck.material.ior = 1.5;
  79. duck.material.metalness = 0;
  80. duck.material.roughness = .1;
  81. duck.castShadow = true;
  82. // tsl shader
  83. const causticOcclusion = uniform( 20 );
  84. duck.material.castShadowPositionNode = Fn( () => {
  85. // optional: add some distortion to the geometry shadow position if needed
  86. return positionLocal;
  87. } )();
  88. duck.material.castShadowNode = Fn( () => {
  89. const refractionVector = refract( positionViewDirection.negate(), normalView, div( 1.0, duck.material.ior ) ).normalize();
  90. const viewZ = normalView.z.pow( causticOcclusion );
  91. const textureUV = refractionVector.xy.mul( .6 );
  92. const causticColor = uniform( duck.material.color );
  93. const chromaticAberrationOffset = normalView.z.pow( - .9 ).mul( .004 );
  94. const causticProjection = vec3(
  95. texture( causticMap, textureUV.add( vec2( chromaticAberrationOffset.negate(), 0 ) ) ).r,
  96. texture( causticMap, textureUV.add( vec2( 0, chromaticAberrationOffset.negate() ) ) ).g,
  97. texture( causticMap, textureUV.add( vec2( chromaticAberrationOffset, chromaticAberrationOffset ) ) ).b
  98. );
  99. return causticProjection.mul( viewZ.mul( 25 ) ).add( viewZ ).mul( causticColor );
  100. } )();
  101. //
  102. const textureLoader = new THREE.TextureLoader();
  103. // glass
  104. const colorMap = textureLoader.load( 'textures/colors.png' );
  105. colorMap.wrapS = colorMap.wrapT = THREE.RepeatWrapping;
  106. colorMap.colorSpace = THREE.SRGBColorSpace;
  107. const glassMaterial = new THREE.MeshPhysicalNodeMaterial();
  108. glassMaterial.map = colorMap;
  109. glassMaterial.side = THREE.DoubleSide;
  110. glassMaterial.transparent = true;
  111. glassMaterial.color = new THREE.Color( 0xffffff );
  112. glassMaterial.transmission = 1;
  113. glassMaterial.ior = 1.5;
  114. glassMaterial.metalness = 0;
  115. glassMaterial.roughness = .1;
  116. glassMaterial.castShadowNode = vec4( texture( colorMap ).rgb, .8 );
  117. const glass = new THREE.Mesh( new THREE.PlaneGeometry( .2, .2 ), glassMaterial );
  118. glass.position.y = .1;
  119. glass.castShadow = true;
  120. glass.visible = false;
  121. scene.add( glass );
  122. // ground
  123. const map = textureLoader.load( 'textures/hardwood2_diffuse.jpg' );
  124. map.wrapS = map.wrapT = THREE.RepeatWrapping;
  125. map.repeat.set( 10, 10 );
  126. const geometry = new THREE.PlaneGeometry( 2, 2 );
  127. const material = new THREE.MeshStandardMaterial( { color: 0x999999, map } );
  128. const ground = new THREE.Mesh( geometry, material );
  129. ground.rotation.x = - Math.PI / 2;
  130. ground.receiveShadow = true;
  131. scene.add( ground );
  132. // renderer
  133. renderer = new THREE.WebGPURenderer( { antialias: true } );
  134. renderer.setPixelRatio( window.devicePixelRatio );
  135. renderer.setSize( window.innerWidth, window.innerHeight );
  136. renderer.setAnimationLoop( animate );
  137. renderer.shadowMap.enabled = true;
  138. renderer.shadowMap.transmitted = true;
  139. renderer.inspector = new Inspector();
  140. document.body.appendChild( renderer.domElement );
  141. // gui
  142. const gui = renderer.inspector.createParameters( 'Settings' );
  143. gui.add( causticOcclusion, 'value', 0, 20 ).name( 'caustic occlusion' );
  144. gui.addColor( duck.material, 'color' ).name( 'material color' );
  145. gui.add( { model: 'duck' }, 'model', [
  146. 'duck',
  147. 'glass'
  148. ] ).onChange( model => {
  149. duck.visible = glass.visible = false;
  150. if ( model === 'duck' ) {
  151. duck.visible = true;
  152. } else if ( model === 'glass' ) {
  153. glass.visible = true;
  154. }
  155. } );
  156. // controls
  157. controls = new OrbitControls( camera, renderer.domElement );
  158. controls.maxDistance = 3;
  159. controls.maxPolarAngle = Math.PI / 2;
  160. window.addEventListener( 'resize', onWindowResize );
  161. }
  162. function onWindowResize() {
  163. camera.aspect = window.innerWidth / window.innerHeight;
  164. camera.updateProjectionMatrix();
  165. renderer.setSize( window.innerWidth, window.innerHeight );
  166. }
  167. function animate() {
  168. for ( const mesh of gltf.children ) {
  169. mesh.rotation.y -= .01;
  170. }
  171. controls.update();
  172. renderer.render( scene, camera );
  173. }
  174. </script>
  175. </body>
  176. </html>
粤ICP备19079148号