webgpu_lights_selective.html 6.6 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. <!-- WebGPU (For Chrome 94-97), expires 10/11/2021 -->
  9. <meta http-equiv="origin-trial" content="Agfr4hEaaoH1kaTtGTZY4OU2lQQOv+gWq+8rYeHZBPWNnLww/smePFCIJOUdRFnQZkO3KAio+SNJapjzaoyFfQQAAABLeyJvcmlnaW4iOiJodHRwczovL3RocmVlanMub3JnOjQ0MyIsImZlYXR1cmUiOiJXZWJHUFUiLCJleHBpcnkiOjE2NDMxNTUxOTl9">
  10. </head>
  11. <body>
  12. <div id="info">
  13. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - WebGPU - Selective Lights<br />
  14. (Chrome Canary with flag: --enable-unsafe-webgpu)<br />
  15. <b style="color:red">Left: Red lights</b> - <b>Center: All lights</b> - <b style="color:blue">Right: blue light</b>
  16. </div>
  17. <script type="importmap">
  18. {
  19. "imports": {
  20. "three": "../build/three.module.js"
  21. }
  22. }
  23. </script>
  24. <script type="module">
  25. import * as THREE from 'three';
  26. import Stats from './jsm/libs/stats.module.js';
  27. import { GUI } from './jsm/libs/dat.gui.module.js';
  28. import { OrbitControls } from './jsm/controls/OrbitControls.js';
  29. import { TeapotGeometry } from './jsm/geometries/TeapotGeometry.js';
  30. import WebGPURenderer from './jsm/renderers/webgpu/WebGPURenderer.js';
  31. import WebGPU from './jsm/renderers/webgpu/WebGPU.js';
  32. import LightsNode from './jsm/renderers/nodes/lights/LightsNode.js';
  33. import TextureNode from './jsm/renderers/nodes/inputs/TextureNode.js';
  34. import NormalMapNode from './jsm/renderers/nodes/display/NormalMapNode.js';
  35. let camera, scene, renderer,
  36. light1, light2, light3, light4,
  37. stats, controls;
  38. init().then( animate ).catch( error );
  39. async function init() {
  40. if ( WebGPU.isAvailable() === false ) {
  41. document.body.appendChild( WebGPU.getErrorMessage() );
  42. throw 'No WebGPU support';
  43. }
  44. camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 1, 1000 );
  45. camera.position.z = 70;
  46. scene = new THREE.Scene();
  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, 100 );
  58. light1.add( new THREE.Mesh( sphere, new THREE.MeshBasicMaterial( { color: 0xff0040 } ) ) );
  59. scene.add( light1 );
  60. light2 = new THREE.PointLight( 0x0040ff, 2, 100 );
  61. light2.add( new THREE.Mesh( sphere, new THREE.MeshBasicMaterial( { color: 0x0040ff } ) ) );
  62. scene.add( light2 );
  63. light3 = new THREE.PointLight( 0x80ff80, 2, 100 );
  64. light3.add( new THREE.Mesh( sphere, new THREE.MeshBasicMaterial( { color: 0x80ff80 } ) ) );
  65. scene.add( light3 );
  66. light4 = new THREE.PointLight( 0xffaa00, 2, 100 );
  67. light4.add( new THREE.Mesh( sphere, new THREE.MeshBasicMaterial( { color: 0xffaa00 } ) ) );
  68. scene.add( light4 );
  69. //light nodes ( selective lights )
  70. const allLightsNode = LightsNode.fromLights( [ light1, light2, light3, light4 ] );
  71. const redLightNode = LightsNode.fromLights( [ light1 ] );
  72. const blueLightNode = LightsNode.fromLights( [ light2 ] );
  73. //models
  74. const geometryTeapot = new TeapotGeometry( 8, 18 );
  75. const leftObject = new THREE.Mesh( geometryTeapot, new THREE.MeshStandardMaterial( { color: 0x555555 } ) );
  76. leftObject.material.lightNode = redLightNode;
  77. leftObject.material.roughnessNode = new TextureNode( alphaTexture );
  78. leftObject.material.metalness = 0;
  79. leftObject.position.x = - 30;
  80. scene.add( leftObject );
  81. const centerObject = new THREE.Mesh( geometryTeapot, new THREE.MeshStandardMaterial( { color: 0x555555 } ) );
  82. centerObject.material.lightNode = allLightsNode;
  83. centerObject.material.normalNode = new NormalMapNode( new TextureNode( normalMapTexture ) );
  84. centerObject.material.roughness = .5;
  85. scene.add( centerObject );
  86. const rightObject = new THREE.Mesh( geometryTeapot, new THREE.MeshStandardMaterial( { color: 0x555555 } ) );
  87. rightObject.material.lightNode = blueLightNode;
  88. rightObject.material.metalnessNode = new TextureNode( alphaTexture );
  89. rightObject.position.x = 30;
  90. scene.add( rightObject );
  91. leftObject.rotation.y = centerObject.rotation.y = rightObject.rotation.y = Math.PI * - 0.5;
  92. leftObject.position.y = centerObject.position.y = rightObject.position.y = - 10;
  93. //renderer
  94. renderer = new WebGPURenderer();
  95. renderer.setPixelRatio( window.devicePixelRatio );
  96. renderer.setSize( window.innerWidth, window.innerHeight );
  97. document.body.appendChild( renderer.domElement );
  98. //controls
  99. controls = new OrbitControls( camera, renderer.domElement );
  100. controls.minDistance = 30;
  101. controls.maxDistance = 150;
  102. //stats
  103. stats = new Stats();
  104. document.body.appendChild( stats.dom );
  105. window.addEventListener( 'resize', onWindowResize );
  106. //gui
  107. const gui = new GUI();
  108. gui.add( centerObject.material, 'roughness', 0, 1, 0.01 );
  109. gui.add( centerObject.material, 'metalness', 0, 1, 0.01 );
  110. return renderer.init();
  111. }
  112. function onWindowResize() {
  113. camera.aspect = window.innerWidth / window.innerHeight;
  114. camera.updateProjectionMatrix();
  115. renderer.setSize( window.innerWidth, window.innerHeight );
  116. }
  117. function animate() {
  118. requestAnimationFrame( animate );
  119. render();
  120. stats.update();
  121. }
  122. function render() {
  123. const time = Date.now() * 0.0005;
  124. light1.position.x = Math.sin( time * 0.7 ) * 30;
  125. light1.position.y = Math.cos( time * 0.5 ) * 40;
  126. light1.position.z = Math.cos( time * 0.3 ) * 30;
  127. light2.position.x = Math.cos( time * 0.3 ) * 30;
  128. light2.position.y = Math.sin( time * 0.5 ) * 40;
  129. light2.position.z = Math.sin( time * 0.7 ) * 30;
  130. light3.position.x = Math.sin( time * 0.7 ) * 30;
  131. light3.position.y = Math.cos( time * 0.3 ) * 40;
  132. light3.position.z = Math.sin( time * 0.5 ) * 30;
  133. light4.position.x = Math.sin( time * 0.3 ) * 30;
  134. light4.position.y = Math.cos( time * 0.7 ) * 40;
  135. light4.position.z = Math.sin( time * 0.5 ) * 30;
  136. renderer.render( scene, camera );
  137. }
  138. function error( error ) {
  139. console.error( error );
  140. }
  141. </script>
  142. </body>
  143. </html>