Advertisement
Isti115

elm-webgl-triangles-2

Mar 22nd, 2017
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.04 KB | None | 0 0
  1. module Triangles exposing (..)
  2.  
  3. import WebGL as GL exposing (..)
  4. import Math.Vector2 exposing (Vec2, vec2)
  5. import Math.Vector3 exposing (Vec3, vec3)
  6. import Math.Matrix4 exposing (..)
  7. import Html exposing (..)
  8. import Time exposing (Time, millisecond)
  9.  
  10. type alias Model =
  11. { rotation : Float
  12. }
  13.  
  14. type alias Vertex =
  15. { position : Vec2
  16. , color : Vec3
  17. }
  18.  
  19. type alias Varying =
  20. { vColor : Vec3
  21. }
  22.  
  23. type Msg = Tick Time
  24.  
  25. update : Msg -> Model -> ( Model, Cmd Msg )
  26. update e m = {m | rotation = m.rotation + 0.01} ! []
  27.  
  28. --main : Program Never {} {}
  29. main =
  30. Html.program
  31. { init =
  32. Model 1.0 ! []
  33. , view = view
  34. , update = update
  35. , subscriptions = subscriptions
  36. }
  37.  
  38.  
  39. subscriptions : Model -> Sub Msg
  40. subscriptions model =
  41. Time.every millisecond Tick
  42.  
  43. view : Model -> Html msg
  44. view m =
  45. GL.toHtml
  46. []
  47. [ boxEntity m.rotation ]
  48.  
  49.  
  50. boxEntity : Float -> Entity
  51. boxEntity f =
  52. GL.entity
  53. vertexShader
  54. fragmentShader
  55. boxMesh
  56. { rotation = makeRotate f (vec3 1 1 1) }
  57.  
  58.  
  59. vertexShader : Shader { position : Vec2, color : Vec3 } { rotation : Mat4 } { vColor : Vec3 }
  60. vertexShader =
  61. [glsl|
  62.  
  63. precision mediump float;
  64. attribute vec2 position;
  65. attribute vec3 color;
  66. uniform mat4 rotation;
  67. varying vec3 vColor;
  68.  
  69. void main () {
  70. gl_Position = rotation * vec4(position, 0.0, 1.0);
  71. vColor = color;
  72. }
  73.  
  74. |]
  75.  
  76.  
  77. fragmentShader : Shader {} { rotation : Mat4 } Varying
  78. fragmentShader =
  79. [glsl|
  80.  
  81. precision mediump float;
  82. varying vec3 vColor;
  83.  
  84. void main () {
  85. gl_FragColor = vec4(vColor, 1.);
  86. }
  87.  
  88. |]
  89.  
  90.  
  91. boxMesh : Mesh Vertex
  92. boxMesh =
  93. GL.triangles
  94. [ ( (Vertex (vec2 -1 1) (vec3 1 0 0))
  95. , (Vertex (vec2 1 1) (vec3 0 1 0))
  96. , (Vertex (vec2 -1 -1) (vec3 0 0 1))
  97. )
  98. , ( (Vertex (vec2 -1 -1) (vec3 1 0 0))
  99. , (Vertex (vec2 1 -1) (vec3 0 1 0))
  100. , (Vertex (vec2 1 1) (vec3 0 0 1))
  101. )
  102. ]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement