forked from adolfintel/warpspeed
-
Notifications
You must be signed in to change notification settings - Fork 0
/
component.js
135 lines (124 loc) · 2.8 KB
/
component.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
import WarpSpeed from "./warpspeed"
import WarpWorker from "web-worker:./worker"
AFRAME.registerComponent("warpspeed", {
schema: {
width: {
type: "int",
default: 512,
},
height: {
type: "int",
default: 512,
},
speed: {
type: "number",
default: 0.7,
},
density: {
type: "number",
// default: 0.7, // min: >0
default: 25.7, // min: >0
},
useCircles: {
type: "boolean",
default: true,
},
depthAlpha: {
type: "boolean",
default: true,
},
warpEffect: {
type: "boolean",
default: true,
},
warpEffectLength: {
type: "number",
default: 5, // min: 0
},
starScale: {
type: "number",
default: 3, // min: >0
},
backgroundColor: {
type: "color",
default: "#100a1a",
},
starColor: {
type: "color",
default: "#ffffff",
},
useWorker: {
type: "boolean",
default: false,
},
},
update(oldData) {
const reloadTexture =
oldData.useWorker !== this.data.useWorker ||
oldData.width !== this.data.width ||
oldData.height !== this.data.height
if (reloadTexture) {
if (oldData.useWorker !== undefined) {
this.destroyCanvasMap()
}
this.createCanvasMap()
this.initWarpspeed()
}
if (this.workerInUse()) {
this.worker.postMessage({ config: this.data, pause: !this.isPlaying })
} else {
this.warpspeed.update(this.data)
}
},
initWarpspeed() {
this.el.removeState("worker")
if (this.data.useWorker) {
try {
const offscreen = this.canvas.transferControlToOffscreen()
this.worker = new WarpWorker()
this.worker.postMessage({ canvas: offscreen }, [offscreen])
this.el.addState("worker")
return
} catch (e) {
console.error(e)
}
}
this.warpspeed = new WarpSpeed(this.canvas)
},
createCanvasMap() {
this.canvas = document.createElement("canvas")
this.canvas.width = this.data.width
this.canvas.height = this.data.height
this.canvasMap = new THREE.Texture(this.canvas)
this.el.getObject3D("mesh").material.map = this.canvasMap
},
destroyCanvasMap() {
if (this.workerInUse()) {
this.worker.terminate()
delete this.worker
} else {
delete this.warpspeed
}
this.canvasMap.dispose()
delete this.canvas
},
play() {
if (this.workerInUse()) {
this.worker.postMessage({ pause: false })
}
},
pause() {
if (this.workerInUse()) {
this.worker.postMessage({ pause: true })
}
},
workerInUse() {
return this.el.is("worker")
},
tick(_time, timeDelta) {
if (!this.workerInUse()) {
this.warpspeed.draw(timeDelta)
}
this.canvasMap.needsUpdate = true
},
})