Creating a basic planet shader for mobile

I think it will, and so do old (Built-In ages) mobiles.

1 Like

Thanks for the tips. I tried doing those changes but for some reason itā€™s giving me an error when I enable the toggle. The planet disappears and I get this error:

Metal: Error creating pipeline state (Custom/CloudedSurface): Fragment input(s) user(TEXCOORD3) mismatching vertex shader output type(s) or not written by vertex shader
(null)

Pretty sure I copied it correctly so not sure why itā€™s returning an error.

UPDATE: TEXCOORD3 corresponds to my worldPos inside v2f.

1 Like

Maybe can try this:

v2f structure:

struct v2f
{
    float4 pos : SV_POSITION;
    float2 uv : TEXCOORD0;
    float3 normal : NORMAL;
#if defined(_RIM_ENABLED)
    float3 worldPos : TEXCOORD1;
#endif
    float2 cloudUV : TEXCOORD2;
};
1 Like

Unfortunately that seems to have made no differenceā€¦ still the same error.

Sorry that I edited the code (#20) while you changing the shader.

Are you using "#pragma multi_compile_fragment"?

It should be "multi_compile" because I forgot that it also appears in vetex shader.

1 Like

That fixed it! Thanks so much buddyā€¦ you literally solved all my issues. I cannot thank you enough. :slight_smile:

EDIT: I think I will rename the title of this thread to reflect the contents more accurately, so others can find it useful.

1 Like

Hi @[wwWwwwW1]( Creating a basic planet shader for mobile members/wwwwwww1.8413720/), Iā€™ve been making a lot of great assets with your tips so a big thanks again. I have run into an issue though and I was wondering if you might know what is going on?

Sometimes when I move the camera about my scene, the visuals of the atmosphere shader become distorted. For example please below:

8499140--1131545--Screenshot 2022-10-08 at 20.02.25.png
^ Here you can see the atmosphere shader is displaying a nice subtle glow around the edges, as expected.

But if I move my camera about:

8499140--1131551--Screenshot 2022-10-08 at 20.02.42.png
^ For some reason, you can see the effect is distorted, as a solid blue overlay has appeared. This gets larger and worse the more I move the camera. Please note that literally all I did is move the camera, I did not change anything related to the shader.

Do you know why this is happening? Iā€™ve tried adjusting camera settings but to no avail. Iā€™m not sure if itā€™s down to the camera or the shader itself.

The same thing also happens in the Scene View when moving the camera about and zooming in/out.

At a distance it looks fine (editor view):
8499152--1131566--Screenshot 2022-10-08 at 20.17.45.png

If I simply zoom in without even changing the camera angle at all (editor view):
8499152--1131572--Screenshot 2022-10-08 at 20.18.17.png

Hi, what it looks like if you uncheck ā€œEnable Rimā€ (in material)?

Then the effect just disappears completely

Hi, Iā€™ve tested the shader and found that the calculation of worldPos is wrong.

To fix this, replace the following line:

o.worldPos = mul((float3x3)unity_ObjectToWorld, v.vertex.xyz);

with this:

o.worldPos = mul(unity_ObjectToWorld, v.vertex).xyz;
1 Like

That solved the problemā€¦ thank you so much!

1 Like

@[wwWwwwW1]( Creating a basic planet shader for mobile members/wwwwwww1.8413720/) I think I spoke too soon. The more I test this, I think it actually has not fixed the issue. Please see example below:

This is after I changed the line that you mentioned above. Again, all I am doing here is moving the camera, nothing to do with the shader or material.

By the way I should mention that my camera is Orthographic, not sure if that makes a difference? If I switch to perspective I am not seeing the same kind of distortion, so perhaps it does have something to do with it?

Looks like View Direction should always be forward for orthographic camera. ( bgolusā€™s post )

To support orthographic camera, we can add another toggle:

1 Like

Dude you are a star! I fixed the issue by changing line:

float3 viewDirWS = normalize(_WorldSpaceCameraPos - i.worldPos);

to:

float3 viewDirWS = UNITY_MATRIX_IT_MV[2].xyz;

This seems to have fixed it! But I will be interested to see your solution too, as your one is probably better!

1 Like

Itā€™s also based on bgolusā€™s 2nd suggestion, because the 1st one requires a matrix multiplication. (a bit more complex that the 2nd)

Properties:

Properties
{
    //...
    [Toggle(_USE_ORTHOGRAPHIC)] _UseOrthographic("Is Orthographic", Float) = 0.0
}

Pragma:

#pragma vertex vert
#pragma fragment frag
// 4 shader variants.
#pragma multi_compile _ _RIM_ENABLED
// use "shader_feature" instead of "multi_compile" if you don't need changing this property at runtime.
// This can reduce total variants to 2.
#pragma multi_compile _ _USE_ORTHOGRAPHIC
#include "UnityCG.cginc"

Fragment:

fixed4 frag(v2f i) : SV_Target
{
    float dif = max(0.05, dot(i.normal, normalize(_WorldSpaceLightPos0.xyz)));
    fixed4 col = tex2D(_MainTex, i.uv);
    // Sample the cloud texture.
    fixed4 clouds = tex2D(_Clouds, i.cloudUV);
    // Cloud Texture has alpha, so we can use alpha to lerp colors.
    col = lerp(col, clouds, clouds.a);
#if defined(_RIM_ENABLED)
#if !defined(_USE_ORTHOGRAPHIC)
    // (Perspective) Calculate world space view direction.
    float3 viewDirWS = normalize(_WorldSpaceCameraPos - i.worldPos);
#else
    // (Orthographic) Get forward view vector from inverse view matrix.
    float3 viewDirWS = UNITY_MATRIX_IT_MV[2].xyz;
#endif
    // Fresnel Effect
    float fresnel = FresnelEffect(i.normal, viewDirWS, _Power);
    // Apply before lighting, so that darkness will affect Fresnel.
    col = lerp(_FresnelColor, col, fresnel);
#endif
    fixed4 result = fixed4(col.rgb * dif * _LightColor0.rgb, 1);
    return result;
}
1 Like

Thanks so much, it looks like your tweak matches the one I found so that is encouraging. I can only thank you again, you are awesome.

1 Like

I have just discovered a new technical problem with the shaderā€¦

@[wwWwwwW1]( Creating a basic planet shader for mobile members/wwwwwww1.8413720/) Iā€™m really sorry to keep bothering you but I have now discovered another technical problem with the shader.

When it is applied, if I try to rotate the object in the axis of the camera, everything works fine:

8500814--1131953--ezgif-3-87d4af9a12.gif

But, as soon as I try to rotate the object in a different axis, i.e the x or z axis, you can see the rim effect is once again distorted:

8500814--1131956--ezgif-3-090569536e.gif

This is a serious issue for me as my planets need to rotate in multiple axes. Any chance you know what is going wrong here?

I am thinking the problem possibly lies here:

o.normal = normalize(mul(v.normal, unity_WorldToObject).xyz);

P.S. This is teaching me how complex shaders are!