webgpu_lights_custom.html 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. <!-- WebGPU (For Chrome 94-101), expires 09/01/2022 -->
  8. <meta http-equiv="origin-trial" content="AoS1pSJwCV3KRe73TO0YgJkK9FZ/qhmvKeafztp0ofiE8uoGrnKzfxGVKKICvoBfL8dgE0zpkp2g/oEJNS0fDgkAAABeeyJvcmlnaW4iOiJodHRwczovL3RocmVlanMub3JnOjQ0MyIsImZlYXR1cmUiOiJXZWJHUFUiLCJleHBpcnkiOjE2NTI4MzE5OTksImlzU3ViZG9tYWluIjp0cnVlfQ==">
  9. </head>
  10. <body>
  11. <div id="info">
  12. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> WebGPU - Custom Lighting Model
  13. </div>
  14. <script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
  15. <script type="importmap">
  16. {
  17. "imports": {
  18. "three": "../build/three.module.js",
  19. "three-nodes/": "./jsm/nodes/"
  20. }
  21. }
  22. </script>
  23. <script type="module">
  24. import * as THREE from 'three';
  25. import * as Nodes from 'three-nodes/Nodes.js';
  26. import WebGPU from './jsm/capabilities/WebGPU.js';
  27. import WebGPURenderer from './jsm/renderers/webgpu/WebGPURenderer.js';
  28. import { OrbitControls } from './jsm/controls/OrbitControls.js';
  29. let camera, scene, renderer;
  30. let light1, light2, light3;
  31. init().then( animate ).catch( error );
  32. async function init() {
  33. if ( WebGPU.isAvailable() === false ) {
  34. document.body.appendChild( WebGPU.getErrorMessage() );
  35. throw new Error( 'No WebGPU support' );
  36. }
  37. camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 0.1, 10 );
  38. camera.position.z = 2;
  39. scene = new THREE.Scene();
  40. scene.background = new THREE.Color( 0x222222 );
  41. // lights
  42. const sphere = new THREE.SphereGeometry( 0.02, 16, 8 );
  43. light1 = new THREE.PointLight( 0xffaa00, 2, 1 );
  44. light1.add( new THREE.Mesh( sphere, new THREE.MeshBasicMaterial( { color: 0xffaa00 } ) ) );
  45. scene.add( light1 );
  46. light2 = new THREE.PointLight( 0x0040ff, 2, 1 );
  47. light2.add( new THREE.Mesh( sphere, new THREE.MeshBasicMaterial( { color: 0x0040ff } ) ) );
  48. scene.add( light2 );
  49. light3 = new THREE.PointLight( 0x80ff80, 2, 1 );
  50. light3.add( new THREE.Mesh( sphere, new THREE.MeshBasicMaterial( { color: 0x80ff80 } ) ) );
  51. scene.add( light3 );
  52. //light nodes ( selective lights )
  53. const allLightsNode = new Nodes.LightsNode().fromLights( [ light1, light2, light3 ] );
  54. // points
  55. const points = [];
  56. for ( let i = 0; i < 3000; i ++ ) {
  57. const point = new THREE.Vector3().random().subScalar( 0.5 ).multiplyScalar( 2 );
  58. points.push( point );
  59. }
  60. const geometryPoints = new THREE.BufferGeometry().setFromPoints( points );
  61. const materialPoints = new Nodes.PointsNodeMaterial();
  62. // custom lighting model
  63. const customLightingModel = new Nodes.ShaderNode( ( inputs ) => {
  64. const { lightColor, reflectedLight } = inputs;
  65. reflectedLight.directDiffuse.add( lightColor );
  66. } );
  67. const lightingModelContext = new Nodes.LightContextNode( allLightsNode, customLightingModel );
  68. materialPoints.lightNode = lightingModelContext;
  69. //
  70. const pointCloud = new THREE.Points( geometryPoints, materialPoints );
  71. scene.add( pointCloud );
  72. //
  73. renderer = new WebGPURenderer();
  74. renderer.setPixelRatio( window.devicePixelRatio );
  75. renderer.setSize( window.innerWidth, window.innerHeight );
  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>