-
-
Notifications
You must be signed in to change notification settings - Fork 141
/
Copy pathMain.hs
214 lines (167 loc) · 6.04 KB
/
Main.hs
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
{-# LANGUAGE CPP #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RecordWildCards #-}
module Main where
import Control.Monad
import Control.Monad.IO.Class (liftIO)
import Data.IORef
import qualified Data.Map as M
import Language.Javascript.JSaddle hiding ((<#))
import Miso
data Action
= GetTime
| Init
| SetTime !Double
withStats :: JSVal -> IO () -> IO ()
withStats stats m = do
statsBegin stats >> m
statsEnd stats
data Context = Context
{ rotateCube :: IO ()
, renderScene :: IO ()
, stats :: JSVal
}
initContext :: IORef Context -> IO ()
initContext ref = do
canvas <- getElementById "canvas"
scene <- newScene
camera <- newCamera
renderer <- newRenderer canvas
setSize renderer
cube <-
join $
newMesh
<$> newBoxGeometry 1 1 1
<*> newMeshBasicMaterial
addToScene scene cube
positionCamera camera 5
stats <- newStats
statsContainer <- getElementById "stats"
addStatsToDOM statsContainer stats
writeIORef
ref
Context
{ stats = stats
, rotateCube = do
rotateX cube 0.1
rotateY cube 0.1
, renderScene =
render renderer scene camera
}
main :: IO ()
main = run $ do
stats <- newStats
ref <- newIORef $ Context (pure ()) (pure ()) stats
m <- now
startApp (defaultApp m (updateModel ref) viewModel)
{ initialAction = Just Init
}
viewModel :: Double -> View action
viewModel _ =
div_
[]
[ div_
[ id_ "stats"
, style_ $ M.singleton "position" "absolute"
]
[]
, canvas_
[ id_ "canvas"
, width_ "400"
, height_ "300"
]
[]
]
updateModel ::
IORef Context ->
Action ->
Effect Double Action ()
updateModel ref Init = do
io (initContext ref)
issue GetTime
updateModel ref GetTime = do
io $ do
Context{..} <- liftIO (readIORef ref)
withStats stats $ do
rotateCube
renderScene
scheduleIO (SetTime <$> now)
updateModel _ (SetTime m) = do
noEff m
issue GetTime
#ifdef GHCJS_NEW
foreign import javascript unsafe "(() => { return new Stats(); })"
newStats :: IO JSVal
foreign import javascript unsafe "((x) => { x.begin(); })"
statsBegin :: JSVal -> IO ()
foreign import javascript unsafe "((x) => { x.end(); })"
statsEnd :: JSVal -> IO ()
foreign import javascript unsafe "((x) => { x.showPanel(0); })"
showPanel :: JSVal -> IO ()
foreign import javascript unsafe "(() => { return new THREE.Scene();})"
newScene :: IO JSVal
foreign import javascript unsafe "((x,y,z) => { return new THREE.BoxGeometry(x,y,z); })"
newBoxGeometry :: Int -> Int -> Int -> IO JSVal
foreign import javascript unsafe "(() => { return new THREE.PerspectiveCamera( 75, window.innerWidth/window.innerHeight, 0.1, 1000 ); })"
newCamera :: IO JSVal
foreign import javascript unsafe "((x,y) => { return new THREE.Mesh( x, y ); })"
newMesh :: JSVal -> JSVal -> IO JSVal
foreign import javascript unsafe "(() => { return new THREE.MeshBasicMaterial( { color: 0x00ff00 } ); })"
newMeshBasicMaterial :: IO JSVal
foreign import javascript unsafe "((x) => { return new THREE.WebGLRenderer({canvas:x, antialias : true}); })"
newRenderer :: JSVal -> IO JSVal
foreign import javascript unsafe "((x) => { x.setSize( window.innerWidth, window.innerHeight ); })"
setSize :: JSVal -> IO ()
foreign import javascript unsafe "((x, y) => { x.add(y); })"
addToScene :: JSVal -> JSVal -> IO ()
foreign import javascript unsafe "((x, y) => { x.position.z = y; })"
cameraZ :: JSVal -> Int -> IO ()
foreign import javascript unsafe "((a, y) => { a.rotation.x += y; })"
rotateX :: JSVal -> Double -> IO ()
foreign import javascript unsafe "((x, a) => { x.rotation.y += a; })"
rotateY :: JSVal -> Double -> IO ()
foreign import javascript unsafe "((x, y, z) => { x.render(y, z); })"
render :: JSVal -> JSVal -> JSVal -> IO ()
foreign import javascript unsafe "((x, y) => { x.position.z = y; })"
positionCamera :: JSVal -> Double -> IO ()
foreign import javascript unsafe "((x, y) => { x.appendChild( y.domElement ); })"
addStatsToDOM :: JSVal -> JSVal -> IO ()
#endif
#ifdef GHCJS_OLD
foreign import javascript unsafe "$r = new Stats();"
newStats :: IO JSVal
foreign import javascript unsafe "$1.begin();"
statsBegin :: JSVal -> IO ()
foreign import javascript unsafe "$1.end();"
statsEnd :: JSVal -> IO ()
foreign import javascript unsafe "$1.showPanel(0);"
showPanel :: JSVal -> IO ()
foreign import javascript unsafe "$r = new THREE.Scene();"
newScene :: IO JSVal
foreign import javascript unsafe "$r = new THREE.BoxGeometry( $1, $2, $3 );"
newBoxGeometry :: Int -> Int -> Int -> IO JSVal
foreign import javascript unsafe "$r = new THREE.PerspectiveCamera( 75, window.innerWidth/window.innerHeight, 0.1, 1000 );"
newCamera :: IO JSVal
foreign import javascript unsafe "$r = new THREE.Mesh( $1, $2 );"
newMesh :: JSVal -> JSVal -> IO JSVal
foreign import javascript unsafe "$r = new THREE.MeshBasicMaterial( { color: 0x00ff00 } );"
newMeshBasicMaterial :: IO JSVal
foreign import javascript unsafe "$r = new THREE.WebGLRenderer({canvas:$1, antialias : true});"
newRenderer :: JSVal -> IO JSVal
foreign import javascript unsafe "$1.setSize( window.innerWidth, window.innerHeight );"
setSize :: JSVal -> IO ()
foreign import javascript unsafe "$1.add($2);"
addToScene :: JSVal -> JSVal -> IO ()
foreign import javascript unsafe "$1.position.z = $2;"
cameraZ :: JSVal -> Int -> IO ()
foreign import javascript unsafe "$1.rotation.x += $2;"
rotateX :: JSVal -> Double -> IO ()
foreign import javascript unsafe "$1.rotation.y += $2;"
rotateY :: JSVal -> Double -> IO ()
foreign import javascript unsafe "$1.render($2, $3);"
render :: JSVal -> JSVal -> JSVal -> IO ()
foreign import javascript unsafe "$1.position.z = $2;"
positionCamera :: JSVal -> Double -> IO ()
foreign import javascript unsafe "$1.appendChild( $2.domElement );"
addStatsToDOM :: JSVal -> JSVal -> IO ()
#endif