Hello,
I have a cylinder with an Unlit Material and a base color set. Now I want to apply a gradient as the alpha mask so I get this kind of halo GTA-like checkpoint look. The code:
var baseMaterial = UnlitMaterial(color: UIColor.red)
baseMaterial.blending = .transparent(opacity: .init(scale: 100, texture: .init(maskTextureResource))) // maskTextureResource is the gradient mask
baseMaterial.opacityThreshold = 0
This works but unfortunately leads to some ugly visible gradient banding. I've also tried to play with the scale of the blending texture but that did not help.
As an alternative approach I tried to solve this via a custom surface shader. Code below:
[[visible]]
void gradientShader(realitykit::surface_parameters params) {
auto surface = params.surface();
float2 uv = params.geometry().uv0();
float h = 0.5; // adjust position of middleColor
half startAlpha = 0.001;
half middleAlpha = 1;
half endAlpha = 0.001;
half alpha = mix(mix(startAlpha, middleAlpha, half(uv.y/h)), mix(middleAlpha, endAlpha, half((uv.y - h)/(1.0 - h))), half(step(h, uv.y)));
surface.set_emissive_color(half3(params.material_constants().emissive_color()));
surface.set_base_color(half3(params.material_constants().base_color_tint()));
surface.set_opacity(alpha);
}
The result looks really nice and smooth but unfortunately this now also culls the inner part of the cylinder. Even on the semitransparent parts. What I want is a having the effect applied on both the outer and inner part of the cylinder being visible. So the transparent part of the outside allows you to seethrough to the inside. I've got this working by using a PhysicallyBasedMaterial instead of an UnlitMaterial (which does not support blending out of the box) but again had to issue with the banding.
On my Custom Material faceCulling is set to .none.
Here is how it currently looks – as you can see in the left one the alpha mask is not smooth and has banding artefacts:
Thank you for any help!