module Prism::Shader::UniformStruct

Overview

Turns a class into a serializable uniform structure. Such a class can be used as a uniform in a shader Program.

Example

class A
  include Shader::UniformStruct

  @[Shader::Field]
  @a : String = "a"
end

class B < A
  include Shader::UniformStruct

  @[Shader::Field]
  @b : Float32 = 1
end

my_b = B.new
my_b.to_uniform # => {"a" => "a", "b" => 1.0}

Usage

Including Shader::UniformStruct will create a #to_uniform method on the current class. By default, this method will serialize into a uniform object containing the value of every annotated instance variable, the keys being the instance variable name. Supported primitives are (string, integer, float, Vector3f), along with objects which include Shader::UniformStruct. Union types are not supported.

To change how individual instance variables are parsed and serialized, the annotation Shader::Field can be placed on the instance variable. Annotating methods is also allowed.

class A
  include Shader::UniformStruct

  @[Shader::Field(name: "attribute")]
  @a : String = "value"
end

Shader::Field properties:

Class annotation Shader::UniformStruct::Options

DEPRECATED the class anotation will be removed in a future version.

supported properties:

@[Shader::UniformStruct::Options(name: "R_spotLight")]
class A
  include Shader::UniformStruct
  @[Shader::Field]
  @a : Int32 = 1
end

c = A.new
c.to_uniform # => {"R_spotLight.a" => 1}

TODO Add the same macro syntax as the shader program so we can use field "fieldName", SomeType. We could still keep the annotation.

Direct including types

Defined in:

prism/shader/uniform_struct.cr

Instance Method Summary

Instance Method Detail

def to_uniform #

Serializes the uniform struct into a map of uniform names and values.


[View source]