欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

UnityShader3實現(xiàn)轉圈與冷卻效果

 更新時間:2019年03月03日 10:06:40   作者:宏哥1995  
這篇文章主要為大家詳細介紹了UnityShader3實現(xiàn)轉圈與冷卻效果,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了UnityShader3實現(xiàn)轉圈與冷卻效果的具體代碼,供大家參考,具體內(nèi)容如下

參考鏈接:OpenGL Shader實例分析(3)等待標識效果

一、轉圈效果

效果圖:

如何實現(xiàn)一個圓繞中心點運動呢?原理很簡單,就是隨著時間的流逝,起始邊固定,而另一條邊不斷地移動,弧度從0到2*PI,只需求出移動邊與圓邊的交點,然后畫圓即可。至于這個交點,因為圓心的uv為(0.5,0.5),所以交點的坐標就是(0.5 - r * cos(a) , 0.5 + r * sin(a))。

Shader "Custom/Loading"
{
 Properties
 {
 _Color ("Color", Color) = (0, 1, 0, 1)
 _Speed ("Speed", Range(1, 10)) = 1
 _Radius ("Radius", Range(0, 0.5)) = 0.3
 }
 SubShader
 {
 Tags { "Queue" = "Transparent" }
 Blend SrcAlpha OneMinusSrcAlpha
 ZWrite Off
 
 Pass
 {
 CGPROGRAM
 #pragma vertex vert
 #pragma fragment frag
 #include "UnityCG.cginc"
 
 #define PI 3.14159
 
 struct appdata
 {
 float4 vertex : POSITION;
 float2 uv : TEXCOORD0;
 };
 
 struct v2f
 { 
 float4 vertex : SV_POSITION;
 float2 uv : TEXCOORD0;
 };
 
 fixed4 _Color;
 half _Speed;
 fixed _Radius;
 
 fixed4 circle(float2 uv, float2 center, float radius)
 {
 //if(pow(uv.x - center.x, 2) + pow(uv.y - center.y, 2) < pow(radius, 2)) return _Color;
 
 if(length(uv - center) < radius) return _Color;
 else return fixed4(0, 0, 0, 0); 
 }
 
 v2f vert (appdata v)
 {
 v2f o;
 o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
 o.uv = v.uv;
 
 return o;
 }
 
 fixed4 frag (v2f i) : SV_Target
 {
 fixed4 finalCol = (0, 0, 0, 0);
 
 for(int count = 7; count > 1; count--)
 {
  half radian = fmod(_Time.y * _Speed + count * 0.5, 2 * PI);//弧度
  half2 center = half2(0.5 - _Radius * cos(radian), 0.5 + _Radius * sin(radian)); 
 
  finalCol += circle(i.uv, center, count * 0.01);
 }
 
 return finalCol;
 }
 ENDCG
 }
 }
}

二.冷卻效果

效果圖:

參考上面那張原理圖,稍加修改就可以了。

Shader "Custom/Cooling"
{
 Properties
 {
 _MainTex ("Texture", 2D) = "white" {}
 _Speed ("Speed", Range(1, 10)) = 1
 _Color ("Color", Color) = (0, 0, 0, 1)
 }
 SubShader
 { 
 Pass
 {
 CGPROGRAM
 #pragma vertex vert
 #pragma fragment frag
 #include "UnityCG.cginc"
 
 #define PI 3.142
 
 struct appdata
 {
 float4 vertex : POSITION;
 float2 uv : TEXCOORD0;
 };
 
 struct v2f
 { 
 float4 vertex : SV_POSITION;
 float2 uv : TEXCOORD0;
 };
 
 sampler2D _MainTex;
 float4 _MainTex_ST;
 half _Speed;
 fixed4 _Color;
 
 v2f vert (appdata v)
 {
 v2f o;
 o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
 o.uv = TRANSFORM_TEX(v.uv, _MainTex);
 return o;
 }
 
 fixed4 frag (v2f i) : SV_Target
 {
 fixed4 col = tex2D(_MainTex, i.uv);
 
 //以正中間為中心,所以將uv范圍映射到(-0.5, 0.5)
 float2 uv = i.uv - float2(0.5, 0.5);
 //atan2(y, x):反正切,y/x的反正切范圍在[-π, π]內(nèi)
 //-1用于控制方向
 float radian = atan2(uv.y, uv.x) * -1 + PI;
 
 float2 radian2 = fmod(_Time.y * _Speed, 2 * PI);
 fixed v = step(radian, radian2);
 
 if(v > 0) return col;
 else return col * _Color;
 }
 ENDCG
 }
 }
}

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

最新評論