webgpu_lights_custom.html 4.6 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 async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
  13. <script type="importmap">
  14. {
  15. "imports": {
  16. "three": "../build/three.module.js",
  17. "three/addons/": "./jsm/",
  18. "three/nodes": "./jsm/nodes/Nodes.js"
  19. }
  20. }
  21. </script>
  22. <script type="module">
  23. import * as THREE from 'three';
  24. import * as Nodes from 'three/nodes';
  25. import WebGPU from 'three/addons/capabilities/WebGPU.js';
  26. import WebGPURenderer from 'three/addons/renderers/webgpu/WebGPURenderer.js';
  27. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  28. let camera, scene, renderer;
  29. let light1, light2, light3;
  30. init().then( animate ).catch( error );
  31. async function init() {
  32. if ( WebGPU.isAvailable() === false ) {
  33. document.body.appendChild( WebGPU.getErrorMessage() );
  34. throw new Error( 'No WebGPU support' );
  35. }
  36. camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 0.1, 10 );
  37. camera.position.z = 2;
  38. scene = new THREE.Scene();
  39. scene.background = new THREE.Color( 0x222222 );
  40. // lights
  41. const sphere = new THREE.SphereGeometry( 0.02, 16, 8 );
  42. light1 = new THREE.PointLight( 0xffaa00, 2, 1 );
  43. light1.add( new THREE.Mesh( sphere, new THREE.MeshBasicMaterial( { color: 0xffaa00 } ) ) );
  44. scene.add( light1 );
  45. light2 = new THREE.PointLight( 0x0040ff, 2, 1 );
  46. light2.add( new THREE.Mesh( sphere, new THREE.MeshBasicMaterial( { color: 0x0040ff } ) ) );
  47. scene.add( light2 );
  48. light3 = new THREE.PointLight( 0x80ff80, 2, 1 );
  49. light3.add( new THREE.Mesh( sphere, new THREE.MeshBasicMaterial( { color: 0x80ff80 } ) ) );
  50. scene.add( light3 );
  51. //light nodes ( selective lights )
  52. const allLightsNode = new Nodes.LightsNode().fromLights( [ light1, light2, light3 ] );
  53. // points
  54. const points = [];
  55. for ( let i = 0; i < 3000; i ++ ) {
  56. const point = new THREE.Vector3().random().subScalar( 0.5 ).multiplyScalar( 2 );
  57. points.push( point );
  58. }
  59. const geometryPoints = new THREE.BufferGeometry().setFromPoints( points );
  60. const materialPoints = new Nodes.PointsNodeMaterial();
  61. // custom lighting model
  62. const customLightingModel = new Nodes.ShaderNode( ( inputs ) => {
  63. const { lightColor, reflectedLight } = inputs;
  64. reflectedLight.directDiffuse.add( lightColor );
  65. } );
  66. const lightingModelContext = new Nodes.ContextNode( allLightsNode, { lightingModelNode: { direct: customLightingModel } } );
  67. materialPoints.lightsNode = lightingModelContext;
  68. //
  69. const pointCloud = new THREE.Points( geometryPoints, materialPoints );
  70. scene.add( pointCloud );
  71. //
  72. renderer = new WebGPURenderer();
  73. renderer.setPixelRatio( window.devicePixelRatio );
  74. renderer.setSize( window.innerWidth, window.innerHeight );
  75. renderer.toneMappingNode = new Nodes.ToneMappingNode( THREE.LinearToneMapping, 1 );
  76. renderer.outputEncoding = THREE.sRGBEncoding;
  77. document.body.appendChild( renderer.domElement );
  78. // controls
  79. const controls = new OrbitControls( camera, renderer.domElement );
  80. controls.minDistance = 0;
  81. controls.maxDistance = 4;
  82. // events
  83. window.addEventListener( 'resize', onWindowResize );
  84. //
  85. return renderer.init();
  86. }
  87. function onWindowResize() {
  88. camera.aspect = window.innerWidth / window.innerHeight;
  89. camera.updateProjectionMatrix();
  90. renderer.setSize( window.innerWidth, window.innerHeight );
  91. }
  92. function animate() {
  93. requestAnimationFrame( animate );
  94. const time = Date.now() * 0.0005;
  95. const scale = .5;
  96. light1.position.x = Math.sin( time * 0.7 ) * scale;
  97. light1.position.y = Math.cos( time * 0.5 ) * scale;
  98. light1.position.z = Math.cos( time * 0.3 ) * scale;
  99. light2.position.x = Math.cos( time * 0.3 ) * scale;
  100. light2.position.y = Math.sin( time * 0.5 ) * scale;
  101. light2.position.z = Math.sin( time * 0.7 ) * scale;
  102. light3.position.x = Math.sin( time * 0.7 ) * scale;
  103. light3.position.y = Math.cos( time * 0.3 ) * scale;
  104. light3.position.z = Math.sin( time * 0.5 ) * scale;
  105. renderer.render( scene, camera );
  106. }
  107. function error( error ) {
  108. console.error( error );
  109. }
  110. </script>
  111. </body>
  112. </html>