Search Unity

Combine Two Shaders

Discussion in 'Shaders' started by LustrumGames, Oct 29, 2020.

  1. LustrumGames

    LustrumGames

    Joined:
    Oct 3, 2019
    Posts:
    4
    Hello everyone! I have two shaders: First one is
    CurvedWorld.shader
    which makes the world bend effect. The second one is
    Specular.shader 
    which creates fake glossiness and metallic effects. I want to combine them into one shader so that objects that have to be glossy can also have the bending effect. If that's not possible, does anyone know of a shader that includes: Curved world effect, transparency, shininess/metallic effect and color tint? But it should also take in a diffuse texture. Can anyone help please? Thank you!

    Code (CSharp):
    1. Shader "Unlit/CurvedWorld"
    2. {
    3.     Properties
    4.     {
    5.         _MainTex ("Base (RGB)", 2D) = "white" {}
    6.         _QOffset ("Offset", Vector) = (0,0,0,0)
    7.         _Dist ("Distance", Float) = 100.0
    8.         _Alpha ("Alpha", Range(0.0,1.0)) = 1.0
    9.     }
    10.     SubShader {
    11.         Tags {"Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent"}
    12.         LOD 100
    13.         ZWrite On
    14.         Blend SrcAlpha OneMinusSrcAlpha
    15.         Pass
    16.         {
    17.         Lighting Off
    18.         CGPROGRAM
    19.             #pragma vertex vert
    20.             #pragma fragment frag
    21.             #include "UnityCG.cginc"
    22.             sampler2D _MainTex;
    23.             float4 _QOffset;
    24.             float _Dist;
    25.             float _Alpha;
    26.             uniform float4 _MainTex_ST;
    27.             struct v2f {
    28.             float4 pos : SV_POSITION;
    29.             float2 uv : TEXCOORD0;
    30.             };
    31.             v2f vert (appdata_base v)
    32.             {
    33.                 v2f o;
    34.             float4 vPos = mul (UNITY_MATRIX_MV, v.vertex);
    35.             float zOff = vPos.z/_Dist;
    36.                 vPos += _QOffset*zOff*zOff;
    37.                 o.pos = mul (UNITY_MATRIX_P, vPos);
    38.                 o.uv = TRANSFORM_TEX(v.texcoord, _MainTex);
    39.             return o;
    40.             }
    41.             half4 frag (v2f i) : COLOR
    42.             {
    43.             return tex2D(_MainTex, i.uv) * float4(1,1,1,_Alpha);
    44.             }
    45.         ENDCG
    46.         }
    47.     }
    48.       FallBack "Curved/Unlit Texture Alpha"
    49. }
    Code (CSharp):
    1. Shader "Custom/Specular" {
    2.     Properties {
    3.         _Color ("Color", Color) = (1,1,1,1)
    4.         _MainTex ("Albedo (RGB)", 2D) = "white" {}
    5.         _Glossiness ("Smoothness", Range(0,1)) = 0.5
    6.         _Metallic ("Metallic", Range(0,1)) = 0.0
    7.         _LightDirection ("Light Direction", Vector) = (0,0,0,1)
    8.         _lightSpread ("Light Spread", Range(0.01, 20)) = 2.0
    9.         _LightColor ("Light Color", Color) = (1,0,1,1)
    10.     }
    11.     SubShader {
    12.         Tags { "RenderType"="Opaque" }
    13.         LOD 200
    14.      
    15.         CGPROGRAM
    16.         // Physically based Standard lighting model, and enable shadows on all light types
    17.         #pragma surface surf Standard fullforwardshadows vertex:vert nolightmap
    18.  
    19.         // Use shader model 3.0 target, to get nicer looking lighting
    20.         #pragma target 3.0
    21.  
    22.         sampler2D _MainTex;
    23.  
    24.         struct Input {
    25.             float2 uv_MainTex;
    26.             float3 normal;
    27.             float3 posWorld;
    28.         };
    29.  
    30.         half _Glossiness;
    31.         half _Metallic;
    32.         half _lightSpread;
    33.         fixed4 _LightDirection;
    34.         fixed4 _LightColor;
    35.         fixed4 _Color;
    36.  
    37.         void vert(inout appdata_full v, out Input o) {
    38.             UNITY_INITIALIZE_OUTPUT(Input, o);
    39.             o.posWorld = mul(unity_ObjectToWorld, v.vertex);
    40.             o.normal = normalize(mul(float4(v.normal, 0.0), unity_WorldToObject).xyz);
    41.         }
    42.  
    43.         void surf (Input IN, inout SurfaceOutputStandard o) {
    44.             float3 normalDirection = IN.normal;
    45.             float3 viewDirection = normalize(_WorldSpaceCameraPos.xyz - IN.posWorld.xyz);
    46.             float3 lightDirection = normalize(-_LightDirection.xyz);
    47.             float spec = saturate(dot(normalDirection, lightDirection))
    48.                 * pow(saturate(dot(reflect(-lightDirection, normalDirection), viewDirection)), _lightSpread);
    49.             // Albedo comes from a texture tinted by color
    50.             fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
    51.             o.Albedo = spec * _LightColor + c.rgb;
    52.             // Metallic and smoothness come from slider variables
    53.             o.Metallic = _Metallic;
    54.             o.Smoothness = _Glossiness;
    55.             o.Alpha = c.a;
    56.         }
    57.         ENDCG
    58.     }
    59.     FallBack "Diffuse"
    60. }
     

    Attached Files:

  2. unityuserunity85496

    unityuserunity85496

    Joined:
    Jan 9, 2019
    Posts:
    89
    you can render each shader in a pass Pass in the same file. Order matters and you can set up various settings to handle the pixels from the first. You can do this using Surface shaders. Otherwise you can merge the code, just cram the vert parts and the frag parts at the bottom before your return but you'll make to figure out the nuance and some hardwares have limits on TEXCOORD that you can use like 8 most times. So youll have to use the .w in a float4 to compress some datas
     
    ArminTajik likes this.