将光滑度和金属质与碎片着色器相结合

时间:2018-02-23 11:20:46

标签: unity3d textures shader fragment-shader vertex-shader

我正在尝试将一些Smoothness和Metallic添加到我的自定义着色器中,以便在日/夜循环中使用深色纹理。

目前,我有一个程序行星,我的着色器根据高度(行星中心与表面顶点之间的距离)绘制不同的纹理。

直到那里,它才有效!

现在我真的需要为材质添加Smoothness和Metallic,但是使用我的着色器,我的程序行星的纹理是黑色的,不知道为什么?我有一个fra的通行证,然后是一个平滑和金属的冲浪功能。为什么这不起作用?

然而,当我看到材料图标并且我改变它看起来很好但是无论我改变的颜色和我设置的颜色,程序行星总是黑色。

太久以来无法解决这个问题。请需要一些帮助。

提前致谢。

这是我的着色器:

 Shader "Custom/Planet" {
 Properties {
     _Color ("Color", Color) = (1,1,1,1)
     _MainTex ("Albedo (RGB)", 2D) = "white" {}
     _Tex0 ("Tex 0", 2D) = "white" {}
     _Tex1 ("Tex 1", 2D) = "white" {}
     _Tex2 ("Tex 2", 2D) = "white" {}
     _Tex3 ("Tex 3", 2D) = "white" {}
     _Tex4 ("Tex 4", 2D) = "white" {}

     _Blend0to1and1to2 ("Blend between 0 and 1, 1 and 2", Vector) = (0,1,2,3)
     _Blend2to3and3to4 ("Blend between 2 and 3, 3 and 4", Vector) = (0,1,2,3)
     _Glossiness ("Smoothness", Range(0,1)) = 0.5
     _Metallic ("Metallic", Range(0,1)) = 0.0
 }
 SubShader {
     Tags { "RenderType"="Opaque" }
     LOD 200

     Pass
      {
         Blend SrcAlpha OneMinusSrcAlpha

         CGPROGRAM

                 #pragma vertex vert
                 #pragma fragment frag

                 #include "UnityCG.cginc"
                 #pragma target 3.0

                 sampler2D _Tex0;
                 sampler2D _Tex1;
                 sampler2D _Tex2;
                 sampler2D _Tex3;
                 sampler2D _Tex4;

                 float4 _Blend0to1and1to2;
                 float4 _Blend2to3and3to4;

                 uniform float4 _Tex0_ST;
                 uniform float4 _Tex1_ST;
                 uniform float4 _Tex2_ST;
                 uniform float4 _Tex3_ST;



                 struct appData {

                     float4 vertex : POSITION;
                     float2 uv1 : TEXCOORD0;
                     float2 uv2 : TEXCOORD1;
                     float2 uv3 : TEXCOORD2;
                     float2 uv4 : TEXCOORD3;

                 };

                 struct v2f {
                     float4 pos : SV_POSITION;
                     float2 uv1 : TEXCOORD0;
                     float2 uv2 : TEXCOORD1;
                     float2 uv3 : TEXCOORD2;
                     float2 uv4 : TEXCOORD3;
                     float4 col : COLOR;

                 };
                       #define PI 3.141592653589793

                  inline float2 RadialCoords(float3 a_coords)
                  {
                      float3 a_coords_n = normalize(a_coords);
                      float lon = atan2(a_coords_n.z, a_coords_n.x);
                      float lat = acos(a_coords_n.y);
                      float2 sphereCoords = float2(lon, lat) * (1.0 / PI);
                      return float2(sphereCoords.x * 0.5 + 0.5, 1 - sphereCoords.y);

                  }
                 v2f vert (appData vInput) {
                     v2f output;
                     output.pos = UnityObjectToClipPos (vInput.vertex);
                     output.uv1 = TRANSFORM_TEX(vInput.uv1, _Tex0);
                     output.uv2 = TRANSFORM_TEX(vInput.uv2, _Tex1);
                      output.uv3 = TRANSFORM_TEX(vInput.uv3, _Tex2);
                       output.uv4 = TRANSFORM_TEX(vInput.uv4, _Tex3);
                     output.col = length(vInput.vertex);
                     return output;
                 }

                  half4 frag (v2f fInput) : COLOR {

                 half4 c0 = tex2D (_Tex0, fInput.uv1);


                 half4 c1 = tex2D (_Tex1, fInput.uv2);


                 half4 c2 = tex2D (_Tex2, fInput.uv3);

                 half4 c3 = tex2D (_Tex3, fInput.uv4);
                 half4 c4 = tex2D (_Tex4, fInput.uv1);

                 if (fInput.col.x < _Blend0to1and1to2.x) {

                     return c0;
                 }
                 if (fInput.col.x > _Blend0to1and1to2.x  && fInput.col.x < _Blend0to1and1to2.y) {
                     return lerp(c0,c1,((fInput.col.x - _Blend0to1and1to2.x)/(_Blend0to1and1to2.y-_Blend0to1and1to2.x)));
                 }
                 if (fInput.col.x > _Blend0to1and1to2.y  && fInput.col.x < _Blend0to1and1to2.z) {
                     return c1;
                 }
                 if (fInput.col.x > _Blend0to1and1to2.z  && fInput.col.x < _Blend0to1and1to2.w) {
                     return lerp(c1,c2,((fInput.col.x - _Blend0to1and1to2.z)/(_Blend0to1and1to2.w-_Blend0to1and1to2.z)));
                 }
                 if (fInput.col.x > _Blend0to1and1to2.w  && fInput.col.x < _Blend2to3and3to4.x) {
                     return c2;
                 }
                 if (fInput.col.x > _Blend2to3and3to4.x  && fInput.col.x < _Blend2to3and3to4.y) {
                     return lerp(c2,c3,((fInput.col.x - _Blend2to3and3to4.x)/(_Blend2to3and3to4.y-_Blend2to3and3to4.x)));
                 }
                 if (fInput.col.x > _Blend2to3and3to4.y  && fInput.col.x < _Blend2to3and3to4.z) {
                     return c3;
                 }
                 if (fInput.col.x > _Blend2to3and3to4.z  && fInput.col.x < _Blend2to3and3to4.w) {
                     return lerp(c3,c4,((fInput.col.x - _Blend2to3and3to4.z)/(_Blend2to3and3to4.w-_Blend2to3and3to4.z)));
                 }
                 return c4;
             }

         ENDCG
      }

     CGPROGRAM
     // Physically based Standard lighting model, and enable shadows on all light types
     #pragma surface surf Standard fullforwardshadows

     // Use shader model 3.0 target, to get nicer looking lighting
     #pragma target 3.0

     sampler2D _Tex1;

     struct Input {
         float2 uv_MainTex;
     };

     half _Glossiness;
     half _Metallic;
     fixed4 _Color;

     void surf (Input IN, inout SurfaceOutputStandard o) {
         // Albedo comes from a texture tinted by color
         fixed4 c = tex2D (_Tex1, IN.uv_MainTex) * _Color;
         o.Albedo = c.rgb;
         // Metallic and smoothness come from slider variables
         o.Metallic = _Metallic;
         o.Smoothness = _Glossiness;
         o.Alpha = c.a;
     }
     ENDCG
 }
 FallBack "Diffuse"

}

0 个答案:

没有答案