123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335 |
- <!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">
- <link type="text/css" rel="stylesheet" href="main.css">
- </head>
- <body>
- <div id="info">
- <a href="https://threejs.org" target="_blank" rel="noopener">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
- </div>
- <script type="module">
- import * as THREE from '../build/three.module.js';
- import Stats from './jsm/libs/stats.module.js';
- import { FirstPersonControls } from './jsm/controls/FirstPersonControls.js';
- import { GLTFLoader } from './jsm/loaders/GLTFLoader.js';
- var SHADOW_MAP_WIDTH = 2048, SHADOW_MAP_HEIGHT = 1024;
- var SCREEN_WIDTH = window.innerWidth;
- var SCREEN_HEIGHT = window.innerHeight;
- var FLOOR = - 250;
- var ANIMATION_GROUPS = 25;
- var camera, controls, scene, renderer;
- var container, stats;
- var NEAR = 5, FAR = 3000;
- var morph, morphs = [], mixer, animGroups = [];
- var light;
- var clock = new THREE.Clock();
- init();
- animate();
- function init() {
- container = document.createElement( 'div' );
- document.body.appendChild( container );
- // CAMERA
- camera = new THREE.PerspectiveCamera( 23, SCREEN_WIDTH / SCREEN_HEIGHT, NEAR, FAR );
- camera.position.set( 700, 50, 1900 );
- // SCENE
- scene = new THREE.Scene();
- scene.background = new THREE.Color( 0x59472b );
- 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 / 5, 0.3 );
- light.position.set( 0, 1500, 1000 );
- light.target.position.set( 0, 0, 0 );
- light.castShadow = true;
- light.shadow.camera.near = 1200;
- light.shadow.camera.far = 2500;
- light.shadow.bias = 0.0001;
- light.shadow.mapSize.width = SHADOW_MAP_WIDTH;
- light.shadow.mapSize.height = SHADOW_MAP_HEIGHT;
- scene.add( light );
- createScene();
- // RENDERER
- renderer = new THREE.WebGLRenderer();
- renderer.setPixelRatio( window.devicePixelRatio );
- renderer.setSize( SCREEN_WIDTH, SCREEN_HEIGHT );
- container.appendChild( renderer.domElement );
- renderer.outputEncoding = THREE.sRGBEncoding;
- renderer.autoClear = false;
- //
- renderer.shadowMap.enabled = true;
- renderer.shadowMap.type = THREE.PCFSoftShadowMap;
- // CONTROLS
- controls = new FirstPersonControls( camera, renderer.domElement );
- controls.lookSpeed = 0.0125;
- controls.movementSpeed = 500;
- controls.noFly = false;
- controls.lookVertical = true;
- controls.lookAt( scene.position );
- // STATS
- stats = new Stats();
- container.appendChild( stats.dom );
- //
- window.addEventListener( 'resize', onWindowResize, 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 );
- controls.handleResize();
- }
- function createScene( ) {
- // GROUND
- var geometry = new THREE.PlaneBufferGeometry( 100, 100 );
- var planeMaterial = new THREE.MeshPhongMaterial( { color: 0xffb851 } );
- 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 loader = new THREE.FontLoader();
- loader.load( 'fonts/helvetiker_bold.typeface.json', function ( font ) {
- var textGeo = new THREE.TextBufferGeometry( "THREE.JS", {
- font: font,
- size: 200,
- height: 50,
- curveSegments: 12,
- 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.BoxBufferGeometry( 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.BoxBufferGeometry( 1600, 170, 250 ), planeMaterial );
- mesh.position.y = FLOOR - 50;
- mesh.position.z = 20;
- mesh.castShadow = true;
- mesh.receiveShadow = true;
- scene.add( mesh );
- mixer = new THREE.AnimationMixer( scene );
- for ( var i = 0; i !== ANIMATION_GROUPS; ++ i ) {
- var group = new THREE.AnimationObjectGroup();
- animGroups.push( group );
- }
- // MORPHS
- function addMorph( mesh, clip, speed, duration, x, y, z, fudgeColor, massOptimization ) {
- mesh = mesh.clone();
- mesh.material = mesh.material.clone();
- if ( fudgeColor ) {
- mesh.material.color.offsetHSL( 0, Math.random() * 0.5 - 0.25, Math.random() * 0.5 - 0.25 );
- }
- mesh.speed = speed;
- if ( massOptimization ) {
- var index = Math.floor( Math.random() * ANIMATION_GROUPS ),
- animGroup = animGroups[ index ];
- animGroup.add( mesh );
- if ( ! mixer.existingAction( clip, animGroup ) ) {
- var randomness = 0.6 * Math.random() - 0.3;
- var phase = ( index + randomness ) / ANIMATION_GROUPS;
- mixer.clipAction( clip, animGroup ).
- setDuration( duration ).
- startAt( - duration * phase ).
- play();
- }
- } else {
- mixer.clipAction( clip, mesh ).
- setDuration( duration ).
- startAt( - duration * Math.random() ).
- play();
- }
- mesh.position.set( x, y, z );
- mesh.rotation.y = Math.PI / 2;
- mesh.castShadow = true;
- mesh.receiveShadow = true;
- scene.add( mesh );
- morphs.push( mesh );
- }
- var loader = new GLTFLoader();
- loader.load( "models/gltf/Horse.glb", function ( gltf ) {
- var mesh = gltf.scene.children[ 0 ];
- var clip = gltf.animations[ 0 ];
- for ( var i = - 600; i < 601; i += 2 ) {
- addMorph( mesh, clip, 550, 1, 100 - Math.random() * 3000, FLOOR, i, true, true );
- }
- } );
- }
- //
- function animate() {
- requestAnimationFrame( animate );
- stats.begin();
- render();
- stats.end();
- }
- function render() {
- var delta = clock.getDelta();
- if ( mixer ) mixer.update( delta );
- for ( var i = 0; i < morphs.length; i ++ ) {
- morph = morphs[ i ];
- 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 );
- }
- </script>
- </body>
- </html>
|