Unity 自定义standard shader

可以对standard shader 做一些自定义的扩展

下载Unity官方对应版本的shader
在这里插入图片描述
复制 Standard -> CustomStandard
Unity 自定义standard shader_第1张图片
这个文件里有其他pass的frag着色器,复制粘贴到CustomStandardCoreForward就好了,或者再建一个文件 include一下,方法同CustomStandardCoreForward
Unity 自定义standard shader_第2张图片

目录结构
在这里插入图片描述
CustomStandardCoreForward.cginc

#ifndef CUSTOM_STANDARD_CORE_FORWARD_INCLUDED
#define CUSTOM_STANDARD_CORE_FORWARD_INCLUDED

#include "UnityStandardConfig.cginc"
#include "UnityStandardCore.cginc"

half4 fragCustomInternal(VertexOutputForwardBase i)
{
    UNITY_APPLY_DITHER_CROSSFADE(i.pos.xy);

    FRAGMENT_SETUP(s)

    UNITY_SETUP_INSTANCE_ID(i);
    UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(i);

    UnityLight mainLight = MainLight ();
    UNITY_LIGHT_ATTENUATION(atten, i, s.posWorld);

    half occlusion = Occlusion(i.tex.xy);
    UnityGI gi = FragmentGI (s, occlusion, i.ambientOrLightmapUV, atten, mainLight);
    
    half4 c = UNITY_BRDF_PBS (s.diffColor, s.specColor, s.oneMinusReflectivity, s.smoothness, s.normalWorld, -s.eyeVec, gi.light, gi.indirect);
    c.rgb += Emission(i.tex.xy);

    UNITY_EXTRACT_FOG_FROM_EYE_VEC(i);
    UNITY_APPLY_FOG(_unity_fogCoord, c.rgb);
    return OutputForward (c, s.alpha);
}

half4 fragCustom (VertexOutputForwardBase i) : SV_Target   // backward compatibility (this used to be the fragment entry function)
{
    return fragCustomInternal(i);
}


#endif // CUSTOM_STANDARD_CORE_FORWARD_INCLUDED

  1. include CustomStandardCoreForward
  2. 修改 #pragma fragment fragCustom

CustomStandard.shader

  // .........................
  // ------------------------------------------------------------------
  //  Base forward pass (directional light, emission, lightmaps, ...)
   Pass
   {
       // ..................

       #pragma vertex vertBase
       #pragma fragment fragCustom
       #include "UnityStandardCoreForward.cginc"
       #include "CustomStandardCoreForward.cginc"

       ENDCG
   }
   //................................

然后直接在 fragCustomInternal里编写相应的操作就可以了

最后记得把CustomStandard里的最后一行注释掉,或者重写一下,否则会不显示部分 propertie

CustomEditor "CustomShaderGUI"

自定义Material编辑器的时候,每添加一个propertie,就需要添加几行代码
然后 FindProper,再在ShaderPropertiesGUI里添加set…
Unity 自定义standard shader_第3张图片
文件参考
Unity 自定义standard shader_第4张图片

你可能感兴趣的:(unity3d)