webgpu_lights_physical.html 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgpu - physical 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 - physical lights">
  8. <meta property="og:type" content="website">
  9. <meta property="og:url" content="https://threejs.org/examples/webgpu_lights_physical.html">
  10. <meta property="og:image" content="https://threejs.org/examples/screenshots/webgpu_lights_physical.jpg">
  11. <link type="text/css" rel="stylesheet" href="example.css">
  12. </head>
  13. <body>
  14. <div id="container"></div>
  15. <div id="info">
  16. <a href="https://threejs.org/" target="_blank" rel="noopener" class="logo-link"></a>
  17. <div class="title-wrapper">
  18. <a href="https://threejs.org/" target="_blank" rel="noopener">three.js</a><span>Physical Lighting Model</span>
  19. </div>
  20. <small>
  21. Physically accurate incandescent bulb by <a href="http://clara.io" target="_blank" rel="noopener">Ben Houston</a><br />
  22. Real world scale: Brick cube is 50 cm in size. Globe is 50 cm in diameter.<br/>
  23. Reinhard tonemapping with real-world light falloff (decay = 2).
  24. </small>
  25. </div>
  26. <script type="importmap">
  27. {
  28. "imports": {
  29. "three": "../build/three.webgpu.js",
  30. "three/tsl": "../build/three.tsl.js",
  31. "three/webgpu": "../build/three.webgpu.js",
  32. "three/addons/": "./jsm/"
  33. }
  34. }
  35. </script>
  36. <script type="module">
  37. import * as THREE from 'three/webgpu';
  38. import { Inspector } from 'three/addons/inspector/Inspector.js';
  39. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  40. let camera, scene, renderer, bulbLight, bulbMat, hemiLight;
  41. let ballMat, cubeMat, floorMat;
  42. let previousShadowMap = false;
  43. // ref for lumens: http://www.power-sure.com/lumens.htm
  44. const bulbLuminousPowers = {
  45. '110000 lm (1000W)': 110000,
  46. '3500 lm (300W)': 3500,
  47. '1700 lm (100W)': 1700,
  48. '800 lm (60W)': 800,
  49. '400 lm (40W)': 400,
  50. '180 lm (25W)': 180,
  51. '20 lm (4W)': 20,
  52. 'Off': 0
  53. };
  54. // ref for solar irradiances: https://en.wikipedia.org/wiki/Lux
  55. const hemiLuminousIrradiances = {
  56. '0.0001 lx (Moonless Night)': 0.0001,
  57. '0.002 lx (Night Airglow)': 0.002,
  58. '0.5 lx (Full Moon)': 0.5,
  59. '3.4 lx (City Twilight)': 3.4,
  60. '50 lx (Living Room)': 50,
  61. '100 lx (Very Overcast)': 100,
  62. '350 lx (Office Room)': 350,
  63. '400 lx (Sunrise/Sunset)': 400,
  64. '1000 lx (Overcast)': 1000,
  65. '18000 lx (Daylight)': 18000,
  66. '50000 lx (Direct Sun)': 50000
  67. };
  68. const params = {
  69. shadows: true,
  70. exposure: 0.68,
  71. bulbPower: Object.keys( bulbLuminousPowers )[ 4 ],
  72. hemiIrradiance: Object.keys( hemiLuminousIrradiances )[ 0 ]
  73. };
  74. init();
  75. function init() {
  76. const container = document.getElementById( 'container' );
  77. //
  78. camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 0.1, 100 );
  79. camera.position.x = - 4;
  80. camera.position.z = 4;
  81. camera.position.y = 2;
  82. scene = new THREE.Scene();
  83. const bulbGeometry = new THREE.SphereGeometry( 0.02, 16, 8 );
  84. bulbLight = new THREE.PointLight( 0xffee88, 1, 100, 2 );
  85. bulbMat = new THREE.MeshStandardMaterial( {
  86. emissive: 0xffffee,
  87. emissiveIntensity: 1,
  88. color: 0x000000
  89. } );
  90. bulbLight.add( new THREE.Mesh( bulbGeometry, bulbMat ) );
  91. bulbLight.position.set( 0, 2, 0 );
  92. bulbLight.castShadow = true;
  93. scene.add( bulbLight );
  94. hemiLight = new THREE.HemisphereLight( 0xddeeff, 0x0f0e0d, 0.02 );
  95. scene.add( hemiLight );
  96. floorMat = new THREE.MeshStandardMaterial( {
  97. roughness: 0.8,
  98. color: 0xffffff,
  99. metalness: 0.2,
  100. bumpScale: 1
  101. } );
  102. const textureLoader = new THREE.TextureLoader();
  103. textureLoader.load( 'textures/hardwood2_diffuse.jpg', function ( map ) {
  104. map.wrapS = THREE.RepeatWrapping;
  105. map.wrapT = THREE.RepeatWrapping;
  106. map.anisotropy = 4;
  107. map.repeat.set( 10, 24 );
  108. map.colorSpace = THREE.SRGBColorSpace;
  109. floorMat.map = map;
  110. floorMat.needsUpdate = true;
  111. } );
  112. textureLoader.load( 'textures/hardwood2_bump.jpg', function ( map ) {
  113. map.wrapS = THREE.RepeatWrapping;
  114. map.wrapT = THREE.RepeatWrapping;
  115. map.anisotropy = 4;
  116. map.repeat.set( 10, 24 );
  117. floorMat.bumpMap = map;
  118. floorMat.needsUpdate = true;
  119. } );
  120. textureLoader.load( 'textures/hardwood2_roughness.jpg', function ( map ) {
  121. map.wrapS = THREE.RepeatWrapping;
  122. map.wrapT = THREE.RepeatWrapping;
  123. map.anisotropy = 4;
  124. map.repeat.set( 10, 24 );
  125. floorMat.roughnessMap = map;
  126. floorMat.needsUpdate = true;
  127. } );
  128. cubeMat = new THREE.MeshStandardMaterial( {
  129. roughness: 0.7,
  130. color: 0xffffff,
  131. bumpScale: 1,
  132. metalness: 0.2
  133. } );
  134. textureLoader.load( 'textures/brick_diffuse.jpg', function ( map ) {
  135. map.wrapS = THREE.RepeatWrapping;
  136. map.wrapT = THREE.RepeatWrapping;
  137. map.anisotropy = 4;
  138. map.repeat.set( 1, 1 );
  139. map.colorSpace = THREE.SRGBColorSpace;
  140. cubeMat.map = map;
  141. cubeMat.needsUpdate = true;
  142. } );
  143. textureLoader.load( 'textures/brick_bump.jpg', function ( map ) {
  144. map.wrapS = THREE.RepeatWrapping;
  145. map.wrapT = THREE.RepeatWrapping;
  146. map.anisotropy = 4;
  147. map.repeat.set( 1, 1 );
  148. cubeMat.bumpMap = map;
  149. cubeMat.needsUpdate = true;
  150. } );
  151. ballMat = new THREE.MeshStandardMaterial( {
  152. color: 0xffffff,
  153. roughness: 0.5,
  154. metalness: 1.0
  155. } );
  156. textureLoader.load( 'textures/planets/earth_atmos_2048.jpg', function ( map ) {
  157. map.anisotropy = 4;
  158. map.colorSpace = THREE.SRGBColorSpace;
  159. ballMat.map = map;
  160. ballMat.needsUpdate = true;
  161. } );
  162. textureLoader.load( 'textures/planets/earth_specular_2048.jpg', function ( map ) {
  163. map.anisotropy = 4;
  164. map.colorSpace = THREE.SRGBColorSpace;
  165. ballMat.metalnessMap = map;
  166. ballMat.needsUpdate = true;
  167. } );
  168. const floorGeometry = new THREE.PlaneGeometry( 20, 20 );
  169. const floorMesh = new THREE.Mesh( floorGeometry, floorMat );
  170. floorMesh.receiveShadow = true;
  171. floorMesh.rotation.x = - Math.PI / 2.0;
  172. scene.add( floorMesh );
  173. const ballGeometry = new THREE.SphereGeometry( 0.25, 32, 32 );
  174. const ballMesh = new THREE.Mesh( ballGeometry, ballMat );
  175. ballMesh.position.set( 1, 0.25, 1 );
  176. ballMesh.rotation.y = Math.PI;
  177. ballMesh.castShadow = true;
  178. scene.add( ballMesh );
  179. const boxGeometry = new THREE.BoxGeometry( 0.5, 0.5, 0.5 );
  180. const boxMesh = new THREE.Mesh( boxGeometry, cubeMat );
  181. boxMesh.position.set( - 0.5, 0.25, - 1 );
  182. boxMesh.castShadow = true;
  183. scene.add( boxMesh );
  184. const boxMesh2 = new THREE.Mesh( boxGeometry, cubeMat );
  185. boxMesh2.position.set( 0, 0.25, - 5 );
  186. boxMesh2.castShadow = true;
  187. scene.add( boxMesh2 );
  188. const boxMesh3 = new THREE.Mesh( boxGeometry, cubeMat );
  189. boxMesh3.position.set( 7, 0.25, 0 );
  190. boxMesh3.castShadow = true;
  191. scene.add( boxMesh3 );
  192. //
  193. renderer = new THREE.WebGPURenderer();
  194. renderer.setPixelRatio( window.devicePixelRatio );
  195. renderer.setSize( window.innerWidth, window.innerHeight );
  196. renderer.setAnimationLoop( animate );
  197. renderer.shadowMap.enabled = true;
  198. renderer.toneMapping = THREE.ReinhardToneMapping;
  199. renderer.inspector = new Inspector();
  200. container.appendChild( renderer.domElement );
  201. const controls = new OrbitControls( camera, renderer.domElement );
  202. controls.minDistance = 1;
  203. controls.maxDistance = 20;
  204. window.addEventListener( 'resize', onWindowResize );
  205. //
  206. const gui = renderer.inspector.createParameters( 'Settings' );
  207. gui.add( params, 'hemiIrradiance', Object.keys( hemiLuminousIrradiances ) );
  208. gui.add( params, 'bulbPower', Object.keys( bulbLuminousPowers ) );
  209. gui.add( params, 'exposure', 0, 1 );
  210. gui.add( params, 'shadows' );
  211. }
  212. function onWindowResize() {
  213. camera.aspect = window.innerWidth / window.innerHeight;
  214. camera.updateProjectionMatrix();
  215. renderer.setSize( window.innerWidth, window.innerHeight );
  216. }
  217. //
  218. function animate() {
  219. renderer.toneMappingExposure = Math.pow( params.exposure, 5.0 ); // to allow for very bright scenes.
  220. renderer.shadowMap.enabled = params.shadows;
  221. bulbLight.castShadow = params.shadows;
  222. if ( params.shadows !== previousShadowMap ) {
  223. ballMat.needsUpdate = true;
  224. cubeMat.needsUpdate = true;
  225. floorMat.needsUpdate = true;
  226. previousShadowMap = params.shadows;
  227. }
  228. bulbLight.power = bulbLuminousPowers[ params.bulbPower ];
  229. bulbMat.emissiveIntensity = bulbLight.intensity / Math.pow( 0.02, 2.0 ); // convert from intensity to irradiance at bulb surface
  230. hemiLight.intensity = hemiLuminousIrradiances[ params.hemiIrradiance ];
  231. const time = Date.now() * 0.0005;
  232. bulbLight.position.y = Math.cos( time ) * 0.75 + 1.25;
  233. renderer.render( scene, camera );
  234. }
  235. </script>
  236. </body>
  237. </html>
粤ICP备19079148号