Skip to content

Discriminated Unions in FShade

aszabo314 edited this page Aug 29, 2017 · 1 revision
  • Discriminated Unions in FShade

GLSL requires Enumerations. However, discriminated unions can be used in FShade.

The following example defines different light types using an enum, which is then used in a shader.

  • Example
// define light types
type LightType =
    | Directional = 0
    | Spot = 1
    | Point = 2
 
type LightSource = {
        typ : LightType  
        ...
    }
 
let directional ... = {
        typ = LightType.Directional
        ...
    }
 
let spot ... = {
        typ = LightType.Spot
        ...
    }
 
let point ... = {
        typ = LightType.Point
        ...
    }
 
// Add them to the uniform scope
type UniformScope with
    member uniform.LightType : LightType  = uniform?LightType
    ...
 
// Shader using LightType enum
let shader (v : Effects.Vertex) =
    fragment {
        ...
             
        match uniform.LightType with
        | LightType.Directional -> ...
        | LightType.Spot -> ...
        | LightType.Point -> ...
 
        ...
    }
 
// Create a light and set its uniform in a scenegraph to use it in the shader
light light = Light.point V3d.OOI ...
 
let sceneGraph =
        |> ...
        |> Sg.uniform "LightType" (( light.typ |> int ) |> Mod.constant )
        |> ...

Clone this wiki locally
You can’t perform that action at this time.