Unity 3D柱状图效果

1.单个柱状效果展示

2.从上到下渐变透明材质Shader

cs 复制代码
Shader "Unlit/NewUnlitShader"
{
    Properties
    {
        _MainTex ("Texture", 2D) = "white" {}
        _Color("Color",Color) = (1,1,1,1)
        _Alpha("Alpha",Range(0,1) ) = 0.2
        _Alpha2("Alpha2",Range(0,1) ) = 0.2
        _Power("Power",range(0,10)) = 1
    }
    SubShader
    {
              Pass
        {
            Tags { "RenderType"="Transparent" }
            Cull Front
            Blend SrcAlpha One
            
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag

            #include "UnityCG.cginc"

            struct appdata
            {
                float4 vertex : POSITION;
                float2 uv : TEXCOORD0;
            };

            struct v2f
            {
                float2 uv : TEXCOORD0;
                float4 vertex : SV_POSITION;
            };

            sampler2D _MainTex;
            float4 _MainTex_ST;
            fixed4 _Color;
            float _Alpha2;
            
            v2f vert (appdata v)
            {
                v2f o;
                o.vertex = UnityObjectToClipPos(v.vertex);
                o.uv = TRANSFORM_TEX(v.uv, _MainTex);
                UNITY_TRANSFER_FOG(o,o.vertex);
                return o;
            }

            fixed4 frag (v2f i) : SV_Target
            {
                // sample the texture
                //fixed4 col = tex2D(_MainTex, i.uv);
                float v = i.uv.y;
                //float v = 1 - pow((1 - i.uv.y), 4);
                fixed4 c = fixed4(_Color.xyz ,v * _Alpha2);
                return c;
            }
            ENDCG
        }

        Pass
        {
            Tags { "RenderType"="Transparent" }
             Cull Back
            Blend SrcAlpha One 
            
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            // make fog work
            #pragma multi_compile_fog

            #include "UnityCG.cginc"

            struct appdata
            {
                float4 vertex : POSITION;
                float2 uv : TEXCOORD0;
            };

            struct v2f
            {
                float2 uv : TEXCOORD0;
                float4 vertex : SV_POSITION;
            };

            sampler2D _MainTex;
            float4 _MainTex_ST;
            fixed4 _Color;
            float _Alpha;


            v2f vert (appdata v)
            {
                v2f o;
                o.vertex = UnityObjectToClipPos(v.vertex);
                o.uv = TRANSFORM_TEX(v.uv, _MainTex);
                UNITY_TRANSFER_FOG(o,o.vertex);
                return o;
            }

            fixed4 frag (v2f i) : SV_Target
            {
                // sample the texture
                //fixed4 col = tex2D(_MainTex, i.uv);
                float y = i.uv.y;
                float v = 1 - pow(1 - (1 - y ), 4);
                fixed4 c = fixed4(_Color.xyz * _Alpha, (1 - v) *_Alpha);
                return c;
            }
            ENDCG
        }

      Pass
        {
            Tags { "RenderType"="Transparent" }
            Blend SrcAlpha One 
            
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            // make fog work
            #pragma multi_compile_fog

            #include "UnityCG.cginc"

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

            struct v2f
            {
                float2 uv : TEXCOORD0;
                float3 viewDir : TEXCOORD1;
                float4 vertex : SV_POSITION;
                float3 normal : normal;
            };

            sampler2D _MainTex;
            float4 _MainTex_ST;
            fixed4 _Color;
            float _Alpha;
            float _Power;

            v2f vert (appdata v)
            {
                v2f o;
                o.vertex = UnityObjectToClipPos(v.vertex);
                o.uv = TRANSFORM_TEX(v.uv, _MainTex);
                o.viewDir = ObjSpaceViewDir(v.vertex);
                o.normal = v.normal;
                UNITY_TRANSFER_FOG(o,o.vertex);
                return o;
            }


            fixed4 frag (v2f i) : SV_Target
            {
                // sample the texture
                float y2 = i.uv.y;
                float v = 1 - pow(1 - (1 - y2 ), 4);
                //float y =  i.uv.y +  _Time.y * _Speed;
                //float sy = (sin(_Time.y) + 1) / 2;
                float fresnel = pow(1 - saturate(dot(normalize(i.viewDir), normalize(i.normal))), _Power);
                for(int m = 0; m <= 10; m++)
                {
                        if(i.uv.y <= (m + 1) / 10  && i.uv.y >= m / 10)
                        {
                            return float4(_Color.rgb *_Alpha * fresnel,_Alpha * ((m + 1) / 10)) ;
                        }
                }
               
                return _Color * fresnel;
                // if(i.uv.y <= sy && i.uv.y >= sy - 0.1)
                // {
                //     return float4(_Color.rgb,_Alpha * 2) ;
                // }
                // else
                // {
                    
                //     fixed4 c = fixed4(_Color.xyz * _Alpha, (1 - v) *_Alpha);
                //     return c;
                // }
                //fixed4 col = tex2D(_MainTex, uv * _Alpha);

               
            }
            ENDCG
        }

    }
}

3.效果用例

unity 新建柱状体,赋予上面材质,调整合适参数。

相关推荐
晓13133 小时前
【Cocos Creator 2.x】篇——第二章 入门
javascript·游戏引擎
CG_MAGIC5 小时前
从光影到物理渲染:Substance Sampler 照片转材质
3d·材质·贴图·uv·建模教程·渲云渲染
nnsix5 小时前
Unity 贴图压缩格式 笔记
笔记·unity·贴图
ysn1111115 小时前
搭建状态同步框架的实践心得
unity·架构
jinxindeep16 小时前
PhysX-Omni:面向刚体、形变体与铰接体的统一仿真就绪 3D 生成
3d
一线灵17 小时前
Axmol:小众引擎的硬核逆袭
游戏引擎
weixin_4419400117 小时前
【Unity教程】使用vuforia创建简单的AR实例
unity·游戏引擎·ar
郝学胜-神的一滴17 小时前
[简化版 GAMES 101] 计算机图形学 12:可见性与 Z‑Buffer 深度缓存
unity·godot·图形渲染·three.js·opengl·unreal
归真仙人1 天前
【UE】LineTraceByProfile
ue5·游戏引擎·ue4·unreal engine
游乐码1 天前
Unity基础(十一 )资源同步加载
unity·游戏引擎