webgpu_lights_selective.html 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js - WebGPU - Selective Lights</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 - Selective Lights<br />
  12. <b style="color:red">Left: Red lights</b> - <b>Center: All lights</b> - <b style="color:blue">Right: blue light</b>
  13. </div>
  14. <script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
  15. <script type="importmap">
  16. {
  17. "imports": {
  18. "three": "../build/three.module.js",
  19. "three/addons/": "./jsm/",
  20. "three/nodes": "./jsm/nodes/Nodes.js"
  21. }
  22. }
  23. </script>
  24. <script type="module">
  25. import * as THREE from 'three';
  26. import * as Nodes from 'three/nodes';
  27. import Stats from 'three/addons/libs/stats.module.js';
  28. import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
  29. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  30. import { TeapotGeometry } from 'three/addons/geometries/TeapotGeometry.js';
  31. import WebGPU from 'three/addons/capabilities/WebGPU.js';
  32. import WebGPURenderer from 'three/addons/renderers/webgpu/WebGPURenderer.js';
  33. import { color, float } from 'three/nodes';
  34. let camera, scene, renderer,
  35. light1, light2, light3, light4,
  36. stats, controls;
  37. init().then( animate ).catch( error );
  38. async function init() {
  39. if ( WebGPU.isAvailable() === false ) {
  40. document.body.appendChild( WebGPU.getErrorMessage() );
  41. throw new Error( 'No WebGPU support' );
  42. }
  43. camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 1, 1000 );
  44. camera.position.z = 70;
  45. scene = new THREE.Scene();
  46. scene.fogNode = new Nodes.FogRangeNode( color( 0xFF00FF ), float( 30 ), float( 300 ) );
  47. const sphere = new THREE.SphereGeometry( 0.5, 16, 8 );
  48. //textures
  49. const textureLoader = new THREE.TextureLoader();
  50. const normalMapTexture = textureLoader.load( './textures/water/Water_1_M_Normal.jpg' );
  51. normalMapTexture.wrapS = THREE.RepeatWrapping;
  52. normalMapTexture.wrapT = THREE.RepeatWrapping;
  53. const alphaTexture = textureLoader.load( './textures/roughness_map.jpg' );
  54. alphaTexture.wrapS = THREE.RepeatWrapping;
  55. alphaTexture.wrapT = THREE.RepeatWrapping;
  56. //lights
  57. light1 = new THREE.PointLight( 0xff0040, 2, 1000 );
  58. light1.add( new THREE.Mesh( sphere, new THREE.MeshBasicMaterial( { color: 0xff0040 } ) ) );
  59. scene.add( light1 );
  60. light2 = new THREE.PointLight( 0x0040ff, 2, 1000 );
  61. light2.add( new THREE.Mesh( sphere, new THREE.MeshBasicMaterial( { color: 0x0040ff } ) ) );
  62. scene.add( light2 );
  63. light3 = new THREE.PointLight( 0x80ff80, 2, 1000 );
  64. light3.add( new THREE.Mesh( sphere, new THREE.MeshBasicMaterial( { color: 0x80ff80 } ) ) );
  65. scene.add( light3 );
  66. light4 = new THREE.PointLight( 0xffaa00, 2, 1000 );
  67. light4.add( new THREE.Mesh( sphere, new THREE.MeshBasicMaterial( { color: 0xffaa00 } ) ) );
  68. scene.add( light4 );
  69. //light nodes ( selective lights )
  70. const redLightsNode = new Nodes.LightsNode().fromLights( [ light1 ] );
  71. const blueLightsNode = new Nodes.LightsNode().fromLights( [ light2 ] );
  72. //models
  73. const geometryTeapot = new TeapotGeometry( 8, 18 );
  74. const leftObject = new THREE.Mesh( geometryTeapot, new Nodes.MeshStandardNodeMaterial( { color: 0x555555 } ) );
  75. leftObject.material.lightsNode = redLightsNode;
  76. leftObject.material.roughnessNode = new Nodes.TextureNode( alphaTexture );
  77. leftObject.material.metalness = 0;
  78. leftObject.position.x = - 30;
  79. scene.add( leftObject );
  80. const centerObject = new THREE.Mesh( geometryTeapot, new Nodes.MeshStandardNodeMaterial( { color: 0x555555 } ) );
  81. centerObject.material.normalNode = new Nodes.NormalMapNode( new Nodes.TextureNode( normalMapTexture ) );
  82. centerObject.material.metalness = .5;
  83. centerObject.material.roughness = .5;
  84. scene.add( centerObject );
  85. const rightObject = new THREE.Mesh( geometryTeapot, new Nodes.MeshStandardNodeMaterial( { color: 0x555555 } ) );
  86. rightObject.material.lightsNode = blueLightsNode;
  87. rightObject.material.metalnessNode = new Nodes.TextureNode( alphaTexture );
  88. rightObject.position.x = 30;
  89. scene.add( rightObject );
  90. leftObject.rotation.y = centerObject.rotation.y = rightObject.rotation.y = Math.PI * - 0.5;
  91. leftObject.position.y = centerObject.position.y = rightObject.position.y = - 10;
  92. //renderer
  93. renderer = new WebGPURenderer();
  94. renderer.setPixelRatio( window.devicePixelRatio );
  95. renderer.setSize( window.innerWidth, window.innerHeight );
  96. document.body.appendChild( renderer.domElement );
  97. renderer.outputEncoding = THREE.sRGBEncoding;
  98. renderer.toneMappingNode = new Nodes.ToneMappingNode( THREE.LinearToneMapping, 100 );
  99. //controls
  100. controls = new OrbitControls( camera, renderer.domElement );
  101. controls.minDistance = 30;
  102. controls.maxDistance = 250;
  103. //stats
  104. stats = new Stats();
  105. document.body.appendChild( stats.dom );
  106. window.addEventListener( 'resize', onWindowResize );
  107. //gui
  108. const gui = new GUI();
  109. gui.add( centerObject.material, 'roughness', 0, 1, 0.01 );
  110. gui.add( centerObject.material, 'metalness', 0, 1, 0.01 );
  111. return renderer.init();
  112. }
  113. function onWindowResize() {
  114. camera.aspect = window.innerWidth / window.innerHeight;
  115. camera.updateProjectionMatrix();
  116. renderer.setSize( window.innerWidth, window.innerHeight );
  117. }
  118. function animate() {
  119. requestAnimationFrame( animate );
  120. render();
  121. stats.update();
  122. }
  123. function render() {
  124. const time = performance.now() / 1000;
  125. const lightTime = time * 0.5;
  126. light1.position.x = Math.sin( lightTime * 0.7 ) * 30;
  127. light1.position.y = Math.cos( lightTime * 0.5 ) * 40;
  128. light1.position.z = Math.cos( lightTime * 0.3 ) * 30;
  129. light2.position.x = Math.cos( lightTime * 0.3 ) * 30;
  130. light2.position.y = Math.sin( lightTime * 0.5 ) * 40;
  131. light2.position.z = Math.sin( lightTime * 0.7 ) * 30;
  132. light3.position.x = Math.sin( lightTime * 0.7 ) * 30;
  133. light3.position.y = Math.cos( lightTime * 0.3 ) * 40;
  134. light3.position.z = Math.sin( lightTime * 0.5 ) * 30;
  135. light4.position.x = Math.sin( lightTime * 0.3 ) * 30;
  136. light4.position.y = Math.cos( lightTime * 0.7 ) * 40;
  137. light4.position.z = Math.sin( lightTime * 0.5 ) * 30;
  138. /*
  139. @TODO: Used to test scene light change ( currently unavailable )
  140. if ( time > 2.0 && light1.parent === null ) scene.add( light1 );
  141. if ( time > 2.5 && light2.parent === null ) scene.add( light2 );
  142. if ( time > 3.0 && light3.parent === null ) scene.add( light3 );
  143. if ( time > 3.5 && light4.parent === null ) scene.add( light4 );
  144. */
  145. renderer.render( scene, camera );
  146. }
  147. function error( error ) {
  148. console.error( error );
  149. }
  150. </script>
  151. </body>
  152. </html>