webgpu_lights_selective.html 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. (Chrome Canary with flag: --enable-unsafe-webgpu)<br />
  13. <b style="color:red">Left: Red lights</b> - <b>Center: All lights</b> - <b style="color:blue">Right: blue light</b>
  14. </div>
  15. <script type="importmap">
  16. {
  17. "imports": {
  18. "three": "../build/three.module.js"
  19. }
  20. }
  21. </script>
  22. <script type="module">
  23. import * as THREE from 'three';
  24. import Stats from './jsm/libs/stats.module.js';
  25. import { OrbitControls } from './jsm/controls/OrbitControls.js';
  26. import { TeapotGeometry } from './jsm/geometries/TeapotGeometry.js';
  27. import WebGPURenderer from './jsm/renderers/webgpu/WebGPURenderer.js';
  28. import WebGPU from './jsm/renderers/webgpu/WebGPU.js';
  29. import LightsNode from './jsm/renderers/nodes/lights/LightsNode.js';
  30. let camera, scene, renderer,
  31. light1, light2, light3, light4,
  32. stats, controls;
  33. init().then( animate ).catch( error );
  34. async function init() {
  35. if ( WebGPU.isAvailable() === false ) {
  36. document.body.appendChild( WebGPU.getErrorMessage() );
  37. throw 'No WebGPU support';
  38. }
  39. camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 1, 1000 );
  40. camera.position.z = 70;
  41. scene = new THREE.Scene();
  42. const sphere = new THREE.SphereGeometry( 0.5, 16, 8 );
  43. //lights
  44. light1 = new THREE.PointLight( 0xff0040, 2, 100 );
  45. light1.add( new THREE.Mesh( sphere, new THREE.MeshBasicMaterial( { color: 0xff0040 } ) ) );
  46. scene.add( light1 );
  47. light2 = new THREE.PointLight( 0x0040ff, 2, 100 );
  48. light2.add( new THREE.Mesh( sphere, new THREE.MeshBasicMaterial( { color: 0x0040ff } ) ) );
  49. scene.add( light2 );
  50. light3 = new THREE.PointLight( 0x80ff80, 2, 100 );
  51. light3.add( new THREE.Mesh( sphere, new THREE.MeshBasicMaterial( { color: 0x80ff80 } ) ) );
  52. scene.add( light3 );
  53. light4 = new THREE.PointLight( 0xffaa00, 2, 100 );
  54. light4.add( new THREE.Mesh( sphere, new THREE.MeshBasicMaterial( { color: 0xffaa00 } ) ) );
  55. scene.add( light4 );
  56. //light nodes ( selective lights )
  57. const allLightsNode = LightsNode.fromLights( [ light1, light2, light3, light4 ] );
  58. const redLightNode = LightsNode.fromLights( [ light1 ] );
  59. const blueLightNode = LightsNode.fromLights( [ light2 ] );
  60. //models
  61. const geometryTeapot = new TeapotGeometry( 8, 18 );
  62. const leftObject = new THREE.Mesh( geometryTeapot, new THREE.MeshPhongMaterial( { color: 0x555555 } ) );
  63. leftObject.material.lightNode = redLightNode;
  64. leftObject.position.x = - 30;
  65. scene.add( leftObject );
  66. const centerObject = new THREE.Mesh( geometryTeapot, new THREE.MeshPhongMaterial( { color: 0x555555 } ) );
  67. centerObject.material.lightNode = allLightsNode;
  68. scene.add( centerObject );
  69. const rightObject = new THREE.Mesh( geometryTeapot, new THREE.MeshPhongMaterial( { color: 0x555555 } ) );
  70. rightObject.material.lightNode = blueLightNode;
  71. rightObject.position.x = 30;
  72. scene.add( rightObject );
  73. leftObject.material.shininess = centerObject.material.shininess = rightObject.material.shininess = 70;
  74. leftObject.rotation.y = centerObject.rotation.y = rightObject.rotation.y = Math.PI * - 0.5;
  75. leftObject.position.y = centerObject.position.y = rightObject.position.y = - 10;
  76. //renderer
  77. renderer = new WebGPURenderer();
  78. renderer.setPixelRatio( window.devicePixelRatio );
  79. renderer.setSize( window.innerWidth, window.innerHeight );
  80. document.body.appendChild( renderer.domElement );
  81. //controls
  82. controls = new OrbitControls( camera, renderer.domElement );
  83. controls.minDistance = 30;
  84. controls.maxDistance = 150;
  85. //stats
  86. stats = new Stats();
  87. document.body.appendChild( stats.dom );
  88. window.addEventListener( 'resize', onWindowResize );
  89. return renderer.init();
  90. }
  91. function onWindowResize() {
  92. camera.aspect = window.innerWidth / window.innerHeight;
  93. camera.updateProjectionMatrix();
  94. renderer.setSize( window.innerWidth, window.innerHeight );
  95. }
  96. function animate() {
  97. requestAnimationFrame( animate );
  98. render();
  99. stats.update();
  100. }
  101. function render() {
  102. const time = Date.now() * 0.0005;
  103. light1.position.x = Math.sin( time * 0.7 ) * 30;
  104. light1.position.y = Math.cos( time * 0.5 ) * 40;
  105. light1.position.z = Math.cos( time * 0.3 ) * 30;
  106. light2.position.x = Math.cos( time * 0.3 ) * 30;
  107. light2.position.y = Math.sin( time * 0.5 ) * 40;
  108. light2.position.z = Math.sin( time * 0.7 ) * 30;
  109. light3.position.x = Math.sin( time * 0.7 ) * 30;
  110. light3.position.y = Math.cos( time * 0.3 ) * 40;
  111. light3.position.z = Math.sin( time * 0.5 ) * 30;
  112. light4.position.x = Math.sin( time * 0.3 ) * 30;
  113. light4.position.y = Math.cos( time * 0.7 ) * 40;
  114. light4.position.z = Math.sin( time * 0.5 ) * 30;
  115. renderer.render( scene, camera );
  116. }
  117. function error( error ) {
  118. console.error( error );
  119. }
  120. </script>
  121. </body>
  122. </html>