-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathWebGL.elm
104 lines (77 loc) · 2.23 KB
/
WebGL.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
import AnimationFrame
import Html exposing (Html)
import Html.Attributes exposing (width, height, style)
import Math.Matrix4 as Mat4 exposing (Mat4)
import Math.Vector3 as Vec3 exposing (vec3, Vec3)
import Time exposing (Time)
import WebGL exposing (Mesh, Shader)
import Component.ThreeDViewer exposing (icosahedron, defaultVertexShader, defaultFragmentShader)
main : Program Never Time Time
main =
Html.program
{ init = ( 0, Cmd.none )
, view = view
, subscriptions = (\model -> AnimationFrame.diffs Basics.identity)
, update = (\elapsed currentTime -> ( elapsed + currentTime, Cmd.none ))
}
view : Float -> Html msg
view t =
WebGL.toHtml
[ width 400
, height 400
, style [ ( "display", "block" ) ]
]
[ WebGL.entity
vertexShader
fragmentShader
mesh
{ perspective = perspective (t / 1000) }
]
perspective : Float -> Mat4
perspective t =
Mat4.mul
(Mat4.makePerspective 45 1 0.01 100)
(Mat4.makeLookAt (vec3 (4 * cos t) 0 (4 * sin t)) (vec3 0 0 0) (vec3 0 1 0))
-- Mesh
type alias Vertex =
{ position : Vec3
, color : Vec3
}
mesh : Mesh Vertex
mesh =
WebGL.triangles
[ ( Vertex (vec3 0 0 0) (vec3 1 0 0)
, Vertex (vec3 1 1 0) (vec3 0 1 0)
, Vertex (vec3 1 -1 0) (vec3 0 0 1)
)
]
-- Shaders
type alias Uniforms =
{ perspective : Mat4 }
vertexShader : Shader Vertex Uniforms { vcolor : Vec3 }
vertexShader =
[glsl|
attribute vec3 position;
attribute vec3 color;
uniform mat4 perspective;
varying vec3 vcolor;
void main () {
gl_Position = perspective * vec4(position, 1.0);
vcolor = color;
}
|]
fragmentShader : Shader {} Uniforms { vcolor : Vec3 }
fragmentShader =
[glsl|
precision mediump float;
varying vec3 vcolor;
void main () {
gl_FragColor = vec4(vcolor, 1.0);
}
|]
view 790
perspective 790
mesh
icosahedron
WebGL.entity vertexShader fragmentShader mesh { perspective = perspective 790 }
WebGL.entity defaultVertexShader defaultFragmentShader icosahedron { perspective = perspective 20 }