Skip to content

Instantly share code, notes, and snippets.

@bstro
Created June 11, 2016 22:24
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 bstro/b45305f8a8c737740b2ca949fba6d242 to your computer and use it in GitHub Desktop.
Save bstro/b45305f8a8c737740b2ca949fba6d242 to your computer and use it in GitHub Desktop.
import Html exposing (Html)
import Html.App as Html
import Html.Attributes as Attr
import WebGL exposing (..)
import AnimationFrame exposing (..)
import Basics.Extra exposing (never)
import List.Extra exposing (andThen)
import Time
import Window
import Mouse
import Task
import Random exposing (initialSeed)
import Dict exposing (Dict)
import Math.Matrix4 exposing (..)
import Math.Vector3 exposing (..)
import Noise exposing (..)
type alias Model =
{ res : Maybe Window.Size
, pos : Maybe Mouse.Position
, terrain : Maybe (Drawable Attribute)
, tick : Int
}
type Msg
= Resize Window.Size
| MouseMove Mouse.Position
| Init Window.Size
| Tick Time.Time
| NoOp
size = 16.18
model =
{ res = Nothing
, pos = Nothing
, tick = 0
, terrain = Nothing
}
main =
Html.program
{ view = view
, update = update
, init = init
, subscriptions = subscriptions
}
init = model => Task.perform never Init Window.size
update msg ({res, tick} as model) =
case msg of
NoOp
-> model
=> Cmd.none
Init ({width, height} as res)
->
let
w = width // round size
h = height // round size
(perm, newSeed) = permutationTable (initialSeed 42)
in
{ model
| res = Just res
, terrain = Just mesh
-- Just <| [1..h] `andThen` \x -> [1..w] `andThen` \y ->
-- [(x => y => Attribute (vec3 0 0 0) (vec3 0 0 0) )]
}
=> Cmd.none
MouseMove pos
-> { model | pos = Just pos }
=> Cmd.none
Tick time
-> { model | tick = tick + 1 }
=> Cmd.none
Resize res
-> { model | res = Just res }
=> Cmd.none
subscriptions : Model -> Sub Msg
subscriptions model =
Sub.batch
[ Window.resizes Resize
, Mouse.moves MouseMove
, AnimationFrame.diffs Tick
]
(=>) : a -> b -> (a, b)
(=>) = (,)
type alias Attribute = { position : Vec3, color : Vec3 }
type alias Varying = { vColor : Vec3 }
mesh =
TriangleStrip [
Attribute (vec3 0 0 0) (vec3 1 0 1),
Attribute (vec3 1 1 0) (vec3 1 1 0),
Attribute (vec3 1 -1 0) (vec3 0 0 1),
Attribute (vec3 1.5 0 0) (vec3 1 0 0),
Attribute (vec3 1.5 -1.5 0) (vec3 1 0 0)]
view ({res, tick} as model) =
case res of
Nothing -> Html.text "Nothing"
Just {height, width} ->
WebGL.toHtml
[ Attr.width width, Attr.height height ]
[ render vertexShader fragmentShader mesh {} ]
vertexShader : Shader Attribute {} Varying
vertexShader = [glsl|
attribute vec3 position;
attribute vec3 color;
varying vec3 vColor;
void main () {
gl_Position = vec4(position, 1.0);
vColor = color;
}
|]
fragmentShader : Shader {} {} Varying
fragmentShader = [glsl|
precision mediump float;
varying vec3 vColor;
void main () {
gl_fragColor = vec4(vColor, 1.0);
}
|]
-- fragmentShader : Shader {} u { vcolor: Vec3 }
-- fragmentShader = [glsl|
-- precision mediump float;
-- varying vec3 vcolor;
-- void main () {
-- gl_FragColor = vec4(vcolor, 1.0);
-- }
-- |]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment