123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165 |
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <title>three.js webgl - materials - lightmap</title>
- <meta charset="utf-8">
- <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
- <link type="text/css" rel="stylesheet" href="main.css">
- </head>
- <body>
- <script type="x-shader/x-vertex" id="vertexShader">
- varying vec3 vWorldPosition;
- void main() {
- vec4 worldPosition = modelMatrix * vec4( position, 1.0 );
- vWorldPosition = worldPosition.xyz;
- gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
- }
- </script>
- <script type="x-shader/x-fragment" id="fragmentShader">
- uniform vec3 topColor;
- uniform vec3 bottomColor;
- uniform float offset;
- uniform float exponent;
- varying vec3 vWorldPosition;
- void main() {
- float h = normalize( vWorldPosition + offset ).y;
- gl_FragColor = vec4( mix( bottomColor, topColor, max( pow( max( h, 0.0 ), exponent ), 0.0 ) ), 1.0 );
- }
- </script>
- <script type="module">
- import * as THREE from '../build/three.module.js';
- import Stats from './jsm/libs/stats.module.js';
- import { OrbitControls } from './jsm/controls/OrbitControls.js';
- var SCREEN_WIDTH = window.innerWidth;
- var SCREEN_HEIGHT = window.innerHeight;
- var container, stats;
- var camera, scene, renderer;
- init();
- animate();
- function init() {
- container = document.createElement( 'div' );
- document.body.appendChild( container );
- // CAMERA
- camera = new THREE.PerspectiveCamera( 40, SCREEN_WIDTH / SCREEN_HEIGHT, 1, 10000 );
- camera.position.set( 700, 200, - 500 );
- // SCENE
- scene = new THREE.Scene();
- // LIGHTS
- var light = new THREE.DirectionalLight( 0xaabbff, 0.3 );
- light.position.x = 300;
- light.position.y = 250;
- light.position.z = - 500;
- scene.add( light );
- // SKYDOME
- var vertexShader = document.getElementById( 'vertexShader' ).textContent;
- var fragmentShader = document.getElementById( 'fragmentShader' ).textContent;
- var uniforms = {
- topColor: { value: new THREE.Color( 0x0077ff ) },
- bottomColor: { value: new THREE.Color( 0xffffff ) },
- offset: { value: 400 },
- exponent: { value: 0.6 }
- };
- uniforms.topColor.value.copy( light.color );
- var skyGeo = new THREE.SphereBufferGeometry( 4000, 32, 15 );
- var skyMat = new THREE.ShaderMaterial( {
- uniforms: uniforms,
- vertexShader: vertexShader,
- fragmentShader: fragmentShader,
- side: THREE.BackSide
- } );
- var sky = new THREE.Mesh( skyGeo, skyMat );
- scene.add( sky );
- // RENDERER
- renderer = new THREE.WebGLRenderer( { antialias: true } );
- renderer.setPixelRatio( window.devicePixelRatio );
- renderer.setSize( SCREEN_WIDTH, SCREEN_HEIGHT );
- container.appendChild( renderer.domElement );
- renderer.outputEncoding = THREE.sRGBEncoding;
- // CONTROLS
- var controls = new OrbitControls( camera, renderer.domElement );
- controls.maxPolarAngle = 0.9 * Math.PI / 2;
- controls.enableZoom = false;
- // STATS
- stats = new Stats();
- container.appendChild( stats.dom );
- // MODEL
- var loader = new THREE.ObjectLoader();
- loader.load( "models/json/lightmap/lightmap.json", function ( object ) {
- scene.add( object );
- } );
- //
- window.addEventListener( 'resize', onWindowResize, false );
- }
- function onWindowResize() {
- camera.aspect = window.innerWidth / window.innerHeight;
- camera.updateProjectionMatrix();
- renderer.setSize( window.innerWidth, window.innerHeight );
- }
- //
- function animate() {
- requestAnimationFrame( animate );
- renderer.render( scene, camera );
- stats.update();
- }
- </script>
- </body>
- </html>
|