Search Unity

Standard Shader that supports (1) WorldSpace Coordinates and (2) Texture Rotation

Discussion in 'Shaders' started by jintz, Jul 3, 2020.

  1. jintz

    jintz

    Joined:
    Mar 2, 2013
    Posts:
    1
    Please help me modify the default Standard Shader so it uses WorldSpace Coordinates, instead of a mesh' UVs, as well as allow setting a rotation of its texture. I'm coming from Unreal where this is easy, but have no idea what to do now in Unity :(

    I have managed to find a copy of the original shader though; but that's really it. Please help!

    Code (CSharp):
    1.  
    2. Shader "StandardWorldSpace"
    3. {
    4.     Properties
    5.     {
    6.         _Color("Color", Color) = (1,1,1,1)
    7.         _MainTex("Albedo", 2D) = "white" {}
    8.  
    9.         _Cutoff("Alpha Cutoff", Range(0.0, 1.0)) = 0.5
    10.  
    11.         _Glossiness("Smoothness", Range(0.0, 1.0)) = 0.5
    12.         _GlossMapScale("Smoothness Scale", Range(0.0, 1.0)) = 1.0
    13.         [Enum(Metallic Alpha,0,Albedo Alpha,1)] _SmoothnessTextureChannel ("Smoothness texture channel", Float) = 0
    14.  
    15.         [Gamma] _Metallic("Metallic", Range(0.0, 1.0)) = 0.0
    16.         _MetallicGlossMap("Metallic", 2D) = "white" {}
    17.  
    18.         [ToggleOff] _SpecularHighlights("Specular Highlights", Float) = 1.0
    19.         [ToggleOff] _GlossyReflections("Glossy Reflections", Float) = 1.0
    20.  
    21.         _BumpScale("Scale", Float) = 1.0
    22.         [Normal] _BumpMap("Normal Map", 2D) = "bump" {}
    23.  
    24.         _Parallax ("Height Scale", Range (0.005, 0.08)) = 0.02
    25.         _ParallaxMap ("Height Map", 2D) = "black" {}
    26.  
    27.         _OcclusionStrength("Strength", Range(0.0, 1.0)) = 1.0
    28.         _OcclusionMap("Occlusion", 2D) = "white" {}
    29.  
    30.         _EmissionColor("Color", Color) = (0,0,0)
    31.         _EmissionMap("Emission", 2D) = "white" {}
    32.  
    33.         _DetailMask("Detail Mask", 2D) = "white" {}
    34.  
    35.         _DetailAlbedoMap("Detail Albedo x2", 2D) = "grey" {}
    36.         _DetailNormalMapScale("Scale", Float) = 1.0
    37.         [Normal] _DetailNormalMap("Normal Map", 2D) = "bump" {}
    38.  
    39.         [Enum(UV0,0,UV1,1)] _UVSec ("UV Set for secondary textures", Float) = 0
    40.  
    41.  
    42.         // Blending state
    43.         [HideInInspector] _Mode ("__mode", Float) = 0.0
    44.         [HideInInspector] _SrcBlend ("__src", Float) = 1.0
    45.         [HideInInspector] _DstBlend ("__dst", Float) = 0.0
    46.         [HideInInspector] _ZWrite ("__zw", Float) = 1.0
    47.     }
    48.  
    49.     CGINCLUDE
    50.         #define UNITY_SETUP_BRDF_INPUT MetallicSetup
    51.     ENDCG
    52.  
    53.     SubShader
    54.     {
    55.         Tags { "RenderType"="Opaque" "PerformanceChecks"="False" }
    56.         LOD 300
    57.  
    58.  
    59.         // ------------------------------------------------------------------
    60.         //  Base forward pass (directional light, emission, lightmaps, ...)
    61.         Pass
    62.         {
    63.             Name "FORWARD"
    64.             Tags { "LightMode" = "ForwardBase" }
    65.  
    66.             Blend [_SrcBlend] [_DstBlend]
    67.             ZWrite [_ZWrite]
    68.  
    69.             CGPROGRAM
    70.             #pragma target 3.0
    71.  
    72.             // -------------------------------------
    73.  
    74.             #pragma shader_feature_local _NORMALMAP
    75.             #pragma shader_feature_local _ _ALPHATEST_ON _ALPHABLEND_ON _ALPHAPREMULTIPLY_ON
    76.             #pragma shader_feature _EMISSION
    77.             #pragma shader_feature_local _METALLICGLOSSMAP
    78.             #pragma shader_feature_local _DETAIL_MULX2
    79.             #pragma shader_feature_local _SMOOTHNESS_TEXTURE_ALBEDO_CHANNEL_A
    80.             #pragma shader_feature_local _SPECULARHIGHLIGHTS_OFF
    81.             #pragma shader_feature_local _GLOSSYREFLECTIONS_OFF
    82.             #pragma shader_feature_local _PARALLAXMAP
    83.  
    84.             #pragma multi_compile_fwdbase
    85.             #pragma multi_compile_fog
    86.             #pragma multi_compile_instancing
    87.             // Uncomment the following line to enable dithering LOD crossfade. Note: there are more in the file to uncomment for other passes.
    88.             //#pragma multi_compile _ LOD_FADE_CROSSFADE
    89.  
    90.             #pragma vertex vertBase
    91.             #pragma fragment fragBase
    92.             #include "UnityStandardCoreForward.cginc"
    93.  
    94.             ENDCG
    95.         }
    96.         // ------------------------------------------------------------------
    97.         //  Additive forward pass (one light per pass)
    98.         Pass
    99.         {
    100.             Name "FORWARD_DELTA"
    101.             Tags { "LightMode" = "ForwardAdd" }
    102.             Blend [_SrcBlend] One
    103.             Fog { Color (0,0,0,0) } // in additive pass fog should be black
    104.             ZWrite Off
    105.             ZTest LEqual
    106.  
    107.             CGPROGRAM
    108.             #pragma target 3.0
    109.  
    110.             // -------------------------------------
    111.  
    112.  
    113.             #pragma shader_feature_local _NORMALMAP
    114.             #pragma shader_feature_local _ _ALPHATEST_ON _ALPHABLEND_ON _ALPHAPREMULTIPLY_ON
    115.             #pragma shader_feature_local _METALLICGLOSSMAP
    116.             #pragma shader_feature_local _SMOOTHNESS_TEXTURE_ALBEDO_CHANNEL_A
    117.             #pragma shader_feature_local _SPECULARHIGHLIGHTS_OFF
    118.             #pragma shader_feature_local _DETAIL_MULX2
    119.             #pragma shader_feature_local _PARALLAXMAP
    120.  
    121.             #pragma multi_compile_fwdadd_fullshadows
    122.             #pragma multi_compile_fog
    123.             // Uncomment the following line to enable dithering LOD crossfade. Note: there are more in the file to uncomment for other passes.
    124.             //#pragma multi_compile _ LOD_FADE_CROSSFADE
    125.  
    126.             #pragma vertex vertAdd
    127.             #pragma fragment fragAdd
    128.             #include "UnityStandardCoreForward.cginc"
    129.  
    130.             ENDCG
    131.         }
    132.         // ------------------------------------------------------------------
    133.         //  Shadow rendering pass
    134.         Pass {
    135.             Name "ShadowCaster"
    136.             Tags { "LightMode" = "ShadowCaster" }
    137.  
    138.             ZWrite On ZTest LEqual
    139.  
    140.             CGPROGRAM
    141.             #pragma target 3.0
    142.  
    143.             // -------------------------------------
    144.  
    145.  
    146.             #pragma shader_feature_local _ _ALPHATEST_ON _ALPHABLEND_ON _ALPHAPREMULTIPLY_ON
    147.             #pragma shader_feature_local _METALLICGLOSSMAP
    148.             #pragma shader_feature_local _SMOOTHNESS_TEXTURE_ALBEDO_CHANNEL_A
    149.             #pragma shader_feature_local _PARALLAXMAP
    150.             #pragma multi_compile_shadowcaster
    151.             #pragma multi_compile_instancing
    152.             // Uncomment the following line to enable dithering LOD crossfade. Note: there are more in the file to uncomment for other passes.
    153.             //#pragma multi_compile _ LOD_FADE_CROSSFADE
    154.  
    155.             #pragma vertex vertShadowCaster
    156.             #pragma fragment fragShadowCaster
    157.  
    158.             #include "UnityStandardShadow.cginc"
    159.  
    160.             ENDCG
    161.         }
    162.         // ------------------------------------------------------------------
    163.         //  Deferred pass
    164.         Pass
    165.         {
    166.             Name "DEFERRED"
    167.             Tags { "LightMode" = "Deferred" }
    168.  
    169.             CGPROGRAM
    170.             #pragma target 3.0
    171.             #pragma exclude_renderers nomrt
    172.  
    173.  
    174.             // -------------------------------------
    175.  
    176.             #pragma shader_feature_local _NORMALMAP
    177.             #pragma shader_feature_local _ _ALPHATEST_ON _ALPHABLEND_ON _ALPHAPREMULTIPLY_ON
    178.             #pragma shader_feature _EMISSION
    179.             #pragma shader_feature_local _METALLICGLOSSMAP
    180.             #pragma shader_feature_local _SMOOTHNESS_TEXTURE_ALBEDO_CHANNEL_A
    181.             #pragma shader_feature_local _SPECULARHIGHLIGHTS_OFF
    182.             #pragma shader_feature_local _DETAIL_MULX2
    183.             #pragma shader_feature_local _PARALLAXMAP
    184.  
    185.             #pragma multi_compile_prepassfinal
    186.             #pragma multi_compile_instancing
    187.             // Uncomment the following line to enable dithering LOD crossfade. Note: there are more in the file to uncomment for other passes.
    188.             //#pragma multi_compile _ LOD_FADE_CROSSFADE
    189.  
    190.             #pragma vertex vertDeferred
    191.             #pragma fragment fragDeferred
    192.  
    193.             #include "UnityStandardCore.cginc"
    194.  
    195.             ENDCG
    196.         }
    197.  
    198.         // ------------------------------------------------------------------
    199.         // Extracts information for lightmapping, GI (emission, albedo, ...)
    200.         // This pass it not used during regular rendering.
    201.         Pass
    202.         {
    203.             Name "META"
    204.             Tags { "LightMode"="Meta" }
    205.  
    206.             Cull Off
    207.  
    208.             CGPROGRAM
    209.             #pragma vertex vert_meta
    210.             #pragma fragment frag_meta
    211.  
    212.             #pragma shader_feature _EMISSION
    213.             #pragma shader_feature_local _METALLICGLOSSMAP
    214.             #pragma shader_feature_local _SMOOTHNESS_TEXTURE_ALBEDO_CHANNEL_A
    215.             #pragma shader_feature_local _DETAIL_MULX2
    216.             #pragma shader_feature EDITOR_VISUALIZATION
    217.  
    218.             #include "UnityStandardMeta.cginc"
    219.             ENDCG
    220.         }
    221.     }
    222.  
    223.     SubShader
    224.     {
    225.         Tags { "RenderType"="Opaque" "PerformanceChecks"="False" }
    226.         LOD 150
    227.  
    228.         // ------------------------------------------------------------------
    229.         //  Base forward pass (directional light, emission, lightmaps, ...)
    230.         Pass
    231.         {
    232.             Name "FORWARD"
    233.             Tags { "LightMode" = "ForwardBase" }
    234.  
    235.             Blend [_SrcBlend] [_DstBlend]
    236.             ZWrite [_ZWrite]
    237.  
    238.             CGPROGRAM
    239.             #pragma target 2.0
    240.  
    241.             #pragma shader_feature_local _NORMALMAP
    242.             #pragma shader_feature_local _ _ALPHATEST_ON _ALPHABLEND_ON _ALPHAPREMULTIPLY_ON
    243.             #pragma shader_feature _EMISSION
    244.             #pragma shader_feature_local _METALLICGLOSSMAP
    245.             #pragma shader_feature_local _SMOOTHNESS_TEXTURE_ALBEDO_CHANNEL_A
    246.             #pragma shader_feature_local _SPECULARHIGHLIGHTS_OFF
    247.             #pragma shader_feature_local _GLOSSYREFLECTIONS_OFF
    248.             // SM2.0: NOT SUPPORTED shader_feature_local _DETAIL_MULX2
    249.             // SM2.0: NOT SUPPORTED shader_feature_local _PARALLAXMAP
    250.  
    251.             #pragma skip_variants SHADOWS_SOFT DIRLIGHTMAP_COMBINED
    252.  
    253.             #pragma multi_compile_fwdbase
    254.             #pragma multi_compile_fog
    255.  
    256.             #pragma vertex vertBase
    257.             #pragma fragment fragBase
    258.             #include "UnityStandardCoreForward.cginc"
    259.  
    260.             ENDCG
    261.         }
    262.         // ------------------------------------------------------------------
    263.         //  Additive forward pass (one light per pass)
    264.         Pass
    265.         {
    266.             Name "FORWARD_DELTA"
    267.             Tags { "LightMode" = "ForwardAdd" }
    268.             Blend [_SrcBlend] One
    269.             Fog { Color (0,0,0,0) } // in additive pass fog should be black
    270.             ZWrite Off
    271.             ZTest LEqual
    272.  
    273.             CGPROGRAM
    274.             #pragma target 2.0
    275.  
    276.             #pragma shader_feature_local _NORMALMAP
    277.             #pragma shader_feature_local _ _ALPHATEST_ON _ALPHABLEND_ON _ALPHAPREMULTIPLY_ON
    278.             #pragma shader_feature_local _METALLICGLOSSMAP
    279.             #pragma shader_feature_local _SMOOTHNESS_TEXTURE_ALBEDO_CHANNEL_A
    280.             #pragma shader_feature_local _SPECULARHIGHLIGHTS_OFF
    281.             #pragma shader_feature_local _DETAIL_MULX2
    282.             // SM2.0: NOT SUPPORTED shader_feature_local _PARALLAXMAP
    283.             #pragma skip_variants SHADOWS_SOFT
    284.  
    285.             #pragma multi_compile_fwdadd_fullshadows
    286.             #pragma multi_compile_fog
    287.  
    288.             #pragma vertex vertAdd
    289.             #pragma fragment fragAdd
    290.             #include "UnityStandardCoreForward.cginc"
    291.  
    292.             ENDCG
    293.         }
    294.         // ------------------------------------------------------------------
    295.         //  Shadow rendering pass
    296.         Pass {
    297.             Name "ShadowCaster"
    298.             Tags { "LightMode" = "ShadowCaster" }
    299.  
    300.             ZWrite On ZTest LEqual
    301.  
    302.             CGPROGRAM
    303.             #pragma target 2.0
    304.  
    305.             #pragma shader_feature_local _ _ALPHATEST_ON _ALPHABLEND_ON _ALPHAPREMULTIPLY_ON
    306.             #pragma shader_feature_local _METALLICGLOSSMAP
    307.             #pragma shader_feature_local _SMOOTHNESS_TEXTURE_ALBEDO_CHANNEL_A
    308.             #pragma skip_variants SHADOWS_SOFT
    309.             #pragma multi_compile_shadowcaster
    310.  
    311.             #pragma vertex vertShadowCaster
    312.             #pragma fragment fragShadowCaster
    313.  
    314.             #include "UnityStandardShadow.cginc"
    315.  
    316.             ENDCG
    317.         }
    318.  
    319.         // ------------------------------------------------------------------
    320.         // Extracts information for lightmapping, GI (emission, albedo, ...)
    321.         // This pass it not used during regular rendering.
    322.         Pass
    323.         {
    324.             Name "META"
    325.             Tags { "LightMode"="Meta" }
    326.  
    327.             Cull Off
    328.  
    329.             CGPROGRAM
    330.             #pragma vertex vert_meta
    331.             #pragma fragment frag_meta
    332.  
    333.             #pragma shader_feature _EMISSION
    334.             #pragma shader_feature_local _METALLICGLOSSMAP
    335.             #pragma shader_feature_local _SMOOTHNESS_TEXTURE_ALBEDO_CHANNEL_A
    336.             #pragma shader_feature_local _DETAIL_MULX2
    337.             #pragma shader_feature EDITOR_VISUALIZATION
    338.  
    339.             #include "UnityStandardMeta.cginc"
    340.             ENDCG
    341.         }
    342.     }
    343.  
    344.  
    345.     FallBack "VertexLit"
    346.     CustomEditor "StandardShaderGUI"
    347. }
    348.  
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,352
    The short version is you probably don't want to attempt to modify the built in shaders at all. They're way to complex.

    Surface Shaders exist to help with this, but you're probably better off buying the Amplify Shader Editor from the store if you want something that's more familiar to you coming from Unreal.