|
@@ -20,7 +20,7 @@
|
|
<div id="info">
|
|
<div id="info">
|
|
<a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - webgl gpgpu birds + GLTF mesh<br/>
|
|
<a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - webgl gpgpu birds + GLTF mesh<br/>
|
|
Flamingo by <a href="http://mirada.com/">mirada</a> from <a href="http://ro.me">rome</a><br/>
|
|
Flamingo by <a href="http://mirada.com/">mirada</a> from <a href="http://ro.me">rome</a><br/>
|
|
- Move mouse to disturb birds.
|
|
|
|
|
|
+ Move mouse to disturb birds.
|
|
</div>
|
|
</div>
|
|
|
|
|
|
<!-- shader for bird's position -->
|
|
<!-- shader for bird's position -->
|
|
@@ -214,65 +214,69 @@
|
|
import { GPUComputationRenderer } from './jsm/misc/GPUComputationRenderer.js';
|
|
import { GPUComputationRenderer } from './jsm/misc/GPUComputationRenderer.js';
|
|
|
|
|
|
/* TEXTURE WIDTH FOR SIMULATION */
|
|
/* TEXTURE WIDTH FOR SIMULATION */
|
|
- var WIDTH = 64
|
|
|
|
|
|
+ var WIDTH = 64;
|
|
var BIRDS = WIDTH * WIDTH;
|
|
var BIRDS = WIDTH * WIDTH;
|
|
-
|
|
|
|
|
|
+
|
|
/* BAKE ANIMATION INTO TEXTURE and CREATE GEOMETRY FROM BASE MODEL */
|
|
/* BAKE ANIMATION INTO TEXTURE and CREATE GEOMETRY FROM BASE MODEL */
|
|
var BirdGeometry = new THREE.BufferGeometry();
|
|
var BirdGeometry = new THREE.BufferGeometry();
|
|
- var textureAnimation, durationAnimation, originalMaterial,birdMesh, materialShader, vertexPerBird;
|
|
|
|
- function nextPowerOf2( n ) { return Math.pow( 2, Math.ceil( Math.log( n ) / Math.log( 2 ) ) ) };
|
|
|
|
|
|
+ var textureAnimation, durationAnimation, birdMesh, materialShader, vertexPerBird;
|
|
|
|
+
|
|
|
|
+ function nextPowerOf2( n ) {
|
|
|
|
+
|
|
|
|
+ return Math.pow( 2, Math.ceil( Math.log( n ) / Math.log( 2 ) ) );
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
|
|
- Math.lerp = function (value1, value2, amount) {
|
|
|
|
|
|
+ Math.lerp = function ( value1, value2, amount ) {
|
|
|
|
|
|
amount = Math.max( Math.min( amount, 1 ), 0 );
|
|
amount = Math.max( Math.min( amount, 1 ), 0 );
|
|
- return value1 + (value2 - value1) * amount;
|
|
|
|
-
|
|
|
|
|
|
+ return value1 + ( value2 - value1 ) * amount;
|
|
|
|
+
|
|
};
|
|
};
|
|
|
|
|
|
- var gltfs = [ 'models/gltf/Parrot.glb', 'models/gltf/Flamingo.glb' ]
|
|
|
|
- var colors = [ 0xccFFFF, 0xffdeff ]
|
|
|
|
- var sizes = [ 0.2, 0.1 ]
|
|
|
|
- var selectModel = Math.floor( Math.random() * gltfs.length )
|
|
|
|
|
|
+ var gltfs = [ 'models/gltf/Parrot.glb', 'models/gltf/Flamingo.glb' ];
|
|
|
|
+ var colors = [ 0xccFFFF, 0xffdeff ];
|
|
|
|
+ var sizes = [ 0.2, 0.1 ];
|
|
|
|
+ var selectModel = Math.floor( Math.random() * gltfs.length );
|
|
new GLTFLoader().load( gltfs[ selectModel ], function ( gltf ) {
|
|
new GLTFLoader().load( gltfs[ selectModel ], function ( gltf ) {
|
|
|
|
|
|
var animations = gltf.animations;
|
|
var animations = gltf.animations;
|
|
durationAnimation = Math.round( animations[ 0 ].duration * 60 );
|
|
durationAnimation = Math.round( animations[ 0 ].duration * 60 );
|
|
var birdGeo = gltf.scene.children[ 0 ].geometry;
|
|
var birdGeo = gltf.scene.children[ 0 ].geometry;
|
|
- originalMaterial = gltf.scene.children[ 0 ].material;
|
|
|
|
var morphAttributes = birdGeo.morphAttributes.position;
|
|
var morphAttributes = birdGeo.morphAttributes.position;
|
|
var tHeight = nextPowerOf2( durationAnimation );
|
|
var tHeight = nextPowerOf2( durationAnimation );
|
|
var tWidth = nextPowerOf2( birdGeo.getAttribute( 'position' ).count );
|
|
var tWidth = nextPowerOf2( birdGeo.getAttribute( 'position' ).count );
|
|
vertexPerBird = birdGeo.getAttribute( 'position' ).count;
|
|
vertexPerBird = birdGeo.getAttribute( 'position' ).count;
|
|
var tData = new Float32Array( 3 * tWidth * tHeight );
|
|
var tData = new Float32Array( 3 * tWidth * tHeight );
|
|
|
|
|
|
- for( var i = 0 ; i < tWidth ; i++ ){
|
|
|
|
|
|
+ for ( var i = 0; i < tWidth; i ++ ) {
|
|
|
|
|
|
- for( var j = 0 ; j < tHeight ; j++ ){
|
|
|
|
|
|
+ for ( var j = 0; j < tHeight; j ++ ) {
|
|
|
|
|
|
var offset = j * tWidth * 3;
|
|
var offset = j * tWidth * 3;
|
|
|
|
|
|
- var curFrame = j;
|
|
|
|
var curMorph = Math.floor( j / durationAnimation * morphAttributes.length );
|
|
var curMorph = Math.floor( j / durationAnimation * morphAttributes.length );
|
|
var nextMorph = ( Math.floor( j / durationAnimation * morphAttributes.length ) + 1 ) % morphAttributes.length;
|
|
var nextMorph = ( Math.floor( j / durationAnimation * morphAttributes.length ) + 1 ) % morphAttributes.length;
|
|
var lerpAmount = j / durationAnimation * morphAttributes.length % 1;
|
|
var lerpAmount = j / durationAnimation * morphAttributes.length % 1;
|
|
|
|
|
|
- if( j < durationAnimation ){
|
|
|
|
|
|
+ if ( j < durationAnimation ) {
|
|
|
|
|
|
tData[ offset + i * 3 ] = Math.lerp( morphAttributes[ curMorph ].array[ i * 3 ], morphAttributes[ nextMorph ].array[ i * 3 ], lerpAmount );
|
|
tData[ offset + i * 3 ] = Math.lerp( morphAttributes[ curMorph ].array[ i * 3 ], morphAttributes[ nextMorph ].array[ i * 3 ], lerpAmount );
|
|
tData[ offset + i * 3 + 1 ] = Math.lerp( morphAttributes[ curMorph ].array[ i * 3 + 1 ], morphAttributes[ nextMorph ].array[ i * 3 + 1 ], lerpAmount );
|
|
tData[ offset + i * 3 + 1 ] = Math.lerp( morphAttributes[ curMorph ].array[ i * 3 + 1 ], morphAttributes[ nextMorph ].array[ i * 3 + 1 ], lerpAmount );
|
|
tData[ offset + i * 3 + 2 ] = Math.lerp( morphAttributes[ curMorph ].array[ i * 3 + 2 ], morphAttributes[ nextMorph ].array[ i * 3 + 2 ], lerpAmount );
|
|
tData[ offset + i * 3 + 2 ] = Math.lerp( morphAttributes[ curMorph ].array[ i * 3 + 2 ], morphAttributes[ nextMorph ].array[ i * 3 + 2 ], lerpAmount );
|
|
-
|
|
|
|
|
|
+
|
|
}
|
|
}
|
|
- }
|
|
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
|
|
}
|
|
}
|
|
-
|
|
|
|
|
|
+
|
|
textureAnimation = new THREE.DataTexture( tData, tWidth, tHeight, THREE.RGBFormat, THREE.FloatType );
|
|
textureAnimation = new THREE.DataTexture( tData, tWidth, tHeight, THREE.RGBFormat, THREE.FloatType );
|
|
textureAnimation.needsUpdate = true;
|
|
textureAnimation.needsUpdate = true;
|
|
|
|
|
|
var vertices = [], color = [], reference = [], seeds = [], indices = [];
|
|
var vertices = [], color = [], reference = [], seeds = [], indices = [];
|
|
var totalVertices = birdGeo.getAttribute( 'position' ).count * 3 * BIRDS;
|
|
var totalVertices = birdGeo.getAttribute( 'position' ).count * 3 * BIRDS;
|
|
- for( var i = 0 ; i < totalVertices ; i++ ){
|
|
|
|
|
|
+ for ( var i = 0; i < totalVertices; i ++ ) {
|
|
|
|
|
|
var bIndex = i % ( birdGeo.getAttribute( 'position' ).count * 3 );
|
|
var bIndex = i % ( birdGeo.getAttribute( 'position' ).count * 3 );
|
|
vertices.push( birdGeo.getAttribute( 'position' ).array[ bIndex ] );
|
|
vertices.push( birdGeo.getAttribute( 'position' ).array[ bIndex ] );
|
|
@@ -281,11 +285,11 @@
|
|
}
|
|
}
|
|
|
|
|
|
var r = Math.random();
|
|
var r = Math.random();
|
|
- for( var i = 0 ; i < birdGeo.getAttribute( 'position' ).count * BIRDS ; i++ ){
|
|
|
|
-
|
|
|
|
|
|
+ for ( var i = 0; i < birdGeo.getAttribute( 'position' ).count * BIRDS; i ++ ) {
|
|
|
|
+
|
|
var bIndex = i % ( birdGeo.getAttribute( 'position' ).count );
|
|
var bIndex = i % ( birdGeo.getAttribute( 'position' ).count );
|
|
var bird = Math.floor( i / birdGeo.getAttribute( 'position' ).count );
|
|
var bird = Math.floor( i / birdGeo.getAttribute( 'position' ).count );
|
|
- if( bIndex == 0 ) r = Math.random();
|
|
|
|
|
|
+ if ( bIndex == 0 ) r = Math.random();
|
|
var j = ~ ~ bird;
|
|
var j = ~ ~ bird;
|
|
var x = ( j % WIDTH ) / WIDTH;
|
|
var x = ( j % WIDTH ) / WIDTH;
|
|
var y = ~ ~ ( j / WIDTH ) / WIDTH;
|
|
var y = ~ ~ ( j / WIDTH ) / WIDTH;
|
|
@@ -294,9 +298,9 @@
|
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
- for( var i = 0 ; i < birdGeo.index.array.length * BIRDS ; i++ ){
|
|
|
|
|
|
+ for ( var i = 0; i < birdGeo.index.array.length * BIRDS; i ++ ) {
|
|
|
|
|
|
- var offset = Math.floor( i / birdGeo.index.array.length ) * ( birdGeo.getAttribute( 'position' ).count );
|
|
|
|
|
|
+ var offset = Math.floor( i / birdGeo.index.array.length ) * ( birdGeo.getAttribute( 'position' ).count );
|
|
indices.push( birdGeo.index.array[ i % birdGeo.index.array.length ] + offset );
|
|
indices.push( birdGeo.index.array[ i % birdGeo.index.array.length ] + offset );
|
|
|
|
|
|
}
|
|
}
|
|
@@ -330,7 +334,6 @@
|
|
var positionVariable;
|
|
var positionVariable;
|
|
var positionUniforms;
|
|
var positionUniforms;
|
|
var velocityUniforms;
|
|
var velocityUniforms;
|
|
- var birdUniforms;
|
|
|
|
|
|
|
|
function init() {
|
|
function init() {
|
|
|
|
|
|
@@ -358,7 +361,7 @@
|
|
dirLight.position.multiplyScalar( 30 );
|
|
dirLight.position.multiplyScalar( 30 );
|
|
scene.add( dirLight );
|
|
scene.add( dirLight );
|
|
|
|
|
|
- renderer = new THREE.WebGLRenderer( { antialias : true, alpha : true } );
|
|
|
|
|
|
+ renderer = new THREE.WebGLRenderer( { antialias: true, alpha: true } );
|
|
renderer.setPixelRatio( window.devicePixelRatio );
|
|
renderer.setPixelRatio( window.devicePixelRatio );
|
|
renderer.setSize( window.innerWidth, window.innerHeight );
|
|
renderer.setSize( window.innerWidth, window.innerHeight );
|
|
container.appendChild( renderer.domElement );
|
|
container.appendChild( renderer.domElement );
|
|
@@ -393,8 +396,8 @@
|
|
velocityUniforms[ "alignmentDistance" ].value = effectController.alignment;
|
|
velocityUniforms[ "alignmentDistance" ].value = effectController.alignment;
|
|
velocityUniforms[ "cohesionDistance" ].value = effectController.cohesion;
|
|
velocityUniforms[ "cohesionDistance" ].value = effectController.cohesion;
|
|
velocityUniforms[ "freedomFactor" ].value = effectController.freedom;
|
|
velocityUniforms[ "freedomFactor" ].value = effectController.freedom;
|
|
- if( materialShader ) materialShader.uniforms[ "size" ].value = effectController.size;
|
|
|
|
- BirdGeometry.setDrawRange ( 0, vertexPerBird * effectController.count )
|
|
|
|
|
|
+ if ( materialShader ) materialShader.uniforms[ "size" ].value = effectController.size;
|
|
|
|
+ BirdGeometry.setDrawRange( 0, vertexPerBird * effectController.count );
|
|
|
|
|
|
};
|
|
};
|
|
|
|
|
|
@@ -445,35 +448,39 @@
|
|
velocityVariable.wrapT = THREE.RepeatWrapping;
|
|
velocityVariable.wrapT = THREE.RepeatWrapping;
|
|
positionVariable.wrapS = THREE.RepeatWrapping;
|
|
positionVariable.wrapS = THREE.RepeatWrapping;
|
|
positionVariable.wrapT = THREE.RepeatWrapping;
|
|
positionVariable.wrapT = THREE.RepeatWrapping;
|
|
-
|
|
|
|
|
|
+
|
|
var error = gpuCompute.init();
|
|
var error = gpuCompute.init();
|
|
|
|
+
|
|
if ( error !== null ) {
|
|
if ( error !== null ) {
|
|
|
|
+
|
|
console.error( error );
|
|
console.error( error );
|
|
|
|
+
|
|
}
|
|
}
|
|
|
|
+
|
|
}
|
|
}
|
|
|
|
|
|
function initBirds() {
|
|
function initBirds() {
|
|
|
|
|
|
var geometry = BirdGeometry;
|
|
var geometry = BirdGeometry;
|
|
|
|
|
|
- var m = new THREE.MeshStandardMaterial({
|
|
|
|
- vertexColors : true,
|
|
|
|
- flatShading : true,
|
|
|
|
- roughness : 1,
|
|
|
|
- metalness : 0
|
|
|
|
- })
|
|
|
|
|
|
+ var m = new THREE.MeshStandardMaterial( {
|
|
|
|
+ vertexColors: true,
|
|
|
|
+ flatShading: true,
|
|
|
|
+ roughness: 1,
|
|
|
|
+ metalness: 0
|
|
|
|
+ } );
|
|
|
|
|
|
m.onBeforeCompile = ( shader ) => {
|
|
m.onBeforeCompile = ( shader ) => {
|
|
|
|
|
|
- shader.uniforms.texturePosition = { value : null };
|
|
|
|
- shader.uniforms.textureVelocity = { value : null };
|
|
|
|
- shader.uniforms.textureAnimation = { value : textureAnimation };
|
|
|
|
- shader.uniforms.time = { value : 1.0 };
|
|
|
|
- shader.uniforms.size = { value : 0.1 };
|
|
|
|
- shader.uniforms.delta = { value : 0.0 };
|
|
|
|
|
|
+ shader.uniforms.texturePosition = { value: null };
|
|
|
|
+ shader.uniforms.textureVelocity = { value: null };
|
|
|
|
+ shader.uniforms.textureAnimation = { value: textureAnimation };
|
|
|
|
+ shader.uniforms.time = { value: 1.0 };
|
|
|
|
+ shader.uniforms.size = { value: 0.1 };
|
|
|
|
+ shader.uniforms.delta = { value: 0.0 };
|
|
|
|
|
|
var token = '#define STANDARD';
|
|
var token = '#define STANDARD';
|
|
-
|
|
|
|
|
|
+
|
|
var insert = /* glsl */`
|
|
var insert = /* glsl */`
|
|
attribute vec4 reference;
|
|
attribute vec4 reference;
|
|
attribute vec4 seeds;
|
|
attribute vec4 seeds;
|
|
@@ -488,18 +495,18 @@
|
|
shader.vertexShader = shader.vertexShader.replace( token, token + insert );
|
|
shader.vertexShader = shader.vertexShader.replace( token, token + insert );
|
|
|
|
|
|
var token = '#include <begin_vertex>';
|
|
var token = '#include <begin_vertex>';
|
|
-
|
|
|
|
|
|
+
|
|
var insert = /* glsl */`
|
|
var insert = /* glsl */`
|
|
vec4 tmpPos = texture2D( texturePosition, reference.xy );
|
|
vec4 tmpPos = texture2D( texturePosition, reference.xy );
|
|
-
|
|
|
|
|
|
+
|
|
vec3 pos = tmpPos.xyz;
|
|
vec3 pos = tmpPos.xyz;
|
|
vec3 velocity = normalize(texture2D( textureVelocity, reference.xy ).xyz);
|
|
vec3 velocity = normalize(texture2D( textureVelocity, reference.xy ).xyz);
|
|
vec3 aniPos = texture2D( textureAnimation, vec2( reference.z, mod( ( time + seeds.x ) * ( ( 0.0004 + seeds.y / 10000.0) + normalize( velocity ) / 20000.0 ), reference.w ) ) ).xyz;
|
|
vec3 aniPos = texture2D( textureAnimation, vec2( reference.z, mod( ( time + seeds.x ) * ( ( 0.0004 + seeds.y / 10000.0) + normalize( velocity ) / 20000.0 ), reference.w ) ) ).xyz;
|
|
vec3 newPosition = position;
|
|
vec3 newPosition = position;
|
|
-
|
|
|
|
|
|
+
|
|
newPosition = mat3( modelMatrix ) * ( newPosition + aniPos );
|
|
newPosition = mat3( modelMatrix ) * ( newPosition + aniPos );
|
|
newPosition *= size + seeds.y * size * 0.2;
|
|
newPosition *= size + seeds.y * size * 0.2;
|
|
-
|
|
|
|
|
|
+
|
|
velocity.z *= -1.;
|
|
velocity.z *= -1.;
|
|
float xz = length( velocity.xz );
|
|
float xz = length( velocity.xz );
|
|
float xyz = 1.;
|
|
float xyz = 1.;
|
|
@@ -516,19 +523,19 @@
|
|
|
|
|
|
newPosition = maty * matz * newPosition;
|
|
newPosition = maty * matz * newPosition;
|
|
newPosition += pos;
|
|
newPosition += pos;
|
|
-
|
|
|
|
|
|
+
|
|
vec3 transformed = vec3( newPosition );
|
|
vec3 transformed = vec3( newPosition );
|
|
`;
|
|
`;
|
|
-
|
|
|
|
|
|
+
|
|
shader.vertexShader = shader.vertexShader.replace( token, insert );
|
|
shader.vertexShader = shader.vertexShader.replace( token, insert );
|
|
|
|
|
|
materialShader = shader;
|
|
materialShader = shader;
|
|
|
|
|
|
- }
|
|
|
|
|
|
+ };
|
|
|
|
|
|
birdMesh = new THREE.Mesh( geometry, m );
|
|
birdMesh = new THREE.Mesh( geometry, m );
|
|
birdMesh.rotation.y = Math.PI / 2;
|
|
birdMesh.rotation.y = Math.PI / 2;
|
|
-
|
|
|
|
|
|
+
|
|
birdMesh.castShadow = true;
|
|
birdMesh.castShadow = true;
|
|
birdMesh.receiveShadow = true;
|
|
birdMesh.receiveShadow = true;
|
|
|
|
|
|
@@ -635,7 +642,7 @@
|
|
|
|
|
|
var now = performance.now();
|
|
var now = performance.now();
|
|
var delta = ( now - last ) / 1000;
|
|
var delta = ( now - last ) / 1000;
|
|
-
|
|
|
|
|
|
+
|
|
if ( delta > 1 ) delta = 1; // safety cap on large deltas
|
|
if ( delta > 1 ) delta = 1; // safety cap on large deltas
|
|
last = now;
|
|
last = now;
|
|
|
|
|
|
@@ -643,8 +650,8 @@
|
|
positionUniforms[ "delta" ].value = delta;
|
|
positionUniforms[ "delta" ].value = delta;
|
|
velocityUniforms[ "time" ].value = now;
|
|
velocityUniforms[ "time" ].value = now;
|
|
velocityUniforms[ "delta" ].value = delta;
|
|
velocityUniforms[ "delta" ].value = delta;
|
|
- if( materialShader ) materialShader.uniforms[ "time" ].value = now;
|
|
|
|
- if( materialShader ) materialShader.uniforms[ "delta" ].value = delta;
|
|
|
|
|
|
+ if ( materialShader ) materialShader.uniforms[ "time" ].value = now;
|
|
|
|
+ if ( materialShader ) materialShader.uniforms[ "delta" ].value = delta;
|
|
|
|
|
|
velocityUniforms[ "predator" ].value.set( 0.5 * mouseX / windowHalfX, - 0.5 * mouseY / windowHalfY, 0 );
|
|
velocityUniforms[ "predator" ].value.set( 0.5 * mouseX / windowHalfX, - 0.5 * mouseY / windowHalfY, 0 );
|
|
|
|
|
|
@@ -652,11 +659,12 @@
|
|
mouseY = 10000;
|
|
mouseY = 10000;
|
|
|
|
|
|
gpuCompute.compute();
|
|
gpuCompute.compute();
|
|
-
|
|
|
|
- if( materialShader ) materialShader.uniforms[ "texturePosition" ].value = gpuCompute.getCurrentRenderTarget( positionVariable ).texture;
|
|
|
|
- if( materialShader ) materialShader.uniforms[ "textureVelocity" ].value = gpuCompute.getCurrentRenderTarget( velocityVariable ).texture;
|
|
|
|
-
|
|
|
|
|
|
+
|
|
|
|
+ if ( materialShader ) materialShader.uniforms[ "texturePosition" ].value = gpuCompute.getCurrentRenderTarget( positionVariable ).texture;
|
|
|
|
+ if ( materialShader ) materialShader.uniforms[ "textureVelocity" ].value = gpuCompute.getCurrentRenderTarget( velocityVariable ).texture;
|
|
|
|
+
|
|
renderer.render( scene, camera );
|
|
renderer.render( scene, camera );
|
|
|
|
+
|
|
}
|
|
}
|
|
|
|
|
|
</script>
|
|
</script>
|