webgpu_caustics.html 7.1 KB

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