-
Notifications
You must be signed in to change notification settings - Fork 0
/
graphics.coffee
174 lines (144 loc) · 4.98 KB
/
graphics.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
Math.TAU = 2 * Math.PI
class Graphics
constructor: (game) ->
@game = game
@nastyCamera = new THREE.Camera()
@frame = 0
@cells = []
initialize: () ->
@setup()
@loadScene()
start: () ->
@initialize()
@camera.updateSelectionPosition()
@loop()
loop: () ->
@updateGraphics()
TWEEN.update()
@renderer.render(@scene, @camera)
@frame += 1
requestAnimationFrame(() => @loop())
focus: {near : 0.1, far : 10000}
viewAngle: 45
speed: 1/10
updateGraphics: ->
setup: () ->
# get the DOM element to attach to
# - assume we've got jQuery to hand
@container = $('#container')
@viewport = {}
@viewport.width = @container.width()
@viewport.height = @container.height()
# create a WebGL renderer, camera
# and a scene
@renderer = new THREE.WebGLRenderer(antialias: true)
@camera =
new Camera(@game,
@viewAngle,
@viewport.width / @viewport.height,
@focus.near, @focus.far)
@renderer.setSize(@viewport.width, @viewport.height)
# attach the render-supplied DOM element
@container.append(@renderer.domElement)
addToScene: (cell) ->
cell.mesh.position = @calculatePosition(cell.row, cell.column, Cell.Distance)
cell.mesh.rotation = @calculateRotation(cell.mesh.position)
@scene.add(cell.mesh)
@cells.push cell
calculatePosition: (r,c, distance) ->
newPosition = new THREE.Vector3()
newPosition.x = 0
newPosition.y = 0
newPosition.z = 1
@rotY(newPosition, (r - (World.HEIGHT - 1) / 2) * World.ANGLE)
@rotX(newPosition, c * World.ANGLE)
newPosition.normalize().multiplyScalar(distance)
newPosition
calculateRotation: (position) ->
# HOLY SHIT UGLY HACK, but it works
@nastyCamera.position = position.clone()
@nastyCamera.lookAt(World.CENTER)
@nastyCamera.rotation.clone()
rotY: (op,val) ->
y = op.y*Math.cos(val) - op.z*Math.sin(val)
z = op.y*Math.sin(val) + op.z*Math.cos(val)
op.y = y
op.z = z
rotX: (op,val) ->
x = op.x*Math.cos(val) + op.z*Math.sin(val)
z = op.z*Math.cos(val) - op.x*Math.sin(val)
op.x = x
op.z = z
render: () ->
@renderer.render(@scene, @camera)
loadScene: () ->
scene = new THREE.Scene()
# add the camera to the scene
scene.add(@camera)
sunMaterial = new THREE.MeshLambertMaterial(color: 0xDDDD33)
sun = new THREE.Mesh(
new THREE.SphereGeometry(
World.RADIUS*2,
100,
100),
sunMaterial);
sun.position.x = 10
sun.position.y = 50
sun.position.z = 2000
texture = THREE.ImageUtils.loadTexture('assets/moon.png')
moonMaterial = new THREE.MeshLambertMaterial
map: texture,
reflectivity: 0,
specular: 0
moon = new THREE.Mesh(
new THREE.SphereGeometry(
World.RADIUS,
100,
100),
moonMaterial);
moon.position.x = 200
moon.position.y = 100
moon.position.z = -400
# create a point light
moonLight = new THREE.PointLight(0xFFFFFF,0.4);
# set its position
moonLight.position.x = 150
moonLight.position.y = 50
moonLight.position.z = -350
# create a point light
spotLight = new THREE.SpotLight(0xFFC0B0, 3, 300, Math.TAU / 8);
# set its position
spotLight.position.x = 0
spotLight.position.y = 50
spotLight.position.z = 1700
spotLight.lookAt(sun.position)
pointLight = new THREE.PointLight(0xFFFFFF,0.8)
faintLight = new THREE.PointLight(0xFFFFFF,0.2)
faintLight.position.x = -200
pointLight.position = spotLight.position.clone()
# add to the scene
scene.add(moon)
scene.add(moonLight)
scene.add(spotLight)
scene.add(pointLight)
scene.add(faintLight)
startex = THREE.ImageUtils.loadTexture('assets/stars.png')
startex.wrapS = THREE.RepeatWrapping;
startex.wrapT = THREE.RepeatWrapping;
startex.repeat.x = 10;
startex.repeat.y = 10;
starMaterial = new THREE.MeshBasicMaterial(map: startex)
stars = new THREE.Mesh(new THREE.SphereGeometry(2300,100,100), starMaterial)
stars.flipSided = true
scene.add(stars)
scene.add(sun)
@scene = scene
@scene.add(@game.world.mesh)
@loadCells()
loadCells: () ->
for c in @cells
@scene.remove c.mesh
@cells = []
for row,r in @game.world.world
for cell,c in row
@addToScene(cell)