-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsoftbodyrenderer.js
More file actions
211 lines (193 loc) · 8.91 KB
/
softbodyrenderer.js
File metadata and controls
211 lines (193 loc) · 8.91 KB
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
'use strict';
var SoftBodyRenderer = function(gl, src) {
this.sprite = new Sprite(src, Sprite.loadAsGLTexture(gl));
this.gl = gl;
this.vertexBuffer = gl.createBuffer();
this.texCoordBuffer = gl.createBuffer();
this.positionData = null;
};
/**
* @param {Object} grid Positions calculated by physics in the following format:
* {
* width: {number}
* height: {number}
* positions: [
* {
* x: {number}
* y: {number}
* getRadius: {function returning number}
* }
* ]
* }
* With positions of points forming a rectangular grid in a column-major order.
* @param {Array} worldTransform Transform from world to GL unit coordinates as 4x4 matrix.
*/
SoftBodyRenderer.prototype.render = function(grid, worldTransform, hilight, hilightTexCoord) {
if (!this.sprite.loaded)
return;
var gl = this.gl;
SoftBodyRenderer.shader.use({'uTex': this.sprite.texture, 'uWorldTransform': worldTransform, 'uHilight': hilight, 'uHilightTexCoord': hilightTexCoord});
var triangleCount = 4 * (grid.width + 1) * (grid.height + 1);
var vertexCount = triangleCount * 3;
var texCoordData = null;
if (this.positionData == null || vertexCount * 2 != this.positionData.length) {
this.positionData = new Float32Array(vertexCount * 2);
texCoordData = new Float32Array(vertexCount * 2);
}
var arrInd = 0;
for (var x = 0; x <= grid.width; ++x) {
for (var y = 0; y <= grid.height; ++y) {
// Should do with an index buffer...
SoftBodyRenderer.pushExtendedGridTriangles(this.positionData, texCoordData, arrInd, grid, x, y);
arrInd += 2 /* per vertex */ * 3 /* per triangle */ * 4 /* 4 triangles */;
}
}
gl.bindBuffer(gl.ARRAY_BUFFER, this.vertexBuffer);
gl.bufferData(gl.ARRAY_BUFFER, this.positionData, gl.DYNAMIC_DRAW);
var positionAttribLocation = 0;
gl.vertexAttribPointer(positionAttribLocation, 2, gl.FLOAT, false, 0, 0);
gl.bindBuffer(gl.ARRAY_BUFFER, this.texCoordBuffer);
if (texCoordData != null) {
gl.bufferData(gl.ARRAY_BUFFER, texCoordData, gl.DYNAMIC_DRAW);
}
var texCoordAttribLocation = 1;
gl.vertexAttribPointer(texCoordAttribLocation, 2, gl.FLOAT, false, 0, 0);
gl.drawArrays(gl.TRIANGLES, 0, triangleCount * 3);
};
SoftBodyRenderer.getGridPosition = function(grid, xInd, yInd) {
var positionsIndex = yInd + xInd * (grid.height + 1);
return grid.positions[positionsIndex];
};
SoftBodyRenderer.pushExtendedGridCoords = function(target, targetTexCoords, arrInd, grid, x, y) {
/* Corner */
if ((x == 0 || x == grid.width + 1) && (y == 0 || y == grid.height + 1)) {
var x2 = x > 0 ? grid.width : 0;
var y2 = y > 0 ? grid.height : 0;
var pos = SoftBodyRenderer.getGridPosition(grid, x2, y2);
var xInner = x2 == 0 ? 1 : x2 - 1;
var yInner = y2 == 0 ? 1 : y2 - 1;
if (xInner < 0 || yInner < 0 || xInner > grid.width || yInner > grid.height) {
if (xInner < 0 || xInner > grid.width) {
throw 'tubes must be horizontal!';
}
yInner = 0;
var posInner = SoftBodyRenderer.getGridPosition(grid, xInner, yInner);
var posDiff = new Vec2(pos.x - posInner.x, pos.y - posInner.y);
var angle = posDiff.angle();
if ((y == 0) == (x == 0)) {
angle += Math.PI * 0.25; // normal angle
} else {
angle -= Math.PI * 0.25; // normal angle
}
var diff = new Vec2(Math.cos(angle), Math.sin(angle));
diff.scale(pos.getRadius() * 1.6);
} else {
var posInner = SoftBodyRenderer.getGridPosition(grid, xInner, yInner);
var diff = new Vec2(pos.x - posInner.x, pos.y - posInner.y);
diff.normalize();
diff.scale(pos.getRadius());
}
target[arrInd] = pos.x + diff.x;
target[arrInd + 1] = pos.y + diff.y;
}
/* Left or right side */
else if (x == 0 || x == grid.width + 1) {
var x2 = x > 0 ? grid.width : 0;
var xInner = x2 == 0 ? 1 : x2 - 1;
var pos1 = SoftBodyRenderer.getGridPosition(grid, x2, y - 1);
var pos2 = SoftBodyRenderer.getGridPosition(grid, x2, y);
var pos = new Vec2((pos1.x + pos2.x) * 0.5, (pos1.y + pos2.y) * 0.5);
var posDiff = new Vec2(pos2.x - pos1.x, pos2.y - pos1.y);
var angle = posDiff.angle();
if (x == 0) {
angle += Math.PI * 0.5; // normal angle
} else {
angle -= Math.PI * 0.5; // normal angle
}
var diff = new Vec2(Math.cos(angle), Math.sin(angle));
diff.scale((pos1.getRadius() + pos2.getRadius()) * 0.5);
target[arrInd] = pos.x + diff.x;
target[arrInd + 1] = pos.y + diff.y;
}
/* Top or bottom side */
else if (y == 0 || y == grid.height + 1) {
var y2 = y > 0 ? grid.height : 0;
var yInner = y2 == 0 ? 1 : y2 - 1;
var pos1 = SoftBodyRenderer.getGridPosition(grid, x - 1, y2);
var pos2 = SoftBodyRenderer.getGridPosition(grid, x, y2);
var pos = new Vec2((pos1.x + pos2.x) * 0.5, (pos1.y + pos2.y) * 0.5);
var posDiff = new Vec2(pos2.x - pos1.x, pos2.y - pos1.y);
var angle = posDiff.angle();
if (y != 0) {
angle += Math.PI * 0.5; // normal angle
} else {
angle -= Math.PI * 0.5; // normal angle
}
var diff = new Vec2(Math.cos(angle), Math.sin(angle));
diff.scale((pos1.getRadius() + pos2.getRadius()) * 0.5);
target[arrInd] = pos.x + diff.x;
target[arrInd + 1] = pos.y + diff.y;
}
/* Grid point */
else if (Math.abs(x % 1 - 0.5) < 0.2 && Math.abs(y % 1 - 0.5) < 0.2) {
var pos = SoftBodyRenderer.getGridPosition(grid, Math.floor(x), Math.floor(y));
target[arrInd] = pos.x;
target[arrInd + 1] = pos.y;
}
/* In the middle of four grid points */
else {
var pos1 = SoftBodyRenderer.getGridPosition(grid, x - 1, y - 1);
var pos2 = SoftBodyRenderer.getGridPosition(grid, x - 1, y);
var pos3 = SoftBodyRenderer.getGridPosition(grid, x, y - 1);
var pos4 = SoftBodyRenderer.getGridPosition(grid, x, y);
target[arrInd] = (pos1.x + pos2.x + pos3.x + pos4.x) * 0.25;
target[arrInd + 1] = (pos1.y + pos2.y + pos3.y + pos4.y) * 0.25;
}
if (targetTexCoords != null) {
targetTexCoords[arrInd] = x / (grid.width + 1);
targetTexCoords[arrInd + 1] = y / (grid.height + 1);
}
};
SoftBodyRenderer.pushExtendedGridTriangles = function(target, targetTexCoords, arrInd, grid, xInd, yInd) {
// Draw an envelope shape centered on the grid point using 4 triangles
SoftBodyRenderer.pushExtendedGridCoords(target, targetTexCoords, arrInd, grid, xInd, yInd);
SoftBodyRenderer.pushExtendedGridCoords(target, targetTexCoords, arrInd + 2, grid, xInd + 0.5, yInd + 0.5);
SoftBodyRenderer.pushExtendedGridCoords(target, targetTexCoords, arrInd + 4, grid, xInd, yInd + 1);
arrInd += 6;
SoftBodyRenderer.pushExtendedGridCoords(target, targetTexCoords, arrInd, grid, xInd, yInd);
SoftBodyRenderer.pushExtendedGridCoords(target, targetTexCoords, arrInd + 2, grid, xInd + 1, yInd);
SoftBodyRenderer.pushExtendedGridCoords(target, targetTexCoords, arrInd + 4, grid, xInd + 0.5, yInd + 0.5);
arrInd += 6;
SoftBodyRenderer.pushExtendedGridCoords(target, targetTexCoords, arrInd, grid, xInd + 1, yInd);
SoftBodyRenderer.pushExtendedGridCoords(target, targetTexCoords, arrInd + 2, grid, xInd + 1, yInd + 1);
SoftBodyRenderer.pushExtendedGridCoords(target, targetTexCoords, arrInd + 4, grid, xInd + 0.5, yInd + 0.5);
arrInd += 6;
SoftBodyRenderer.pushExtendedGridCoords(target, targetTexCoords, arrInd, grid, xInd + 0.5, yInd + 0.5);
SoftBodyRenderer.pushExtendedGridCoords(target, targetTexCoords, arrInd + 2, grid, xInd + 1, yInd + 1);
SoftBodyRenderer.pushExtendedGridCoords(target, targetTexCoords, arrInd + 4, grid, xInd, yInd + 1);
arrInd += 6;
};
SoftBodyRenderer.pushGridCoords = function(target, targetTexCoords, arrInd, grid, xInd, yInd) {
var pos = SoftBodyRenderer.getGridPosition(grid, xInd, yInd);
target[arrInd] = (pos.x);
target[arrInd + 1] = (pos.y);
if (targetTexCoords != null) {
targetTexCoords[arrInd] = (xInd / grid.width);
targetTexCoords[arrInd + 1] = (yInd / grid.height);
}
};
SoftBodyRenderer.vertexSrc = ShaderProgram.vertexLibrary.textured;
SoftBodyRenderer.fragmentSrc = ShaderProgram.fragmentLibrary.texturedHilighted;
SoftBodyRenderer.loadShaders = function(gl) {
var uniforms = {
'uTex': 'tex2d',
'uWorldTransform': 'Matrix4fv',
'uHilight': '1f',
'uHilightTexCoord': '2fv'
};
var attributes = {
'aVertexPosition': 0,
'aTexCoord': 1
};
SoftBodyRenderer.shader = new ShaderProgram(gl, SoftBodyRenderer.fragmentSrc, SoftBodyRenderer.vertexSrc, uniforms, attributes);
};