-
Notifications
You must be signed in to change notification settings - Fork 0
/
simulatorrenderer.js
115 lines (86 loc) · 4.15 KB
/
simulatorrenderer.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
var SimulatorRenderer = (function () {
function SimulatorRenderer (canvas, wgl, projectionMatrix, camera, gridDimensions, onLoaded) {
this.canvas = canvas;
this.wgl = wgl;
this.projectionMatrix = projectionMatrix;
this.camera = camera;
wgl.getExtension('OES_texture_float');
wgl.getExtension('OES_texture_float_linear');
var rendererLoaded = false,
simulatorLoaded = false;
this.renderer = new Renderer(this.canvas, this.wgl, gridDimensions, (function () {
rendererLoaded = true;
if (rendererLoaded && simulatorLoaded) {
start.call(this);
}
}).bind(this));
this.simulator = new Simulator(this.wgl, (function () {
simulatorLoaded = true;
if (rendererLoaded && simulatorLoaded) {
start.call(this);
}
}).bind(this));
function start () {
/////////////////////////////////////////////
// interaction stuff
//mouse position is in [-1, 1]
this.mouseX = 0;
this.mouseY = 0;
//the mouse plane is a plane centered at the camera orbit point and orthogonal to the view direction
this.lastMousePlaneX = 0;
this.lastMousePlaneY = 0;
setTimeout(onLoaded, 1);
}
}
SimulatorRenderer.prototype.onMouseMove = function (event) {
var position = Utilities.getMousePosition(event, this.canvas);
var normalizedX = position.x / this.canvas.width;
var normalizedY = position.y / this.canvas.height;
this.mouseX = normalizedX * 2.0 - 1.0;
this.mouseY = (1.0 - normalizedY) * 2.0 - 1.0;
this.camera.onMouseMove(event);
};
SimulatorRenderer.prototype.onMouseDown = function (event) {
this.camera.onMouseDown(event);
};
SimulatorRenderer.prototype.onMouseUp = function (event) {
this.camera.onMouseUp(event);
};
SimulatorRenderer.prototype.reset = function (particlesWidth, particlesHeight, particlePositions, gridSize, gridResolution, particleDensity, sphereRadius) {
this.simulator.reset(particlesWidth, particlesHeight, particlePositions, gridSize, gridResolution, particleDensity);
this.renderer.reset(particlesWidth, particlesHeight, sphereRadius);
}
SimulatorRenderer.prototype.update = function (timeStep) {
var fov = 2.0 * Math.atan(1.0 / this.projectionMatrix[5]);
var viewSpaceMouseRay = [
this.mouseX * Math.tan(fov / 2.0) * (this.canvas.width / this.canvas.height),
this.mouseY * Math.tan(fov / 2.0),
-1.0];
var mousePlaneX = viewSpaceMouseRay[0] * this.camera.distance;
var mousePlaneY = viewSpaceMouseRay[1] * this.camera.distance;
var mouseVelocityX = mousePlaneX - this.lastMousePlaneX;
var mouseVelocityY = mousePlaneY - this.lastMousePlaneY;
if (this.camera.isMouseDown()) {
mouseVelocityX = 0.0;
mouseVelocityY = 0.0;
}
this.lastMousePlaneX = mousePlaneX;
this.lastMousePlaneY = mousePlaneY;
var inverseViewMatrix = Utilities.invertMatrix([], this.camera.getViewMatrix());
var worldSpaceMouseRay = Utilities.transformDirectionByMatrix([], viewSpaceMouseRay, inverseViewMatrix);
Utilities.normalizeVector(worldSpaceMouseRay, worldSpaceMouseRay);
var cameraViewMatrix = this.camera.getViewMatrix();
var cameraRight = [cameraViewMatrix[0], cameraViewMatrix[4], cameraViewMatrix[8]];
var cameraUp = [cameraViewMatrix[1], cameraViewMatrix[5], cameraViewMatrix[9]];
var mouseVelocity = [];
for (var i = 0; i < 3; ++i) {
mouseVelocity[i] = mouseVelocityX * cameraRight[i] + mouseVelocityY * cameraUp[i];
}
this.simulator.simulate(timeStep, mouseVelocity, this.camera.getPosition(), worldSpaceMouseRay);
this.renderer.draw(this.simulator, this.projectionMatrix, this.camera.getViewMatrix());
}
SimulatorRenderer.prototype.onResize = function (event) {
this.renderer.onResize(event);
}
return SimulatorRenderer;
}());