webgpu_lights_custom.html 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. <meta property="og:title" content="three.js webgpu - custom lighting model">
  7. <meta property="og:type" content="website">
  8. <meta property="og:url" content="https://threejs.org/examples/webgpu_lights_custom.html">
  9. <meta property="og:image" content="https://threejs.org/examples/screenshots/webgpu_lights_custom.jpg">
  10. <link type="text/css" rel="stylesheet" href="example.css">
  11. </head>
  12. <body>
  13. <div id="info">
  14. <a href="https://threejs.org/" target="_blank" rel="noopener" class="logo-link"></a>
  15. <div class="title-wrapper">
  16. <a href="https://threejs.org/" target="_blank" rel="noopener">three.js</a><span>Custom Lighting Model</span>
  17. </div>
  18. <small>
  19. Custom lighting model with selective lights.
  20. </small>
  21. </div>
  22. <script type="importmap">
  23. {
  24. "imports": {
  25. "three": "../build/three.webgpu.js",
  26. "three/webgpu": "../build/three.webgpu.js",
  27. "three/tsl": "../build/three.tsl.js",
  28. "three/addons/": "./jsm/"
  29. }
  30. }
  31. </script>
  32. <script type="module">
  33. import * as THREE from 'three/webgpu';
  34. import { color, lights } from 'three/tsl';
  35. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  36. class CustomLightingModel extends THREE.LightingModel {
  37. direct( { lightColor, reflectedLight }/*, builder */ ) {
  38. reflectedLight.directDiffuse.addAssign( lightColor );
  39. }
  40. }
  41. let camera, scene, renderer;
  42. let light1, light2, light3;
  43. init();
  44. function init() {
  45. camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 0.1, 10 );
  46. camera.position.z = 1.5;
  47. scene = new THREE.Scene();
  48. scene.background = new THREE.Color( 0x000000 );
  49. // lights
  50. const sphereGeometry = new THREE.SphereGeometry( 0.02, 16, 8 );
  51. const addLight = ( hexColor ) => {
  52. const material = new THREE.NodeMaterial();
  53. material.colorNode = color( hexColor );
  54. material.lightsNode = lights(); // ignore scene lights
  55. const mesh = new THREE.Mesh( sphereGeometry, material );
  56. const light = new THREE.PointLight( hexColor, 0.1, 1 );
  57. light.add( mesh );
  58. scene.add( light );
  59. return light;
  60. };
  61. light1 = addLight( 0xffaa00 );
  62. light2 = addLight( 0x0040ff );
  63. light3 = addLight( 0x80ff80 );
  64. //light nodes ( selective lights )
  65. const allLightsNode = lights( [ light1, light2, light3 ] );
  66. // points
  67. const points = [];
  68. for ( let i = 0; i < 500000; i ++ ) {
  69. const point = new THREE.Vector3().random().subScalar( 0.5 ).multiplyScalar( 3 );
  70. points.push( point );
  71. }
  72. const geometryPoints = new THREE.BufferGeometry().setFromPoints( points );
  73. const materialPoints = new THREE.PointsNodeMaterial();
  74. // custom lighting model
  75. const lightingModel = new CustomLightingModel();
  76. const lightingModelContext = allLightsNode.context( { lightingModel } );
  77. materialPoints.lightsNode = lightingModelContext;
  78. //
  79. const pointCloud = new THREE.Points( geometryPoints, materialPoints );
  80. scene.add( pointCloud );
  81. //
  82. renderer = new THREE.WebGPURenderer( { antialias: true } );
  83. renderer.setPixelRatio( window.devicePixelRatio );
  84. renderer.setSize( window.innerWidth, window.innerHeight );
  85. renderer.setAnimationLoop( animate );
  86. document.body.appendChild( renderer.domElement );
  87. // controls
  88. const controls = new OrbitControls( camera, renderer.domElement );
  89. controls.minDistance = 0;
  90. controls.maxDistance = 4;
  91. // events
  92. window.addEventListener( 'resize', onWindowResize );
  93. }
  94. function onWindowResize() {
  95. camera.aspect = window.innerWidth / window.innerHeight;
  96. camera.updateProjectionMatrix();
  97. renderer.setSize( window.innerWidth, window.innerHeight );
  98. }
  99. function animate() {
  100. const time = Date.now() * 0.001;
  101. const scale = .5;
  102. light1.position.x = Math.sin( time * 0.7 ) * scale;
  103. light1.position.y = Math.cos( time * 0.5 ) * scale;
  104. light1.position.z = Math.cos( time * 0.3 ) * scale;
  105. light2.position.x = Math.cos( time * 0.3 ) * scale;
  106. light2.position.y = Math.sin( time * 0.5 ) * scale;
  107. light2.position.z = Math.sin( time * 0.7 ) * scale;
  108. light3.position.x = Math.sin( time * 0.7 ) * scale;
  109. light3.position.y = Math.cos( time * 0.3 ) * scale;
  110. light3.position.z = Math.sin( time * 0.5 ) * scale;
  111. scene.rotation.y = time * 0.1;
  112. renderer.render( scene, camera );
  113. }
  114. </script>
  115. </body>
  116. </html>
粤ICP备19079148号