|
@@ -88,7 +88,30 @@ function SimulatorRenderer(WIDTH, renderer) {
|
|
|
|
|
|
scene.add( mesh );
|
|
scene.add( mesh );
|
|
|
|
|
|
- this.getRenderTarget = function() {
|
|
|
|
|
|
+ var flipflop = true;
|
|
|
|
+ var rtPosition1, rtPosition2, rtVelocity1, rtVelocity2;
|
|
|
|
+
|
|
|
|
+ function init() {
|
|
|
|
+ var dtPosition = generateDataTexture();
|
|
|
|
+ var dtVelocity = generateVelocityTexture();
|
|
|
|
+
|
|
|
|
+ rtPosition1 = getRenderTarget();
|
|
|
|
+ rtPosition2 = rtPosition1.clone();
|
|
|
|
+ rtVelocity1 = rtPosition1.clone();
|
|
|
|
+ rtVelocity2 = rtPosition1.clone();
|
|
|
|
+
|
|
|
|
+ simulator.renderTexture(dtPosition, rtPosition1);
|
|
|
|
+ simulator.renderTexture(rtPosition1, rtPosition2);
|
|
|
|
+
|
|
|
|
+ simulator.renderTexture(dtVelocity, rtVelocity1);
|
|
|
|
+ simulator.renderTexture(rtVelocity1, rtVelocity2);
|
|
|
|
+
|
|
|
|
+ simulator.velocityUniforms.testing.value = 10;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ this.init = init;
|
|
|
|
+
|
|
|
|
+ function getRenderTarget() {
|
|
var renderTarget = new THREE.WebGLRenderTarget(WIDTH, WIDTH, {
|
|
var renderTarget = new THREE.WebGLRenderTarget(WIDTH, WIDTH, {
|
|
wrapS: THREE.RepeatWrapping,
|
|
wrapS: THREE.RepeatWrapping,
|
|
wrapT: THREE.RepeatWrapping,
|
|
wrapT: THREE.RepeatWrapping,
|
|
@@ -103,7 +126,7 @@ function SimulatorRenderer(WIDTH, renderer) {
|
|
}
|
|
}
|
|
|
|
|
|
// Takes a texture, and render out as another texture
|
|
// Takes a texture, and render out as another texture
|
|
- this.renderTexture = function( input, output ) {
|
|
|
|
|
|
+ this.renderTexture = function ( input, output ) {
|
|
mesh.material = passThruShader;
|
|
mesh.material = passThruShader;
|
|
uniforms.texture.value = input;
|
|
uniforms.texture.value = input;
|
|
renderer.render( scene, camera, output );
|
|
renderer.render( scene, camera, output );
|
|
@@ -117,6 +140,7 @@ function SimulatorRenderer(WIDTH, renderer) {
|
|
positionShader.uniforms.time.value = performance.now();
|
|
positionShader.uniforms.time.value = performance.now();
|
|
positionShader.uniforms.delta.value = delta;
|
|
positionShader.uniforms.delta.value = delta;
|
|
renderer.render( scene, camera, output );
|
|
renderer.render( scene, camera, output );
|
|
|
|
+ this.currentPosition = output;
|
|
}
|
|
}
|
|
|
|
|
|
this.renderVelocity = function(position, velocity, output, delta) {
|
|
this.renderVelocity = function(position, velocity, output, delta) {
|
|
@@ -126,6 +150,87 @@ function SimulatorRenderer(WIDTH, renderer) {
|
|
velocityShader.uniforms.time.value = performance.now();
|
|
velocityShader.uniforms.time.value = performance.now();
|
|
velocityShader.uniforms.delta.value = delta;
|
|
velocityShader.uniforms.delta.value = delta;
|
|
renderer.render( scene, camera, output );
|
|
renderer.render( scene, camera, output );
|
|
|
|
+ this.currentVelocity = output;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ this.simulate = function( delta ) {
|
|
|
|
+
|
|
|
|
+ if (flipflop) {
|
|
|
|
+
|
|
|
|
+ simulator.renderVelocity( rtPosition1, rtVelocity1, rtVelocity2, delta );
|
|
|
|
+ simulator.renderPosition( rtPosition1, rtVelocity2, rtPosition2, delta );
|
|
|
|
+
|
|
|
|
+ } else {
|
|
|
|
+
|
|
|
|
+ simulator.renderVelocity( rtPosition2, rtVelocity2, rtVelocity1, delta );
|
|
|
|
+ simulator.renderPosition( rtPosition2, rtVelocity1, rtPosition1, delta );
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ flipflop = !flipflop;
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ function generateDataTexture() {
|
|
|
|
+
|
|
|
|
+ var x, y, z;
|
|
|
|
+
|
|
|
|
+ var w = WIDTH, h = WIDTH;
|
|
|
|
+
|
|
|
|
+ var a = new Float32Array(PARTICLES * 4);
|
|
|
|
+
|
|
|
|
+ for (var k = 0; k < PARTICLES; k++) {
|
|
|
|
+
|
|
|
|
+ x = Math.random() * BOUNDS - BOUNDS_HALF;
|
|
|
|
+ y = Math.random() * BOUNDS - BOUNDS_HALF;
|
|
|
|
+ z = Math.random() * BOUNDS - BOUNDS_HALF;
|
|
|
|
+
|
|
|
|
+ a[ k*4 + 0 ] = x;
|
|
|
|
+ a[ k*4 + 1 ] = y;
|
|
|
|
+ a[ k*4 + 2 ] = z;
|
|
|
|
+ a[ k*4 + 3 ] = 1;
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ var texture = new THREE.DataTexture( a, WIDTH, WIDTH, THREE.RGBAFormat, THREE.FloatType );
|
|
|
|
+ texture.minFilter = THREE.NearestFilter;
|
|
|
|
+ texture.magFilter = THREE.NearestFilter;
|
|
|
|
+ texture.needsUpdate = true;
|
|
|
|
+ texture.flipY = false;
|
|
|
|
+
|
|
|
|
+ return texture;
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ function generateVelocityTexture() {
|
|
|
|
+
|
|
|
|
+ var x, y, z;
|
|
|
|
+
|
|
|
|
+ var w = WIDTH, h = WIDTH;
|
|
|
|
+
|
|
|
|
+ var a = new Float32Array(PARTICLES * 4);
|
|
|
|
+
|
|
|
|
+ for (var k = 0; k < PARTICLES; k++) {
|
|
|
|
+
|
|
|
|
+ x = Math.random() - 0.5;
|
|
|
|
+ y = Math.random() - 0.5;
|
|
|
|
+ z = Math.random() - 0.5;
|
|
|
|
+
|
|
|
|
+ a[ k*4 + 0 ] = x * 10;
|
|
|
|
+ a[ k*4 + 1 ] = y * 10;
|
|
|
|
+ a[ k*4 + 2 ] = z * 10;
|
|
|
|
+ a[ k*4 + 3 ] = 1;
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ var texture = new THREE.DataTexture( a, WIDTH, WIDTH, THREE.RGBAFormat, THREE.FloatType );
|
|
|
|
+ texture.minFilter = THREE.NearestFilter;
|
|
|
|
+ texture.magFilter = THREE.NearestFilter;
|
|
|
|
+ texture.needsUpdate = true;
|
|
|
|
+ texture.flipY = false;
|
|
|
|
+
|
|
|
|
+ return texture;
|
|
|
|
+
|
|
}
|
|
}
|
|
|
|
|
|
}
|
|
}
|