webgpu_lights_custom.html 4.9 KB

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