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

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
    关注
    评论

报告相同问题?

悬赏问题

  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100