Commit 53541c14 authored by Andrey Kiselev's avatar Andrey Kiselev
Browse files

Initial commit. Workign code, used for demo.

parents
html, body {
font-family: Verdana,Arial,sans-serif;
font-size: 14px;
height: 100%;
width: 100%;
padding: 0;
margin: 0;
border: none;
cursor: crosshair;
}
#container{
/* z-index: 20; */
/* position:absolute; */
/* top: 0px; */
/* left: 0px; */
background-color: gray;
height: 100%;
width: 100%;
padding: 0;
margin: 0;
border: none;
overflow: hidden;
/* box-shadow: 0px 0px 10px 2px rgba(0,0,0,0.75); */
}
/* #statusbar{ */
/* z-index: 10; */
/* position: absolute; */
/* bottom: 0px; */
/* left: 0px; */
/* background-color: #2B2B2B; */
/* height: 20px; */
/* line-height: 20px; */
/* width: 100%; */
/* } */
#info {
position: absolute;
bottom: 0; left: 0;
padding: 5px;
/* opacity: 0.5; */
}
#info .close-button {
display: none;
}
#info:hover{
opacity: 1;
}
#dialog {
position: absolute;
bottom: 0; right: 0;
padding: 5px;
opacity: 0.5;
}
#dialog .close-button {
display: none;
}
#dialog:hover{
opacity: 1;
}
#menu {
position: absolute;
top: 0; right: 0;
padding: 5px;
/* width: 300px; */
/* opacity: 0.5; */
}
#menu:hover{
opacity: 1;
}
#progressBar{
position: absolute;
top: 50%;
left: 50%;
margin-top: -14px;
margin-left: -250px;
}
#progressBar .close-button {
display: none;
}
a {
color: red;
}
<!DOCTYPE html>
<html lang="en">
<head>
<title>Giraff in space</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:#000;
color: #eee;
padding:0;
margin:0;
font-weight:bold;
overflow:hidden;
font-family:Monospace;
font-size:13px;
text-align:center;
}
#info {
position: absolute;
top: 0px; width: 100%;
padding: 5px;
z-index:100;
}
a {
color: #0080ff;
}
b { color:orange }
</style>
<!-- Sky shaders -->
<script type="application/x-glsl" id="sky-vertex">
varying vec2 vUV;
void main() {
vUV = uv;
vec4 pos = vec4(position, 1.0);
gl_Position = projectionMatrix * modelViewMatrix * pos;
}
</script>
<script type="application/x-glsl" id="sky-fragment">
uniform sampler2D texture;
varying vec2 vUV;
void main() {
vec4 sample = texture2D(texture, vUV);
gl_FragColor = vec4(sample.xyz, sample.w);
}
</script>
<script src="js/three.js"></script>
<script src="js/OrbitControls.js"></script>
<script src="js/shaders/CopyShader.js"></script>
<script src="js/shaders/FilmShader.js"></script>
<script src="js/shaders/DigitalGlitch.js"></script>
<script src="js/postprocessing/EffectComposer.js"></script>
<script src="js/postprocessing/ShaderPass.js"></script>
<script src="js/postprocessing/MaskPass.js"></script>
<script src="js/postprocessing/RenderPass.js"></script>
<script src="js/postprocessing/FilmPass.js"></script>
<script src="js/postprocessing/GlitchPass.js"></script>
<script src="js/Detector.js"></script>
<!-- <script src="js/libs/stats.min.js"></script> -->
</head>
<body>
<script>
if ( ! Detector.webgl ) Detector.addGetWebGLMessage();
var radius = 6371;
var tilt = 0.41;
var rotationSpeed = 0.02;
var cloudsScale = 1.005;
var moonScale = 0.23;
var MARGIN = 0;
var SCREEN_HEIGHT = window.innerHeight - MARGIN * 2;
var SCREEN_WIDTH = window.innerWidth;
var container, stats;
var camera, controls, scene, sceneCube, renderer;
var geometry, meshPlanet, meshClouds, meshMoon;
var dirLight, pointLight, ambientLight;
var astronaut;
var offset = 2500;
var d, dPlanet, dMoon, dMoonVec = new THREE.Vector3();
var glitchPass;
var clock = new THREE.Clock();
init();
animate();
function init() {
container = document.createElement( 'div' );
document.body.appendChild( container );
camera = new THREE.PerspectiveCamera( 25, SCREEN_WIDTH / SCREEN_HEIGHT, 50, 1e7 );
camera.position.z = radius * 3;
camera.position.x = offset;
var listener = new THREE.AudioListener();
camera.add(listener);
scene = new THREE.Scene();
scene.fog = new THREE.FogExp2( 0x000000, 0.00000025 );
// controls = new THREE.OrbitControls( camera );
// controls.movementSpeed = 1000;
// controls.domElement = container;
// controls.rollSpeed = Math.PI / 24;
// controls.autoForward = false;
// controls.dragToLook = false;
ambientLight = new THREE.AmbientLight(0x555555);
scene.add(ambientLight);
dirLight = new THREE.DirectionalLight( 0xffffff );
dirLight.position.set( -1, 0, 1 ).normalize();
scene.add( dirLight );
var materialNormalMap = new THREE.MeshPhongMaterial( {
// color: 0xff0000,
specular: 0x333333,
shininess: 15,
map: THREE.ImageUtils.loadTexture( "assets/earth/earth_atmos_2048.jpg" ),
specularMap: THREE.ImageUtils.loadTexture( "assets/earth/earth_specular_2048.jpg" ),
normalMap: THREE.ImageUtils.loadTexture( "assets/earth/earth_normal_2048.jpg" ),
normalScale: new THREE.Vector2( 0.85, 0.85 )
} );
// planet
geometry = new THREE.SphereGeometry( radius, 100, 50 );
meshPlanet = new THREE.Mesh( geometry, materialNormalMap );
meshPlanet.rotation.y = 0;
meshPlanet.rotation.z = tilt;
scene.add( meshPlanet );
// clouds
var materialClouds = new THREE.MeshLambertMaterial( {
map: THREE.ImageUtils.loadTexture( "assets/earth/earth_clouds_1024.png" ),
transparent: true
} );
meshClouds = new THREE.Mesh( geometry, materialClouds );
meshClouds.scale.set( cloudsScale, cloudsScale, cloudsScale );
meshClouds.rotation.z = tilt;
scene.add( meshClouds );
// moon
var materialMoon = new THREE.MeshPhongMaterial( {
map: THREE.ImageUtils.loadTexture( "assets/earth/moon_1024.jpg" )
} );
meshMoon = new THREE.Mesh( geometry, materialMoon );
meshMoon.position.set( radius, radius, 0 );
meshMoon.scale.set( moonScale, moonScale, moonScale );
scene.add( meshMoon );
// // Skydome for our little world
// var geometry = new THREE.SphereGeometry(25000, 32, 24);
// var uniforms = {
// texture : {
// type : 't',
// value : THREE.ImageUtils.loadTexture('assets/GalacticCenter.jpg')
// }
// };
// var material = new THREE.ShaderMaterial({
// uniforms : uniforms,
// vertexShader : document.getElementById('sky-vertex').textContent,
// fragmentShader : document.getElementById('sky-fragment').textContent
// });
// var skyDome = new THREE.Mesh(geometry, material);
// skyDome.scale.set(-1, 1, 1);
// skyDome.rotation.order = 'XZY';
// skyDome.renderDepth = 1000.0;
// scene.add(skyDome);
// // stars
var i, r = radius, starsGeometry = [ new THREE.Geometry(), new THREE.Geometry() ];
for ( i = 0; i < 2500; i ++ ) {
var vertex = new THREE.Vector3();
vertex.x = Math.random() * 2 - 1;
vertex.y = Math.random() * 2 - 1;
vertex.z = Math.random() * 2 - 1;
vertex.multiplyScalar( r );
starsGeometry[ 0 ].vertices.push( vertex );
}
for ( i = 0; i < 1500; i ++ ) {
var vertex = new THREE.Vector3();
vertex.x = Math.random() * 2 - 1;
vertex.y = Math.random() * 2 - 1;
vertex.z = Math.random() * 2 - 1;
vertex.multiplyScalar( r );
starsGeometry[ 1 ].vertices.push( vertex );
}
var stars;
var starsMaterials = [
new THREE.PointsMaterial( { color: 0x555555, size: 2, sizeAttenuation: false } ),
new THREE.PointsMaterial( { color: 0x555555, size: 1, sizeAttenuation: false } ),
new THREE.PointsMaterial( { color: 0x333333, size: 2, sizeAttenuation: false } ),
new THREE.PointsMaterial( { color: 0x3a3a3a, size: 1, sizeAttenuation: false } ),
new THREE.PointsMaterial( { color: 0x1a1a1a, size: 2, sizeAttenuation: false } ),
new THREE.PointsMaterial( { color: 0x1a1a1a, size: 1, sizeAttenuation: false } )
];
for ( i = 10; i < 30; i ++ ) {
stars = new THREE.Points( starsGeometry[ i % 2 ], starsMaterials[ i % 6 ] );
stars.rotation.x = Math.random() * 6;
stars.rotation.y = Math.random() * 6;
stars.rotation.z = Math.random() * 6;
s = i * 10;
stars.scale.set( s, s, s );
stars.matrixAutoUpdate = false;
stars.updateMatrix();
scene.add( stars );
}
var loader = new THREE.JSONLoader();
// var loader = new THREE.ObjectLoader();
loader.load( "assets/astronaut.json", function( geometry, material ) {
var mat = new THREE.MeshPhongMaterial({specular: 0xcccccc, shininess: 30});
mat.map = THREE.ImageUtils.loadTexture('assets/Astronaut_D.jpg');
mat.normalMap = THREE.ImageUtils.loadTexture('assets/Astronaut_N.jpg');
astronaut = new THREE.Mesh( geometry, mat );
astronaut.scale.set( 20, 20, 20 );
// astronaut.position.y = 20;
astronaut.position.z = radius * 3 - 100;
astronaut.position.x = offset;
// astronaut.position.y = - 70;
scene.add( astronaut );
var sound1 = new THREE.Audio( listener );
sound1.load( 'assets/audio/go-downstairs.ogg' );
sound1.setRefDistance( 100 );
sound1.autoplay = true;
sound1.loop = true;
sound1.setLoop(true);
astronaut.add( sound1 );
console.log(sound1.getLoop());
} );
renderer = new THREE.WebGLRenderer({
antialias : false
});
renderer.setPixelRatio( window.devicePixelRatio );
renderer.setSize( SCREEN_WIDTH, SCREEN_HEIGHT );
renderer.sortObjects = true;
renderer.autoClear = true;
container.appendChild( renderer.domElement );
// stats = new Stats();
// stats.domElement.style.position = 'absolute';
// stats.domElement.style.top = '0px';
// stats.domElement.style.zIndex = 100;
// container.appendChild( stats.domElement );
window.addEventListener( 'resize', onWindowResize, false );
// postprocessing
var renderModel = new THREE.RenderPass( scene, camera );
var effectFilm = new THREE.FilmPass( 0.35, 0.75, 2048, false );
effectFilm.renderToScreen = false;
composer = new THREE.EffectComposer( renderer );
composer.addPass( new THREE.RenderPass( scene, camera ) );
glitchPass = new THREE.GlitchPass();
glitchPass.renderToScreen = true;
// composer.addPass( renderModel );
composer.addPass( effectFilm );
composer.addPass( glitchPass );
}
function onWindowResize( event ) {
SCREEN_HEIGHT = window.innerHeight;
SCREEN_WIDTH = window.innerWidth;
renderer.setSize( SCREEN_WIDTH, SCREEN_HEIGHT );
camera.aspect = SCREEN_WIDTH / SCREEN_HEIGHT;
camera.updateProjectionMatrix();
composer.reset();
}
function animate() {
requestAnimationFrame( animate );
render();
// stats.update();
}
function render() {
// rotate the planet and clouds
var delta = clock.getDelta();
meshPlanet.rotation.y += rotationSpeed * delta;
meshClouds.rotation.y += 1.25 * rotationSpeed * delta;
// slow down as we approach the surface
astronaut.rotation.z+= rotationSpeed * delta;
camera.rotation.z+= rotationSpeed * delta;
dPlanet = camera.position.length();
dMoonVec.subVectors( camera.position, meshMoon.position );
dMoon = dMoonVec.length();
if ( dMoon < dPlanet ) {
d = ( dMoon - radius * moonScale * 1.01 );
} else {
d = ( dPlanet - radius * 1.01 );
}
// controls.movementSpeed = 0.33 * d;
// controls.update( delta );
renderer.clear();
composer.render( delta );
}
</script>
</body>
</html>
/**
* @author mikael emtinger / http://gomo.se/
* @author mrdoob / http://mrdoob.com/
* @author alteredq / http://alteredqualia.com/
*/
THREE.Animation = function ( root, data ) {
this.root = root;
this.data = THREE.AnimationHandler.init( data );
this.hierarchy = THREE.AnimationHandler.parse( root );
this.currentTime = 0;
this.timeScale = 1;
this.isPlaying = false;
this.loop = true;
this.weight = 0;
this.interpolationType = THREE.AnimationHandler.LINEAR;
};
THREE.Animation.prototype = {
constructor: THREE.Animation,
keyTypes: [ "pos", "rot", "scl" ],
play: function ( startTime, weight ) {
this.currentTime = startTime !== undefined ? startTime : 0;
this.weight = weight !== undefined ? weight : 1;
this.isPlaying = true;
this.reset();
THREE.AnimationHandler.play( this );
},
stop: function() {
this.isPlaying = false;
THREE.AnimationHandler.stop( this );
},
reset: function () {
for ( var h = 0, hl = this.hierarchy.length; h < hl; h ++ ) {
var object = this.hierarchy[ h ];
if ( object.animationCache === undefined ) {
object.animationCache = {
animations: {},
blending: {
positionWeight: 0.0,
quaternionWeight: 0.0,
scaleWeight: 0.0
}
};
}
var name = this.data.name;
var animations = object.animationCache.animations;
var animationCache = animations[ name ];
if ( animationCache === undefined ) {
animationCache = {
prevKey: { pos: 0, rot: 0, scl: 0 },
nextKey: { pos: 0, rot: 0, scl: 0 },
originalMatrix: object.matrix
};
animations[ name ] = animationCache;
}
// Get keys to match our current time
for ( var t = 0; t < 3; t ++ ) {
var type = this.keyTypes[ t ];
var prevKey = this.data.hierarchy[ h ].keys[ 0 ];
var nextKey = this.getNextKeyWith( type, h, 1 );
while ( nextKey.time < this.currentTime && nextKey.index > prevKey.index ) {
prevKey = nextKey;
nextKey = this.getNextKeyWith( type, h, nextKey.index + 1 );
}
animationCache.prevKey[ type ] = prevKey;
animationCache.nextKey[ type ] = nextKey;
}
}
},
resetBlendWeights: function () {
for ( var h = 0, hl = this.hierarchy.length; h < hl; h ++ ) {
var object = this.hierarchy[ h ];
var animationCache = object.animationCache;
if ( animationCache !== undefined ) {
var blending = animationCache.blending;
blending.positionWeight = 0.0;
blending.quaternionWeight = 0.0;
blending.scaleWeight = 0.0;
}
}
},