Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question How do I merge shaders?

Discussion in 'Scripting' started by Richard_Ingalls, Feb 16, 2023.

  1. Richard_Ingalls

    Richard_Ingalls

    Joined:
    Dec 16, 2021
    Posts:
    88
    I know I have to open the shader files and modify the code, but I don't know how I would add them together. These were downloaded off the asset store.
    Card Shader (front and back textures)
    Code (CSharp):
    1. Shader "Custom/Cutout Double-sided"
    2. {
    3.     Properties
    4.     {
    5.         _Cutoff ("Cutoff", Range(0,1)) = 0.5
    6.         _Color ("Color", Color) = (1,1,1,1)
    7.         _MainTex ("Albedo (RGB)", 2D) = "white" {}
    8.         _MainTex2("Texture2", 2D) = "white"{}
    9.         _Glossiness ("Smoothness", Range(0,1)) = 0.5
    10.         _Metallic ("Metallic", Range(0,1)) = 0.0
    11.     }
    12.     SubShader
    13.     {
    14.         Tags { "Queue"="AlphaTest" "RenderType"="TransparentCutout" }
    15.         LOD 200
    16.  
    17.         Cull Off
    18.  
    19.         CGPROGRAM
    20.  
    21.         #pragma surface surf Standard alphatest:_Cutoff addshadow fullforwardshadows
    22.         #pragma target 3.0
    23.  
    24.         sampler2D _MainTex;
    25.         sampler2D _MainTex2;
    26.  
    27.         struct Input
    28.         {
    29.             float2 uv_MainTex;
    30.         };
    31.  
    32.         half _Glossiness;
    33.         half _Metallic;
    34.         fixed4 _Color;
    35.  
    36.         void surf (Input IN, inout SurfaceOutputStandard o)
    37.         {
    38.             fixed4 c = tex2D (_MainTex2, IN.uv_MainTex) * _Color;
    39.             o.Albedo = c.rgb;
    40.             o.Metallic = _Metallic;
    41.             o.Smoothness = _Glossiness;
    42.             o.Alpha = c.a;
    43.         }
    44.  
    45.         ENDCG
    46.  
    47.         Cull Front
    48.  
    49.         CGPROGRAM
    50.  
    51.         #pragma surface surf Standard alphatest:_Cutoff fullforwardshadows vertex:vert
    52.         #pragma target 3.0
    53.  
    54.         sampler2D _MainTex;
    55.  
    56.  
    57.         struct Input
    58.         {
    59.             float2 uv_MainTex;
    60.         };
    61.  
    62.         void vert (inout appdata_full v)
    63.         {
    64.             v.normal.xyz = v.normal * -1;
    65.         }
    66.  
    67.         half _Glossiness;
    68.         half _Metallic;
    69.         fixed4 _Color;
    70.  
    71.  
    72.         void surf (Input IN, inout SurfaceOutputStandard o)
    73.         {
    74.             fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
    75.             o.Albedo = c.rgb;
    76.             o.Metallic = _Metallic;
    77.             o.Smoothness = _Glossiness;
    78.             o.Alpha = c.a;
    79.         }
    80.  
    81.         ENDCG
    82.     }
    83.     FallBack "Diffuse"
    84. }
    Outline Shader
    Code (CSharp):
    1. Shader "Ultimate 10+ Shaders/Outline"
    2. {
    3.     Properties
    4.     {
    5.         _Color ("Color", Color) = (1,1,1,1)
    6.         _MainTex ("Albedo (RGB)", 2D) = "white" {}
    7.  
    8.         _OutlineColor ("Outline Color", Color) = (1,1,1,1)
    9.         _OutlineWidth ("Outline Width", Range(0, 4)) = 0.25
    10.  
    11.         [Enum(UnityEngine.Rendering.CullMode)] _Cull ("Cull", Float) = 2
    12.     }
    13.     SubShader
    14.     {
    15.         Tags { "RenderType"="Geometry" "Queue"="Transparent" }
    16.         LOD 200
    17.         Cull [_Cull]
    18.  
    19.         Pass{
    20.             ZWrite Off
    21.             CGPROGRAM
    22.          
    23.             #pragma vertex vert
    24.             #pragma fragment frag
    25.  
    26.             struct appdata {
    27.             float4 vertex : POSITION;
    28.             float4 tangent : TANGENT;
    29.             float3 normal : NORMAL;
    30.             float4 texcoord : TEXCOORD0;
    31.             fixed4 color : COLOR;
    32.             };
    33.  
    34.             struct v2f{
    35.                 float4 pos : SV_POSITION;
    36.                 float3 normal : NORMAL;
    37.             };
    38.  
    39.             fixed4 _OutlineColor;
    40.             half _OutlineWidth;
    41.  
    42.             v2f vert(appdata input){
    43.                 input.vertex += float4(input.normal * _OutlineWidth, 1);
    44.  
    45.                 v2f output;
    46.  
    47.                 output.pos = UnityObjectToClipPos(input.vertex);
    48.                 output.normal = mul(unity_ObjectToWorld, input.normal);
    49.  
    50.                 return output;
    51.             }
    52.  
    53.             fixed4 frag(v2f input) : SV_Target
    54.             {
    55.                 return _OutlineColor;
    56.             }
    57.  
    58.             ENDCG
    59.         }
    60.  
    61.         ZWrite On
    62.         CGPROGRAM
    63.  
    64.         #pragma surface surf Standard fullforwardshadows
    65.  
    66.         #ifndef SHADER_API_D3D11
    67.             #pragma target 3.0
    68.         #else
    69.             #pragma target 4.0
    70.         #endif
    71.  
    72.         struct Input
    73.         {
    74.             float2 uv_MainTex;
    75.         };
    76.  
    77.         fixed4 _Color;
    78.         sampler2D _MainTex;
    79.  
    80.         fixed4 pixel;
    81.         void surf (Input IN, inout SurfaceOutputStandard o)
    82.         {
    83.             pixel = tex2D (_MainTex, IN.uv_MainTex) * _Color;
    84.             o.Albedo = pixel.rgb;
    85.         }
    86.         ENDCG
    87.     }
    88.     FallBack "Diffuse"
    89. }
    I need to know what kind of things I would add, what I would change, what I would remove, because I have no clue how the heck to do this.
     
  2. Niter88

    Niter88

    Joined:
    Jul 24, 2019
    Posts:
    112
    We don't know what you're trying to do lol, specify it.
    Google and find a shader like the one you want.

    If you can't find anything, then you'll have to learn shader code.
    It's one of the hardest things you could learn. (ShaderGraph is a bit easier)

    There are some channels that are awesome for shader learning:

    NedMakesGames

    Freya Holmer

    MinionsArt
     
  3. chemicalcrux

    chemicalcrux

    Joined:
    Mar 16, 2017
    Posts:
    717
    Merging two arbitrary shaders is a very broad task -- it's like asking "how do I combine these two scripts?"

    Perhaps you can just add an extra material slot to your card and stick an outline material into it? Extra materials get drawn on top of whatever geometry the last material slot affects (so if your card only has one material slot, this'll be super simple).

    I dunno if they'll play nice, but it's worth a try.
     
  4. Richard_Ingalls

    Richard_Ingalls

    Joined:
    Dec 16, 2021
    Posts:
    88
    I have tried this, it doesn't work.
     
  5. Richard_Ingalls

    Richard_Ingalls

    Joined:
    Dec 16, 2021
    Posts:
    88
    I am trying to merge an outline shader with a card shader, I want to have a way to add the second texture and settings to make that work, but I have no idea where that would fit. Also, I can't find shadergraph anywhere.
     
    Niter88 likes this.