webgpu_lights_selective.html 5.2 KB

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