Doodle3D-Slicer/three.js-master/examples/webgl_shadowmap.html
2017-06-22 13:21:07 +02:00

440 lines
10 KiB
HTML
Executable File

<!DOCTYPE html>
<html lang="en">
<head>
<title>three.js webgl - shadow map</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 {
font-family: Monospace;
background-color: #000;
color: #fff;
margin: 0px;
overflow: hidden;
}
#info {
position: absolute;
top: 10px;
width: 100%;
text-align: center;
z-index: 100;
display:block;
}
#info a { color: #f00; font-weight: bold; text-decoration: underline; cursor: pointer }
</style>
</head>
<body>
<div id="info">
<a href="http://threejs.org" target="_blank">three.js</a> - shadowmap - models by <a href="http://mirada.com/">mirada</a> from <a href="http://ro.me">rome</a></br>
move camera with WASD / RF + mouse<br/>
t: toggle HUD</a>
</div>
<script src="../build/three.min.js"></script>
<script src="js/controls/FirstPersonControls.js"></script>
<script src="js/shaders/UnpackDepthRGBAShader.js"></script>
<script src="js/Detector.js"></script>
<script src="js/libs/stats.min.js"></script>
<script src="fonts/helvetiker_bold.typeface.js"></script>
<script>
if ( ! Detector.webgl ) Detector.addGetWebGLMessage();
var SHADOW_MAP_WIDTH = 2048, SHADOW_MAP_HEIGHT = 1024;
var HUD_MARGIN = 0.05;
var SCREEN_WIDTH = window.innerWidth;
var SCREEN_HEIGHT = window.innerHeight;
var FLOOR = -250;
var camera, controls, scene, renderer;
var container, stats;
var NEAR = 10, FAR = 3000;
var sceneHUD, cameraOrtho, hudMesh;
var morph, morphs = [];
var light;
var clock = new THREE.Clock();
var showHUD = false;
init();
animate();
function init() {
container = document.createElement( 'div' );
document.body.appendChild( container );
// SCENE CAMERA
camera = new THREE.PerspectiveCamera( 23, SCREEN_WIDTH / SCREEN_HEIGHT, NEAR, FAR );
camera.position.set( 700, 50, 1900 );
controls = new THREE.FirstPersonControls( camera );
controls.lookSpeed = 0.0125;
controls.movementSpeed = 500;
controls.noFly = false;
controls.lookVertical = true;
controls.constrainVertical = true;
controls.verticalMin = 1.5;
controls.verticalMax = 2.0;
controls.lon = 250;
controls.lat = 30;
// SCENE
scene = new THREE.Scene();
scene.fog = new THREE.Fog( 0x59472b, 1000, FAR );
// LIGHTS
var ambient = new THREE.AmbientLight( 0x444444 );
scene.add( ambient );
light = new THREE.SpotLight( 0xffffff, 1, 0, Math.PI / 2, 1 );
light.position.set( 0, 1500, 1000 );
light.target.position.set( 0, 0, 0 );
light.castShadow = true;
light.shadowCameraNear = 1200;
light.shadowCameraFar = 2500;
light.shadowCameraFov = 50;
//light.shadowCameraVisible = true;
light.shadowBias = 0.0001;
light.shadowDarkness = 0.5;
light.shadowMapWidth = SHADOW_MAP_WIDTH;
light.shadowMapHeight = SHADOW_MAP_HEIGHT;
scene.add( light );
createHUD();
createScene();
// RENDERER
renderer = new THREE.WebGLRenderer( { antialias: true } );
renderer.setClearColor( scene.fog.color );
renderer.setPixelRatio( window.devicePixelRatio );
renderer.setSize( SCREEN_WIDTH, SCREEN_HEIGHT );
container.appendChild( renderer.domElement );
renderer.autoClear = false;
//
renderer.shadowMapEnabled = true;
renderer.shadowMapType = THREE.PCFShadowMap;
// STATS
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 );
window.addEventListener( 'keydown', onKeyDown, false );
}
function onWindowResize() {
SCREEN_WIDTH = window.innerWidth;
SCREEN_HEIGHT = window.innerHeight;
camera.aspect = SCREEN_WIDTH / SCREEN_HEIGHT;
camera.updateProjectionMatrix();
renderer.setSize( SCREEN_WIDTH, SCREEN_HEIGHT );
var aspect = SCREEN_WIDTH / SCREEN_HEIGHT;
cameraOrtho.left = - aspect;
cameraOrtho.right = aspect;
cameraOrtho.top = 1;
cameraOrtho.bottom = - 1;
cameraOrtho.updateProjectionMatrix();
hudMesh.position.x = cameraOrtho.left + HUD_MARGIN;
hudMesh.position.y = cameraOrtho.bottom + HUD_MARGIN;
controls.handleResize();
}
function onKeyDown ( event ) {
switch( event.keyCode ) {
case 84: /*t*/ showHUD = !showHUD; break;
}
}
function createHUD() {
var aspect = SCREEN_WIDTH / SCREEN_HEIGHT;
cameraOrtho = new THREE.OrthographicCamera( - aspect, aspect, 1, - 1, 1, 10 );
cameraOrtho.position.z = 5;
var shader = THREE.UnpackDepthRGBAShader;
var uniforms = new THREE.UniformsUtils.clone( shader.uniforms );
uniforms.tDiffuse.value = light.shadowMap;
var hudMaterial = new THREE.ShaderMaterial( {
uniforms: uniforms,
vertexShader: shader.vertexShader,
fragmentShader: shader.fragmentShader
} );
var hudHeight = 2 / 3;
var hudWidth = hudHeight * SHADOW_MAP_WIDTH / SHADOW_MAP_HEIGHT;
var hudGeo = new THREE.PlaneBufferGeometry( hudWidth, hudHeight );
hudGeo.applyMatrix( new THREE.Matrix4().makeTranslation( hudWidth / 2, hudHeight / 2, 0 ) );
hudMesh = new THREE.Mesh( hudGeo, hudMaterial );
hudMesh.position.x = cameraOrtho.left + HUD_MARGIN;
hudMesh.position.y = cameraOrtho.bottom + HUD_MARGIN;
sceneHUD = new THREE.Scene();
sceneHUD.add( hudMesh );
cameraOrtho.lookAt( sceneHUD.position );
}
function createScene( ) {
// GROUND
var geometry = new THREE.PlaneBufferGeometry( 100, 100 );
var planeMaterial = new THREE.MeshPhongMaterial( { color: 0xffdd99 } );
var ground = new THREE.Mesh( geometry, planeMaterial );
ground.position.set( 0, FLOOR, 0 );
ground.rotation.x = - Math.PI / 2;
ground.scale.set( 100, 100, 100 );
ground.castShadow = false;
ground.receiveShadow = true;
scene.add( ground );
// TEXT
var textGeo = new THREE.TextGeometry( "THREE.JS", {
size: 200,
height: 50,
curveSegments: 12,
font: "helvetiker",
weight: "bold",
style: "normal",
bevelThickness: 2,
bevelSize: 5,
bevelEnabled: true
});
textGeo.computeBoundingBox();
var centerOffset = -0.5 * ( textGeo.boundingBox.max.x - textGeo.boundingBox.min.x );
var textMaterial = new THREE.MeshPhongMaterial( { color: 0xff0000, specular: 0xffffff } );
var mesh = new THREE.Mesh( textGeo, textMaterial );
mesh.position.x = centerOffset;
mesh.position.y = FLOOR + 67;
mesh.castShadow = true;
mesh.receiveShadow = true;
scene.add( mesh );
// CUBES
var mesh = new THREE.Mesh( new THREE.BoxGeometry( 1500, 220, 150 ), planeMaterial );
mesh.position.y = FLOOR - 50;
mesh.position.z = 20;
mesh.castShadow = true;
mesh.receiveShadow = true;
scene.add( mesh );
var mesh = new THREE.Mesh( new THREE.BoxGeometry( 1600, 170, 250 ), planeMaterial );
mesh.position.y = FLOOR - 50;
mesh.position.z = 20;
mesh.castShadow = true;
mesh.receiveShadow = true;
scene.add( mesh );
// MORPHS
function addMorph( geometry, speed, duration, x, y, z, fudgeColor ) {
var material = new THREE.MeshLambertMaterial( { color: 0xffaa55, morphTargets: true, vertexColors: THREE.FaceColors } );
if ( fudgeColor ) {
material.color.offsetHSL( 0, Math.random() * 0.5 - 0.25, Math.random() * 0.5 - 0.25 );
}
var meshAnim = new THREE.MorphAnimMesh( geometry, material );
meshAnim.speed = speed;
meshAnim.duration = duration;
meshAnim.time = 600 * Math.random();
meshAnim.position.set( x, y, z );
meshAnim.rotation.y = Math.PI/2;
meshAnim.castShadow = true;
meshAnim.receiveShadow = true;
scene.add( meshAnim );
morphs.push( meshAnim );
}
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 ];
}
}
}
var loader = new THREE.JSONLoader();
loader.load( "models/animated/horse.js", function( geometry ) {
morphColorsToFaceColors( geometry );
addMorph( geometry, 550, 1000, 100 - Math.random() * 1000, FLOOR, 300, true );
addMorph( geometry, 550, 1000, 100 - Math.random() * 1000, FLOOR, 450, true );
addMorph( geometry, 550, 1000, 100 - Math.random() * 1000, FLOOR, 600, true );
addMorph( geometry, 550, 1000, 100 - Math.random() * 1000, FLOOR, -300, true );
addMorph( geometry, 550, 1000, 100 - Math.random() * 1000, FLOOR, -450, true );
addMorph( geometry, 550, 1000, 100 - Math.random() * 1000, FLOOR, -600, true );
} );
loader.load( "models/animated/flamingo.js", function( geometry ) {
morphColorsToFaceColors( geometry );
addMorph( geometry, 500, 1000, 500 - Math.random() * 500, FLOOR + 350, 40 );
} );
loader.load( "models/animated/stork.js", function( geometry ) {
morphColorsToFaceColors( geometry );
addMorph( geometry, 350, 1000, 500 - Math.random() * 500, FLOOR + 350, 340 );
} );
loader.load( "models/animated/parrot.js", function( geometry ) {
morphColorsToFaceColors( geometry );
addMorph( geometry, 450, 500, 500 - Math.random() * 500, FLOOR + 300, 700 );
} );
}
function animate() {
requestAnimationFrame( animate );
render();
stats.update();
}
function render() {
var delta = clock.getDelta();
for ( var i = 0; i < morphs.length; i ++ ) {
morph = morphs[ i ];
morph.updateAnimation( 1000 * delta );
morph.position.x += morph.speed * delta;
if ( morph.position.x > 2000 ) {
morph.position.x = -1000 - Math.random() * 500;
}
}
controls.update( delta );
renderer.clear();
renderer.render( scene, camera );
// Render debug HUD with shadow map
if ( showHUD ) {
renderer.clearDepth();
renderer.render( sceneHUD, cameraOrtho );
}
}
</script>
</body>
</html>