-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrectangle.js
372 lines (308 loc) · 12.5 KB
/
rectangle.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
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
/**
* The colors can be a vertex attribute, everything is done at runtime
Exception might be the textured things
Could you do it with a three buffer attribute?
Some things have opacity
You'll have to resubmit every frame
*/
function temporarilyVisibleWarningSign(str) {
let sign = Rectangle({
h: 3.5,
z: 4.9,
label: str,
getScaleFromLabel: true,
haveFrame: true
})
sign.timeVisible = 0
updateFunctions.push(() => {
sign.timeVisible -= frameDelta
sign.visible = sign.timeVisible > 0.
})
return sign
}
function initRectangles() {
let blackMaterial = new THREE.MeshBasicMaterial({ color: 0x000000 })
let tblr = "tblr"
let invisibleMaterial = new THREE.MeshBasicMaterial({visible: false})
let materials = []
let materialRects = []
let unitCoords = [
-0.5, -0.5, 0.0,
0.5, -0.5, 0.0,
0.5, 0.5, 0.0,
0.5, 0.5, 0.0,
-0.5, 0.5, 0.0,
-0.5, -0.5, 0.0
]
let unitUvs = [
0.,0.,
1.,0.,
1.,1.,
1.,1.,
0.,1.,
0.,0.
]
roundOffRectangleCreation = () => {
materials.forEach((mat,i)=>{
if (mat === invisibleMaterial)
return
let rects = materialRects[i]
let numRects = rects.length
if(numRects === 1) {
var mesh = new THREE.Mesh(unitSquareGeo, mat)
scene.add(mesh)
updateFunctions.push(() => {
mesh.position.copy(rects[0].position)
mesh.scale.copy(rects[0].scale)
mesh.visible = rects[0].visible
mesh.rotation.z = rects[0].rotationZ
})
}
else {
let bufferMesh = new THREE.Mesh(new THREE.BufferGeometry(), mat)
let coords = new Float32Array(3 * 3 * 2 * numRects)
let uvs = new Float32Array(3 * 2 * 2 * numRects)
bufferMesh.geometry.addAttribute("position", new THREE.BufferAttribute(coords, 3))
bufferMesh.geometry.addAttribute("uv", new THREE.BufferAttribute(uvs, 2))
scene.add(bufferMesh)
rects.forEach((rect, j) => {
for (let k = 0; k < 12; ++k) {
uvs[(j * 6 + k) * 2 + 0] = unitUvs[k * 2 + 0]
uvs[(j * 6 + k) * 2 + 1] = unitUvs[k * 2 + 1]
}
})
updateFunctions.push(() => {
rects.forEach((rect, j) => {
for (let k = 0; k < 12; ++k) {
if (rect.visible === false) {
coords[(j * 6 + k) * 3 + 0] = 0.
coords[(j * 6 + k) * 3 + 1] = 900.
coords[(j * 6 + k) * 3 + 2] = -900.
}
else {
coords[(j * 6 + k) * 3 + 0] = unitCoords[k * 3 + 0] * rect.scale.x + rect.position.x
coords[(j * 6 + k) * 3 + 1] = unitCoords[k * 3 + 1] * rect.scale.y + rect.position.y
coords[(j * 6 + k) * 3 + 2] = rect.position.z
}
}
})
bufferMesh.geometry.attributes.position.needsUpdate = true
})
}
})
Rectangle = () => { console.error("this isn't meant to be called after init") }
}
//starting out with 1700 materials, this binning takes it down to 270
//Rectangle only gets called 40 times so that should mean it can go down to that
//then some could even be merged, using vertex colors
//ones with textures need their own
Rectangle = function(params) {
if(frameCount > 1)
log("yo")
if (params === undefined)
params = {}
const rect = {}
rectangles.push(rect)
rect.getEdgeCenter = function (edge, target) {
target.y = this.position.y
if (edge === "t" || edge === "b")
target.y += this.scale.y * .5 * (edge === "b" ? -1. : 1.)
target.x = this.position.x
if (edge === "l" || edge === "r")
target.x += this.scale.x * .5 * (edge === "l" ? -1. : 1.)
return target
}
rect.goToIntendedPosition = () => {
rect.position.x = rect.intendedPosition.x
rect.position.y = rect.intendedPosition.y
}
rect.getCorner = function (corner, target) {
target.x = this.position.x
target.y = this.position.y
target.y += this.scale.y / 2. * (corner[0] === "t" ? 1. : -1.)
target.x += this.scale.x / 2. * (corner[1] === "l" ? -1. : 1.)
}
//the bets are all the same length as the money they're worth
rect.setPositionFromEdge = function (edge,x,y) {
rect.position.x = 0
rect.position.y = 0
rect.getEdgeCenter(edge, v0)
rect.position.x = x - v0.x
rect.position.y = y - v0.y
}
rect.setPositionFromCorner = function (corner, x, y) {
let xAddition = (corner[1] === "l" ? 1. : -1.) * this.scale.x / 2.
this.position.x = x + xAddition
if (y !== undefined) {
let yAddition = (corner[0] === "t" ? -1. : 1.) * this.scale.y / 2.
this.position.y = y + yAddition
}
if (this.intendedPosition !== undefined) {
this.intendedPosition.x = this.position.x
this.intendedPosition.y = this.position.y
}
}
{
let mat = null
if(params.mat)
mat = params.mat
else if(params.frameOnly || params.getScaleFromLabel)
mat = invisibleMaterial
else
mat = new THREE.MeshBasicMaterial()
rect.material = mat
if (materials.indexOf(mat) === -1) {
materials.push(mat)
materialRects.push([])
}
materialRects[materials.indexOf(mat)].push(rect)
if (params.label) {
let labelLines = typeof params.label === "string" ? [params.label] : params.label
rect.textMeshes = Array(labelLines.length)
labelLines.forEach((line,i)=>{
rect.textMeshes[i] = text(line, false)
scene.add(rect.textMeshes[i])
})
updateFunctions.push(() => {
let widestTextMesh = getMax(rect.textMeshes, (tm) => { return tm.scale.x })
if (!widestTextMesh)
return
const widestWidth = widestTextMesh.scale.x
const intendedWidth = rect.scale.x
const scaleMultiple = intendedWidth / widestWidth
rect.textMeshes.forEach((tm,i) => {
tm.position.x = rect.position.x
tm.position.y = rect.position.y + tm.scale.y * -(i-(rect.textMeshes.length-1.)/2.)
tm.position.z = rect.position.z + .05
tm.visible = rect.visible
tm.scale.multiplyScalar(scaleMultiple)
tm.position.z = rect.position.z
})
if(params.getScaleFromLabel ) {
let widestAspect = -1.
rect.textMeshes.forEach((tm)=>{
widestAspect = Math.max(widestAspect,tm.material.getAspect())
})
rect.scale.x = widestAspect * rect.scale.y / rect.textMeshes.length
}
})
}
if (params.map)
mat.map = params.map
if (params.col)
mat.color.setHex(params.col)
if (params.opacity) {
mat.transparent = true
mat.opacity = params.opacity
}
rect.rotationZ = 0.
rect.setRotationZ = (newRotationZ) => {
rect.rotationZ = newRotationZ
//TODO if you want to do this with something with more than one material...
}
{
rect.position = new THREE.Vector3()
rect.scale = new THREE.Vector3(1.,1.,1.)
rect.color = new THREE.Color()
rect.visible = true
if (params.visible !== undefined)
rect.visible = params.visible
updateFunctions.push(() => {
if (rect.edges) {
rect.edges.forEach((edge) => {
edge.visible = rect.visible
})
}
})
rect.toggleVisibility = () => {
let newVal = !rect.visible
rect.visible = newVal
if (rect.textMeshes !== undefined)
rect.textMeshes.forEach((tm)=>{tm.visible = newVal})
}
}
}
{
if (params.x)
rect.position.x = params.x
if (params.y)
rect.position.y = params.y
if (params.z)
rect.position.z = params.z
if (params.w)
rect.scale.x = params.w
if (params.h)
rect.scale.y = params.h
if (params.getPosition) {
updateFunctions.push(() => {
params.getPosition(rect.position)
})
}
if (params.getScale) {
updateFunctions.push(() => {
params.getScale(rect.scale)
})
}
if (params.haveIntendedPosition) {
rect.intendedPosition = new THREE.Vector3().copy(rect.position)
let settlementRate = params.settlementRate || .1
updateFunctions.push(() => {
rect.position.x += (rect.intendedPosition.x - rect.position.x) * settlementRate * (60. * frameDelta)
rect.position.y += (rect.intendedPosition.y - rect.position.y) * settlementRate * (60. * frameDelta)
})
}
if (params.haveIntendedScale) {
rect.intendedScale = new THREE.Vector3().copy(rect.scale)
updateFunctions.push(() => {
rect.scale.x += (rect.intendedScale.x - rect.scale.x) * .1
rect.scale.y += (rect.intendedScale.y - rect.scale.y) * .1
})
}
}
if (params.frameThickness || params.haveFrame) {
rect.frameThickness = .05
rect.edges = Array(4)
for (let i = 0; i < 4; ++i) {
const edge = tblr[i]
let frameR = Rectangle({
mat: blackMaterial
})
rect.edges[i] = frameR
updateFunctions.push(() => {
rect.getEdgeCenter(edge, frameR.position)
frameR.position.z = params.frameZ === undefined ? rect.position.z : params.frameZ
if (edge === "l" || edge === "r") {
frameR.position.x += rect.frameThickness * .5 * (edge === "l" ? -1. : 1.)
frameR.scale.x = rect.frameThickness
frameR.scale.y = rect.scale.y + rect.frameThickness * 2.
}
if (edge === "t" || edge === "b") {
frameR.position.y += rect.frameThickness * .5 * (edge === "t" ? 1. : -1.)
frameR.scale.y = rect.frameThickness
frameR.scale.x = rect.scale.x + rect.frameThickness * 2.
}
})
}
}
{
if (params.onClick)
rect.onClick = params.onClick
rect.pointInside = (p) => {
v0.copy(p)
v0.x -= rect.position.x
v0.y -= rect.position.y
v0.x /= rect.scale.x
v0.y /= rect.scale.y
if (-.5 < v0.x && v0.x < .5 &&
-.5 < v0.y && v0.y < .5)
return true
else
return false
}
rect.mouseInside = () => {
return rect.pointInside(mouse.position)
}
}
return rect
}
}