123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206 |
- <!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">
- <style>
- body {
- background:#fff;
- padding:0;
- margin:0;
- overflow:hidden;
- font-family:georgia;
- text-align:center;
- }
- h1 { }
- a { color:skyblue }
- #stats { position: absolute; top:0; left: 0 }
- #stats #fps { background: transparent !important }
- #stats #fps #fpsText { color: #abc !important }
- #stats #fps #fpsGraph { display: none }
- </style>
- </head>
- <body>
- <script src="../build/three.min.js"></script>
- <script src="js/controls/TrackballControls.js"></script>
- <script src="js/Detector.js"></script>
- <script src="js/libs/stats.min.js"></script>
- <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( h, exponent ), 0.0 ) ), 1.0 );
- }
- </script>
- <script>
- var SCREEN_WIDTH = window.innerWidth;
- var SCREEN_HEIGHT = window.innerHeight;
- var container,stats;
- var camera, scene, renderer;
- var clock = new THREE.Clock();
- 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.x = 700;
- camera.position.z = -500;
- camera.position.y = 180;
- // SCENE
- scene = new THREE.Scene();
- scene.fog = new THREE.Fog( 0xffffff, 1000, 10000 );
- // CONTROLS
- controls = new THREE.TrackballControls( camera );
- controls.target.z = 150;
- // LIGHTS
- var directionalLight = new THREE.DirectionalLight( 0xffffff, 1.475 );
- directionalLight.position.set( 100, 100, -100 );
- scene.add( directionalLight );
- var hemiLight = new THREE.HemisphereLight( 0xffffff, 0xffffff, 1.25 );
- hemiLight.color.setHSL( 0.6, 1, 0.75 );
- hemiLight.groundColor.setHSL( 0.1, 0.8, 0.7 );
- hemiLight.position.y = 500;
- scene.add( hemiLight );
- // SKYDOME
- var vertexShader = document.getElementById( 'vertexShader' ).textContent;
- var fragmentShader = document.getElementById( 'fragmentShader' ).textContent;
- var uniforms = {
- topColor: { type: "c", value: new THREE.Color( 0x0077ff ) },
- bottomColor: { type: "c", value: new THREE.Color( 0xffffff ) },
- offset: { type: "f", value: 400 },
- exponent: { type: "f", value: 0.6 }
- }
- uniforms.topColor.value.copy( hemiLight.color );
- scene.fog.color.copy( uniforms.bottomColor.value );
- var skyGeo = new THREE.SphereGeometry( 4000, 32, 15 );
- var skyMat = new THREE.ShaderMaterial( { vertexShader: vertexShader, fragmentShader: fragmentShader, uniforms: uniforms, side: THREE.BackSide } );
- var sky = new THREE.Mesh( skyGeo, skyMat );
- scene.add( sky );
- // RENDERER
- renderer = new THREE.WebGLRenderer( { antialias: true } );
- renderer.setClearColor( scene.fog.color, 1 );
- renderer.setSize( SCREEN_WIDTH, SCREEN_HEIGHT );
- renderer.domElement.style.position = "relative";
- container.appendChild( renderer.domElement );
- renderer.gammaInput = true;
- renderer.gammaOutput = true;
- // STATS
- stats = new Stats();
- container.appendChild( stats.domElement );
- // MODEL
- var loader = new THREE.JSONLoader();
- var callback = function ( geometry, materials ) { createScene( geometry, materials, 0, 0, 0, 0, 100 ) };
- loader.load( "obj/lightmap/lightmap.js", callback );
- //
- window.addEventListener( 'resize', onWindowResize, false );
- }
- function onWindowResize() {
- camera.aspect = window.innerWidth / window.innerHeight;
- camera.updateProjectionMatrix();
- renderer.setSize( window.innerWidth, window.innerHeight );
- }
- function createScene( geometry, materials, x, y, z, b, s ) {
- var mesh = new THREE.Mesh( geometry, new THREE.MeshFaceMaterial( materials ) );
- mesh.position.set( x, y, z );
- mesh.scale.set( s, s, s );
- scene.add( mesh );
- }
- //
- function animate() {
- requestAnimationFrame( animate );
- render();
- stats.update();
- }
- function render() {
- var delta = clock.getDelta();
- controls.update( delta );
- renderer.render( scene, camera );
- }
- </script>
- </body>
- </html>
|