<!--
{
  "availability" : [
    "iOS: 15.0.0 -",
    "iPadOS: 15.0.0 -",
    "macCatalyst: 15.0.0 -",
    "macOS: 12.0.0 -",
    "tvOS: 26.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "RealityKit",
  "identifier" : "/documentation/RealityKit/CustomMaterial/Opacity/init(_:)",
  "metadataVersion" : "0.1.0",
  "role" : "Initializer",
  "symbol" : {
    "kind" : "Initializer",
    "modules" : [
      "RealityKit"
    ],
    "preciseIdentifier" : "s:17RealityFoundation14CustomMaterialV7OpacityVyAeA015PhysicallyBasedD0VADVcfc"
  },
  "title" : "init(_:)"
}
-->

# init(_:)

Creates an object from the opacity property of an existing
physically based material.

```
init(_ value: PhysicallyBasedMaterial.Opacity)
```

## Parameters

`value`

The opacity property from the [`PhysicallyBasedMaterial`](/documentation/RealityKit/PhysicallyBasedMaterial)’s
[`blending`](/documentation/RealityKit/PhysicallyBasedMaterial/blending-swift.property) property.

## Discussion

This initializer creates a new object by copying values from an
existing physically based material’s
[`blending`](/documentation/RealityKit/PhysicallyBasedMaterial/blending-swift.property) property. All of
the copied values, including [`scale`](/documentation/RealityKit/CustomMaterial/Opacity/scale),
[`texture`](/documentation/RealityKit/CustomMaterial/Opacity/texture), and
[`opacityThreshold`](/documentation/RealityKit/CustomMaterial/opacityThreshold) are available to the material’s
surface shader function, but RealityKit draws the entity fully
opaque unless the surface shader function calls
`params.surface().set_opacity()`.

The following Metal code demonstrates how to emulate the blending
logic RealityKit uses to render entities with a
[`PhysicallyBasedMaterial`](/documentation/RealityKit/PhysicallyBasedMaterial) in your custom material’s surface shader
function:

```swift
    // Retrieve the opacity scale and threshold from the CustomMaterial.
    float opacityScale = params.material_constants().opacity_scale();
    float opacityThreshold = params.material_constants().opacity_threshold();

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

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

    // Retrieve the opacity texture from the material.
    auto tex = params.textures();

    // Sample the value from the opacity texture.
    half opacity = tex.opacity().sample(textureSampler, uv).r;

    if (opacityThreshold > 0.0) {
        // If the opacity threshold is greater than 0, use masking
        // behavior and set the opacity to either 1.0 or 0.0 depending
        // on the value of the opacity threshold.
        opacity = (opacity <= opacityThreshold) ? 0.0 : 1.0;
    } else {
        // If the opacity threshold is 0, mutiply the opacity by scale.
        opacity *= opacityScale;
    }

    // Use the calculated value to set the opacity for rendering.
    params.surface().set_opacity(opacity);
```

---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)