webgpu_lights_projector.html 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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. projectorLight.shadow.bias = - .003;
  88. scene.add( projectorLight );
  89. lightHelper = new THREE.SpotLightHelper( projectorLight );
  90. scene.add( lightHelper );
  91. //
  92. const geometry = new THREE.PlaneGeometry( 200, 200 );
  93. const material = new THREE.MeshLambertMaterial( { color: 0xbcbcbc } );
  94. const mesh = new THREE.Mesh( geometry, material );
  95. mesh.position.set( 0, - 1, 0 );
  96. mesh.rotation.x = - Math.PI / 2;
  97. mesh.receiveShadow = true;
  98. scene.add( mesh );
  99. // Models
  100. new PLYLoader().load( 'models/ply/binary/Lucy100k.ply', function ( geometry ) {
  101. geometry.scale( 0.0024, 0.0024, 0.0024 );
  102. geometry.computeVertexNormals();
  103. const material = new THREE.MeshLambertMaterial();
  104. const mesh = new THREE.Mesh( geometry, material );
  105. mesh.rotation.y = - Math.PI / 2;
  106. mesh.position.y = 0.8;
  107. mesh.castShadow = true;
  108. mesh.receiveShadow = true;
  109. scene.add( mesh );
  110. } );
  111. window.addEventListener( 'resize', onWindowResize );
  112. // GUI
  113. const gui = renderer.inspector.createParameters( 'Projector Light' );
  114. const params = {
  115. type: 'procedural',
  116. color: projectorLight.color.getHex(),
  117. intensity: projectorLight.intensity,
  118. distance: projectorLight.distance,
  119. angle: projectorLight.angle,
  120. penumbra: projectorLight.penumbra,
  121. decay: projectorLight.decay,
  122. focus: projectorLight.shadow.focus,
  123. shadows: true,
  124. };
  125. let videoTexture, mapTexture;
  126. gui.add( params, 'type', [ 'procedural', 'video', 'texture' ] ).onChange( function ( val ) {
  127. projectorLight.colorNode = null;
  128. projectorLight.map = null;
  129. if ( val === 'procedural' ) {
  130. projectorLight.colorNode = causticEffect;
  131. focus.setValue( 1 );
  132. } else if ( val === 'video' ) {
  133. if ( videoTexture === undefined ) {
  134. const video = document.getElementById( 'video' );
  135. video.play();
  136. videoTexture = new THREE.VideoTexture( video );
  137. videoTexture.colorSpace = THREE.SRGBColorSpace;
  138. }
  139. projectorLight.map = videoTexture;
  140. focus.setValue( .46 );
  141. } else if ( val === 'texture' ) {
  142. mapTexture = loader.load( 'colors.png' );
  143. mapTexture.minFilter = THREE.LinearFilter;
  144. mapTexture.magFilter = THREE.LinearFilter;
  145. mapTexture.generateMipmaps = false;
  146. mapTexture.colorSpace = THREE.SRGBColorSpace;
  147. projectorLight.map = mapTexture;
  148. focus.setValue( 1 );
  149. }
  150. } );
  151. gui.addColor( params, 'color' ).onChange( function ( val ) {
  152. projectorLight.color.setHex( val );
  153. } );
  154. gui.add( params, 'intensity', 0, 500 ).onChange( function ( val ) {
  155. projectorLight.intensity = val;
  156. } );
  157. gui.add( params, 'distance', 0, 20 ).onChange( function ( val ) {
  158. projectorLight.distance = val;
  159. } );
  160. gui.add( params, 'angle', 0, Math.PI / 3 ).onChange( function ( val ) {
  161. projectorLight.angle = val;
  162. } );
  163. gui.add( params, 'penumbra', 0, 1 ).onChange( function ( val ) {
  164. projectorLight.penumbra = val;
  165. } );
  166. gui.add( params, 'decay', 1, 2 ).onChange( function ( val ) {
  167. projectorLight.decay = val;
  168. } );
  169. const focus = gui.add( params, 'focus', 0, 1 ).onChange( function ( val ) {
  170. projectorLight.shadow.focus = val;
  171. } );
  172. gui.add( params, 'shadows' ).onChange( function ( val ) {
  173. renderer.shadowMap.enabled = val;
  174. scene.traverse( function ( child ) {
  175. if ( child.material ) {
  176. child.material.needsUpdate = true;
  177. }
  178. } );
  179. } );
  180. }
  181. function onWindowResize() {
  182. camera.aspect = window.innerWidth / window.innerHeight;
  183. camera.updateProjectionMatrix();
  184. renderer.setSize( window.innerWidth, window.innerHeight );
  185. }
  186. function animate() {
  187. const time = performance.now() / 3000;
  188. projectorLight.position.x = Math.cos( time ) * 2.5;
  189. projectorLight.position.z = Math.sin( time ) * 2.5;
  190. lightHelper.update();
  191. renderer.render( scene, camera );
  192. }
  193. </script>
  194. </body>
  195. </html>
粤ICP备19079148号