webgpu_lights_custom.html 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <html lang="en">
  2. <head>
  3. <title>three.js webgpu - custom lighting model</title>
  4. <meta charset="utf-8">
  5. <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
  6. <link type="text/css" rel="stylesheet" href="main.css">
  7. </head>
  8. <body>
  9. <div id="info">
  10. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> webgpu - custom lighting model
  11. </div>
  12. <script type="importmap">
  13. {
  14. "imports": {
  15. "three": "../build/three.webgpu.js",
  16. "three/tsl": "../build/three.webgpu.js",
  17. "three/addons/": "./jsm/"
  18. }
  19. }
  20. </script>
  21. <script type="module">
  22. import * as THREE from 'three';
  23. import { color, lights } from 'three/tsl';
  24. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  25. class CustomLightingModel extends THREE.LightingModel {
  26. direct( { lightColor, reflectedLight }, stack ) {
  27. reflectedLight.directDiffuse.addAssign( lightColor );
  28. }
  29. }
  30. let camera, scene, renderer;
  31. let light1, light2, light3;
  32. init();
  33. function init() {
  34. camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 0.1, 10 );
  35. camera.position.z = 1.5;
  36. scene = new THREE.Scene();
  37. scene.background = new THREE.Color( 0x222222 );
  38. // lights
  39. const sphereGeometry = new THREE.SphereGeometry( 0.01, 16, 8 );
  40. const addLight = ( hexColor ) => {
  41. const material = new THREE.MeshStandardNodeMaterial();
  42. material.colorNode = color( hexColor );
  43. material.lightsNode = lights(); // ignore scene lights
  44. const mesh = new THREE.Mesh( sphereGeometry, material );
  45. const light = new THREE.PointLight( hexColor, 0.1, 0.8 );
  46. light.add( mesh );
  47. scene.add( light );
  48. return light;
  49. };
  50. light1 = addLight( 0xffaa00 );
  51. light2 = addLight( 0x0040ff );
  52. light3 = addLight( 0x80ff80 );
  53. //light nodes ( selective lights )
  54. const allLightsNode = lights( [ light1, light2, light3 ] );
  55. // points
  56. const points = [];
  57. for ( let i = 0; i < 1_000_000; i ++ ) {
  58. const point = new THREE.Vector3().random().subScalar( 0.5 ).multiplyScalar( 2 );
  59. points.push( point );
  60. }
  61. const geometryPoints = new THREE.BufferGeometry().setFromPoints( points );
  62. const materialPoints = new THREE.PointsNodeMaterial();
  63. // custom lighting model
  64. const lightingModel = new CustomLightingModel();
  65. const lightingModelContext = allLightsNode.context( { lightingModel } );
  66. materialPoints.lightsNode = lightingModelContext;
  67. //
  68. const pointCloud = new THREE.Points( geometryPoints, materialPoints );
  69. scene.add( pointCloud );
  70. //
  71. renderer = new THREE.WebGPURenderer( { antialias: true } );
  72. renderer.setPixelRatio( window.devicePixelRatio );
  73. renderer.setSize( window.innerWidth, window.innerHeight );
  74. renderer.setAnimationLoop( animate );
  75. document.body.appendChild( renderer.domElement );
  76. // controls
  77. const controls = new OrbitControls( camera, renderer.domElement );
  78. controls.minDistance = 0;
  79. controls.maxDistance = 4;
  80. // events
  81. window.addEventListener( 'resize', onWindowResize );
  82. }
  83. function onWindowResize() {
  84. camera.aspect = window.innerWidth / window.innerHeight;
  85. camera.updateProjectionMatrix();
  86. renderer.setSize( window.innerWidth, window.innerHeight );
  87. }
  88. function animate() {
  89. const time = Date.now() * 0.0005;
  90. const scale = .5;
  91. light1.position.x = Math.sin( time * 0.7 ) * scale;
  92. light1.position.y = Math.cos( time * 0.5 ) * scale;
  93. light1.position.z = Math.cos( time * 0.3 ) * scale;
  94. light2.position.x = Math.cos( time * 0.3 ) * scale;
  95. light2.position.y = Math.sin( time * 0.5 ) * scale;
  96. light2.position.z = Math.sin( time * 0.7 ) * scale;
  97. light3.position.x = Math.sin( time * 0.7 ) * scale;
  98. light3.position.y = Math.cos( time * 0.3 ) * scale;
  99. light3.position.z = Math.sin( time * 0.5 ) * scale;
  100. scene.rotation.y = time * 0.6;
  101. renderer.render( scene, camera );
  102. }
  103. </script>
  104. </body>
  105. </html>