|
@@ -0,0 +1,324 @@
|
|
|
|
+<!DOCTYPE html>
|
|
|
|
+<html lang="en">
|
|
|
|
+ <head>
|
|
|
|
+ <title>three.js webgl - lights - hemisphere light</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-color: #fff;
|
|
|
|
+ color: #111;
|
|
|
|
+ margin: 0px;
|
|
|
|
+ overflow: hidden;
|
|
|
|
+ font-family: Monospace;
|
|
|
|
+ font-size: 13px;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ #info {
|
|
|
|
+ position: absolute;
|
|
|
|
+ top: 0px; width: 100%;
|
|
|
|
+ padding: 5px;
|
|
|
|
+ text-align: center;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ a {
|
|
|
|
+ color: #0080ff;
|
|
|
|
+ text-decoration: none;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ a:hover {
|
|
|
|
+ color: #f00;
|
|
|
|
+ }
|
|
|
|
+ #footer { width: 100%; margin: 2em auto; text-align: center; position: absolute; bottom: 0 }
|
|
|
|
+ strong { color: red }
|
|
|
|
+ </style>
|
|
|
|
+ </head>
|
|
|
|
+ <body>
|
|
|
|
+
|
|
|
|
+ <div id="container"></div>
|
|
|
|
+ <div id="info">
|
|
|
|
+ <a href="http://github.com/mrdoob/three.js" target="_blank">three.js</a> - webgl hemisphere light example -
|
|
|
|
+ flamingo by <a href="http://mirada.com/">mirada</a> from <a href="http://ro.me">rome</a><br/>
|
|
|
|
+ </div>
|
|
|
|
+ <div id="footer">
|
|
|
|
+ press <strong>h</strong> to toggle hemisphere light, <strong>d</strong> to toggle directional light
|
|
|
|
+ </div>
|
|
|
|
+
|
|
|
|
+ <script src="../build/three.js"></script>
|
|
|
|
+
|
|
|
|
+ <script src="js/Detector.js"></script>
|
|
|
|
+ <script src="js/Stats.js"></script>
|
|
|
|
+
|
|
|
|
+ <script type="x-shader/x-vertex" id="vertexShader">
|
|
|
|
+
|
|
|
|
+ varying vec3 worldPosition;
|
|
|
|
+
|
|
|
|
+ void main() {
|
|
|
|
+
|
|
|
|
+ vec4 mPosition = modelMatrix * vec4( position, 1.0 );
|
|
|
|
+
|
|
|
|
+ gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
|
|
|
|
+ worldPosition = mPosition.xyz;
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ </script>
|
|
|
|
+
|
|
|
|
+ <script type="x-shader/x-fragment" id="fragmentShader">
|
|
|
|
+
|
|
|
|
+ uniform vec3 topColor;
|
|
|
|
+ uniform vec3 bottomColor;
|
|
|
|
+ uniform float offset;
|
|
|
|
+ uniform float exponent;
|
|
|
|
+
|
|
|
|
+ varying vec3 worldPosition;
|
|
|
|
+
|
|
|
|
+ void main() {
|
|
|
|
+
|
|
|
|
+ float h = normalize( worldPosition + offset ).y;
|
|
|
|
+ gl_FragColor = vec4( mix( bottomColor, topColor, max( pow( h, exponent ), 0.0 ) ), 1.0 );
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ </script>
|
|
|
|
+ <script>
|
|
|
|
+
|
|
|
|
+ if ( ! Detector.webgl ) Detector.addGetWebGLMessage();
|
|
|
|
+
|
|
|
|
+ var camera, scene, renderer, dirLight, hemiLight;
|
|
|
|
+ var morphs = [];
|
|
|
|
+ var stats;
|
|
|
|
+
|
|
|
|
+ var clock = new THREE.Clock();
|
|
|
|
+
|
|
|
|
+ init();
|
|
|
|
+ animate();
|
|
|
|
+
|
|
|
|
+ function init() {
|
|
|
|
+
|
|
|
|
+ var container = document.getElementById( 'container' );
|
|
|
|
+
|
|
|
|
+ camera = new THREE.PerspectiveCamera( 30, window.innerWidth / window.innerHeight, 1, 5000 );
|
|
|
|
+ camera.position.set( 0, 0, 250 );
|
|
|
|
+
|
|
|
|
+ scene = new THREE.Scene();
|
|
|
|
+
|
|
|
|
+ scene.fog = new THREE.Fog( 0xffffff, 1, 5000 );
|
|
|
|
+ scene.fog.color.setHSV( 0.6, 0, 1 );
|
|
|
|
+
|
|
|
|
+ /*
|
|
|
|
+ controls = new THREE.TrackballControls( camera );
|
|
|
|
+
|
|
|
|
+ controls.rotateSpeed = 1.0;
|
|
|
|
+ controls.zoomSpeed = 1.2;
|
|
|
|
+ controls.panSpeed = 0.8;
|
|
|
|
+
|
|
|
|
+ controls.noZoom = false;
|
|
|
|
+ controls.noPan = false;
|
|
|
|
+
|
|
|
|
+ controls.staticMoving = true;
|
|
|
|
+ controls.dynamicDampingFactor = 0.15;
|
|
|
|
+ */
|
|
|
|
+
|
|
|
|
+ // LIGHTS
|
|
|
|
+
|
|
|
|
+ hemiLight = new THREE.HemisphereLight( 0xffffff, 0xffffff, 0.6 );
|
|
|
|
+ hemiLight.color.setHSV( 0.6, 0.75, 1 );
|
|
|
|
+ hemiLight.groundColor.setHSV( 0.095, 0.5, 1 );
|
|
|
|
+ hemiLight.position.set( 0, 500, 0 );
|
|
|
|
+ scene.add( hemiLight );
|
|
|
|
+
|
|
|
|
+ //
|
|
|
|
+
|
|
|
|
+ dirLight = new THREE.DirectionalLight( 0xffffff, 1 );
|
|
|
|
+ dirLight.color.setHSV( 0.1, 0.1, 1 );
|
|
|
|
+ dirLight.position.set( -1, 1.75, 1 );
|
|
|
|
+ dirLight.position.multiplyScalar( 50 );
|
|
|
|
+ scene.add( dirLight );
|
|
|
|
+
|
|
|
|
+ dirLight.castShadow = true;
|
|
|
|
+
|
|
|
|
+ dirLight.shadowMapWidth = 2048;
|
|
|
|
+ dirLight.shadowMapHeight = 2048;
|
|
|
|
+
|
|
|
|
+ var d = 50;
|
|
|
|
+
|
|
|
|
+ dirLight.shadowCameraLeft = -d;
|
|
|
|
+ dirLight.shadowCameraRight = d;
|
|
|
|
+ dirLight.shadowCameraTop = d;
|
|
|
|
+ dirLight.shadowCameraBottom = -d;
|
|
|
|
+
|
|
|
|
+ dirLight.shadowCameraFar = 3500;
|
|
|
|
+ dirLight.shadowBias = -0.0001;
|
|
|
|
+ dirLight.shadowDarkness = 0.35;
|
|
|
|
+ //dirLight.shadowCameraVisible = true;
|
|
|
|
+
|
|
|
|
+ // GROUND
|
|
|
|
+
|
|
|
|
+ var groundGeo = new THREE.PlaneGeometry( 10000, 10000 );
|
|
|
|
+ var groundMat = new THREE.MeshPhongMaterial( { ambient: 0xffffff, color: 0xffffff, specular: 0x050505, perPixel: true } );
|
|
|
|
+ groundMat.color.setHSV( 0.095, 0.5, 1 );
|
|
|
|
+
|
|
|
|
+ var ground = new THREE.Mesh( groundGeo, groundMat );
|
|
|
|
+ ground.rotation.x = -Math.PI/2;
|
|
|
|
+ ground.position.y = -33;
|
|
|
|
+ scene.add( ground );
|
|
|
|
+
|
|
|
|
+ ground.receiveShadow = true;
|
|
|
|
+
|
|
|
|
+ // 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: 33 },
|
|
|
|
+ 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 );
|
|
|
|
+
|
|
|
|
+ // MODEL
|
|
|
|
+
|
|
|
|
+ var loader = new THREE.JSONLoader();
|
|
|
|
+
|
|
|
|
+ loader.load( "models/animated/flamingo.js", function( geometry ) {
|
|
|
|
+
|
|
|
|
+ morphColorsToFaceColors( geometry );
|
|
|
|
+ geometry.computeMorphNormals();
|
|
|
|
+
|
|
|
|
+ var material = new THREE.MeshPhongMaterial( { color: 0xffffff, specular: 0xffffff, shininess: 20, morphTargets: true, morphNormals: true, vertexColors: THREE.FaceColors, shading: THREE.FlatShading, perPixel: false } );
|
|
|
|
+ var meshAnim = new THREE.MorphAnimMesh( geometry, material );
|
|
|
|
+
|
|
|
|
+ meshAnim.duration = 1000;
|
|
|
|
+
|
|
|
|
+ var s = 0.35;
|
|
|
|
+ meshAnim.scale.set( s, s, s );
|
|
|
|
+ meshAnim.position.y = 15;
|
|
|
|
+ meshAnim.rotation.y = -1;
|
|
|
|
+
|
|
|
|
+ meshAnim.castShadow = true;
|
|
|
|
+ meshAnim.receiveShadow = true;
|
|
|
|
+
|
|
|
|
+ scene.add( meshAnim );
|
|
|
|
+ morphs.push( meshAnim );
|
|
|
|
+
|
|
|
|
+ } );
|
|
|
|
+
|
|
|
|
+ // RENDERER
|
|
|
|
+
|
|
|
|
+ renderer = new THREE.WebGLRenderer( { antialias: true } );
|
|
|
|
+ renderer.setSize( window.innerWidth, window.innerHeight );
|
|
|
|
+ container.appendChild( renderer.domElement );
|
|
|
|
+
|
|
|
|
+ renderer.setClearColor( scene.fog.color, 1 );
|
|
|
|
+
|
|
|
|
+ renderer.gammaInput = true;
|
|
|
|
+ renderer.gammaOutput = true;
|
|
|
|
+ renderer.physicallyBasedShading = true;
|
|
|
|
+
|
|
|
|
+ renderer.shadowMapEnabled = true;
|
|
|
|
+ renderer.shadowMapCullFrontFaces = false;
|
|
|
|
+
|
|
|
|
+ // STATS
|
|
|
|
+
|
|
|
|
+ stats = new Stats();
|
|
|
|
+ stats.domElement.style.position = 'absolute';
|
|
|
|
+ stats.domElement.style.top = '0px';
|
|
|
|
+ stats.domElement.style.zIndex = 100;
|
|
|
|
+ container.appendChild( stats.domElement );
|
|
|
|
+
|
|
|
|
+ stats.domElement.children[ 0 ].children[ 0 ].style.color = "#777";
|
|
|
|
+ stats.domElement.children[ 0 ].style.background = "transparent";
|
|
|
|
+ stats.domElement.children[ 0 ].children[ 1 ].style.display = "none";
|
|
|
|
+
|
|
|
|
+ //
|
|
|
|
+
|
|
|
|
+ window.addEventListener( 'resize', onWindowResize, false );
|
|
|
|
+ document.addEventListener( 'keydown', onKeyDown, false );
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ function morphColorsToFaceColors( geometry ) {
|
|
|
|
+
|
|
|
|
+ if ( geometry.morphColors && geometry.morphColors.length ) {
|
|
|
|
+
|
|
|
|
+ var colorMap = geometry.morphColors[ 0 ];
|
|
|
|
+
|
|
|
|
+ for ( var i = 0; i < colorMap.colors.length; i ++ ) {
|
|
|
|
+
|
|
|
|
+ geometry.faces[ i ].color = colorMap.colors[ i ];
|
|
|
|
+ THREE.ColorUtils.adjustHSV( geometry.faces[ i ].color, 0, -0.1, 0 );
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ function onWindowResize() {
|
|
|
|
+
|
|
|
|
+ camera.aspect = window.innerWidth / window.innerHeight;
|
|
|
|
+ camera.updateProjectionMatrix();
|
|
|
|
+
|
|
|
|
+ renderer.setSize( window.innerWidth, window.innerHeight );
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ function onKeyDown ( event ) {
|
|
|
|
+
|
|
|
|
+ switch ( event.keyCode ) {
|
|
|
|
+
|
|
|
|
+ case 72: /*h*/
|
|
|
|
+
|
|
|
|
+ hemiLight.visible = !hemiLight.visible;
|
|
|
|
+ break;
|
|
|
|
+
|
|
|
|
+ case 68: /*d*/
|
|
|
|
+
|
|
|
|
+ dirLight.visible = !dirLight.visible;
|
|
|
|
+ break;
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ //
|
|
|
|
+
|
|
|
|
+ function animate() {
|
|
|
|
+
|
|
|
|
+ requestAnimationFrame( animate );
|
|
|
|
+
|
|
|
|
+ render();
|
|
|
|
+ stats.update();
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ function render() {
|
|
|
|
+
|
|
|
|
+ var delta = clock.getDelta();
|
|
|
|
+
|
|
|
|
+ //controls.update();
|
|
|
|
+
|
|
|
|
+ for ( var i = 0; i < morphs.length; i ++ ) {
|
|
|
|
+
|
|
|
|
+ morph = morphs[ i ];
|
|
|
|
+ morph.updateAnimation( 1000 * delta );
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ renderer.render( scene, camera );
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ </script>
|
|
|
|
+ </body>
|
|
|
|
+</html>
|