圆柱体多段上色的方法 VR资源

仅为年时 2017-09-26 18:33:58

 

想着是钻孔数据用三维柱状体显示,结果就做出这么个金箍棒。

没有C#代码,简单shader可行。

将参数显示出以便于程序控制,所以说需要定义属性:

 
01
02
03
04
05
06
07
08
09
10
Properties{
        _ColorRange1("Color1 Range", Range(0, 1)) = 0.3
        _ColorRange2("Color2 Range", Range(0, 1)) = 0.6
        _ColorRange3("Color3 Range", Range(0, 1)) = 0.7
        _ColorRange4("Color4 Range", Range(0, 1)) = 0.8
        _Color1("color1", Color) = (1.0, 0.0, 0.0, 1.0)//红
        _Color2("color2", Color) = (0.0, 1.0, 0.0, 1.0)//绿
        _Color3("color3", Color) = (0.0, 0.0, 1.0, 1.0)//蓝
        _Color4("color4", Color) = (1.0, 1.0, 1.0, 1.0)//白
        _Color5("color5", Color) = (0.0, 0.0, 0.0, 1.0)//黑
    }

图:
 



剩下的基本上就是片段着色器的位置和颜色的控制,if-else足够实现.
代码:
 
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
Shader "Custom/aaa" {
    Properties{
        _ColorRange1("Color1 Range", Range(0, 1)) = 0.3
        _ColorRange2("Color2 Range", Range(0, 1)) = 0.6
        _ColorRange3("Color3 Range", Range(0, 1)) = 0.7
        _ColorRange4("Color4 Range", Range(0, 1)) = 0.8
        _Color1("color1", Color) = (1.0, 0.0, 0.0, 1.0)//红
        _Color2("color2", Color) = (0.0, 1.0, 0.0, 1.0)//绿
        _Color3("color3", Color) = (0.0, 0.0, 1.0, 1.0)//蓝
        _Color4("color4", Color) = (1.0, 1.0, 1.0, 1.0)//白
        _Color5("color5", Color) = (0.0, 0.0, 0.0, 1.0)//黑
    }
        SubShader{
            Pass{
            CGPROGRAM
    #pragma vertex vert
    #pragma fragment frag
    #include "UnityCG.cginc"
 
        float _ColorRange1;
        float _ColorRange2;
        float _ColorRange3;
        float _ColorRange4;
        fixed4 _Color1;
        fixed4 _Color2;
        fixed4 _Color3;
        fixed4 _Color4;
        fixed4 _Color5;
 
        //输入顶点结构体,包含位置和颜色
    struct vertexInput {
        float4 vertex : POSITION;
        float4 texcoord0 : TEXCOORD0;
    };
    //片段结构体,包含位置和颜色
    struct fragmentInput{
        float4 position : SV_POSITION;
        float4 texcoord0 : TEXCOORD0;
    };
    //顶点处理
    fragmentInput vert(vertexInput i){
        fragmentInput o;
        o.position = mul (UNITY_MATRIX_MVP, i.vertex);
        o.texcoord0 = i.texcoord0;
        return o;
    }
    //片段处理
    float4 frag(fragmentInput i) : COLOR {
        float4 color;
        if (i.texcoord0.y > 0 && i.texcoord0.y < _ColorRange1) {
            color = _Color1;//红色
        }
        if (i.texcoord0.y > _ColorRange1 && i.texcoord0.y < _ColorRange2) {
            color = _Color2;//绿色
        }
        if(i.texcoord0.y > _ColorRange2 && i.texcoord0.y < _ColorRange3){
            color = _Color3;//蓝色
        }
        if (i.texcoord0.y > _ColorRange3 && i.texcoord0.y < _ColorRange4) {
            color = _Color4;//白色
        }
        if (i.texcoord0.y > _ColorRange4 && i.texcoord0.y < 1) {
            color = _Color5;//黑色
        }
            return color;
    }
        ENDCG
    }
    }
        FallBack "Diffuse"
}
 
99VR视界二维码
热门推荐
Hot Recommended
在线客服