coloyf 2016-10-26 02:43 采纳率: 16.7%
浏览 10764

unity3d中加入shader后材质球的颜色变为黑色的问题

我想在unity中给cube添加轮廓线,从某个帖子中得知shader可以实现,还给了一段shader的代码,但是只要给材质球挂载上这个shader,轮廓是有了,但是材质球会变成黑色。麻烦各位大神给看看,是不是这个shader哪里写的有问题

 Shader "Custom/NewSurfaceShader" {
Properties {  
        //定义材质的颜色为白色  
        _Color ("Main Color", Color) = (1,1,1,1)  
        //定义材质的轮廓线为黑色  
        _OutlineColor ("Outline Color", Color) = (0,0,0,1)  //改变这个能改变轮廓边的颜色  
        //定义线宽  
        _Outline ("Outline width", Range (0.0, 0.03)) = 0.001   //改变这个能改变轮廓边的粗细  
        _MainTex ("Base (RGB)", 2D) = "white" { }  
    }  

CGINCLUDE  
#include "UnityCG.cginc"  

struct appdata {  
    float4 vertex : POSITION;  
    float3 normal : NORMAL;  
};  

struct v2f {  
    float4 pos : POSITION;  
    float4 color : COLOR;  
};  

uniform float _Outline;  
uniform float4 _OutlineColor;  

v2f vert(appdata v) {  
    // just make a copy of incoming vertex data but scaled according to normal direction  
    v2f o;  
    o.pos = mul(UNITY_MATRIX_MVP, v.vertex);  

    float3 norm   = mul ((float3x3)UNITY_MATRIX_IT_MV, v.normal);  
    float2 offset = TransformViewToProjection(norm.xy);  

    o.pos.xy += offset * o.pos.z * _Outline;  
    o.color = _OutlineColor;  
    return o;  
}  
ENDCG  

    SubShader {  
        Tags { "Queue" = "Transparent" }  

        // note that a vertex shader is specified here but its using the one above  
        Pass {  
            Name "OUTLINE"  
            Tags { "LightMode" = "Always" }  
            Cull Off  
            ZWrite Off  
            ZTest Always  
            ColorMask RGB // alpha not used  

            // you can choose what kind of blending mode you want for the outline  
            Blend SrcAlpha OneMinusSrcAlpha // Normal  
            //Blend One One // Additive  
            //Blend One OneMinusDstColor // Soft Additive  
            //Blend DstColor Zero // Multiplicative  
            //Blend DstColor SrcColor // 2x Multiplicative  

CGPROGRAM  
#pragma vertex vert  
#pragma fragment frag  

half4 frag(v2f i) :COLOR {  
    return i.color;  
}  
ENDCG  
        }  

        Pass {  
            Name "BASE"  
            ZWrite On  
            ZTest LEqual  
            Blend SrcAlpha OneMinusSrcAlpha  
            Material {  
                Diffuse [_Color]  
                Ambient [_Color]  
            }  
            Lighting On  
            SetTexture [_MainTex] {  
                ConstantColor [_Color]  
                Combine texture * constant  
            }  
            SetTexture [_MainTex] {  
                Combine previous * primary DOUBLE  
            }  
        }  
    }  

    SubShader {  
        Tags { "Queue" = "Transparent" }  

        Pass {  
            Name "OUTLINE"  
            Tags { "LightMode" = "Always" }  
            Cull Front  
            ZWrite Off  
            ZTest Always  
            ColorMask RGB  

            // you can choose what kind of blending mode you want for the outline  
            Blend SrcAlpha OneMinusSrcAlpha // Normal  
            //Blend One One // Additive  
            //Blend One OneMinusDstColor // Soft Additive  
            //Blend DstColor Zero // Multiplicative  
            //Blend DstColor SrcColor // 2x Multiplicative  

            CGPROGRAM  
            #pragma vertex vert  
            #pragma exclude_renderers gles xbox360 ps3  
            ENDCG  
            SetTexture [_MainTex] { combine primary }  
        }  

        Pass {  
            Name "BASE"  
            ZWrite On  
            ZTest LEqual  
            Blend SrcAlpha OneMinusSrcAlpha  
            Material {  
                Diffuse [_Color]  
                Ambient [_Color]  
            }  
            Lighting On  
            SetTexture [_MainTex] {  
                ConstantColor [_Color]  
                Combine texture * constant  
            }  
            SetTexture [_MainTex] {  
                Combine previous * primary DOUBLE  
            }  
        }  
    }  

    Fallback "Diffuse"  
}  

图片说明

  • 写回答

2条回答

  • zqbnqsdsmd 2016-10-27 16:52
    关注
    评论

报告相同问题?

悬赏问题

  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码