unity3d 渐变

2018-06-13 19:08:22 weixin_42452001 阅读数 1205
  • Unity 值得看的500+ 技术内容列表

    Unity3D是由Unity Technologies开发的一个让玩家轻松创建诸如三维视频游戏、建筑可视化、实时三维动画等类型互动内容的多平台的综合型游戏开发工具,是一个全面整合的专业游戏引擎。

Shader "Hidden/Test"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
_FadeNear("FadeNear",float) = 35
_FadeFar("FadeFar",float) = 40
}
SubShader
{
Tags{"RenderType" = "Transparent"}
ZWrite On
Blend SrcAlpha OneMinusSrcAlpha


Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag


#include "UnityCG.cginc"
sampler2D _MainTex;
float4 _MainTex_ST;
float _FadeNear;
float _FadeFar;
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};


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


v2f vert (appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = TRANSFORM_TEX(v.uv,_MainTex);
float3 ViewDir = mul(UNITY_MATRIX_MV, v.vertex).xyz;
float dir = length(ViewDir);
o.fade = 1 - saturate((dir - _FadeNear) / (_FadeFar - _FadeNear));


return o;
}




fixed4 frag (v2f i) : SV_Target
{
fixed4 col = tex2D(_MainTex, i.uv);

return float4 (col.rgb,i.fade);
}
ENDCG
}
}

}

2016-11-07 11:51:00 weixin_30307921 阅读数 0
  • Unity 值得看的500+ 技术内容列表

    Unity3D是由Unity Technologies开发的一个让玩家轻松创建诸如三维视频游戏、建筑可视化、实时三维动画等类型互动内容的多平台的综合型游戏开发工具,是一个全面整合的专业游戏引擎。

研究了下,颜色变化时遵从RGB圆环(就是说颜色条对接一下成环)路径最小变化原则。举个例子,加入ABCDE四个颜色点,并且A和E在同一点(转了一圈)设定从A变化到C的话,有两种路径,一种ABC,还一种ADC,究竟选择哪条路径 就看这两条路径哪个更短些了

1
2
3
4
5
6
7
8
9
10
11
12
13
var colorStart = Color.red;
var colorEnd = Color.green;
var duration = 1.0;
 
var minimum = 0.0;
var maximum = 200.0;
 
 
function Update () {
    var lerp = Mathf.PingPong (Time.time, duration) / duration;
    renderer.material.color = Color.Lerp (colorStart, colorEnd, lerp);
   transform.position.x = Mathf.Lerp(minimum, maximum, lerp);
}

转载于:https://www.cnblogs.com/android-blogs/p/6038226.html

2018-04-28 11:25:50 Cattleya_ 阅读数 1803
  • Unity 值得看的500+ 技术内容列表

    Unity3D是由Unity Technologies开发的一个让玩家轻松创建诸如三维视频游戏、建筑可视化、实时三维动画等类型互动内容的多平台的综合型游戏开发工具,是一个全面整合的专业游戏引擎。

基于unity3d的简单脚本,代码很少,就不废话了,直接上代码和效果图。

效果图:


using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

namespace ExtraFoundation.Components
{
    [AddComponentMenu("UI/Effects/Gradient")]
    public class UIGradient : BaseMeshEffect
    {
        #region Public Declarations
        public enum Type
        {
            Vertical,
            Horizontal
        }
        #endregion

        #region Public Properties
        public Type GradientType = Type.Vertical;
        [Range(-1f, 1f)]
        public float Offset = 0f;
        public Gradient gradient;
        #endregion

        #region Public Methods
        public override void ModifyMesh(VertexHelper helper)
        {
            if (!IsActive() || helper.currentVertCount == 0)
            {
                return;
            }

            vertexList.Clear();
            helper.GetUIVertexStream(vertexList);

            int nCount = vertexList.Count;
            switch (GradientType)
            {
                case Type.Vertical:
                    {
                        float fBottomY = vertexList[0].position.y;
                        float fTopY = vertexList[0].position.y;
                        float fYPos = 0f;

                        for (int i = nCount - 1; i >= 1; --i)
                        {
                            fYPos = vertexList[i].position.y;
                            if (fYPos > fTopY)
                                fTopY = fYPos;
                            else if (fYPos < fBottomY)
                                fBottomY = fYPos;
                        }

                        float fUIElementHeight = 1f / (fTopY - fBottomY);
                        UIVertex v = new UIVertex();

                        for (int i = 0; i < helper.currentVertCount; i++)
                        {
                            helper.PopulateUIVertex(ref v, i);
                            v.color = gradient.Evaluate((v.position.y - fBottomY) *
                                fUIElementHeight - Offset);
                            helper.SetUIVertex(v, i);
                        }
                    }
                    break;
                case Type.Horizontal:
                    {
                        float fLeftX = vertexList[0].position.x;
                        float fRightX = vertexList[0].position.x;
                        float fXPos = 0f;

                        for (int i = nCount - 1; i >= 1; --i)
                        {
                            fXPos = vertexList[i].position.x;
                            if (fXPos > fRightX)
                                fRightX = fXPos;
                            else if (fXPos < fLeftX)
                                fLeftX = fXPos;
                        }

                        float fUIElementWidth = 1f / (fRightX - fLeftX);
                        UIVertex v = new UIVertex();

                        for (int i = 0; i < helper.currentVertCount; i++)
                        {
                            helper.PopulateUIVertex(ref v, i);
                            v.color = gradient.Evaluate((v.position.x - fLeftX) *
                                fUIElementWidth - Offset);
                            helper.SetUIVertex(v, i);
                        }

                    }
                    break;
                default:
                    break;
            }
        }
        #endregion
        
        #region Internal Fields
        private List<UIVertex> vertexList = new List<UIVertex>();
        #endregion
    }
}
虽然支持的内容不多,但是小而精,希望对大家有用。
2019-05-29 14:44:04 weixin_41995872 阅读数 797
  • Unity 值得看的500+ 技术内容列表

    Unity3D是由Unity Technologies开发的一个让玩家轻松创建诸如三维视频游戏、建筑可视化、实时三维动画等类型互动内容的多平台的综合型游戏开发工具,是一个全面整合的专业游戏引擎。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class NewBehaviourScript2 : BaseMeshEffect
{

    private enum Direction
    {
        Vertical,
        Horizontal,
    }
    [SerializeField]
    private Direction mDirection = Direction.Vertical;
    [SerializeField]
    private Color color = Color.white;
    [SerializeField]
    private bool isMid = true;
    public override void ModifyMesh(VertexHelper vh)
    {
        if (!IsActive())
            return;


        if (vh.currentVertCount == 0)
            return;

        if (isMid)
        {
            List<UIVertex> vlist = new List<UIVertex>();
            for (int i = 0; i < vh.currentVertCount; i += 4)
            {
                UIVertex ver0 = new UIVertex();
                UIVertex ver1 = new UIVertex();
                UIVertex ver3 = new UIVertex();
                UIVertex ver4 = new UIVertex();
                vh.PopulateUIVertex(ref ver0, i + 0);
                vh.PopulateUIVertex(ref ver1, i + 1);
                vh.PopulateUIVertex(ref ver3, i + 2);
                vh.PopulateUIVertex(ref ver4, i + 3);


                UIVertex ver2 = new UIVertex();
                ver2.position = (ver1.position + ver3.position) / 2;
                ver2.uv0 = (ver1.uv0 + ver3.uv0) / 2;
                ver2.color = color;
                ver2.normal = ver0.normal;
                ver2.tangent = ver0.tangent;


                UIVertex ver5 = new UIVertex();
                ver5.position = (ver0.position + ver4.position) / 2;
                ver5.uv0 = (ver0.uv0 + ver4.uv0) / 2;
                ver5.color = color;
                ver5.normal = ver0.normal;
                ver5.tangent = ver0.tangent;


                vlist.Add(ver0);
                vlist.Add(ver1);
                vlist.Add(ver2);
                vlist.Add(ver3);
                vlist.Add(ver4);
                vlist.Add(ver5);
            }


            List<int> tlist = new List<int>();
            for (int i = 0; i < vlist.Count; i += 6)
            {
                tlist.Add(i + 0);
                tlist.Add(i + 1);
                tlist.Add(i + 2);


                tlist.Add(i + 0);
                tlist.Add(i + 2);
                tlist.Add(i + 5);


                tlist.Add(i + 2);
                tlist.Add(i + 3);
                tlist.Add(i + 4);


                tlist.Add(i + 2);
                tlist.Add(i + 4);
                tlist.Add(i + 5);
            }


            vh.Clear();
            vh.AddUIVertexStream(vlist, tlist);
        }
        else
        {
            List<UIVertex> vlist = new List<UIVertex>();
            vh.GetUIVertexStream(vlist);


            if (vlist == null || vlist.Count == 0)
                return;


            Color startColr = vlist[0].color;


            float leftX = vlist[0].position.x;
            float rightX = vlist[0].position.x;


            float bottomY = vlist[0].position.y;
            float topY = vlist[0].position.y;


            for (int i = 1; i < vlist.Count; i++)
            {
                float y = vlist[i].position.y;
                if (y > topY)
                {
                    topY = y;
                }
                else if (y < bottomY)
                {
                    bottomY = y;
                }


                float x = vlist[i].position.x;
                if (x > rightX)
                {
                    rightX = x;
                }
                else if (x < leftX)
                {
                    leftX = x;
                }
            }


            for (int i = 0; i < vlist.Count; i++)
            {
                UIVertex ver = vlist[i];
                float t = 0f;
                if (mDirection == Direction.Vertical)
                {
                    t = (ver.position.y - bottomY) / (topY - bottomY);
                }
                else
                {
                    t = (ver.position.x - leftX) / (rightX - leftX);
                }
                ver.color = Color.Lerp(startColr, color, t);
                vlist[i] = ver;
            }


            vh.Clear();
            vh.AddUIVertexTriangleStream(vlist);
        }
    }
}

 

2014-10-16 13:12:41 u013785119 阅读数 0
  • Unity 值得看的500+ 技术内容列表

    Unity3D是由Unity Technologies开发的一个让玩家轻松创建诸如三维视频游戏、建筑可视化、实时三维动画等类型互动内容的多平台的综合型游戏开发工具,是一个全面整合的专业游戏引擎。

【1】

Shader "Custom/NewShader" {
    SubShader {
        Pass {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            #pragma target 3.0
            #include "UnityCG.cginc"
            struct vertexInput {
                float4 vertex : POSITION;
                float4 srcPos : TEXCOORD0;
            };

            struct fragmentInput{
                float4 position : SV_POSITION;
                float4 srcPos : TEXCOORD0;
            };
            
            fragmentInput vert(vertexInput i){
                fragmentInput o;
                o.position = mul (UNITY_MATRIX_MVPi.vertex);
                o.srcPos = i.srcPos;
                return o;
            }
            float4 frag(fragmentInput i) : COLOR {
                return float4(i.srcPos.xy,0.0,1.0);
            }
            ENDCG
        }
    }
}


原点:左下

【2】

   Shader "Custom/another" {
    SubShader {
        Pass {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            #pragma target 3.0
            #include "UnityCG.cginc"
            float4 vert(appdata_base v) : POSITION {
                return mul (UNITY_MATRIX_MVPv.vertex);
            }

            fixed4 frag(float4 sp:WPOS) : COLOR {
                return fixed4(sp.xy/_ScreenParams.xy,0.0,1.0);
            }

            ENDCG
        }
    }
}


原点:右上

【3】

Shader "Custom/rightUp" {
    Properties {
        _MainTex ("MainTex"2D) = "white"
    }
    SubShader {
        Pass {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            #pragma target 3.0

            #include "UnityCG.cginc"
        sampler2D _MainTex;
        half4 _MainTex_ST;
        struct vertOut {    
            float4 pos:SV_POSITION;    
            float4 srcPosTEXCOORD0;   
        };  
  
        vertOut vert(appdata_base v) {  
            vertOut o;  
            o.pos = mul (UNITY_MATRIX_MVPv.vertex);  
            o.srcPos.xy = TRANSFORM_TEX(v.texcoord_MainTex); 
            return o;  
        }  
            float4 frag(vertOut i) : COLOR {
            float2 uv = (i.srcPos.xy);
            return float4(uv,0.0,1.0);
            }
            ENDCG
        }
    }
}

原点:右上

【4】

Shader "Custom/rightDown" {
    Properties {
        _MainTex ("MainTex"2D) = "white"
    }
    SubShader {
        Pass {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            #pragma target 3.0

            #include "UnityCG.cginc"
        sampler2D _MainTex;
        half4 _MainTex_ST;
        struct vertOut {    
            float4 pos:SV_POSITION;    
            float4 srcPosTEXCOORD0;   
        };  
  
        vertOut vert(appdata_base v) {  
            vertOut o;  
            o.pos = mul (UNITY_MATRIX_MVPv.vertex);  
            o.srcPos = ComputeScreenPos(o.pos); 
            return o;  
        }  
            float4 frag(vertOut i) : COLOR {
            float2 uv = (i.srcPos.xy/i.srcPos.w);
            return float4(uv,0.0,1.0);
            }
            ENDCG
        }
    }
}


原点:左下