webgpu_materials_lightmap.html 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgpu - lightmap</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. <script type="importmap">
  11. {
  12. "imports": {
  13. "three": "../build/three.webgpu.js",
  14. "three/webgpu": "../build/three.webgpu.js",
  15. "three/tsl": "../build/three.tsl.js",
  16. "three/addons/": "./jsm/"
  17. }
  18. }
  19. </script>
  20. <script type="module">
  21. import * as THREE from 'three/webgpu';
  22. import { vec4, color, positionLocal, mix } from 'three/tsl';
  23. import { Inspector } from 'three/addons/inspector/Inspector.js';
  24. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  25. let container;
  26. let camera, scene, renderer;
  27. const params = {
  28. intensity: 1
  29. };
  30. init();
  31. async function init() {
  32. const { innerWidth, innerHeight } = window;
  33. container = document.createElement( 'div' );
  34. document.body.appendChild( container );
  35. // CAMERA
  36. camera = new THREE.PerspectiveCamera( 40, innerWidth / innerHeight, 1, 10000 );
  37. camera.position.set( 700, 200, - 500 );
  38. // SCENE
  39. scene = new THREE.Scene();
  40. // LIGHTS
  41. const light = new THREE.DirectionalLight( 0xd5deff );
  42. light.position.x = 300;
  43. light.position.y = 250;
  44. light.position.z = - 500;
  45. scene.add( light );
  46. // SKYDOME
  47. const topColor = new THREE.Color().copy( light.color );
  48. const bottomColor = new THREE.Color( 0xffffff );
  49. const offset = 400;
  50. const exponent = 0.6;
  51. const h = positionLocal.add( offset ).normalize().y;
  52. const skyMat = new THREE.MeshBasicNodeMaterial();
  53. skyMat.colorNode = vec4( mix( color( bottomColor ), color( topColor ), h.max( 0.0 ).pow( exponent ) ), 1.0 );
  54. skyMat.side = THREE.BackSide;
  55. const sky = new THREE.Mesh( new THREE.SphereGeometry( 4000, 32, 15 ), skyMat );
  56. scene.add( sky );
  57. // MODEL
  58. const loader = new THREE.ObjectLoader();
  59. const object = await loader.loadAsync( 'models/json/lightmap/lightmap.json' );
  60. scene.add( object );
  61. // RENDERER
  62. renderer = new THREE.WebGPURenderer( { antialias: true } );
  63. renderer.setAnimationLoop( animate );
  64. renderer.setPixelRatio( window.devicePixelRatio );
  65. renderer.setSize( innerWidth, innerHeight );
  66. renderer.inspector = new Inspector();
  67. container.appendChild( renderer.domElement );
  68. // CONTROLS
  69. const controls = new OrbitControls( camera, renderer.domElement );
  70. controls.maxPolarAngle = 0.9 * Math.PI / 2;
  71. controls.enableZoom = false;
  72. // GUI
  73. const gui = renderer.inspector.createParameters( 'Parameters' );
  74. gui.add( params, 'intensity', 0, 1 ).name( 'Light Map Intensity' ).onChange( ( value ) => {
  75. for ( const material of object.material ) {
  76. material.lightMapIntensity = value;
  77. }
  78. } );
  79. //
  80. window.addEventListener( 'resize', onWindowResize );
  81. }
  82. function onWindowResize() {
  83. camera.aspect = window.innerWidth / window.innerHeight;
  84. camera.updateProjectionMatrix();
  85. renderer.setSize( window.innerWidth, window.innerHeight );
  86. }
  87. //
  88. function animate() {
  89. renderer.render( scene, camera );
  90. }
  91. </script>
  92. </body>
  93. </html>
粤ICP备19079148号