Material

Namespace: material Language: Lua Type: Defold Lua File: script_material.cpp Source: engine/gamesys/src/gamesys/scripts/script_material.cpp

Functions for interacting with materials.

API

material.constant_info

Type: STRUCT Shader constant information

Members

material.constant_info_value

Type: TYPEDEF The value of a user constant returned in a material.constant_info entry by material.get_constants. A scalar constant is returned as a vector4 or matrix4; a shader constant array is returned as an array of those values. Non-user constants do not include a value field.

Parameters

Examples

for _, constant in ipairs(material.get_constants(self.material)) do
    if constant.value then
        pprint(constant.name, constant.value)
    end
end

material.constant_options

Type: STRUCT Shader constant update

Members

material.CONSTANT_TYPE

Type: ENUM Material constant types

Members

material.constant_value

Type: TYPEDEF A value accepted by material.set_constants when updating a shader constant. Use a number or vector for user vector constants, a matrix4 for matrix constants, and an array to update a shader constant array.

Parameters

Examples

material.set_constants(self.material, {
    tint = { value = vmath.vector4(1, 0.5, 0.5, 1) },
    weights = { value = { 0.25, 0.5, 0.75, 1 } }
})

material.get_constants

Type: FUNCTION Returns a table of all the shader constants in the material. This function will return all the shader constants that are used in both the vertex and the fragment shaders.

Parameters

Returns

Examples

Get the shader constants from a material specified as a resource property

go.property("my_material", resource.material())

function init(self)
    local constants = material.get_constants(self.my_material)
end

material.get_samplers

Type: FUNCTION Returns a table of all the texture samplers in the material. This function will return all the texture samplers that are used in both the vertex and the fragment shaders.

Parameters

Returns

Examples

Get the texture samplers from a material specified as a resource property

go.property("my_material", resource.material())

function init(self)
    local samplers = material.get_samplers(self.my_material)
end

material.get_textures

Type: FUNCTION Returns a table of all the textures from the material.

Parameters

Returns

Examples

Get the textures from a material specified as a resource property

go.property("my_material", resource.material())

function init(self)
    local textures = material.get_textures(self.my_material)
end

material.get_vertex_attributes

Type: FUNCTION Returns a table of all the vertex attributes in the material. This function will return all the vertex attributes that are used in the vertex shader of the material.

Parameters

Returns

Examples

Get the vertex attributes from a material specified as a resource property

go.property("my_material", resource.material())

function init(self)
    local vertex_attributes = material.get_vertex_attributes(self.my_material)
end

material.named_vertex_attribute_options

Type: STRUCT Named material vertex attribute update

Members

material.sampler_info

Type: STRUCT Texture sampler information

Members

material.sampler_options

Type: STRUCT Texture sampler update

Members

material.set_constants

Type: FUNCTION Sets shader constants in a material, if the constants exist.

Parameters

Examples

Set a shader constant in a material specified as a resource property

go.property("my_material", resource.material())

function update(self)
    -- update the 'tint' constant
    material.set_constants(self.my_material, {
        tint = { value = vmath.vector4(1, 0, 0, 1) }
    })
    -- change the type of the 'view_proj' constant to CONSTANT_TYPE_USER_MATRIX4 so the renderer can set our custom data
    material.set_constants(self.my_material, {
        view_proj = { value = self.my_view_proj, type = material.CONSTANT_TYPE_USER_MATRIX4 }
    })
end

material.set_samplers

Type: FUNCTION Sets texture samplers in a material, if the samplers exist. Use this function to change the settings of texture samplers. To set actual textures that should be bound to the samplers, use the material.set_textures function instead.

Parameters

Examples

Configures a sampler in a material specified as a resource property

go.property("my_material", resource.material())

function init(self)
    material.set_samplers(self.my_material, {
        texture_sampler = { u_wrap = graphics.TEXTURE_WRAP_REPEAT, v_wrap = graphics.TEXTURE_WRAP_MIRRORED_REPEAT }
    })
end

material.set_textures

Type: FUNCTION Sets textures in a material, if the samplers exist.

Parameters

Examples

Set a texture in a material from a resource

go.property("my_material", resource.material())
go.property("my_texture", resource.texture())

function init(self)
    material.set_textures(self.my_material, {
        my_texture = self.my_texture
    })
end

material.set_vertex_attributes

Type: FUNCTION Sets vertex attributes in a material, if the vertex attributes exist.

Parameters

Examples

Configures a vertex attribute in a material specified as a resource property

go.property("my_material", resource.material())

function init(self)
    material.set_vertex_attributes(self.my_material, {
        tint_attribute = { value = vmath.vec4(1, 0, 0, 1), semantic_type = graphics.SEMANTIC_TYPE_COLOR },
        weights        = { value = vmath.vec4(0, 1, 0, 0), semantic_type = graphics.SEMANTIC_TYPE_NONE }
    })
end

material.texture_info

Type: STRUCT Texture information

Members

material.vertex_attribute_info

Type: STRUCT Material vertex attribute information

Members

material.vertex_attribute_options

Type: STRUCT Material vertex attribute update

Members

material.vertex_attribute_value

Type: TYPEDEF A vertex attribute value accepted by material.set_vertex_attributes and returned by material.get_vertex_attributes. Use a number or vector for scalar and vector attributes, matrix4 for a 4x4 matrix, and a flat array of numbers for matrix shapes that do not map to matrix4.

Parameters

Examples

material.set_vertex_attributes(self.material, {
    tint = { value = vmath.vector4(1, 0, 0, 1) },
    transform_2d = { value = { 1, 0, 0, 0, 1, 0, 0, 0, 1 } }
})