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