webgpu_lights_custom.html 4.6 KB

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