Skip to content

Instantly share code, notes, and snippets.

@Ryan1729
Created June 5, 2016 05:46
Show Gist options
  • Save Ryan1729/25f6468f4ecbd7d268377defe350990c to your computer and use it in GitHub Desktop.
Save Ryan1729/25f6468f4ecbd7d268377defe350990c to your computer and use it in GitHub Desktop.
Simple benchmark for elm-webgl based on the cube example
module Main exposing (..)
import Color exposing (..)
import Math.Vector3 exposing (..)
import Math.Matrix4 exposing (..)
import WebGL exposing (..)
import Html.App as Html
import AnimationFrame
import Html.Attributes exposing (width, height)
import Debug exposing (log)
type alias Model =
{ angle : Float
, cubes : Float
, slowCount : Int
, previousTime : Float
}
main : Program Never
main =
Html.program
{ init = ( initialModel, Cmd.none )
, view = scene >> WebGL.toHtml [ width 400, height 400 ]
, subscriptions = (\model -> AnimationFrame.diffs Basics.identity)
, update = update
}
initialModel =
Model 0 1 0 0
maximumFramTime : Float
maximumFramTime =
1000 / 30
maxSlow : Int
maxSlow =
10
update dt model =
( if dt < maximumFramTime || model.slowCount < maxSlow then
let
newAngle =
model.angle + dt / 5000
newCubes =
model.cubes
+ if dt < maximumFramTime then
1
else
0
newSlowCount =
if dt < maximumFramTime then
0
else
model.slowCount + 1
in
{ model
| angle = newAngle
, cubes = newCubes
, slowCount = newSlowCount
}
else
log ("score: " ++ toString model.cubes) model
, Cmd.none
)
-- MESHES - create a cube in which each vertex has a position and color
type alias Vertex =
{ color : Vec3
, position : Vec3
}
cube : Drawable Vertex
cube =
let
rft =
vec3 1 1 1
-- right, front, top
lft =
vec3 -1 1 1
-- left, front, top
lbt =
vec3 -1 -1 1
rbt =
vec3 1 -1 1
rbb =
vec3 1 -1 -1
rfb =
vec3 1 1 -1
lfb =
vec3 -1 1 -1
lbb =
vec3 -1 -1 -1
in
Triangle
<< List.concat
<| [ face green rft rfb rbb rbt
-- right
, face blue rft rfb lfb lft
-- front
, face yellow rft lft lbt rbt
-- top
, face red rfb lfb lbb rbb
-- bottom
, face purple lft lfb lbb lbt
-- left
, face orange rbt rbb lbb lbt
-- back
]
face : Color -> Vec3 -> Vec3 -> Vec3 -> Vec3 -> List ( Vertex, Vertex, Vertex )
face rawColor a b c d =
let
color =
let
c =
toRgb rawColor
in
vec3 (toFloat c.red / 255)
(toFloat c.green / 255)
(toFloat c.blue / 255)
vertex position =
Vertex color position
in
[ ( vertex a, vertex b, vertex c )
, ( vertex c, vertex d, vertex a )
]
-- VIEW
--this value is used to minimize overlapping cubes
oneOverPhi : Float
oneOverPhi =
0.6180339887498949
scene : Model -> List Renderable
scene model =
List.map (\offset -> renderCube (model.angle + offset * oneOverPhi)) [1..model.cubes]
renderCube : Float -> Renderable
renderCube angle =
render vertexShader fragmentShader cube (uniforms angle)
uniforms : Float -> { rotation : Mat4, perspective : Mat4, camera : Mat4, shade : Float }
uniforms t =
{ rotation = mul (makeRotate (3 * t) (vec3 0 1 0)) (makeRotate (2 * t) (vec3 1 0 0))
, perspective = makePerspective 45 1 0.01 100
, camera = makeLookAt (vec3 0 0 5) (vec3 0 0 0) (vec3 0 1 0)
, shade = 0.8
}
-- SHADERS
vertexShader : Shader { attr | position : Vec3, color : Vec3 } { unif | rotation : Mat4, perspective : Mat4, camera : Mat4 } { vcolor : Vec3 }
vertexShader =
[glsl|
attribute vec3 position;
attribute vec3 color;
uniform mat4 perspective;
uniform mat4 camera;
uniform mat4 rotation;
varying vec3 vcolor;
void main () {
gl_Position = perspective * camera * rotation * vec4(position, 1.0);
vcolor = color;
}
|]
fragmentShader : Shader {} { u | shade : Float } { vcolor : Vec3 }
fragmentShader =
[glsl|
precision mediump float;
uniform float shade;
varying vec3 vcolor;
void main () {
gl_FragColor = shade * vec4(vcolor, 1.0);
}
|]
{
"version": "1.0.0",
"summary": "helpful summary of your project, less than 80 characters",
"repository": "https://github.com/user/project.git",
"license": "BSD3",
"source-directories": [
"."
],
"exposed-modules": [],
"dependencies": {
"elm-community/elm-linear-algebra": "2.0.1 <= v < 3.0.0",
"elm-community/elm-webgl": "3.0.0 <= v < 4.0.0",
"elm-lang/animation-frame": "1.0.0 <= v < 2.0.0",
"elm-lang/core": "4.0.0 <= v <= 4.0.0",
"elm-lang/html": "1.0.0 <= v < 2.0.0",
"elm-lang/window": "1.0.0 <= v < 2.0.0"
},
"elm-version": "0.17.0 <= v < 0.18.0"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment