This is an alternative site for discovering Elm packages. You may be looking for the official Elm package site instead.

Error

(line 16, column 21): unexpected "(" expecting WHITESPACE, NEWLINE, reserved word `as`, reserved word `exposing` or FRESH_LINE
module Engine.Mesh.Sphere where

{-| This module contains the definition of a sphere mesh and of a sphere
renderable object.

# Sphere Mesh
@docs sphereMesh

# Sphere (Renderable)
@docs sphere

-}

import List

import Math.Vector3 (Vec3, add, vec3)
import Engine.Mesh.Mesh (Mesh)
import Engine.Mesh.Rectangle (rectangleMesh)
import Engine.Mesh.Triangle (triangleMesh, triangle)
import Engine.Render.Renderable (Renderable)


{-| Function that takes a center point/vector, the radius, the number of
segments around the sphere radially (like longitude), the number of segments up
and down (like latitude), and returns a mesh that approximates a sphere.

      sphere center segmentsR segmentsY
-}
sphereMesh : Vec3 -> Float -> Float -> Float -> Mesh
sphereMesh center radius segmentsR segmentsY =
  let dt = 2 * pi / segmentsR
      dy = 1 / segmentsY
      halfRadius = radius / 2
      getRadius y = sqrt (max 0 (halfRadius - y*y))
  in [0..segmentsR-1] |> List.concatMap (\i ->
    let theta = i * dt
        x0 = cos theta
        x1 = cos (theta + dt)
        z0 = sin theta
        z1 = sin (theta + dt)
    in [0..segmentsY-1] |> List.concatMap (\j ->
      let y0 = j*dy - radius
          y1 = y0+dy
          r0 = getRadius y0
          r1 = getRadius y1
          bl = center `add` (vec3 (x0*r0) y0 (z0*r0))
          br = center `add` (vec3 (x1*r0) y0 (z1*r0))
          tl = center `add` (vec3 (x0*r1) y1 (z0*r1))
          tr = center `add` (vec3 (x1*r1) y1 (z1*r1))
      in triangleMesh bl br tr ++ triangleMesh bl tr tl))


{-| Default sphere renderable object. Located at the origin with radius of 0.5.
-}
sphere : Renderable
sphere = {
  triangle | mesh <- sphereMesh (vec3 0 0 0) 0.5 20 20 }