SHARE
TWEET

Untitled

a guest Oct 21st, 2019 76 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. module Triangles exposing (main)
  2.  
  3. import Angle exposing (Angle)
  4. import Animation
  5. import Browser
  6. import Camera3d
  7. import Color
  8. import Direction3d
  9. import Html exposing (Html)
  10. import Length
  11. import Pixels
  12. import Point3d
  13. import Scene3d
  14. import Scene3d.Drawable as Drawable
  15. import Scene3d.Mesh as Mesh
  16. import Triangle3d
  17. import Viewpoint3d
  18.  
  19.  
  20. type alias Model =
  21.     { color : Animation.State
  22.     }
  23.  
  24.  
  25. type Msg
  26.     = Animate Animation.Msg
  27.  
  28.  
  29. init : () -> ( Model, Cmd Msg )
  30. init () =
  31.     ( { color =
  32.             Animation.style [ Animation.color red ]
  33.                 |> Animation.interrupt
  34.                     [ Animation.loop
  35.                         [ Animation.to [ Animation.color red ]
  36.                         , Animation.to [ Animation.color green ]
  37.                         ]
  38.                     ]
  39.       }
  40.     , Cmd.none
  41.     )
  42.  
  43.  
  44. update : Msg -> Model -> ( Model, Cmd Msg )
  45. update msg m =
  46.     case msg of
  47.         Animate animMsg ->
  48.             ( { m | color = Animation.update animMsg m.color }, Cmd.none )
  49.  
  50.  
  51. view : Model -> Html msg
  52. view m =
  53.     let
  54.         color =
  55.             Animation.renderPairs m.color
  56.                 |> List.head
  57.                 |> Maybe.map Tuple.second
  58.                 |> Maybe.map str2color
  59.  
  60.         triangle1 =
  61.             Triangle3d.from
  62.                 (Point3d.meters 0 0 0)
  63.                 (Point3d.meters 1 0 0)
  64.                 (Point3d.meters 1 1 0)
  65.  
  66.         triangle2 =
  67.             Triangle3d.from
  68.                 (Point3d.meters 0 0 0)
  69.                 (Point3d.meters 1 1 0)
  70.                 (Point3d.meters 0 1 0)
  71.  
  72.         mesh1 =
  73.             Mesh.triangles [] [ triangle1 ]
  74.  
  75.         mesh2 =
  76.             Mesh.triangles [] [ triangle2 ]
  77.  
  78.         viewpoint =
  79.             Viewpoint3d.lookAt
  80.                 { focalPoint = Point3d.meters 0.5 0.5 0
  81.                 , eyePoint = Point3d.meters 3 3 2
  82.                 , upDirection = Direction3d.z
  83.                 }
  84.  
  85.         camera =
  86.             Camera3d.perspective
  87.                 { viewpoint = viewpoint
  88.                 , verticalFieldOfView = Angle.degrees 30
  89.                 , clipDepth = Length.meters 0.1
  90.                 }
  91.     in
  92.     Scene3d.unlit []
  93.         { camera = camera
  94.         , width = Pixels.pixels 800
  95.         , height = Pixels.pixels 600
  96.         }
  97.         [ Drawable.colored (Maybe.withDefault Color.orange color) mesh1
  98.         , Drawable.colored Color.blue mesh2
  99.         ]
  100.  
  101.  
  102. subscriptions : Model -> Sub Msg
  103. subscriptions m =
  104.     Animation.subscription Animate [ m.color ]
  105.  
  106.  
  107. main =
  108.     Browser.element
  109.         { init = init
  110.         , update = update
  111.         , subscriptions = subscriptions
  112.         , view = view
  113.         }
  114.  
  115.  
  116. red : Animation.Color
  117. red =
  118.     { red = 256
  119.     , blue = 0
  120.     , green = 0
  121.     , alpha = 1.0
  122.     }
  123.  
  124.  
  125. green : Animation.Color
  126. green =
  127.     { red = 0
  128.     , blue = 0
  129.     , green = 255
  130.     , alpha = 1.0
  131.     }
  132.  
  133.  
  134. str2color : String -> Color.Color
  135. str2color s =
  136.     -- input format: "rgba(181,75,0,1)"
  137.     case
  138.         String.split "(" s
  139.             |> List.drop 1
  140.             |> List.head
  141.             |> Maybe.map (String.split ",")
  142.             |> Maybe.map (List.map String.toInt)
  143.     of
  144.         Just ((Just r) :: (Just g) :: (Just b) :: _) ->
  145.             Color.rgb255 r g b
  146.  
  147.         _ ->
  148.             Color.red
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top