webgpu_lights_selective.html 6.5 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 12/13/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. <b style="color:red">Left: Red lights</b> - <b>Center: All lights</b> - <b style="color:blue">Right: blue light</b>
  15. </div>
  16. <script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
  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/lil-gui.module.min.js';
  28. import { OrbitControls } from './jsm/controls/OrbitControls.js';
  29. import { TeapotGeometry } from './jsm/geometries/TeapotGeometry.js';
  30. import WebGPU from './jsm/capabilities/WebGPU.js';
  31. import WebGPURenderer from './jsm/renderers/webgpu/WebGPURenderer.js';
  32. import * as Nodes from './jsm/renderers/nodes/Nodes.js';
  33. let camera, scene, renderer,
  34. light1, light2, light3, light4,
  35. stats, controls;
  36. init().then( animate ).catch( error );
  37. async function init() {
  38. if ( WebGPU.isAvailable() === false ) {
  39. document.body.appendChild( WebGPU.getErrorMessage() );
  40. throw new Error( 'No WebGPU support' );
  41. }
  42. camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 1, 1000 );
  43. camera.position.z = 70;
  44. scene = new THREE.Scene();
  45. const sphere = new THREE.SphereGeometry( 0.5, 16, 8 );
  46. //textures
  47. const textureLoader = new THREE.TextureLoader();
  48. const normalMapTexture = textureLoader.load( './textures/water/Water_1_M_Normal.jpg' );
  49. normalMapTexture.wrapS = THREE.RepeatWrapping;
  50. normalMapTexture.wrapT = THREE.RepeatWrapping;
  51. const alphaTexture = textureLoader.load( './textures/roughness_map.jpg' );
  52. alphaTexture.wrapS = THREE.RepeatWrapping;
  53. alphaTexture.wrapT = THREE.RepeatWrapping;
  54. //lights
  55. light1 = new THREE.PointLight( 0xff0040, 2, 100 );
  56. light1.add( new THREE.Mesh( sphere, new THREE.MeshBasicMaterial( { color: 0xff0040 } ) ) );
  57. scene.add( light1 );
  58. light2 = new THREE.PointLight( 0x0040ff, 2, 100 );
  59. light2.add( new THREE.Mesh( sphere, new THREE.MeshBasicMaterial( { color: 0x0040ff } ) ) );
  60. scene.add( light2 );
  61. light3 = new THREE.PointLight( 0x80ff80, 2, 100 );
  62. light3.add( new THREE.Mesh( sphere, new THREE.MeshBasicMaterial( { color: 0x80ff80 } ) ) );
  63. scene.add( light3 );
  64. light4 = new THREE.PointLight( 0xffaa00, 2, 100 );
  65. light4.add( new THREE.Mesh( sphere, new THREE.MeshBasicMaterial( { color: 0xffaa00 } ) ) );
  66. scene.add( light4 );
  67. //light nodes ( selective lights )
  68. const allLightsNode = Nodes.LightsNode.fromLights( [ light1, light2, light3, light4 ] );
  69. const redLightNode = Nodes.LightsNode.fromLights( [ light1 ] );
  70. const blueLightNode = Nodes.LightsNode.fromLights( [ light2 ] );
  71. //models
  72. const geometryTeapot = new TeapotGeometry( 8, 18 );
  73. const leftObject = new THREE.Mesh( geometryTeapot, new Nodes.MeshStandardNodeMaterial( { color: 0x555555 } ) );
  74. leftObject.material.lightNode = redLightNode;
  75. leftObject.material.roughnessNode = new Nodes.TextureNode( alphaTexture );
  76. leftObject.material.metalness = 0;
  77. leftObject.position.x = - 30;
  78. scene.add( leftObject );
  79. const centerObject = new THREE.Mesh( geometryTeapot, new Nodes.MeshStandardNodeMaterial( { color: 0x555555 } ) );
  80. centerObject.material.lightNode = allLightsNode;
  81. centerObject.material.normalNode = new Nodes.NormalMapNode( new Nodes.TextureNode( normalMapTexture ) );
  82. centerObject.material.roughness = .5;
  83. scene.add( centerObject );
  84. const rightObject = new THREE.Mesh( geometryTeapot, new Nodes.MeshStandardNodeMaterial( { color: 0x555555 } ) );
  85. rightObject.material.lightNode = blueLightNode;
  86. rightObject.material.metalnessNode = new Nodes.TextureNode( alphaTexture );
  87. rightObject.position.x = 30;
  88. scene.add( rightObject );
  89. leftObject.rotation.y = centerObject.rotation.y = rightObject.rotation.y = Math.PI * - 0.5;
  90. leftObject.position.y = centerObject.position.y = rightObject.position.y = - 10;
  91. //renderer
  92. renderer = new WebGPURenderer();
  93. renderer.setPixelRatio( window.devicePixelRatio );
  94. renderer.setSize( window.innerWidth, window.innerHeight );
  95. document.body.appendChild( renderer.domElement );
  96. //controls
  97. controls = new OrbitControls( camera, renderer.domElement );
  98. controls.minDistance = 30;
  99. controls.maxDistance = 150;
  100. //stats
  101. stats = new Stats();
  102. document.body.appendChild( stats.dom );
  103. window.addEventListener( 'resize', onWindowResize );
  104. //gui
  105. const gui = new GUI();
  106. gui.add( centerObject.material, 'roughness', 0, 1, 0.01 );
  107. gui.add( centerObject.material, 'metalness', 0, 1, 0.01 );
  108. return renderer.init();
  109. }
  110. function onWindowResize() {
  111. camera.aspect = window.innerWidth / window.innerHeight;
  112. camera.updateProjectionMatrix();
  113. renderer.setSize( window.innerWidth, window.innerHeight );
  114. }
  115. function animate() {
  116. requestAnimationFrame( animate );
  117. render();
  118. stats.update();
  119. }
  120. function render() {
  121. const time = Date.now() * 0.0005;
  122. light1.position.x = Math.sin( time * 0.7 ) * 30;
  123. light1.position.y = Math.cos( time * 0.5 ) * 40;
  124. light1.position.z = Math.cos( time * 0.3 ) * 30;
  125. light2.position.x = Math.cos( time * 0.3 ) * 30;
  126. light2.position.y = Math.sin( time * 0.5 ) * 40;
  127. light2.position.z = Math.sin( time * 0.7 ) * 30;
  128. light3.position.x = Math.sin( time * 0.7 ) * 30;
  129. light3.position.y = Math.cos( time * 0.3 ) * 40;
  130. light3.position.z = Math.sin( time * 0.5 ) * 30;
  131. light4.position.x = Math.sin( time * 0.3 ) * 30;
  132. light4.position.y = Math.cos( time * 0.7 ) * 40;
  133. light4.position.z = Math.sin( time * 0.5 ) * 30;
  134. renderer.render( scene, camera );
  135. }
  136. function error( error ) {
  137. console.error( error );
  138. }
  139. </script>
  140. </body>
  141. </html>