1
0

webgpu_lights_selective.html 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgpu - selective lights</title>
  5. <meta charset="utf-8">
  6. <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
  7. <link type="text/css" rel="stylesheet" href="main.css">
  8. </head>
  9. <body>
  10. <div id="info">
  11. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> webgpu - selective lights<br />
  12. <b style="color:red">Left: Red lights</b> - <b>Center: All lights</b> - <b style="color:blue">Right: blue light</b>
  13. </div>
  14. <script type="importmap">
  15. {
  16. "imports": {
  17. "three": "../build/three.webgpu.js",
  18. "three/webgpu": "../build/three.webgpu.js",
  19. "three/tsl": "../build/three.tsl.js",
  20. "three/addons/": "./jsm/"
  21. }
  22. }
  23. </script>
  24. <script type="module">
  25. import * as THREE from 'three';
  26. import { fog, rangeFogFactor, color, lights, texture, normalMap } from 'three/tsl';
  27. import Stats from 'three/addons/libs/stats.module.js';
  28. import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
  29. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  30. import { TeapotGeometry } from 'three/addons/geometries/TeapotGeometry.js';
  31. let camera, scene, renderer,
  32. light1, light2, light3, light4,
  33. stats, controls;
  34. init();
  35. function init() {
  36. camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 0.01, 100 );
  37. camera.position.z = 7;
  38. scene = new THREE.Scene();
  39. scene.fogNode = fog( color( 0xFF00FF ), rangeFogFactor( 12, 30 ) );
  40. const sphereGeometry = new THREE.SphereGeometry( 0.1, 16, 8 );
  41. //textures
  42. const textureLoader = new THREE.TextureLoader();
  43. const normalMapTexture = textureLoader.load( './textures/water/Water_1_M_Normal.jpg' );
  44. normalMapTexture.wrapS = THREE.RepeatWrapping;
  45. normalMapTexture.wrapT = THREE.RepeatWrapping;
  46. const alphaTexture = textureLoader.load( './textures/roughness_map.jpg' );
  47. alphaTexture.wrapS = THREE.RepeatWrapping;
  48. alphaTexture.wrapT = THREE.RepeatWrapping;
  49. //lights
  50. const addLight = ( hexColor, power = 1700, distance = 100 ) => {
  51. const material = new THREE.MeshStandardNodeMaterial();
  52. material.colorNode = color( hexColor );
  53. material.lights = false;
  54. const mesh = new THREE.Mesh( sphereGeometry, material );
  55. const light = new THREE.PointLight( hexColor, 1, distance );
  56. light.power = power;
  57. light.add( mesh );
  58. scene.add( light );
  59. return light;
  60. };
  61. light1 = addLight( 0xff0040 );
  62. light2 = addLight( 0x0040ff );
  63. light3 = addLight( 0x80ff80 );
  64. light4 = addLight( 0xffaa00 );
  65. //light nodes ( selective lights )
  66. const redLightsNode = lights( [ light1 ] );
  67. const blueLightsNode = lights( [ light2 ] );
  68. //models
  69. const geometryTeapot = new TeapotGeometry( .8, 18 );
  70. const leftObject = new THREE.Mesh( geometryTeapot, new THREE.MeshStandardNodeMaterial( { color: 0x555555 } ) );
  71. leftObject.material.lightsNode = redLightsNode;
  72. leftObject.material.roughnessNode = texture( alphaTexture );
  73. leftObject.material.metalness = 0;
  74. leftObject.position.x = - 3;
  75. scene.add( leftObject );
  76. const centerObject = new THREE.Mesh( geometryTeapot, new THREE.MeshStandardNodeMaterial( { color: 0x555555 } ) );
  77. centerObject.material.normalNode = normalMap( texture( normalMapTexture ) );
  78. centerObject.material.metalness = .5;
  79. centerObject.material.roughness = .5;
  80. scene.add( centerObject );
  81. const rightObject = new THREE.Mesh( geometryTeapot, new THREE.MeshStandardNodeMaterial( { color: 0x555555 } ) );
  82. rightObject.material.lightsNode = blueLightsNode;
  83. rightObject.material.metalnessNode = texture( alphaTexture );
  84. rightObject.position.x = 3;
  85. scene.add( rightObject );
  86. leftObject.rotation.y = centerObject.rotation.y = rightObject.rotation.y = Math.PI * - 0.5;
  87. leftObject.position.y = centerObject.position.y = rightObject.position.y = - 1;
  88. //renderer
  89. renderer = new THREE.WebGPURenderer( { antialias: true } );
  90. renderer.setPixelRatio( window.devicePixelRatio );
  91. renderer.setSize( window.innerWidth, window.innerHeight );
  92. renderer.setAnimationLoop( animate );
  93. document.body.appendChild( renderer.domElement );
  94. //controls
  95. controls = new OrbitControls( camera, renderer.domElement );
  96. controls.minDistance = 3;
  97. controls.maxDistance = 25;
  98. //stats
  99. stats = new Stats();
  100. document.body.appendChild( stats.dom );
  101. window.addEventListener( 'resize', onWindowResize );
  102. //gui
  103. const gui = new GUI();
  104. gui.add( centerObject.material, 'roughness', 0, 1, 0.01 );
  105. gui.add( centerObject.material, 'metalness', 0, 1, 0.01 );
  106. }
  107. function onWindowResize() {
  108. camera.aspect = window.innerWidth / window.innerHeight;
  109. camera.updateProjectionMatrix();
  110. renderer.setSize( window.innerWidth, window.innerHeight );
  111. }
  112. function animate() {
  113. const time = performance.now() / 1000;
  114. const lightTime = time * 0.5;
  115. light1.position.x = Math.sin( lightTime * 0.7 ) * 3;
  116. light1.position.y = Math.cos( lightTime * 0.5 ) * 4;
  117. light1.position.z = Math.cos( lightTime * 0.3 ) * 3;
  118. light2.position.x = Math.cos( lightTime * 0.3 ) * 3;
  119. light2.position.y = Math.sin( lightTime * 0.5 ) * 4;
  120. light2.position.z = Math.sin( lightTime * 0.7 ) * 3;
  121. light3.position.x = Math.sin( lightTime * 0.7 ) * 3;
  122. light3.position.y = Math.cos( lightTime * 0.3 ) * 4;
  123. light3.position.z = Math.sin( lightTime * 0.5 ) * 3;
  124. light4.position.x = Math.sin( lightTime * 0.3 ) * 3;
  125. light4.position.y = Math.cos( lightTime * 0.7 ) * 4;
  126. light4.position.z = Math.sin( lightTime * 0.5 ) * 3;
  127. /*
  128. @TODO: Used to test scene light change ( currently unavailable )
  129. if ( time > 2.0 && light1.parent === null ) scene.add( light1 );
  130. if ( time > 2.5 && light2.parent === null ) scene.add( light2 );
  131. if ( time > 3.0 && light3.parent === null ) scene.add( light3 );
  132. if ( time > 3.5 && light4.parent === null ) scene.add( light4 );
  133. */
  134. renderer.render( scene, camera );
  135. stats.update();
  136. }
  137. </script>
  138. </body>
  139. </html>
粤ICP备19079148号