webgpu_lightprobe.html 5.0 KB

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