webgl_materials_lightmap.html 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. }
  37. }
  38. </script>
  39. <script type="module">
  40. import * as THREE from 'three';
  41. import Stats from './jsm/libs/stats.module.js';
  42. import { OrbitControls } from './jsm/controls/OrbitControls.js';
  43. const SCREEN_WIDTH = window.innerWidth;
  44. const SCREEN_HEIGHT = window.innerHeight;
  45. let container, stats;
  46. let camera, scene, renderer;
  47. await init();
  48. animate();
  49. async function init() {
  50. container = document.createElement( 'div' );
  51. document.body.appendChild( container );
  52. // CAMERA
  53. camera = new THREE.PerspectiveCamera( 40, SCREEN_WIDTH / SCREEN_HEIGHT, 1, 10000 );
  54. camera.position.set( 700, 200, - 500 );
  55. // SCENE
  56. scene = new THREE.Scene();
  57. // LIGHTS
  58. const light = new THREE.DirectionalLight( 0xaabbff, 0.3 );
  59. light.position.x = 300;
  60. light.position.y = 250;
  61. light.position.z = - 500;
  62. scene.add( light );
  63. // SKYDOME
  64. const vertexShader = document.getElementById( 'vertexShader' ).textContent;
  65. const fragmentShader = document.getElementById( 'fragmentShader' ).textContent;
  66. const uniforms = {
  67. topColor: { value: new THREE.Color( 0x0077ff ) },
  68. bottomColor: { value: new THREE.Color( 0xffffff ) },
  69. offset: { value: 400 },
  70. exponent: { value: 0.6 }
  71. };
  72. uniforms.topColor.value.copy( light.color );
  73. const skyGeo = new THREE.SphereGeometry( 4000, 32, 15 );
  74. const skyMat = new THREE.ShaderMaterial( {
  75. uniforms: uniforms,
  76. vertexShader: vertexShader,
  77. fragmentShader: fragmentShader,
  78. side: THREE.BackSide
  79. } );
  80. const sky = new THREE.Mesh( skyGeo, skyMat );
  81. scene.add( sky );
  82. // RENDERER
  83. renderer = new THREE.WebGLRenderer( { antialias: true } );
  84. renderer.setPixelRatio( window.devicePixelRatio );
  85. renderer.setSize( SCREEN_WIDTH, SCREEN_HEIGHT );
  86. container.appendChild( renderer.domElement );
  87. renderer.outputEncoding = THREE.sRGBEncoding;
  88. // CONTROLS
  89. const controls = new OrbitControls( camera, renderer.domElement );
  90. controls.maxPolarAngle = 0.9 * Math.PI / 2;
  91. controls.enableZoom = false;
  92. // STATS
  93. stats = new Stats();
  94. container.appendChild( stats.dom );
  95. // MODEL
  96. const loader = new THREE.ObjectLoader();
  97. const object = await loader.loadAsync( 'models/json/lightmap/lightmap.json' );
  98. scene.add( object );
  99. //
  100. window.addEventListener( 'resize', onWindowResize );
  101. }
  102. function onWindowResize() {
  103. camera.aspect = window.innerWidth / window.innerHeight;
  104. camera.updateProjectionMatrix();
  105. renderer.setSize( window.innerWidth, window.innerHeight );
  106. }
  107. //
  108. function animate() {
  109. requestAnimationFrame( animate );
  110. renderer.render( scene, camera );
  111. stats.update();
  112. }
  113. </script>
  114. </body>
  115. </html>