webgl_materials_lightmap.html 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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="x-shader/x-vertex" id="vertexShader">
  11. varying vec3 vWorldPosition;
  12. void main() {
  13. vec4 worldPosition = modelMatrix * vec4( position, 1.0 );
  14. vWorldPosition = worldPosition.xyz;
  15. gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
  16. }
  17. </script>
  18. <script type="x-shader/x-fragment" id="fragmentShader">
  19. uniform vec3 topColor;
  20. uniform vec3 bottomColor;
  21. uniform float offset;
  22. uniform float exponent;
  23. varying vec3 vWorldPosition;
  24. void main() {
  25. float h = normalize( vWorldPosition + offset ).y;
  26. gl_FragColor = vec4( mix( bottomColor, topColor, max( pow( max( h, 0.0 ), exponent ), 0.0 ) ), 1.0 );
  27. }
  28. </script>
  29. <!-- Import maps polyfill -->
  30. <!-- Remove this when import maps will be widely supported -->
  31. <script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
  32. <script type="importmap">
  33. {
  34. "imports": {
  35. "three": "../build/three.module.js",
  36. "three/addons/": "./jsm/"
  37. }
  38. }
  39. </script>
  40. <script type="module">
  41. import * as THREE from 'three';
  42. import Stats from 'three/addons/libs/stats.module.js';
  43. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  44. const SCREEN_WIDTH = window.innerWidth;
  45. const SCREEN_HEIGHT = window.innerHeight;
  46. let container, stats;
  47. let camera, scene, renderer;
  48. await init();
  49. animate();
  50. async function init() {
  51. container = document.createElement( 'div' );
  52. document.body.appendChild( container );
  53. // CAMERA
  54. camera = new THREE.PerspectiveCamera( 40, SCREEN_WIDTH / SCREEN_HEIGHT, 1, 10000 );
  55. camera.position.set( 700, 200, - 500 );
  56. // SCENE
  57. scene = new THREE.Scene();
  58. // LIGHTS
  59. const light = new THREE.DirectionalLight( 0xaabbff, 0.3 );
  60. light.position.x = 300;
  61. light.position.y = 250;
  62. light.position.z = - 500;
  63. scene.add( light );
  64. // SKYDOME
  65. const vertexShader = document.getElementById( 'vertexShader' ).textContent;
  66. const fragmentShader = document.getElementById( 'fragmentShader' ).textContent;
  67. const uniforms = {
  68. topColor: { value: new THREE.Color( 0x0077ff ) },
  69. bottomColor: { value: new THREE.Color( 0xffffff ) },
  70. offset: { value: 400 },
  71. exponent: { value: 0.6 }
  72. };
  73. uniforms.topColor.value.copy( light.color );
  74. const skyGeo = new THREE.SphereGeometry( 4000, 32, 15 );
  75. const skyMat = new THREE.ShaderMaterial( {
  76. uniforms: uniforms,
  77. vertexShader: vertexShader,
  78. fragmentShader: fragmentShader,
  79. side: THREE.BackSide
  80. } );
  81. const sky = new THREE.Mesh( skyGeo, skyMat );
  82. scene.add( sky );
  83. // RENDERER
  84. renderer = new THREE.WebGLRenderer( { antialias: true } );
  85. renderer.setPixelRatio( window.devicePixelRatio );
  86. renderer.setSize( SCREEN_WIDTH, SCREEN_HEIGHT );
  87. container.appendChild( renderer.domElement );
  88. renderer.outputEncoding = THREE.sRGBEncoding;
  89. // CONTROLS
  90. const controls = new OrbitControls( camera, renderer.domElement );
  91. controls.maxPolarAngle = 0.9 * Math.PI / 2;
  92. controls.enableZoom = false;
  93. // STATS
  94. stats = new Stats();
  95. container.appendChild( stats.dom );
  96. // MODEL
  97. const loader = new THREE.ObjectLoader();
  98. const object = await loader.loadAsync( 'models/json/lightmap/lightmap.json' );
  99. scene.add( object );
  100. //
  101. window.addEventListener( 'resize', onWindowResize );
  102. }
  103. function onWindowResize() {
  104. camera.aspect = window.innerWidth / window.innerHeight;
  105. camera.updateProjectionMatrix();
  106. renderer.setSize( window.innerWidth, window.innerHeight );
  107. }
  108. //
  109. function animate() {
  110. requestAnimationFrame( animate );
  111. renderer.render( scene, camera );
  112. stats.update();
  113. }
  114. </script>
  115. </body>
  116. </html>