webgl_materials_lightmap.html 3.7 KB

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