webgpu_lightprobe.html 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgpu - light probe</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="example.css">
  8. </head>
  9. <body>
  10. <div id="info">
  11. <a href="https://threejs.org/" target="_blank" rel="noopener" class="logo-link"></a>
  12. <div class="title-wrapper">
  13. <a href="https://threejs.org/" target="_blank" rel="noopener">three.js</a><span>Light Probe</span>
  14. </div>
  15. <small>
  16. Light probes and environment lighting.
  17. </small>
  18. </div>
  19. <script type="importmap">
  20. {
  21. "imports": {
  22. "three": "../build/three.webgpu.js",
  23. "three/webgpu": "../build/three.webgpu.js",
  24. "three/tsl": "../build/three.tsl.js",
  25. "three/addons/": "./jsm/"
  26. }
  27. }
  28. </script>
  29. <script type="module">
  30. import * as THREE from 'three/webgpu';
  31. import { Inspector } from 'three/addons/inspector/Inspector.js';
  32. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  33. import { LightProbeGenerator } from 'three/addons/lights/LightProbeGenerator.js';
  34. import { LightProbeHelper } from 'three/addons/helpers/LightProbeHelperGPU.js';
  35. let mesh, renderer, scene, camera;
  36. let gui;
  37. let lightProbe;
  38. let directionalLight;
  39. // linear color space
  40. const API = {
  41. lightProbeIntensity: 1.0,
  42. directionalLightIntensity: 0.6,
  43. envMapIntensity: 1
  44. };
  45. init();
  46. function init() {
  47. // renderer
  48. renderer = new THREE.WebGPURenderer( { antialias: true } );
  49. renderer.setPixelRatio( window.devicePixelRatio );
  50. renderer.setSize( window.innerWidth, window.innerHeight );
  51. renderer.setAnimationLoop( animate );
  52. renderer.inspector = new Inspector();
  53. document.body.appendChild( renderer.domElement );
  54. // tone mapping
  55. renderer.toneMapping = THREE.NoToneMapping;
  56. // scene
  57. scene = new THREE.Scene();
  58. // camera
  59. camera = new THREE.PerspectiveCamera( 40, window.innerWidth / window.innerHeight, 1, 1000 );
  60. camera.position.set( 0, 0, 30 );
  61. // controls
  62. const controls = new OrbitControls( camera, renderer.domElement );
  63. controls.minDistance = 10;
  64. controls.maxDistance = 50;
  65. controls.enablePan = false;
  66. // probe
  67. lightProbe = new THREE.LightProbe();
  68. scene.add( lightProbe );
  69. // light
  70. directionalLight = new THREE.DirectionalLight( 0xffffff, API.directionalLightIntensity );
  71. directionalLight.position.set( 10, 10, 10 );
  72. scene.add( directionalLight );
  73. // envmap
  74. const genCubeUrls = function ( prefix, postfix ) {
  75. return [
  76. prefix + 'px' + postfix, prefix + 'nx' + postfix,
  77. prefix + 'py' + postfix, prefix + 'ny' + postfix,
  78. prefix + 'pz' + postfix, prefix + 'nz' + postfix
  79. ];
  80. };
  81. const urls = genCubeUrls( 'textures/cube/pisa/', '.png' );
  82. new THREE.CubeTextureLoader().load( urls, function ( cubeTexture ) {
  83. scene.background = cubeTexture;
  84. lightProbe.copy( LightProbeGenerator.fromCubeTexture( cubeTexture ) );
  85. lightProbe.intensity = API.lightProbeIntensity;
  86. lightProbe.position.set( - 10, 0, 0 ); // position not used in scene lighting calculations (helper honors the position, however)
  87. const geometry = new THREE.SphereGeometry( 5, 64, 32 );
  88. //const geometry = new THREE.TorusKnotGeometry( 4, 1.5, 256, 32, 2, 3 );
  89. const material = new THREE.MeshStandardMaterial( {
  90. color: 0xffffff,
  91. metalness: 0,
  92. roughness: 0,
  93. envMap: cubeTexture,
  94. envMapIntensity: API.envMapIntensity,
  95. } );
  96. // mesh
  97. mesh = new THREE.Mesh( geometry, material );
  98. scene.add( mesh );
  99. // helper
  100. const helper = new LightProbeHelper( lightProbe, 1 );
  101. scene.add( helper );
  102. } );
  103. // gui
  104. gui = renderer.inspector.createParameters( 'Intensity' );
  105. gui.add( API, 'lightProbeIntensity', 0, 1, 0.02 )
  106. .name( 'light probe' )
  107. .onChange( function () {
  108. lightProbe.intensity = API.lightProbeIntensity;
  109. } );
  110. gui.add( API, 'directionalLightIntensity', 0, 1, 0.02 )
  111. .name( 'directional light' )
  112. .onChange( function () {
  113. directionalLight.intensity = API.directionalLightIntensity;
  114. } );
  115. gui.add( API, 'envMapIntensity', 0, 1, 0.02 )
  116. .name( 'envMap' )
  117. .onChange( function () {
  118. mesh.material.envMapIntensity = API.envMapIntensity;
  119. } );
  120. // listener
  121. window.addEventListener( 'resize', onWindowResize );
  122. }
  123. function onWindowResize() {
  124. renderer.setSize( window.innerWidth, window.innerHeight );
  125. camera.aspect = window.innerWidth / window.innerHeight;
  126. camera.updateProjectionMatrix();
  127. }
  128. function animate() {
  129. renderer.render( scene, camera );
  130. }
  131. </script>
  132. </body>
  133. </html>
粤ICP备19079148号