webgpu_lights_custom.html 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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.module.js",
  16. "three/addons/": "./jsm/",
  17. "three/nodes": "./jsm/nodes/Nodes.js"
  18. }
  19. }
  20. </script>
  21. <script type="module">
  22. import * as THREE from 'three';
  23. import { color, lights, toneMapping, MeshStandardNodeMaterial, PointsNodeMaterial, LightingModel } from 'three/nodes';
  24. import WebGPU from 'three/addons/capabilities/WebGPU.js';
  25. import WebGL from 'three/addons/capabilities/WebGL.js';
  26. import WebGPURenderer from 'three/addons/renderers/webgpu/WebGPURenderer.js';
  27. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  28. class CustomLightingModel extends LightingModel {
  29. direct( { lightColor, reflectedLight }, stack ) {
  30. reflectedLight.directDiffuse.addAssign( lightColor );
  31. }
  32. }
  33. let camera, scene, renderer;
  34. let light1, light2, light3;
  35. init();
  36. function init() {
  37. if ( WebGPU.isAvailable() === false && WebGL.isWebGL2Available() === false ) {
  38. document.body.appendChild( WebGPU.getErrorMessage() );
  39. throw new Error( 'No WebGPU or WebGL2 support' );
  40. }
  41. camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 0.1, 10 );
  42. camera.position.z = 2;
  43. scene = new THREE.Scene();
  44. scene.background = new THREE.Color( 0x222222 );
  45. // lights
  46. const sphereGeometry = new THREE.SphereGeometry( 0.02, 16, 8 );
  47. const addLight = ( hexColor, intensity = 2, distance = 1 ) => {
  48. const material = new MeshStandardNodeMaterial();
  49. material.colorNode = color( hexColor );
  50. material.lightsNode = lights(); // ignore scene lights
  51. const mesh = new THREE.Mesh( sphereGeometry, material );
  52. const light = new THREE.PointLight( hexColor, intensity, distance );
  53. light.add( mesh );
  54. scene.add( light );
  55. return light;
  56. };
  57. light1 = addLight( 0xffaa00 );
  58. light2 = addLight( 0x0040ff );
  59. light3 = addLight( 0x80ff80 );
  60. //light nodes ( selective lights )
  61. const allLightsNode = lights( [ light1, light2, light3 ] );
  62. // points
  63. const points = [];
  64. for ( let i = 0; i < 3000; i ++ ) {
  65. const point = new THREE.Vector3().random().subScalar( 0.5 ).multiplyScalar( 2 );
  66. points.push( point );
  67. }
  68. const geometryPoints = new THREE.BufferGeometry().setFromPoints( points );
  69. const materialPoints = new PointsNodeMaterial();
  70. // custom lighting model
  71. const lightingModel = new CustomLightingModel();
  72. const lightingModelContext = allLightsNode.context( { lightingModel } );
  73. materialPoints.lightsNode = lightingModelContext;
  74. //
  75. const pointCloud = new THREE.Points( geometryPoints, materialPoints );
  76. scene.add( pointCloud );
  77. //
  78. renderer = new WebGPURenderer( { antialias: true } );
  79. renderer.setPixelRatio( window.devicePixelRatio );
  80. renderer.setSize( window.innerWidth, window.innerHeight );
  81. renderer.setAnimationLoop( animate );
  82. renderer.toneMappingNode = toneMapping( THREE.LinearToneMapping, 1 );
  83. document.body.appendChild( renderer.domElement );
  84. // controls
  85. const controls = new OrbitControls( camera, renderer.domElement );
  86. controls.minDistance = 0;
  87. controls.maxDistance = 4;
  88. // events
  89. window.addEventListener( 'resize', onWindowResize );
  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. const time = Date.now() * 0.0005;
  98. const scale = .5;
  99. light1.position.x = Math.sin( time * 0.7 ) * scale;
  100. light1.position.y = Math.cos( time * 0.5 ) * scale;
  101. light1.position.z = Math.cos( time * 0.3 ) * scale;
  102. light2.position.x = Math.cos( time * 0.3 ) * scale;
  103. light2.position.y = Math.sin( time * 0.5 ) * scale;
  104. light2.position.z = Math.sin( time * 0.7 ) * scale;
  105. light3.position.x = Math.sin( time * 0.7 ) * scale;
  106. light3.position.y = Math.cos( time * 0.3 ) * scale;
  107. light3.position.z = Math.sin( time * 0.5 ) * scale;
  108. renderer.render( scene, camera );
  109. }
  110. </script>
  111. </body>
  112. </html>