webgpu_lights_selective.html 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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 type="importmap">
  15. {
  16. "imports": {
  17. "three": "../build/three.module.js",
  18. "three/addons/": "./jsm/",
  19. "three/nodes": "./jsm/nodes/Nodes.js"
  20. }
  21. }
  22. </script>
  23. <script type="module">
  24. import * as THREE from 'three';
  25. import { rangeFog, color, lights, texture, normalMap, MeshStandardNodeMaterial } from 'three/nodes';
  26. import Stats from 'three/addons/libs/stats.module.js';
  27. import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
  28. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  29. import { TeapotGeometry } from 'three/addons/geometries/TeapotGeometry.js';
  30. import WebGPU from 'three/addons/capabilities/WebGPU.js';
  31. import WebGL from 'three/addons/capabilities/WebGL.js';
  32. import WebGPURenderer from 'three/addons/renderers/webgpu/WebGPURenderer.js';
  33. let camera, scene, renderer,
  34. light1, light2, light3, light4,
  35. stats, controls;
  36. init();
  37. function init() {
  38. if ( WebGPU.isAvailable() === false && WebGL.isWebGL2Available() === false ) {
  39. document.body.appendChild( WebGPU.getErrorMessage() );
  40. throw new Error( 'No WebGPU or WebGL2 support' );
  41. }
  42. camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 0.01, 100 );
  43. camera.position.z = 7;
  44. scene = new THREE.Scene();
  45. scene.fogNode = rangeFog( color( 0xFF00FF ), 12, 30 );
  46. const sphereGeometry = new THREE.SphereGeometry( 0.1, 16, 8 );
  47. //textures
  48. const textureLoader = new THREE.TextureLoader();
  49. const normalMapTexture = textureLoader.load( './textures/water/Water_1_M_Normal.jpg' );
  50. normalMapTexture.wrapS = THREE.RepeatWrapping;
  51. normalMapTexture.wrapT = THREE.RepeatWrapping;
  52. const alphaTexture = textureLoader.load( './textures/roughness_map.jpg' );
  53. alphaTexture.wrapS = THREE.RepeatWrapping;
  54. alphaTexture.wrapT = THREE.RepeatWrapping;
  55. //lights
  56. const addLight = ( hexColor, power = 1700, distance = 100 ) => {
  57. const material = new MeshStandardNodeMaterial();
  58. material.colorNode = color( hexColor );
  59. material.lights = false;
  60. const mesh = new THREE.Mesh( sphereGeometry, material );
  61. const light = new THREE.PointLight( hexColor, 1, distance );
  62. light.power = power;
  63. light.add( mesh );
  64. scene.add( light );
  65. return light;
  66. };
  67. light1 = addLight( 0xff0040 );
  68. light2 = addLight( 0x0040ff );
  69. light3 = addLight( 0x80ff80 );
  70. light4 = addLight( 0xffaa00 );
  71. //light nodes ( selective lights )
  72. const redLightsNode = lights( [ light1 ] );
  73. const blueLightsNode = lights( [ light2 ] );
  74. //models
  75. const geometryTeapot = new TeapotGeometry( .8, 18 );
  76. const leftObject = new THREE.Mesh( geometryTeapot, new MeshStandardNodeMaterial( { color: 0x555555 } ) );
  77. leftObject.material.lightsNode = redLightsNode;
  78. leftObject.material.roughnessNode = texture( alphaTexture );
  79. leftObject.material.metalness = 0;
  80. leftObject.position.x = - 3;
  81. scene.add( leftObject );
  82. const centerObject = new THREE.Mesh( geometryTeapot, new MeshStandardNodeMaterial( { color: 0x555555 } ) );
  83. centerObject.material.normalNode = normalMap( texture( normalMapTexture ) );
  84. centerObject.material.metalness = .5;
  85. centerObject.material.roughness = .5;
  86. scene.add( centerObject );
  87. const rightObject = new THREE.Mesh( geometryTeapot, new MeshStandardNodeMaterial( { color: 0x555555 } ) );
  88. rightObject.material.lightsNode = blueLightsNode;
  89. rightObject.material.metalnessNode = texture( alphaTexture );
  90. rightObject.position.x = 3;
  91. scene.add( rightObject );
  92. leftObject.rotation.y = centerObject.rotation.y = rightObject.rotation.y = Math.PI * - 0.5;
  93. leftObject.position.y = centerObject.position.y = rightObject.position.y = - 1;
  94. //renderer
  95. renderer = new WebGPURenderer( { antialias: true } );
  96. renderer.setPixelRatio( window.devicePixelRatio );
  97. renderer.setSize( window.innerWidth, window.innerHeight );
  98. renderer.setAnimationLoop( animate );
  99. document.body.appendChild( renderer.domElement );
  100. //controls
  101. controls = new OrbitControls( camera, renderer.domElement );
  102. controls.minDistance = 3;
  103. controls.maxDistance = 25;
  104. //stats
  105. stats = new Stats();
  106. document.body.appendChild( stats.dom );
  107. window.addEventListener( 'resize', onWindowResize );
  108. //gui
  109. const gui = new GUI();
  110. gui.add( centerObject.material, 'roughness', 0, 1, 0.01 );
  111. gui.add( centerObject.material, 'metalness', 0, 1, 0.01 );
  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. const time = performance.now() / 1000;
  120. const lightTime = time * 0.5;
  121. light1.position.x = Math.sin( lightTime * 0.7 ) * 3;
  122. light1.position.y = Math.cos( lightTime * 0.5 ) * 4;
  123. light1.position.z = Math.cos( lightTime * 0.3 ) * 3;
  124. light2.position.x = Math.cos( lightTime * 0.3 ) * 3;
  125. light2.position.y = Math.sin( lightTime * 0.5 ) * 4;
  126. light2.position.z = Math.sin( lightTime * 0.7 ) * 3;
  127. light3.position.x = Math.sin( lightTime * 0.7 ) * 3;
  128. light3.position.y = Math.cos( lightTime * 0.3 ) * 4;
  129. light3.position.z = Math.sin( lightTime * 0.5 ) * 3;
  130. light4.position.x = Math.sin( lightTime * 0.3 ) * 3;
  131. light4.position.y = Math.cos( lightTime * 0.7 ) * 4;
  132. light4.position.z = Math.sin( lightTime * 0.5 ) * 3;
  133. /*
  134. @TODO: Used to test scene light change ( currently unavailable )
  135. if ( time > 2.0 && light1.parent === null ) scene.add( light1 );
  136. if ( time > 2.5 && light2.parent === null ) scene.add( light2 );
  137. if ( time > 3.0 && light3.parent === null ) scene.add( light3 );
  138. if ( time > 3.5 && light4.parent === null ) scene.add( light4 );
  139. */
  140. renderer.render( scene, camera );
  141. stats.update();
  142. }
  143. </script>
  144. </body>
  145. </html>