1
0

webgpu_lights_projector.html 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgpu - projector light</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>Projector Light</span>
  14. </div>
  15. <small>
  16. Projector light with procedural caustics, video and texture projection.
  17. </small>
  18. </div>
  19. <script type="importmap">
  20. {
  21. "imports": {
  22. "three": "../build/three.webgpu.js",
  23. "three/webgpu": "../build/three.webgpu.js",
  24. "three/tsl": "../build/three.tsl.js",
  25. "three/addons/": "./jsm/"
  26. }
  27. }
  28. </script>
  29. <video id="video" loop muted crossOrigin="anonymous" playsinline style="display:none">
  30. <source src="textures/sintel.ogv" type='video/ogg; codecs="theora, vorbis"'>
  31. <source src="textures/sintel.mp4" type='video/mp4; codecs="avc1.42E01E, mp4a.40.2"'>
  32. </video>
  33. <script type="module">
  34. import * as THREE from 'three/webgpu';
  35. import { Fn, color, mx_worley_noise_float, time } from 'three/tsl';
  36. import { Inspector } from 'three/addons/inspector/Inspector.js';
  37. import { PLYLoader } from 'three/addons/loaders/PLYLoader.js';
  38. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  39. let renderer, scene, camera;
  40. let projectorLight, lightHelper;
  41. init();
  42. function init() {
  43. // Renderer
  44. renderer = new THREE.WebGPURenderer( { antialias: true } );
  45. renderer.setPixelRatio( window.devicePixelRatio );
  46. renderer.setSize( window.innerWidth, window.innerHeight );
  47. renderer.setAnimationLoop( animate );
  48. renderer.inspector = new Inspector();
  49. document.body.appendChild( renderer.domElement );
  50. renderer.shadowMap.enabled = true;
  51. renderer.shadowMap.type = THREE.PCFSoftShadowMap;
  52. renderer.toneMapping = THREE.ACESFilmicToneMapping;
  53. renderer.toneMappingExposure = 1;
  54. scene = new THREE.Scene();
  55. camera = new THREE.PerspectiveCamera( 40, window.innerWidth / window.innerHeight, 0.1, 100 );
  56. camera.position.set( 7, 4, 1 );
  57. // Controls
  58. const controls = new OrbitControls( camera, renderer.domElement );
  59. controls.minDistance = 2;
  60. controls.maxDistance = 10;
  61. controls.maxPolarAngle = Math.PI / 2;
  62. controls.target.set( 0, 1, 0 );
  63. controls.update();
  64. // Textures
  65. const loader = new THREE.TextureLoader().setPath( 'textures/' );
  66. // Lights
  67. const causticEffect = Fn( ( [ projectorUV ] ) => {
  68. const waterLayer0 = mx_worley_noise_float( projectorUV.mul( 10 ).add( time ) );
  69. const caustic = waterLayer0.mul( color( 0x5abcd8 ) ).mul( 2 );
  70. return caustic;
  71. } );
  72. const ambient = new THREE.HemisphereLight( 0xffffff, 0x8d8d8d, 0.15 );
  73. scene.add( ambient );
  74. projectorLight = new THREE.ProjectorLight( 0xffffff, 100 );
  75. projectorLight.colorNode = causticEffect;
  76. projectorLight.position.set( 2.5, 5, 2.5 );
  77. projectorLight.angle = Math.PI / 6;
  78. projectorLight.penumbra = 1;
  79. projectorLight.decay = 2;
  80. projectorLight.distance = 0;
  81. projectorLight.castShadow = true;
  82. projectorLight.shadow.mapSize.width = 1024;
  83. projectorLight.shadow.mapSize.height = 1024;
  84. projectorLight.shadow.camera.near = 1;
  85. projectorLight.shadow.camera.far = 10;
  86. projectorLight.shadow.focus = 1;
  87. scene.add( projectorLight );
  88. lightHelper = new THREE.SpotLightHelper( projectorLight );
  89. scene.add( lightHelper );
  90. //
  91. const geometry = new THREE.PlaneGeometry( 200, 200 );
  92. const material = new THREE.MeshLambertMaterial( { color: 0xbcbcbc } );
  93. const mesh = new THREE.Mesh( geometry, material );
  94. mesh.position.set( 0, - 1, 0 );
  95. mesh.rotation.x = - Math.PI / 2;
  96. mesh.receiveShadow = true;
  97. scene.add( mesh );
  98. // Models
  99. new PLYLoader().load( 'models/ply/binary/Lucy100k.ply', function ( geometry ) {
  100. geometry.scale( 0.0024, 0.0024, 0.0024 );
  101. geometry.computeVertexNormals();
  102. const material = new THREE.MeshLambertMaterial();
  103. const mesh = new THREE.Mesh( geometry, material );
  104. mesh.rotation.y = - Math.PI / 2;
  105. mesh.position.y = 0.8;
  106. mesh.castShadow = true;
  107. mesh.receiveShadow = true;
  108. scene.add( mesh );
  109. } );
  110. window.addEventListener( 'resize', onWindowResize );
  111. // GUI
  112. const gui = renderer.inspector.createParameters( 'Projector Light' );
  113. const params = {
  114. type: 'procedural',
  115. color: projectorLight.color.getHex(),
  116. intensity: projectorLight.intensity,
  117. distance: projectorLight.distance,
  118. angle: projectorLight.angle,
  119. penumbra: projectorLight.penumbra,
  120. decay: projectorLight.decay,
  121. focus: projectorLight.shadow.focus,
  122. shadows: true,
  123. };
  124. let videoTexture, mapTexture;
  125. gui.add( params, 'type', [ 'procedural', 'video', 'texture' ] ).onChange( function ( val ) {
  126. projectorLight.colorNode = null;
  127. projectorLight.map = null;
  128. if ( val === 'procedural' ) {
  129. projectorLight.colorNode = causticEffect;
  130. focus.setValue( 1 );
  131. } else if ( val === 'video' ) {
  132. if ( videoTexture === undefined ) {
  133. const video = document.getElementById( 'video' );
  134. video.play();
  135. videoTexture = new THREE.VideoTexture( video );
  136. videoTexture.colorSpace = THREE.SRGBColorSpace;
  137. }
  138. projectorLight.map = videoTexture;
  139. focus.setValue( .46 );
  140. } else if ( val === 'texture' ) {
  141. mapTexture = loader.load( 'colors.png' );
  142. mapTexture.minFilter = THREE.LinearFilter;
  143. mapTexture.magFilter = THREE.LinearFilter;
  144. mapTexture.generateMipmaps = false;
  145. mapTexture.colorSpace = THREE.SRGBColorSpace;
  146. projectorLight.map = mapTexture;
  147. focus.setValue( 1 );
  148. }
  149. } );
  150. gui.addColor( params, 'color' ).onChange( function ( val ) {
  151. projectorLight.color.setHex( val );
  152. } );
  153. gui.add( params, 'intensity', 0, 500 ).onChange( function ( val ) {
  154. projectorLight.intensity = val;
  155. } );
  156. gui.add( params, 'distance', 0, 20 ).onChange( function ( val ) {
  157. projectorLight.distance = val;
  158. } );
  159. gui.add( params, 'angle', 0, Math.PI / 3 ).onChange( function ( val ) {
  160. projectorLight.angle = val;
  161. } );
  162. gui.add( params, 'penumbra', 0, 1 ).onChange( function ( val ) {
  163. projectorLight.penumbra = val;
  164. } );
  165. gui.add( params, 'decay', 1, 2 ).onChange( function ( val ) {
  166. projectorLight.decay = val;
  167. } );
  168. const focus = gui.add( params, 'focus', 0, 1 ).onChange( function ( val ) {
  169. projectorLight.shadow.focus = val;
  170. } );
  171. gui.add( params, 'shadows' ).onChange( function ( val ) {
  172. renderer.shadowMap.enabled = val;
  173. scene.traverse( function ( child ) {
  174. if ( child.material ) {
  175. child.material.needsUpdate = true;
  176. }
  177. } );
  178. } );
  179. }
  180. function onWindowResize() {
  181. camera.aspect = window.innerWidth / window.innerHeight;
  182. camera.updateProjectionMatrix();
  183. renderer.setSize( window.innerWidth, window.innerHeight );
  184. }
  185. function animate() {
  186. const time = performance.now() / 3000;
  187. projectorLight.position.x = Math.cos( time ) * 2.5;
  188. projectorLight.position.z = Math.sin( time ) * 2.5;
  189. lightHelper.update();
  190. renderer.render( scene, camera );
  191. }
  192. </script>
  193. </body>
  194. </html>
粤ICP备19079148号