webgpu_lights_projector.html 7.1 KB

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