- Home /
Custom shader on UI Image
Hello,
I created custom shader, which I assigned to material and material to Image component. Problem is that the image shows as a black square in game view. In scene view it shows correctly.
Only thing that worked is changing canvas from Screen space - Overlay to Screen Space - Camera, but I'm wondering if anybody encountered the same issue and knows how to make it work in Screen space - Overlay?
Thank you
Answer by moment_um · Jul 25, 2018 at 08:04 AM
This is because of the way ui works in 3d engines. First a basic understanding of what is going on:
UI in screen space is used because the object does NOT need to be placed in camera space, and the only value that matters is the screen space coordinates (i.e. (0-screenwidth, 0-screenheight, 0)) which is defined in different ways in the rect transform (depending on what kind of anchor you use).
Normal objects require a great deal of math to get from a world space coordinate to a screen space coordinate, which most people just use Unity's shader function(UnityObjectToClipPos(v.vertex)), and you probably are using as well
But UI doesn't need this function. Instead, you should be putting the object in screen space. The only way to see exactly why this is happening is to show your shader code, but most likely you just need to use the correct math to show your shader.