webgpu_materials_lightmap.html 2.9 KB

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