Contents

init(scale:texture:)

Creates an object from a single value, a UV-mapped texture, or both.

Declaration

init(scale: Float = 1.0, texture: CustomMaterial.Texture? = nil)

Parameters

  • scale:

    A value from 0.0 to 1.0 to use as the specular value for the material.

  • texture:

    An optional UV-mapped image texture.

Discussion

RealityKit automatically draws specular highlights for physically based materials using the values of various properties, primarily roughness and metallic. Specular highlights are bright spots of reflected light that appear on shiny objects.

[Image]

While many real-world objects can be accurately and realistically simulated with just the core physically based rendering (PBR) properties, you can create additional realistic effects by augmenting the specular highlights.

The specular scale and texture values are available to the material’s surface shader, but RealityKit doesn’t render additional specular highlights unless the surface shader function calls params.surface().set_specular().

The following Metal code demonstrates using the specular scale and texture values in a surface shader function to calculate specular highlights, emulating the specular behavior of PhysicallyBasedMaterial:

    // Retrieve the opacity scale from the CustomMaterial.
    float specularScale = params.material_constants().specular_scale();

    // Retrieve the entity's texture coordinates.
    float2 uv = params.geometry().uv0();

    // Entities loaded from a USDZ or .reality file use texture coordinates
    // with a flipped y-axis. This compensates for that.
    uv.y = 1.0 - uv.y;

    // Sample the specular texture.
    auto tex = params.textures();
    half specular = tex.specular().sample(textureSampler, uv).r;

    // Multiply the tint and the sampled value from the texture and assign
    // the result.
    specular *= specularScale;
    params.surface().set_specular(specular);

See Also

Creating a specular object