-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSimulationRenderer.js
executable file
·235 lines (163 loc) · 5.94 KB
/
SimulationRenderer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
/**
* @author zz85 https://github.com/zz85 / http://www.lab4games.net/zz85/blog
*
* Bird Simulation Render
*
* A simple scene rendering a quad of the following shaders
* 1. Pass-thru Shader,
* 2. Bird Position Update Shader,
* 3. Bird Velocity Update Shader
*
*/
function SimulationRenderer( WIDTH, renderer ) {
WIDTH = WIDTH || 4;
var camera = new THREE.Camera();
camera.position.z = 1;
// Init RTT stuff
gl = renderer.getContext();
if ( ! gl.getExtension( "OES_texture_float" ) ) {
alert( "No OES_texture_float support for float textures!" );
return;
}
if ( gl.getParameter( gl.MAX_VERTEX_TEXTURE_IMAGE_UNITS ) == 0 ) {
alert( "No support for vertex shader textures!" );
return;
}
var scene = new THREE.Scene();
var uniforms = {
time: { type: "f", value: 1.0 },
resolution: { type: "v2", value: new THREE.Vector2( WIDTH, WIDTH ) },
texture: { type: "t", value: null }
};
var passThruShader = new THREE.ShaderMaterial( {
uniforms: uniforms,
vertexShader: document.getElementById( 'vertexShader' ).textContent,
fragmentShader: document.getElementById( 'fragmentShader' ).textContent
} );
var mesh = new THREE.Mesh( new THREE.PlaneBufferGeometry( 2, 2 ), passThruShader );
var positionShader = new THREE.ShaderMaterial( {
uniforms: {
time: { type: "f", value: 1.0 },
delta: { type: "f", value: 0.0 },
resolution: { type: "v2", value: new THREE.Vector2( WIDTH, WIDTH ) },
texturePosition: { type: "t", value: null },
textureVelocity: { type: "t", value: null },
},
vertexShader: document.getElementById( 'vertexShader' ).textContent,
fragmentShader: document.getElementById( 'fragmentShaderPosition' ).textContent
} );
this.positionShader = positionShader;
var velocityShader = new THREE.ShaderMaterial( {
uniforms: {
time: { type: "f", value: 1.0 },
delta: { type: "f", value: 0.0 },
resolution: { type: "v2", value: new THREE.Vector2( WIDTH, WIDTH ) },
texturePosition: { type: "t", value: null },
textureVelocity: { type: "t", value: null },
testing: { type: "f", value: 1.0 },
seperationDistance: { type: "f", value: 1.0 },
alignmentDistance: { type: "f", value: 1.0 },
cohesionDistance: { type: "f", value: 1.0 },
freedomFactor: { type: "f", value: 1.0 },
predator: { type: "v3", value: new THREE.Vector3() }
},
defines: {
WIDTH: WIDTH.toFixed( 2 )
},
vertexShader: document.getElementById( 'vertexShader' ).textContent,
fragmentShader: document.getElementById( 'fragmentShaderVelocity' ).textContent
} );
this.velocityUniforms = velocityShader.uniforms;
scene.add( mesh );
var flipflop = true;
var rtPosition1, rtPosition2, rtVelocity1, rtVelocity2;
function init() {
var dtPosition = generatePositionTexture();
var dtVelocity = generateVelocityTexture();
rtPosition1 = getRenderTarget( THREE.RGBAFormat );
rtPosition2 = rtPosition1.clone();
rtVelocity1 = getRenderTarget( THREE.RGBFormat );
rtVelocity2 = rtVelocity1.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( type ) {
var renderTarget = new THREE.WebGLRenderTarget( WIDTH, WIDTH, {
wrapS: THREE.RepeatWrapping,
wrapT: THREE.RepeatWrapping,
minFilter: THREE.NearestFilter,
magFilter: THREE.NearestFilter,
format: type,
type: THREE.FloatType,
stencilBuffer: false
} );
return renderTarget;
}
// Takes a texture, and render out as another texture
this.renderTexture = function ( input, output ) {
mesh.material = passThruShader;
uniforms.texture.value = input;
renderer.render( scene, camera, output );
};
this.renderPosition = function( position, velocity, output, delta ) {
mesh.material = positionShader;
positionShader.uniforms.texturePosition.value = position;
positionShader.uniforms.textureVelocity.value = velocity;
positionShader.uniforms.time.value = performance.now();
positionShader.uniforms.delta.value = delta;
renderer.render( scene, camera, output );
this.currentPosition = output;
};
this.renderVelocity = function( position, velocity, output, delta ) {
mesh.material = velocityShader;
velocityShader.uniforms.texturePosition.value = position;
velocityShader.uniforms.textureVelocity.value = velocity;
velocityShader.uniforms.time.value = performance.now();
velocityShader.uniforms.delta.value = delta;
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 generatePositionTexture() {
var a = new Float32Array( PARTICLES * 4 );
for ( var k = 0, kl = a.length; k < kl; k += 4 ) {
var x = Math.random() * BOUNDS - BOUNDS_HALF;
var y = Math.random() * BOUNDS - BOUNDS_HALF;
var z = Math.random() * BOUNDS - BOUNDS_HALF;
a[ k + 0 ] = x;
a[ k + 1 ] = y;
a[ k + 2 ] = z;
a[ k + 3 ] = 1;
}
var texture = new THREE.DataTexture( a, WIDTH, WIDTH, THREE.RGBAFormat, THREE.FloatType );
texture.needsUpdate = true;
return texture;
}
function generateVelocityTexture() {
var a = new Float32Array( PARTICLES * 3 );
for ( var k = 0, kl = a.length; k < kl; k += 3 ) {
var x = Math.random() - 0.5;
var y = Math.random() - 0.5;
var z = Math.random() - 0.5;
a[ k + 0 ] = x * 10;
a[ k + 1 ] = y * 10;
a[ k + 2 ] = z * 10;
}
var texture = new THREE.DataTexture( a, WIDTH, WIDTH, THREE.RGBFormat, THREE.FloatType );
texture.needsUpdate = true;
return texture;
}
}