webgpu_materials_lightmap.html 3.0 KB

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