float3 spherePos; // External, can be linked to transform position
float3 sphereSize; // External, can be linked to transform scale
float sphereFade; // External, should be between 0 and 1
float3 posWorld; // Pixel world position
float3 posWorldSphere = (posWorld - spherePos) / sphereSize; // Position relative to unit sphere
float sphereAlpha = saturate((1.0 - length(posWorldSphere)) / max(sphereFade, 0.0001));
Color.a *= sphereAlpha;
many thanks for taking the time to reply to my question.
I think that you are fading out from the centre of the sphere getting to fully transparent when you reach the surface of the sphere. Is that correct ? What I’m trying to do is more boolean, either fully opaque inside the bounds of the sphere or fully transparent outside of the bounds of the sphere so I think probably I want to test if the posworld is within the sphere and if so the Color.a will be 1 and otherwise it would be zero.
I haven’t really understood all of the structure of shaders yet so don’t understand how to implement your kind solution or fully get it. I’m reading the documentation so hopefully will get there soon. Looking forward to understanding more … and thanks again for taking the time to give me your reply
Place the code inside a pixel (frag or surf) shader block, and use it to modify final alpha. Shader will need to be transparent ie have a blend mode.
This approach can work nicely but also be subject to sorting issues. Overall this is the approach to take. Another approach is to render the object to a texture of your choice instead, and map to a sphere, generating screen space coordinates. This should work but also has niggling issues with alpha so will need clearing etc and will need a shader for the screenspace issues and has no shadowing from other objects.
The final approach I would imagine is via the stencil buffer.