webgpu_lights_selective.html 6.5 KB

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