-
Notifications
You must be signed in to change notification settings - Fork 0
/
worms.js
225 lines (190 loc) · 8.19 KB
/
worms.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
import * as React from 'react'
import TWEEN from 'tween.js'
import Motor from 'infamous/motor/Motor'
window.Motor = Motor
import 'infamous/motor-html'
import geometry from 'csg'
import RippleFlip from './rippleFlip'
function getUserAudio() {
let resolve, reject
const promise = new Promise((res, rej) => {resolve = res; reject = rej})
navigator.getUserMedia (
{ audio: true },
stream => resolve(stream),
err => reject(err),
)
return promise
}
export default
class App extends React.Component {
constructor(props) {
super(props)
this.sheet = null
//const shape = geometry.sphere({ radius: 130 })
const shape = geometry.cube({ radius: 70 })
const spherePolys = shape.toPolygons()
//const positions = spherePolys.map(poly => poly.vertices[Math.round(poly.vertices.length/2)].pos)
this.positions = spherePolys.reduce((output, poly) => output.concat(poly.vertices.map(v => v.pos)), [])
}
render() {
let items = [0,1,2]
this.dancers = []
function createWorm({numberOfSections, directions, offset, color, mesh}) {
numberOfSections = numberOfSections || 5
directions = directions || {x:1, y:1, z:1}
offset = offset || 5
color = color || '0.5, 0.5, 0.5'
mesh = mesh || 'cube'
const sections = []
const collectSectionsThenAnimate = el => {
if (!el) return
sections.push(el)
if (sections.length === numberOfSections) {
const angle = {w:0}
let done = false
const tween = new TWEEN.Tween(angle)
.to({w:360}, 8000)
.easing(TWEEN.Easing.Exponential.InOut)
.onComplete(() => done = true)
.repeat(Infinity)
.start()
Motor.addRenderTask(time => {
tween.update(time)
if (done) return false
for (const section of sections) {
section.rotation.x = angle.w
section.rotation.y = angle.w
section.rotation.z = angle.w
}
})
}
}
function _createWorm(section) {
const {x,y,z} = directions
return (
<motor-node
ref={collectSectionsThenAnimate}
color={color}
mesh={mesh}
position={`${offset*x} ${offset*y} ${offset*z}`}
>
{section+1 < numberOfSections ?
_createWorm(section+1)
:[]}
</motor-node>
)
}
return _createWorm(0)
}
return (
<div style={{width:'100%', height:'100%'}}>
<div className="rippleFlip" style={{
position: 'absolute',
top: '0',
left: '0',
width:'100%',
height:'100%',
zIndex:'0'
}}>
{/*
<RippleFlip />
*/}
</div>
<div className="rotation" style={{
position: 'absolute',
top: '0',
left: '0',
width:'100%',
height:'100%',
zIndex:'1'
}}>
<motor-scene
ref="scene"
webglenabled="true"
background="0.4 0.3 0.5 0.2"
>
<motor-node
ref='outerRoot'
id='outerRoot'
absoluteSize='100, 100'
>
<motor-node
ref='innerRoot'
id='innerRoot'
absoluteSize='100, 100'
>
{items.map(i => (
<motor-node
ref={el => this.dancers.push(el)}
class='dancer'
id={`dancer${i}`}
absoluteSize='100, 100'
position={`${i*300} 0 0`}
color="0.1 0.9 0.5"
mesh='cube'
>
{/*//////////////////////////////////////////////////////////////////////////////////*/}
{createWorm({directions: {x:1, y:1, z:1 }, color: '0.2, 0.2, 0.9'})}
{createWorm({directions: {x:-1, y:1, z:1 }, color: '0.2, 0.2, 0.9'})}
{createWorm({directions: {x:1, y:-1, z:1 }, color: '0.2, 0.2, 0.9'})}
{createWorm({directions: {x:-1, y:-1, z:1 }, color: '0.2, 0.2, 0.9'})}
{createWorm({directions: {x:1, y:1, z:-1}, color: '0.2, 0.2, 0.9'})}
{createWorm({directions: {x:-1, y:1, z:-1}, color: '0.2, 0.2, 0.9'})}
{createWorm({directions: {x:1, y:-1, z:-1}, color: '0.2, 0.2, 0.9'})}
{createWorm({directions: {x:-1, y:-1, z:-1}, color: '0.2, 0.2, 0.9'})}
{this.positions.map((pos, index) => (
<motor-node
class='cube'
id={`cube${index}`}
key={index}
absoluteSize='10, 10'
position={`${pos.x} ${pos.y} ${pos.z}`}
//color="0.2 0.4 0.9"
mesh='cube'
>
</motor-node>
))}
</motor-node>
))}
</motor-node>
</motor-node>
</motor-scene>
</div>
</div>
)
}
async componentDidMount() {
let deviceOrientation = { x: 0, y: 0, z: 0, }
const broadcast = new Meteor.Broadcast('orientation')
broadcast.on('data', data => {
console.log('data')
deviceOrientation = data
})
broadcast.on('error', e => {throw e})
broadcast.on('ready', () => console.log('broadcast client ready'))
const {dancers} = this
const {innerRoot} = this.refs
await dancers[0].mountPromise
//dancers.rotation.y = -90
Motor.addRenderTask(time => {
let index = 0
for (const dancer of dancers) {
dancer.position.x = 100 * Math.sin(time*0.001) + 300*index++
//dancer.rotation.x += 5
//dancer.rotation.y += 10
//dancer.rotation.z += 5
//dancer.rotation.x += 3
//dancer.rotation.y += 6
//dancer.rotation.z += 3
dancer.rotation.x += 1
dancer.rotation.y += 2
dancer.rotation.z += 1
const backAndForth = deviceOrientation.x
dancer.position.z = 100 * Math.sin(time * 0.005 + index*0.8) - backAndForth
//dancer.position.z = backAndForth
}
let offset = (300*(index-1)) / 2
innerRoot.position.x = -offset
})
}
}