-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.elm
247 lines (221 loc) · 7.63 KB
/
Main.elm
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
import Html exposing (Html)
import Html.Lazy exposing (lazy)
import Html.Attributes exposing (width, height, style)
import Html.Events exposing (onWithOptions)
import Json.Decode
import Math.Matrix4 as Mat4 exposing (Mat4)
import Math.Vector3 as Vec3 exposing (vec3, Vec3)
import Math.Vector4 as Vec4 exposing (vec4, Vec4)
import WebGL exposing (Mesh, Shader)
import Keyboard
import Matrices exposing (..)
import Symmetry
import Time
import AnimationFrame
import Set exposing (Set)
import Dict exposing (Dict)
type alias Model = {pos : Mat4, keys : Set Int, marks : List Vec4, marksMesh : Mesh Vertex, ghostMode : Bool}
type Msg =
KeyDown Int
| KeyUp Int
| Tick Float
| GhostMode
main : Program Never Model Msg
main =
Html.program
{ init =
( { pos = Mat4.identity
, keys = Set.empty
, marks = []
, marksMesh = marksMesh []
, ghostMode = False
}
, Cmd.none
)
, view = lazy view
, subscriptions =
(\_ ->
Sub.batch
[ Keyboard.downs KeyDown
, Keyboard.ups KeyUp
, AnimationFrame.diffs (Tick << Time.inSeconds)])
, update = update
}
update : Msg -> Model -> (Model, Cmd Msg)
update msg model =
case msg of
KeyDown 32 -> -- Space
( updateMarksMesh
{ model
| marks =
transform4 model.pos (vec4 0 0 0 1) :: model.marks}
, Cmd.none
)
KeyDown k -> ({model | keys = Set.insert k model.keys}, Cmd.none)
KeyUp k -> ({model | keys = Set.remove k model.keys}, Cmd.none)
Tick dt ->
if not <| List.any (flip Set.member model.keys) (Dict.keys keys)
then (model, Cmd.none)
else
( { model
| pos =
Set.foldr
(\k pos ->
case Dict.get k keys of
Nothing -> pos
Just f -> Mat4.mul (f dt) pos)
model.pos
model.keys
}
, Cmd.none
)
GhostMode ->
( {model
| ghostMode = not model.ghostMode
}
, Cmd.none
)
keys : Dict Int (Float -> Mat4)
keys = Dict.fromList
[ (73, \dt -> turnUp ( 0.5 * dt)) -- I
, (74, \dt -> turnRight (-0.5 * dt)) -- J
, (75, \dt -> turnUp (-0.5 * dt)) -- K
, (76, \dt -> turnRight ( 0.5 * dt)) -- L
, (79, \dt -> spin ( 0.5 * dt)) -- O
, (85, \dt -> spin (-0.5 * dt)) -- U
, (87, \dt -> moveForward ( 0.5 * dt)) -- W
, (83, \dt -> moveForward (-0.5 * dt)) -- S
, (65, \dt -> moveRight (-0.5 * dt)) -- A
, (68, \dt -> moveRight ( 0.5 * dt)) -- D
, (81, \dt -> moveUp ( 0.5 * dt)) -- Q
, (69, \dt -> moveUp (-0.5 * dt)) -- E
]
updateMarksMesh : Model -> Model
updateMarksMesh model = {model | marksMesh = marksMesh model.marks}
onClick : msg -> Html.Attribute msg
onClick msg =
onWithOptions "mousedown"
{ preventDefault = True, stopPropagation = True }
(Json.Decode.succeed msg)
view : Model -> Html Msg
view model =
Html.div []
[ WebGL.toHtml
[ width 1200
, height 800
, style [ ( "display", "block" ), ("border", "10px solid black"), ("background-color", "black") ]
]
[ WebGL.entity
vertexShader
fragmentShader
mesh2
(uniforms model)
, WebGL.entity
vertexShader
fragmentShader
(if model.ghostMode then WebGL.triangles [] else mesh)
(uniforms model)
, WebGL.entity
vertexShader
fragmentShader
model.marksMesh
(uniforms model)
]
, Html.text "Ghost Mode"
, Html.input
[ Html.Attributes.type_ "checkbox"
, onClick GhostMode
, Html.Attributes.value (toString model.ghostMode)
]
[]
]
uniforms : Model -> Uniforms
uniforms model =
{ rotation = model.pos
, perspective = Mat4.makePerspective 80 (3/2) 0.01 10
, shade = 0.8
}
type alias Uniforms =
{ rotation : Mat4
, perspective : Mat4
, shade : Float
}
type alias Vertex =
{ color : Vec3
, position : Vec4 -- Should have x^2+y^2+z^2+w^2 == 1
}
mkVertex : Vec3 -> Vec4 -> Vertex
mkVertex color position = Vertex color (Vec4.normalize position)
mesh =
WebGL.triangles
(List.concatMap
(\m -> List.map (\(a,b,c) -> (mkVertex (vec3 1 1 1) a, mkVertex (vec3 1 1 1) b, mkVertex (vec3 1 1 1) c))
-- Messed around with the numbers until it looked good.
[ ( transform4 m (vec4 0.0000 0.2618 -0.1 1)
, transform4 m (vec4 0.1618 0.1618 -0.1618 1)
, transform4 m (vec4 0.1618 (0.1618 + 0.100) (-0.1618 - 0.162) (0.94)))
]
)
(Symmetry.h4))
mesh2 =
WebGL.lines
(List.concatMap
(\m -> List.map (\(a,b) -> (mkVertex (vec3 0.5 0.5 0.5) a, mkVertex (vec3 0.5 0.5 0.5) b))
[ ( transform4 m (vec4 0.0000 0.2618 -0.1 1)
, transform4 m (vec4 0.0000 0.2618 0.1 1))
, ( transform4 m (vec4 0.1618 0.1618 -0.1618 1)
, transform4 m (vec4 0.1618 (0.1618 + 0.100) (-0.1618 - 0.162) (0.94)))
]
)
(Symmetry.h4))
marksMesh : List Vec4 -> Mesh Vertex
marksMesh marks =
WebGL.triangles
(List.concatMap
(\v ->
let (x,y,z,w) = Vec4.toTuple v
v2 = Vec4.fromTuple (y,-x,w,-z) |> Vec4.scale 0.01
v3 = Vec4.fromTuple (-z,w,x,-y) |> Vec4.scale 0.01
v4 = Vec4.fromTuple (-w,-z,y,x) |> Vec4.scale 0.01
red = vec3 1 0 0
in
[ ( mkVertex red (Vec4.add v v2)
, mkVertex red (Vec4.add v v3)
, mkVertex red (Vec4.add v v4))
, ( mkVertex red v
, mkVertex red (Vec4.add v v3)
, mkVertex red (Vec4.add v v4))
, ( mkVertex red (Vec4.add v v2)
, mkVertex red v
, mkVertex red (Vec4.add v v4))
, ( mkVertex red (Vec4.add v v2)
, mkVertex red (Vec4.add v v3)
, mkVertex red v)
])
marks)
vertexShader : Shader Vertex Uniforms { vcolor : Vec3 }
vertexShader =
[glsl|
attribute vec4 position;
attribute vec3 color;
uniform mat4 perspective;
uniform mat4 rotation;
varying vec3 vcolor;
void main () {
vec4 pos = rotation * position;
gl_Position = perspective * (pos + vec4(0.0,0.0,0.0,1.0));
vec4 relpos = pos - vec4(0.0,0.0,0.0,1.0);
float dist2 = relpos.x * relpos.x + relpos.y * relpos.y + relpos.z * relpos.z + relpos.w * relpos.w;
vcolor = (1.0 - (dist2 / 4.0)) * color;
}
|]
fragmentShader : Shader {} Uniforms { vcolor : Vec3 }
fragmentShader =
[glsl|
precision mediump float;
uniform float shade;
varying vec3 vcolor;
void main () {
gl_FragColor = clamp(shade * vec4(vcolor, 1), 0.1, 0.9);
}
|]