-
Notifications
You must be signed in to change notification settings - Fork 0
/
Fish.js
244 lines (206 loc) · 5.87 KB
/
Fish.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
var helpers = {
// Retun random integer between two numbers
randomInt: (min, max) => Math.floor(Math.random() * (max - min)) + min,
// Maybe do something
// probability: number between 0 and 1
maybe: (probability, callback) => {
if (Math.random() < probability) {
callback()
}
}
}
class Fish {
constructor() {
this.speed = 2
this.isWiggling = false
this.wigglesToDo = 0
this.xRotation = { x: 0 }
this.INTERVAL = 1000
this.INERTIA = 0.012
this.WIGGLE_DURATION = 1000
this.RATE = 16
this.MAX_SPEED = 5
this.MIN_SPEED = 1
this.PUSH = 0.02
this.WIGGLE_PROBABILITY = 0.3
this.MAX_WIGGLE_ROTATAION = 0.12
}
getFish() {
var geometry, material
// Head
geometry = new THREE.DodecahedronGeometry(19.5, 2)
material = new THREE.MeshLambertMaterial({ color: 0xffb200 })
var fishHead = new THREE.Mesh(geometry, material)
fishHead.scale.z = .5
fishHead.scale.y = .8
fishHead.scale.x = .8
fishHead.position.x = -2
// Eyes
geometry = new THREE.RingGeometry(1, 3, 10)
material = new THREE.MeshBasicMaterial({ color: 0xffffff })
var fishEyeRight = new THREE.Mesh(geometry, material)
fishEyeRight.position.set(0, 5, 7.2)
fishEyeRight.rotation.y = 0.39
fishEyeRight.rotation.x = -.3
var fishEyeLeft = new THREE.Mesh(geometry, material)
fishEyeLeft.position.set(0, 5, -7.3)
fishEyeLeft.rotation.y = Math.PI + -.38
fishEyeLeft.rotation.x = .3
// Body
var points = []
var WIDTH = 21
var CURVE_FASTNESS = 0.9
var POS_FROM_HEAD = 10
var STRETCH = 15
for (var i = 0; i < 3; i++) {
points.push(
new THREE.Vector2(
Math.sin(i * CURVE_FASTNESS) * WIDTH,
i * STRETCH - POS_FROM_HEAD
)
)
}
WIDTH = 21
CURVE_FASTNESS = .80
POS_FROM_HEAD = 1
STRETCH = 11
for (var i = 3; i < 4; i++) {
points.push(
new THREE.Vector2(
Math.sin(i * CURVE_FASTNESS) * WIDTH,
i * STRETCH - POS_FROM_HEAD
)
)
}
WIDTH = 21
CURVE_FASTNESS = .785
POS_FROM_HEAD = 5
STRETCH = 11
for (var i = 4; i < 5; i++) {
points.push(
new THREE.Vector2(
Math.sin(i * CURVE_FASTNESS) * WIDTH,
i * STRETCH - POS_FROM_HEAD
)
)
}
geometry = new THREE.LatheGeometry(points, 50)
material = new THREE.MeshLambertMaterial({ color: 0xffb200 })
var fishBody = new THREE.Mesh(geometry, material)
fishBody.scale.x = .5
fishBody.scale.z = .7
fishBody.scale.y = .7
fishBody.position.set(-20, 0, 0)
fishBody.rotation.x = 11
fishBody.rotation.z = 11
// Tail (top)
points = []
WIDTH = 8
CURVE_FASTNESS = 1.04
POS_FROM_HEAD = 10
STRETCH = 7.5
for (var i = 0; i < 4; i++) {
points.push(
new THREE.Vector2(
Math.sin(i * CURVE_FASTNESS) * WIDTH,
i * STRETCH - POS_FROM_HEAD
)
)
}
geometry = new THREE.LatheGeometry(points)
material = new THREE.MeshLambertMaterial({ color: 0xffb200 })
var fishTail = new THREE.Mesh(geometry, material)
fishTail.rotation.z = 0.4
fishTail.rotation.y = 0
fishTail.position.set(-28, 5, 0)
fishTail.scale.z = .1
fishTail.scale.x = .9
fishTail.scale.y = .7
// Tail (bottom)
var fishTailBottom = new THREE.Mesh(geometry, material)
fishTailBottom.rotation.z = Math.PI - 0.4
fishTailBottom.position.set(-28, -6, 0)
fishTailBottom.scale.z = .1
fishTailBottom.scale.x = .7
fishTailBottom.scale.y = 1
var fish = new THREE.Group()
// fish.add(fishHead)
fish.add(fishEyeRight)
fish.add(fishEyeLeft)
fish.add(fishBody)
fish.add(fishTail)
fish.add(fishTailBottom)
fish.castShadow = true
return fish
}
swim() {
const { INTERVAL, MAX_WIGGLE_ROTATAION, WIGGLE_PROBABILITY, PUSH, MIN_SPEED, MAX_SPEED, RATE, WIGGLE_DURATION, INERTIA } = this
const unsetIsWiggling = () => {
let interval = setInterval(() => {
--this.wigglesToDo
if (this.wigglesToDo == 0) {
this.isWiggling = false
clearInterval(interval)
}
}, WIGGLE_DURATION)
}
const setWiggle = () => {
this.wigglesToDo = helpers.randomInt(1, 6)
this.isWiggling = true
makeTween(MAX_WIGGLE_ROTATAION)
unsetIsWiggling()
}
// Every INTERVAL, if !isWiggling, maybe wiggle some
const maybeWiggle = setInterval(() => {
if (!this.isWiggling) {
helpers.maybe(WIGGLE_PROBABILITY, setWiggle)
}
}, INTERVAL)
const makeTween = (to) => new TWEEN.Tween(this.xRotation)
.to({ x: to }, WIGGLE_DURATION / 4)
.easing(TWEEN.Easing.Cubic.Out)
.onComplete(() => {
makeTweenBack(to)
})
.start()
const makeTweenBack = (to) => new TWEEN.Tween(this.xRotation)
.to({ x: 0 }, WIGGLE_DURATION / 4)
.easing(TWEEN.Easing.Quadratic.In)
.onComplete(() => {
if (this.isWiggling) {
makeTween(-to)
}
})
.start()
}
swimPath(arr) {
var spline = new THREE.SplineCurve3(arr)
var material = new THREE.LineBasicMaterial({
color: 0xff00f0
})
var geometry = new THREE.Geometry()
for (var i = 0; i < spline.getPoints(100).length; i++) {
geometry.vertices.push(spline.getPoints(100)[i])
}
return {
line: new THREE.Line(geometry, material),
spline
}
}
update() {
const { PUSH, MIN_SPEED, MAX_SPEED, INERTIA } = this
// Slow Down
if (this.speed >= INERTIA + MIN_SPEED) {
this.speed -= INERTIA
} else if (this.speed >= INERTIA) {
this.speed = MIN_SPEED
}
// Accelerate
if (this.isWiggling && this.speed + PUSH < MAX_SPEED) {
this.speed += PUSH
}
TWEEN.update()
var { speed, isWiggling, wigglesToDo, xRotation } = this
return { speed, isWiggling, wigglesToDo, xRotation }
}
}