-
-
Notifications
You must be signed in to change notification settings - Fork 4.4k
Expand file tree
/
Copy pathstroke-geometry.js
More file actions
266 lines (218 loc) · 8.89 KB
/
stroke-geometry.js
File metadata and controls
266 lines (218 loc) · 8.89 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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
/* globals THREE */
function StrokeGeometry (material, entityEl) {
this.material = material;
this.maxBufferSize = 5000;
this.geometries = [];
this.currentGeometry = null;
this.entityEl = entityEl;
this.addBuffer(false);
this.first = true;
}
StrokeGeometry.prototype = {
addBuffer: function (copyLast) {
var geometry = new THREE.BufferGeometry();
var vertices = new Float32Array(this.maxBufferSize * 3);
var indices = new Uint32Array(this.maxBufferSize * 4.5);
var normals = new Float32Array(this.maxBufferSize * 3);
var uvs = new Float32Array(this.maxBufferSize * 2);
var colors = new Float32Array(this.maxBufferSize * 3);
var mesh = new THREE.Mesh(geometry, this.material);
mesh.frustumCulled = false;
mesh.vertices = vertices;
this.object3D = new THREE.Object3D();
this.object3D.add(mesh);
this.entityEl.object3D.add(this.object3D);
geometry.setDrawRange(0, 0);
geometry.setAttribute('position', new THREE.BufferAttribute(vertices, 3).setUsage(THREE.DynamicDrawUsage));
geometry.attributes.position.updateRanges.count = 0;
geometry.setIndex(new THREE.BufferAttribute(indices, 3).setUsage(THREE.DynamicDrawUsage));
geometry.index.updateRanges.count = 0;
geometry.setAttribute('uv', new THREE.BufferAttribute(uvs, 2).setUsage(THREE.DynamicDrawUsage));
geometry.attributes.uv.updateRanges.count = 0;
geometry.setAttribute('normal', new THREE.BufferAttribute(normals, 3).setUsage(THREE.DynamicDrawUsage));
geometry.attributes.normal.updateRanges.count = 0;
geometry.setAttribute('color', new THREE.BufferAttribute(colors, 3).setUsage(THREE.DynamicDrawUsage));
geometry.attributes.color.updateRanges.count = 0;
this.previousGeometry = null;
if (this.geometries.length > 0) {
this.previousGeometry = this.currentGeometry;
}
this.indices = {
position: 0,
index: 0,
uv: 0,
normal: 0,
color: 0
};
this.prevIndices = Object.assign({}, this.indices);
this.geometries.push(geometry);
this.currentGeometry = geometry;
// Copies first position from previous buffer so they are continous with no gaps.
// Necessary for example for drawing strokes.
if (this.previousGeometry && copyLast) {
var prev = (this.maxBufferSize - 2) * 3;
var col = (this.maxBufferSize - 2) * 3;
var norm = (this.maxBufferSize - 2) * 3;
var position = this.previousGeometry.attributes.position.array;
this.addVertex(position[prev++], position[prev++], position[prev++]);
this.addVertex(position[prev++], position[prev++], position[prev++]);
var normal = this.previousGeometry.attributes.normal.array;
this.addNormal(normal[norm++], normal[norm++], normal[norm++]);
this.addNormal(normal[norm++], normal[norm++], normal[norm++]);
var color = this.previousGeometry.attributes.color.array;
this.addColor(color[col++], color[col++], color[col++]);
this.addColor(color[col++], color[col++], color[col++]);
uvs = this.previousGeometry.attributes.uv.array;
}
},
addColor: function (r, g, b) {
this.currentGeometry.attributes.color.setXYZ(this.indices.color++, r, g, b);
},
addNormal: function (x, y, z) {
this.currentGeometry.attributes.normal.setXYZ(this.indices.normal++, x, y, z);
},
setSize: (function () {
return function (size) {
if (this.size === size) {
return;
}
this.size = size;
};
})(),
addPoint: (function () {
var direction = new THREE.Vector3();
var vertexA = new THREE.Vector3();
var vertexAOffset = new THREE.Vector3();
var vertexB = new THREE.Vector3();
var vertexBOffset = new THREE.Vector3();
return function (position, orientation, width) {
direction.set(1, 0, 0);
direction.applyQuaternion(orientation);
direction.normalize();
// Add two vertices to the triangle strip separated by the brush size.
vertexA.copy(position);
vertexB.copy(position);
vertexA.add(vertexAOffset.copy(direction).multiplyScalar(width / 2));
vertexB.add(vertexBOffset.copy(direction).multiplyScalar(-width / 2));
// if (this.first && this.indices.position > 0) {
// debugger;
// // Degenerated triangle
// this.first = false;
// this.addVertex(vertexA.x, vertexA.y, vertexA.z);
// this.indices.normal++;
// this.indices.color++;
// this.indices.uv++;
// }
/*
2---3
| \ |
0---1
*/
this.addVertex(vertexA.x, vertexA.y, vertexA.z);
this.addVertex(vertexB.x, vertexB.y, vertexB.z);
this.indices.normal += 2;
this.addColor(this.material.color.r, this.material.color.g, this.material.color.b);
this.addColor(this.material.color.r, this.material.color.g, this.material.color.b);
this.update();
this.computeVertexNormals();
};
})(),
computeVertexNormals: (function () {
var pA = new THREE.Vector3();
var pB = new THREE.Vector3();
var pC = new THREE.Vector3();
var cb = new THREE.Vector3();
var ab = new THREE.Vector3();
return function () {
var start = this.prevIndices.position === 0 ? 0 : (this.prevIndices.position + 1) * 3;
var end = (this.indices.position) * 3;
var vertices = this.currentGeometry.attributes.position.array;
var normals = this.currentGeometry.attributes.normal.array;
for (var i = start; i <= end; i++) {
normals[i] = 0;
}
var pair = true;
for (i = start; i < end - 6; i += 3) {
if (pair) {
pA.fromArray(vertices, i);
pB.fromArray(vertices, i + 3);
pC.fromArray(vertices, i + 6);
} else {
pB.fromArray(vertices, i);
pC.fromArray(vertices, i + 6);
pA.fromArray(vertices, i + 3);
}
pair = !pair;
cb.subVectors(pC, pB);
ab.subVectors(pA, pB);
cb.cross(ab);
cb.normalize();
normals[i] += cb.x;
normals[i + 1] += cb.y;
normals[i + 2] += cb.z;
normals[i + 3] += cb.x;
normals[i + 4] += cb.y;
normals[i + 5] += cb.z;
normals[i + 6] += cb.x;
normals[i + 7] += cb.y;
normals[i + 8] += cb.z;
}
/*
first and last vertices (0 and 8) belongs just to one triangle
second and penultimate (1 and 7) belongs to two triangles
the rest of the vertices belongs to three triangles
1_____3_____5_____7
/\ /\ /\ /\
/ \ / \ / \ / \
/____\/____\/____\/____\
0 2 4 6 8
*/
// Vertices that are shared across three triangles
for (i = start + 2 * 3; i < end - 2 * 3; i++) {
normals[i] = normals[i] / 3;
}
// Second and penultimate triangle, that shares just two triangles
normals[start + 3] = normals[start + 3] / 2;
normals[start + 3 + 1] = normals[start + 3 + 1] / 2;
normals[start + 3 + 2] = normals[start + 3 * 1 + 2] / 2;
normals[end - 2 * 3] = normals[end - 2 * 3] / 2;
normals[end - 2 * 3 + 1] = normals[end - 2 * 3 + 1] / 2;
normals[end - 2 * 3 + 2] = normals[end - 2 * 3 + 2] / 2;
};
})(),
addVertex: function (x, y, z) {
var buffer = this.currentGeometry.attributes.position;
// Initialize a new buffer if full;
if (this.indices.position === buffer.count) {
this.addBuffer(true);
buffer = this.currentGeometry.attributes.position;
}
buffer.setXYZ(this.indices.position++, x, y, z);
// Every two new vertices we add two new triangles.
if ((this.indices.position + 1) % 2 === 0 && this.indices.position > 1) {
/* Line brushes
2---3
| \ |
0---1
{0, 1, 2}, {2, 1, 3}
*/
this.currentGeometry.index.setXYZ(this.indices.index++, this.indices.position - 3, this.indices.position - 2, this.indices.position - 1);
this.currentGeometry.index.setXYZ(this.indices.index++, this.indices.position - 1, this.indices.position - 2, this.indices.position);
}
},
update: function () {
// Draw one less triangle to prevent indexing into blank positions
// on an even-number-positioned undo
this.currentGeometry.setDrawRange(0, (this.indices.position * 3) - 4);
this.currentGeometry.attributes.color.updateRanges.count = this.indices.position * 3;
this.currentGeometry.attributes.color.needsUpdate = true;
this.currentGeometry.attributes.normal.updateRanges.count = this.indices.position * 3;
this.currentGeometry.attributes.normal.needsUpdate = true;
this.currentGeometry.attributes.position.updateRanges.count = this.indices.position * 3;
this.currentGeometry.attributes.position.needsUpdate = true;
this.currentGeometry.attributes.uv.updateRanges.count = this.indices.position * 2;
this.currentGeometry.attributes.uv.needsUpdate = true;
this.currentGeometry.index.updateRanges.count = this.indices.position * 3;
this.currentGeometry.index.needsUpdate = true;
}
};