webgl_materials_lightmap.html 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. <!-- Import maps polyfill -->
  11. <!-- Remove this when import maps will be widely supported -->
  12. <script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
  13. <script type="importmap">
  14. {
  15. "imports": {
  16. "three": "../build/three.module.js",
  17. "three/addons/": "./jsm/",
  18. "three/nodes": "./jsm/nodes/Nodes.js"
  19. }
  20. }
  21. </script>
  22. <script type="module">
  23. import * as THREE from 'three';
  24. import Stats from 'three/addons/libs/stats.module.js';
  25. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  26. import { MeshBasicNodeMaterial, vec4, color, positionLocal, mix } from 'three/nodes';
  27. import { nodeFrame } from 'three/addons/renderers/webgl/nodes/WebGLNodes.js';
  28. let container, stats;
  29. let camera, scene, renderer;
  30. init().then( animate );
  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( 0xaabbff, 0.3 );
  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 ).convertSRGBToLinear();
  48. const bottomColor = new THREE.Color( 0xffffff ).convertSRGBToLinear();
  49. const offset = 400;
  50. const exponent = 0.6;
  51. const h = positionLocal.add( offset ).normalize().y;
  52. const skyMat = new 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. // RENDERER
  58. renderer = new THREE.WebGLRenderer( { antialias: true } );
  59. renderer.setPixelRatio( window.devicePixelRatio );
  60. renderer.setSize( innerWidth, innerHeight );
  61. container.appendChild( renderer.domElement );
  62. renderer.outputEncoding = THREE.sRGBEncoding;
  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. // MODEL
  71. const loader = new THREE.ObjectLoader();
  72. const object = await loader.loadAsync( 'models/json/lightmap/lightmap.json' );
  73. scene.add( object );
  74. //
  75. window.addEventListener( 'resize', onWindowResize );
  76. }
  77. function onWindowResize() {
  78. camera.aspect = window.innerWidth / window.innerHeight;
  79. camera.updateProjectionMatrix();
  80. renderer.setSize( window.innerWidth, window.innerHeight );
  81. }
  82. //
  83. function animate() {
  84. requestAnimationFrame( animate );
  85. nodeFrame.update();
  86. renderer.render( scene, camera );
  87. stats.update();
  88. }
  89. </script>
  90. </body>
  91. </html>