Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Other Trying to modify Curved World shader to work with Mobile particle shaders

Discussion in 'Shaders' started by gw707, Jan 21, 2023.

  1. gw707

    gw707

    Joined:
    May 3, 2021
    Posts:
    12
    I've come across a curved world shader which i've implemented in my game. I've also created varients of the curved shader by modifing and combining it with a few mobile shaders, such as bumped and diffuse shaders and works fine.

    However, i've ran into an issue with trying to combine the curved world shader with the mobile particle shaders (specifically Alpha Blended) as i can't get the curved effect to work when trying to combine them.

    I have sprites in my game that are currently using mobile Alpha Blended particle shader, hence why i need to combine the curved shader with the mobile particle ones.

    My knowledge of shaders is limited, but i've had many attempts to get this to work but no luck so i'm going clearly going wrong somewhere. I've notice that there are difference with the regular mobile shaders and the mobile particle ones, but i don't know how to go about getting this to work.

    I'd appreciate any help in getting the curved shader script to be compatable with mobile particle shaders.


    CurvedWorld.shader
    Code (CSharp):
    1. Shader "Curved/CurvedWorld" {
    2.  
    3. Properties {
    4.       _Color ("Color", Color) = (1,1,1,1)
    5.       _MainTex ("Albedo (RGB)", 2D) = "white" {}
    6. }
    7.  
    8. SubShader {
    9.  
    10.     Tags { "RenderType"="Opaque" }
    11.     LOD 200
    12.  
    13.     CGPROGRAM
    14.  
    15.     #pragma surface surf Standard fullforwardshadows vertex:vert addshadow
    16.     #pragma target 3.0
    17.  
    18.     #include "UnityCG.cginc"
    19.     #include "CurvedWorldCore.cginc"
    20.  
    21.     sampler2D _MainTex;
    22.      
    23.     struct Input {
    24.     float2 uv_MainTex;
    25.     };
    26.      
    27.     fixed4 _Color;
    28.  
    29.     void surf (Input IN, inout SurfaceOutputStandard o)
    30.     {
    31.         fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
    32.         o.Albedo = c.rgb;
    33.         o.Alpha = c.a;
    34.     }

    CurvedWorldCore.cginc
    Code (CSharp):
    1. float3 _Curvature;
    2.  
    3. float _Distance;
    4.  
    5. void vert(inout appdata_full v)
    6. {              
    7.     float4 vPos = mul( unity_ObjectToWorld, v.vertex);
    8.  
    9.     float dist = 0;
    10.     //dist = vPos.z - _WorldSpaceCameraPos.z;
    11.     dist = distance(vPos.z , _WorldSpaceCameraPos.z) - _Distance;
    12.     if( dist < 0)   {   dist = 0;   }      
    13.    
    14.     float addY = dist * dist;
    15.    
    16.     vPos.y -= addY * _Curvature.y;
    17.  
    18.     dist = vPos.x - _WorldSpaceCameraPos.x;
    19.     float addHY = dist * dist;
    20.     vPos.y -= addHY* _Curvature.x ;
    21.    
    22.     // for corner
    23.     vPos.x += addY * _Curvature.z;
    24.  
    25.     vPos = mul ( unity_WorldToObject, vPos);
    26.     v.vertex = vPos;
    27. }

    CurvedWorld.cs
    Code (CSharp):
    1. public class CurvedWorld : MonoBehaviour {
    2.  
    3.  
    4.     public Vector3 Curvature = new Vector3(0, 0.5f, 0);
    5.     public float Distance = 0;
    6.  
    7.     [Space]
    8.     public float CurvatureScaleUnit = 1000f;
    9.    
    10.     int CurvatureID;
    11.     int DistanceID;
    12.  
    13.     private void OnEnable()
    14.     {
    15.         CurvatureID = Shader.PropertyToID("_Curvature");
    16.         DistanceID = Shader.PropertyToID("_Distance");
    17.     }
    18.  
    19.     void Update()
    20.     {
    21.         Vector3 curvature = CurvatureScaleUnit == 0 ? Curvature : Curvature / CurvatureScaleUnit;
    22.  
    23.         Shader.SetGlobalVector(CurvatureID, curvature);
    24.         Shader.SetGlobalFloat(DistanceID, Distance);
    25.     }

    Unity Built-In Shaders Github: https://github.com/TwoTailsGames/Unity-Built-in-Shaders/tree/master/DefaultResourcesExtra