Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified examples/screenshots/webgl_instancing_morph.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
211 changes: 103 additions & 108 deletions examples/webgl_instancing_morph.html
Original file line number Diff line number Diff line change
@@ -1,12 +1,24 @@

<!DOCTYPE html>
<html lang="en">
<head>
<title>three.js webgl - instancing - Morph Target Animations</title>
<title>three.js webgl - morph targets - instancing</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">
<style>
body {
background-color: #666666;
}
</style>
</head>
<body>

<div id="info">
<a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> webgl - morph targets - instancing<br/>
model by <a href="https://www.bannaflak.com/face-cap" target="_blank" rel="noopener">Face Cap</a>
</div>

<script type="importmap">
{
"imports": {
Expand All @@ -20,176 +32,159 @@

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;
import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
import { KTX2Loader } from 'three/addons/loaders/KTX2Loader.js';
import { MeshoptDecoder } from 'three/addons/libs/meshopt_decoder.module.js';

}
import { RoomEnvironment } from 'three/addons/environments/RoomEnvironment.js';

const clock = new THREE.Clock( true );
import { GUI } from 'three/addons/libs/lil-gui.module.min.js';

init();
animate();

function init() {

camera = new THREE.PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 100, 10000 );

scene = new THREE.Scene();
let mixer, mesh, head, duration;
const clock = new THREE.Clock( true );

scene.background = new THREE.Color( 0x99DDFF );
const container = document.createElement( 'div' );
document.body.appendChild( container );

scene.fog = new THREE.Fog( 0x99DDFF, 5000, 10000 );
const camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 500 );
camera.position.set( 0, 0, 0 );

const light = new THREE.DirectionalLight( 0xffffff, 1 );
const scene = new THREE.Scene();

light.position.set( 200, 1000, 50 );

light.castShadow = true;
const renderer = new THREE.WebGLRenderer( {
antialias: true,
powerPreference: 'high-performance'
} );

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;
renderer.setPixelRatio( window.devicePixelRatio );
renderer.setSize( window.innerWidth, window.innerHeight );
renderer.toneMapping = THREE.ACESFilmicToneMapping;

light.shadow.bias = - 0.01;
container.appendChild( renderer.domElement );

light.shadow.camera.updateProjectionMatrix();
const ktx2Loader = new KTX2Loader()
.setTranscoderPath( 'jsm/libs/basis/' )
.detectSupport( renderer );

scene.add( light );
new GLTFLoader()
.setKTX2Loader( ktx2Loader )
.setMeshoptDecoder( MeshoptDecoder )
.load( 'models/gltf/facecap.glb', ( gltf ) => {

const hemi = new THREE.HemisphereLight( 0x99DDFF, 0x669933, 1 / 3 );
const orig = gltf.scene.children[ 0 ];

scene.add( hemi );
mixer = new THREE.AnimationMixer( orig );

const ground = new THREE.Mesh(
new THREE.PlaneGeometry( 1000000, 1000000 ),
new THREE.MeshStandardMaterial( { color: 0x669933, depthWrite: true } )
);
mixer.clipAction( gltf.animations[ 0 ] ).play();

ground.rotation.x = - Math.PI / 2;
duration = gltf.animations[ 0 ].duration;

// GUI

ground.receiveShadow = true;
head = orig.getObjectByName( 'mesh_2' );

scene.add( ground );
mesh = new THREE.InstancedMesh( head.geometry, head.material, 128 );

const loader = new GLTFLoader();
mesh.setMatrixAt( 0, head.matrix );

loader.load( 'models/gltf/Horse.glb', function ( glb ) {
mesh.instanceMatrix.setUsage( THREE.DynamicDrawUsage );

dummy = glb.scene.children[ 0 ];
mesh.setMorphAt( 0, head );

mesh = new THREE.InstancedMesh( dummy.geometry, dummy.material, 1024 );
const gridParams = {
width: 3,
height: 2
};

mesh.castShadow = true;
function updateGrid() {

for ( let x = 0, i = 0; x < 32; x ++ ) {
mesh.count = gridParams.width * gridParams.height;

for ( let y = 0; y < 32; y ++ ) {
const w = gridParams.width;
const h = gridParams.height;

dummy.position.set( offset - 300 * x + 200 * Math.random(), 0, offset - 300 * y );
for ( let i = 0; i < h; i ++ ) {

dummy.updateMatrix();
for ( let j = 0; j < w; j ++ ) {

mesh.setMatrixAt( i, dummy.matrix );
head.position.set( j - w / 2, i - h / 2 + 1, - Math.max( w, h ) - 2 ).multiplyScalar( 25 );

mesh.setColorAt( i, new THREE.Color( `hsl(${Math.random() * 360}, 50%, 66%)` ) );
head.rotation.x = Math.PI / 2;

i ++;

}
head.updateMatrix();


}
mesh.setMatrixAt( j + w * i, head.matrix );

scene.add( mesh );
}

mixer = new THREE.AnimationMixer( glb.scene );
mesh.instanceMatrix.needsUpdate = true;

const action = mixer.clipAction( glb.animations[ 0 ] );
}

action.play();
}

} );

//
scene.add( mesh );

renderer = new THREE.WebGLRenderer( { antialias: true } );
renderer.setPixelRatio( window.devicePixelRatio );
renderer.setSize( window.innerWidth, window.innerHeight );
document.body.appendChild( renderer.domElement );
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.VSMShadowMap;
//
const gui = new GUI();

stats = new Stats();
document.body.appendChild( stats.dom );
gui.add( gridParams, 'width', 2, 16 ).name( 'Grid width' ).step( 1 ).onChange( updateGrid );
gui.add( gridParams, 'height', 2, 8 ).name( 'Grid height' ).step( 1 ).onChange( updateGrid );

//
updateGrid();

window.addEventListener( 'resize', onWindowResize );

}
} );

function onWindowResize() {
const environment = new RoomEnvironment( renderer );
const pmremGenerator = new THREE.PMREMGenerator( renderer );

camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
scene.background = new THREE.Color( 0x666666 );
scene.environment = pmremGenerator.fromScene( environment ).texture;

renderer.setSize( window.innerWidth, window.innerHeight );

}

//
const stats = new Stats();
container.appendChild( stats.dom );

function animate() {
renderer.setAnimationLoop( () => {

requestAnimationFrame( animate );
const time = clock.getElapsedTime();

render();
if ( mixer ) {

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 );
for ( let i = 0; i < mesh.count; i ++ ) {
mixer.setTime( time + i * duration / mesh.count );
mesh.setMorphAt( i, head );
}
mesh.morphTexture.needsUpdate = true;

}

if ( mesh ) {
renderer.render( scene, camera );

for ( let i = 0; i < 1024; i ++ ) {
stats.update();

mixer.setTime( time + timeOffsets[ i ] );
} );

mesh.setMorphAt( i, dummy );
window.addEventListener( 'resize', () => {

}
camera.aspect = window.innerWidth / window.innerHeight;

mesh.morphTexture.needsUpdate = true;
camera.updateProjectionMatrix();

}
renderer.setSize( window.innerWidth, window.innerHeight );

renderer.render( scene, camera );
} );

}

</script>

</body>
</html>
26 changes: 24 additions & 2 deletions src/renderers/shaders/ShaderChunk/morphtarget_pars_vertex.glsl.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,34 @@ export default /* glsl */`

vec4 getMorph( const in int vertexIndex, const in int morphTargetIndex, const in int offset ) {

int texelIndex = vertexIndex * MORPHTARGETS_TEXTURE_STRIDE + offset;
int texelIndex = vertexIndex * MORPHTARGETS_TEXTURE_STRIDE + 3 * offset;

int y = texelIndex / morphTargetsTextureSize.x;
int x = texelIndex - y * morphTargetsTextureSize.x;

ivec3 morphUV = ivec3( x, y, morphTargetIndex );
return texelFetch( morphTargetsTexture, morphUV, 0 );

vec4 ret = vec4(0.);

ret.x = texelFetch( morphTargetsTexture, morphUV, 0 ).r;

morphUV.x++;

ret.y = texelFetch( morphTargetsTexture, morphUV, 0 ).r;

morphUV.x++;

ret.z = texelFetch( morphTargetsTexture, morphUV, 0 ).r;

#if MORPHTARGETS_TEXTURE_STRIDE == 10

morphUV.x++;

ret.a = offset == 2 ? texelFetch( morphTargetsTexture, morphUV, 0 ).r : 0.;

#endif

return ret;

}

Expand Down
Loading