webgpu_lights_projector.html 7.3 KB

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