Skip to content

Instantly share code, notes, and snippets.

@nacmartin
Created October 26, 2016 19:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nacmartin/fb79c2a0a0b26d9bd2c233f8f2da7564 to your computer and use it in GitHub Desktop.
Save nacmartin/fb79c2a0a0b26d9bd2c233f8f2da7564 to your computer and use it in GitHub Desktop.
Example of use of IndexedTriangles
import Math.Vector3 exposing (..)
import WebGL exposing (..)
import Html exposing (Html)
import Html.App as Html
import Html.Attributes exposing (width, height, style)
-- Create a mesh with a triangle and a square
type alias Vertex = { position : Vec3 }
triangle : Drawable Vertex
triangle =
WebGL.Triangle
[ ( { position = vec3 0 1 0 }
, { position = vec3 -1 -1 0 }
, { position = vec3 1 -1 0 }
)
]
square : Drawable Vertex
square =
WebGL.IndexedTriangles
( [ { position = vec3 1 1 0 }
, { position = vec3 -1 1 0 }
, { position = vec3 1 -1 0 }
, { position = vec3 -1 -1 0 }
]
, [ 0, 1, 2, 1, 2, 3]
)
main : Html msg
main =
WebGL.toHtml
[ width 400, height 400, style [("backgroundColor", "black")] ]
( [render vertexShader fragmentShader square { displacement = vec3 -1.5 0 0}] ++
[render vertexShader fragmentShader triangle { displacement = vec3 1.5 0 0}]
)
-- Shaders
vertexShader : Shader Vertex { unif | displacement:Vec3 } {}
vertexShader = [glsl|
precision mediump float;
attribute vec3 position;
uniform vec3 displacement;
void main() {
gl_Position = vec4(0.3 * (position + displacement), 1);
}
|]
fragmentShader : Shader {} u {}
fragmentShader = [glsl|
precision mediump float;
void main () {
gl_FragColor = vec4(1, 1, 1, 1);
}
|]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment