forked from Artysta112/logo
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutil.js
413 lines (368 loc) · 10.8 KB
/
util.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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
const perspective = require('gl-mat4/perspective')
const multiply = require('gl-mat4/multiply')
const lookAt = require('gl-mat4/lookAt')
const invert = require('gl-mat4/invert')
const rotate = require('gl-mat4/rotate')
const transform = require('gl-vec3/transformMat4')
const SVG_NS = 'http://www.w3.org/2000/svg'
module.exports = {
calculateSizingOptions,
createLogoViewer,
createModelRenderer,
loadModelFromJson,
positionsFromModel,
createPolygonsFromModelJson,
createStandardModelPolygon,
createMatrixComputer,
compareZ,
createFaceUpdater,
createNode,
setAttribute,
svgElementToSvgImageContent,
Polygon,
}
function createLogoViewer (container, renderScene, {
followMouse = false,
followMotion = false,
slowDrift = false,
lazyRender = true,
} = {}) {
let shouldRender = true
const mouse = {
x: 0,
y: 0,
}
const lookCurrent = [0, 0]
const lookRate = 0.3
// closes over scene state
const renderCurrentScene = () => {
updateLookCurrent()
renderScene(lookCurrent, slowDrift)
}
function setLookAtTarget (target) {
const bounds = container.getBoundingClientRect()
mouse.x = 1.0 - ((2.0 * (target.x - bounds.left)) / bounds.width)
mouse.y = 1.0 - ((2.0 * (target.y - bounds.top)) / bounds.height)
}
function stopAnimation () {
shouldRender = false
}
function startAnimation () {
shouldRender = true
}
function setFollowMouse (state) {
followMouse = state
}
function setFollowMotion (state) {
followMotion = state
}
window.addEventListener('mousemove', function (ev) {
if (!shouldRender) {
startAnimation()
}
if (followMouse) {
setLookAtTarget({
x: ev.clientX,
y: ev.clientY,
})
renderCurrentScene()
}
})
window.addEventListener('deviceorientation', function (event) {
if (!shouldRender) {
startAnimation()
}
if (followMotion) {
// gamma: left to right
const leftToRight = event.gamma
// beta: front back motion
const frontToBack = event.beta
// x offset: needed to correct the intial position
const xOffset = 200
// y offset: needed to correct the intial position
const yOffset = -300
// acceleration
const acceleration = 10
setLookAtTarget({
x: xOffset + (leftToRight * acceleration),
y: yOffset + (frontToBack * acceleration),
})
renderCurrentScene()
}
})
function lookAtAndRender (target) {
// update look target
setLookAtTarget(target)
// this should prolly just call updateLookCurrent or set lookCurrent values to eaxactly lookTarget
// but im not really sure why its different, so im leaving it alone
lookCurrent[0] = mouse.x
lookCurrent[1] = mouse.y + (0.085 / lookRate)
renderCurrentScene()
}
function renderLoop () {
if (!shouldRender) {
return
}
window.requestAnimationFrame(renderLoop)
renderCurrentScene()
}
function updateLookCurrent () {
const li = (1.0 - lookRate)
lookCurrent[0] = (li * lookCurrent[0]) + (lookRate * mouse.x)
lookCurrent[1] = (li * lookCurrent[1]) + (lookRate * mouse.y) + 0.085
}
if (lazyRender) {
renderCurrentScene()
} else {
renderLoop()
}
return {
container,
lookAt: setLookAtTarget,
setFollowMouse,
setFollowMotion,
stopAnimation,
startAnimation,
lookAtAndRender,
renderCurrentScene,
}
}
function loadModelFromJson (modelJson, createSvgPolygon = createStandardModelPolygon) {
const vertCount = modelJson.positions.length
const positions = new Float32Array(3 * vertCount)
const transformed = new Float32Array(3 * vertCount)
const { polygons, polygonsByChunk } = createPolygonsFromModelJson(modelJson, createSvgPolygon)
positionsFromModel(positions, modelJson)
const updatePositions = createPositionUpdater(positions, transformed, vertCount)
const modelObj = { updatePositions, positions, transformed, polygons, polygonsByChunk }
return modelObj
}
function createModelRenderer (container, cameraDistance, modelObj) {
const { updatePositions, transformed, polygons } = modelObj
for (const polygon of polygons) {
container.appendChild(polygon.svg)
}
const computeMatrix = createMatrixComputer(cameraDistance)
const updateFaces = createFaceUpdater(container, polygons, transformed)
return (rect, lookPos, slowDrift) => {
const matrix = computeMatrix(rect, lookPos, slowDrift)
updatePositions(matrix)
updateFaces(rect, container, polygons, transformed)
}
}
function positionsFromModel (positions, modelJson) {
const pp = modelJson.positions
let ptr = 0
for (let i = 0; i < pp.length; ++i) {
const p = pp[i]
for (let j = 0; j < 3; ++j) {
positions[ptr] = p[j]
ptr += 1
}
}
}
function createPolygonsFromModelJson (modelJson, createSvgPolygon) {
const polygons = []
const polygonsByChunk = modelJson.chunks.map((chunk) => {
const { faces } = chunk
return faces.map((face) => {
const svgPolygon = createSvgPolygon(chunk)
const polygon = new Polygon(svgPolygon, face)
polygons.push(polygon)
return polygon
})
})
return { polygons, polygonsByChunk }
}
function createStandardModelPolygon (chunk) {
const color = `rgb(${chunk.color})`
const svgPolygon = createNode('polygon')
setAttribute(
svgPolygon,
'fill',
color,
)
setAttribute(
svgPolygon,
'stroke',
color,
)
setAttribute(
svgPolygon,
'points',
'0,0, 10,0, 0,10',
)
return svgPolygon
}
function createMatrixComputer (distance) {
const objectCenter = new Float32Array(3)
const up = new Float32Array([0, 1, 0])
const projection = new Float32Array(16)
const model = new Float32Array(16)
const view = lookAt(
new Float32Array(16),
new Float32Array([0, 0, distance]),
objectCenter,
up,
)
const invView = invert(new Float32Array(16), view)
const invProjection = new Float32Array(16)
const target = new Float32Array(3)
const transformedMatrix = new Float32Array(16)
const X = new Float32Array([1, 0, 0])
const Y = new Float32Array([0, 1, 0])
const Z = new Float32Array([0, 0, 1])
return (rect, lookPos, slowDrift) => {
const viewportWidth = rect.width
const viewportHeight = rect.height
perspective(
projection,
Math.PI / 4.0,
viewportWidth / viewportHeight,
100.0,
1000.0,
)
invert(invProjection, projection)
target[0] = lookPos[0]
target[1] = lookPos[1]
target[2] = 1.2
transform(target, target, invProjection)
transform(target, target, invView)
lookAt(
model,
objectCenter,
target,
up,
)
// this shouldnt operate directly on the matrix/model,
// it should likely operate on the lookPos
// if we do want to operate on the matrix/model, it shouldnt happen here
if (slowDrift) {
const time = (Date.now() / 1000.0)
rotate(model, model, 0.1 + (Math.sin(time / 3) * 0.2), X)
rotate(model, model, -0.1 + (Math.sin(time / 2) * 0.03), Z)
rotate(model, model, 0.5 + (Math.sin(time / 3) * 0.2), Y)
}
multiply(transformedMatrix, projection, view)
multiply(transformedMatrix, transformedMatrix, model)
return transformedMatrix
}
}
function createPositionUpdater (positions, transformed, vertCount) {
return (M) => {
const m00 = M[0]
const m01 = M[1]
const m02 = M[2]
const m03 = M[3]
const m10 = M[4]
const m11 = M[5]
const m12 = M[6]
const m13 = M[7]
const m20 = M[8]
const m21 = M[9]
const m22 = M[10]
const m23 = M[11]
const m30 = M[12]
const m31 = M[13]
const m32 = M[14]
const m33 = M[15]
for (let i = 0; i < vertCount; ++i) {
const x = positions[3 * i]
const y = positions[(3 * i) + 1]
const z = positions[(3 * i) + 2]
const tw = (x * m03) + (y * m13) + (z * m23) + m33
transformed[3 * i] =
((x * m00) + (y * m10) + (z * m20) + m30) / tw
transformed[(3 * i) + 1] =
((x * m01) + (y * m11) + (z * m21) + m31) / tw
transformed[(3 * i) + 2] =
((x * m02) + (y * m12) + (z * m22) + m32) / tw
}
}
}
function compareZ (a, b) {
return b.zIndex - a.zIndex
}
function createFaceUpdater (container, polygons, transformed) {
const toDraw = []
return (rect) => {
let i
const w = rect.width
const h = rect.height
toDraw.length = 0
for (i = 0; i < polygons.length; ++i) {
const poly = polygons[i]
const { indices } = poly
const i0 = indices[0]
const i1 = indices[1]
const i2 = indices[2]
const ax = transformed[3 * i0]
const ay = transformed[(3 * i0) + 1]
const bx = transformed[3 * i1]
const by = transformed[(3 * i1) + 1]
const cx = transformed[3 * i2]
const cy = transformed[(3 * i2) + 1]
const det = ((bx - ax) * (cy - ay)) - ((by - ay) * (cx - ax))
if (det < 0) {
continue
}
const points = []
let zmax = -Infinity
let zmin = Infinity
const element = poly.svg
for (let j = 0; j < 3; ++j) {
const idx = indices[j]
points.push(
`${0.5 * w * (1.0 - transformed[3 * idx])},${
0.5 * h * (1.0 - transformed[(3 * idx) + 1])}`,
)
const z = transformed[(3 * idx) + 2]
zmax = Math.max(zmax, z)
zmin = Math.min(zmin, z)
}
poly.zIndex = zmax + (0.25 * zmin)
const joinedPoints = points.join(' ')
if (joinedPoints.indexOf('NaN') === -1) {
setAttribute(element, 'points', joinedPoints)
}
toDraw.push(poly)
}
toDraw.sort(compareZ)
container.innerHTML = ''
for (i = 0; i < toDraw.length; ++i) {
container.appendChild(toDraw[i].svg)
}
}
}
function calculateSizingOptions (options = {}) {
let width = options.width || 400
let height = options.height || 400
if (!options.pxNotRatio) {
width = (window.innerWidth * (options.width || 0.25)) | 0
height = ((window.innerHeight * options.height) || width) | 0
if ('minWidth' in options && width < options.minWidth) {
width = options.minWidth
height = (options.minWidth * options.height / options.width) | 0
}
}
return { width, height }
}
function createNode (type) {
return document.createElementNS(SVG_NS, type)
}
function setAttribute (node, attribute, value) {
node.setAttributeNS(null, attribute, value)
}
function svgElementToSvgImageContent (svgElement) {
const inner = svgElement.innerHTML
const head = `<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"> ` +
`<svg width="521px" height="521px" version="1.1" baseProfile="full" xmlns="${SVG_NS}" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:ev="http://www.w3.org/2001/xml-events">`
const foot = '</svg>'
const content = head + inner + foot
return content
}
function Polygon (svg, indices) {
this.svg = svg
this.indices = indices
this.zIndex = 0
}