123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193 |
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <title>three.js webgl - instancing - Morph Target Animations</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>
- <script type="importmap">
- {
- "imports": {
- "three": "../build/three.module.js",
- "three/addons/": "./jsm/"
- }
- }
- </script>
- <script type="module">
- import * as THREE from 'three';
- import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
- import Stats from 'three/addons/libs/stats.module.js';
-
- let camera, scene, renderer, stats, mesh, mixer, dummy;
- const offset = 5000;
- const timeOffsets = new Float32Array( 1024 );
- for ( let i = 0; i < 1024; i ++ ) {
- timeOffsets[ i ] = Math.random() * 3;
- }
- const clock = new THREE.Clock( true );
- init();
- function init() {
- camera = new THREE.PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 100, 10000 );
- scene = new THREE.Scene();
- scene.background = new THREE.Color( 0x99DDFF );
- scene.fog = new THREE.Fog( 0x99DDFF, 5000, 10000 );
- const light = new THREE.DirectionalLight( 0xffffff, 1 );
- light.position.set( 200, 1000, 50 );
- light.castShadow = true;
- light.shadow.camera.left = - 5000;
- light.shadow.camera.right = 5000;
- light.shadow.camera.top = 5000;
- light.shadow.camera.bottom = - 5000;
- light.shadow.camera.far = 2000;
- light.shadow.bias = - 0.01;
- light.shadow.camera.updateProjectionMatrix();
- scene.add( light );
- const hemi = new THREE.HemisphereLight( 0x99DDFF, 0x669933, 1 / 3 );
- scene.add( hemi );
- const ground = new THREE.Mesh(
- new THREE.PlaneGeometry( 1000000, 1000000 ),
- new THREE.MeshStandardMaterial( { color: 0x669933, depthWrite: true } )
- );
- ground.rotation.x = - Math.PI / 2;
- ground.receiveShadow = true;
- scene.add( ground );
- const loader = new GLTFLoader();
- loader.load( 'models/gltf/Horse.glb', function ( glb ) {
- dummy = glb.scene.children[ 0 ];
- mesh = new THREE.InstancedMesh( dummy.geometry, dummy.material, 1024 );
- mesh.castShadow = true;
- for ( let x = 0, i = 0; x < 32; x ++ ) {
- for ( let y = 0; y < 32; y ++ ) {
- dummy.position.set( offset - 300 * x + 200 * Math.random(), 0, offset - 300 * y );
- dummy.updateMatrix();
- mesh.setMatrixAt( i, dummy.matrix );
- mesh.setColorAt( i, new THREE.Color( `hsl(${Math.random() * 360}, 50%, 66%)` ) );
- i ++;
-
- }
-
- }
- scene.add( mesh );
- mixer = new THREE.AnimationMixer( glb.scene );
- const action = mixer.clipAction( glb.animations[ 0 ] );
- action.play();
-
- } );
- //
- renderer = new THREE.WebGLRenderer( { antialias: true } );
- renderer.setPixelRatio( window.devicePixelRatio );
- renderer.setSize( window.innerWidth, window.innerHeight );
- renderer.setAnimationLoop( animate );
- document.body.appendChild( renderer.domElement );
- renderer.shadowMap.enabled = true;
- renderer.shadowMap.type = THREE.VSMShadowMap;
- //
- stats = new Stats();
- document.body.appendChild( stats.dom );
- //
- window.addEventListener( 'resize', onWindowResize );
- }
- function onWindowResize() {
- camera.aspect = window.innerWidth / window.innerHeight;
- camera.updateProjectionMatrix();
- renderer.setSize( window.innerWidth, window.innerHeight );
- }
- //
- function animate() {
- render();
- stats.update();
- }
- function render() {
- const time = clock.getElapsedTime();
- const r = 3000;
- camera.position.set( Math.sin( time / 10 ) * r, 1500 + 1000 * Math.cos( time / 5 ), Math.cos( time / 10 ) * r );
- camera.lookAt( 0, 0, 0 );
- if ( mesh ) {
- for ( let i = 0; i < 1024; i ++ ) {
- mixer.setTime( time + timeOffsets[ i ] );
- mesh.setMorphAt( i, dummy );
- }
- mesh.morphTexture.needsUpdate = true;
- }
- renderer.render( scene, camera );
- }
- </script>
- </body>
- </html>
|