-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.coffee
309 lines (242 loc) · 6.69 KB
/
main.coffee
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
TAU = Math.PI + Math.PI # or C/r
T = THREE
P = Physijs
V3 = T.Vector3
randy = (x)-> Math.random()*x-x/2
rand = (x)-> Math.random()*x
###################################
# SETUP
###################################
# relative to this file
P.scripts.worker = './lib/physijs_worker.js'
# relative to the above worker file
P.scripts.ammo = './ammo.js'
# SCENE
scene = new P.Scene(fixedTimeStep: 1/30)
scene.setGravity(new V3(0, -300, 0))
# CAMERA
WIDTH = window.innerWidth
HEIGHT = window.innerHeight
ASPECT = WIDTH / HEIGHT
FOV = 45
NEAR = 0.1
FAR = 20000
camera = new T.PerspectiveCamera(FOV, ASPECT, NEAR, FAR)
scene.add(camera)
camera.position.set(150, 550, 400)
camera.lookAt(scene.position)
# RENDERER
renderer =
if Detector.webgl
new T.WebGLRenderer(antialias: yes)
else
new T.CanvasRenderer()
renderer.setSize(WIDTH, HEIGHT)
document.body.appendChild(renderer.domElement)
window.onresize = ->
WIDTH = window.innerWidth
HEIGHT = window.innerHeight
ASPECT = WIDTH / HEIGHT
renderer.setSize(WIDTH, HEIGHT)
camera.aspect = ASPECT
camera.updateProjectionMatrix()
# CONTROLS
controls = new T.OrbitControls(camera, renderer.domElement)
# LIGHTING
light = new T.PointLight(0xffffff, 1, 10000)
light.position.set(0, 100, 0)
scene.add(light)
###
directionalLight = new T.DirectionalLight(0xffffff, 0.5)
directionalLight.position.set(0, 1, 0)
scene.add(directionalLight)
skyLight = new T.HemisphereLight(0xffffff, 0x0000ff, 0.5)
scene.add(skyLight)
###
# SKYBOX/FOG
materialArray =
for dir in ["xpos", "xneg", "ypos", "yneg", "zpos", "zneg"]
new T.MeshBasicMaterial
map: T.ImageUtils.loadTexture "skybox/#{dir}.png"
side: T.BackSide
skyBoxGeometry = new T.BoxGeometry(10000, 10000, 10000)
skyBoxMaterial = new T.MeshFaceMaterial(materialArray)
skyBox = new T.Mesh(skyBoxGeometry, skyBoxMaterial)
scene.add skyBox
###################################
# POOL TABLE
###################################
ptw = 1000
ptl = 2 * ptw
# SURFACE
ground_canvas = document.createElement("canvas")
ground_canvas.width = gcw =
ground_canvas.height = gch = 512
ground_ctx = ground_canvas.getContext("2d")
id = ground_ctx.getImageData(0, 0, gcw, gch)
i = 0
while i < id.data.length
id.data[i+0] =
id.data[i+1] =
id.data[i+2] =
255 - rand(25)
id.data[i+3] = 255
i += 4
ground_ctx.putImageData(id, 0, 0, 0, 0, gcw, gch)
ground_tex = new T.Texture(ground_canvas)
ground_tex.wrapS = ground_tex.wrapT = T.RepeatWrapping
ground_tex.repeat.set(4, 2)
ground_tex.needsUpdate = true
ground_material = P.createMaterial(
new T.MeshBasicMaterial(color: 0x3C8546, map: ground_tex)
0.8 # high friction
0.3 # low restitution
)
ground = new P.BoxMesh(
new T.BoxGeometry(ptl, 50, ptw)
ground_material
0 # mass, 0 = static
)
ground.position.set(0, -25, 0)
ground.receiveShadow = true
scene.add(ground)
# BUMPERS
bumper_material = P.createMaterial(
new T.MeshBasicMaterial(color: 0x002E00, map: ground_tex)
0.8 # high friction
0.3 # low restitution
)
addBumper = (lengthwise, widthwise, r)->
bumper = new P.BoxMesh(
new T.BoxGeometry(ptw-120, 50, 50, 5, 5, 5)
bumper_material
0 # mass, 0 = static
)
bumper.position.set(lengthwise * ptw, 5, widthwise * ptw)
bumper.rotation.set(0, r, 0)
bumper.receiveShadow = true
scene.add(bumper)
foo = 0.48
addBumper(foo, 0.5, 0)
addBumper(foo, -0.5, TAU/2)
addBumper(-foo, 0.5, 0)
addBumper(-foo, -0.5, TAU/2)
addBumper(1, 0, TAU/4)
addBumper(-1, 0, -TAU/4)
###################################
# BALLS
###################################
ball_hit_ball = new Howl
urls: ['sound/ball-hit-ball.ogg']
volume: 0.5
cue_hit_ball = new Howl
urls: ['sound/cue-hit-ball.ogg']
volume: 1
ball_hit_fuzz = new Howl
urls: ['sound/ball-hit-fuzz.ogg']
volume: 1
balls = for i in [0..15]
canvas = document.createElement('canvas')
bcw = bch = 1024
canvas.width = bcw
canvas.height = bch
ctx = canvas.getContext('2d')
white = '#FEFFEA'
ctx.fillStyle = white
ctx.fillRect(0, 0, bcw, bch)
colors = [
'(cue ball)'
'#FCCF04','#1544AD','#E81D13','#7C2E7C','#FF6901','#00680F','#8A0A11','#0B0806'
'#FCCF04','#1544AD','#E81D13','#7C2E7C','#FF6901','#00680F','#8A0A11','#0B0806'
]
ctx.fillStyle = colors[i]
if i > 8
a = 0.3
ctx.fillRect(0, bch*a, bcw, bch*(1-a*2))
else
ctx.fillRect(0, 0, bcw, bch)
if i > 0
ctx.translate(bcw/2, bch/2)
ctx.scale(0.5, 0.9)
ctx.beginPath()
ctx.arc(0, 0, bch/7, 0, TAU)
ctx.fillStyle = white
ctx.fill()
ctx.textBaseline = 'middle'
ctx.textAlign = 'center'
ctx.font = (bch/4)+'px Arial'
ctx.fillStyle = 'black'
ctx.fillText(i, 0, -bch*0.0)
else
# little red dot maybe?
map = new T.Texture(canvas)
map.needsUpdate = true
ball = new P.SphereMesh(
new T.SphereGeometry(25, 25, 25)
new T.MeshPhongMaterial
color: 0xffffff
shininess: 100
emissive: 0xaaaaaa
specular: 0x222222
map: map
)
ball.position.set(randy(500), randy(500)+500, randy(500))
ball.rotation.x = rand(TAU)
ball.rotation.y = rand(TAU)
ball.rotation.z = rand(TAU)
scene.add(ball)
ball.addEventListener 'collision', (collided_with, linearVelocity, angularVelocity)->
vel = linearVelocity.length()
vol = Math.pow(vel, 1.4) / 150
if collided_with instanceof P.SphereMesh
ball_hit_ball.volume(vel/650)
ball_hit_ball.play()
else if collided_with.material is bumper_material
ball_hit_fuzz.volume(vel/65)
ball_hit_fuzz.play()
else if collided_with.material is ground_material
ball_hit_fuzz.volume(vel/650)
ball_hit_fuzz.play()
ball
###################################
# INTERACTION
###################################
unprojector = new T.Projector()
mouse = {x: 0, y: 0}
document.body.onmousemove = (e)->
e.preventDefault()
mouse.x = (e.offsetX / WIDTH) * 2 - 1
mouse.y = (e.offsetY / HEIGHT) * -2 + 1
vector = new V3(mouse.x, mouse.y, 1)
unprojector.unprojectVector(vector, camera)
ray = new T.Raycaster(camera.position, vector.sub(camera.position).normalize())
intersects = ray.intersectObjects(balls)
if mouse.intersect
mat = mouse.intersect.object.material
mat.emissive.setHex(mouse.oeh)
mat.needsUpdate = true
document.body.style.cursor = "default"
mouse.intersect = intersect = intersects[0]
if mouse.intersect
mat = mouse.intersect.object.material
mouse.oeh = mat.emissive.getHex()
mat.emissive.setHex(0xffffff)
mat.needsUpdate = true
document.body.style.cursor = "pointer"
document.body.onmousedown = (e)->
if mouse.intersect
e.preventDefault()
e.stopPropagation()
cue_hit_ball.play()
ball = mouse.intersect.object
force = mouse.intersect.point.sub(ball.position)
force.multiplyScalar(-30)
ball.setLinearVelocity(force)
#=========#
# ...GO! #
#=========#
do animate = ->
requestAnimationFrame(animate)
scene.simulate(undefined, 1)
renderer.render(scene, camera)
controls.update()