日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

偏移shaderuv_Unity Shader 之 uv动画

發布時間:2023/12/15 编程问答 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 偏移shaderuv_Unity Shader 之 uv动画 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Unity 動畫

Unity Shader 內置時間變量

引入時間變量

名稱

類型

描述

_Time

float4

t是自該場景加載開始所經過的時間,4個分量分別是(t/20, t, 2t, 3t)

_SinTime

float4

t是時間的正弦值,(t/8, t/4, t/2, t)

_CosTime

float4

t是時間的余弦值,(t/8, t/4, t/2, t)

unity_DeltaTime

float4

dt是時間增量,(dt, 1/dt, smoothDt, 1/smoothDt)

紋理動畫

使用一張紋理實現的動畫,用于代替復雜的粒子系統來模擬動畫效果。

u方向動畫與v方向動畫對比

序列幀動畫

代碼:

Shader "Unity Shaders Book/Chapter 11/Image Sequence Animation" {

Properties {

_Color ("Color Tint", Color) = (1, 1, 1, 1)

_MainTex ("Image Sequence", 2D) = "white" {}

_HorizontalAmount ("Horizontal Amount", Float) = 8

_VerticalAmount ("Vertical Amount", Float) = 8

_Speed ("Speed", Range(1, 100)) = 30

}

SubShader {

Tags {"Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent"}

Pass {

Tags { "LightMode"="ForwardBase" }

ZWrite Off

Blend SrcAlpha OneMinusSrcAlpha

CGPROGRAM

#pragma vertex vert

#pragma fragment frag

#include "UnityCG.cginc"

fixed4 _Color;

sampler2D _MainTex;

float4 _MainTex_ST;

float _HorizontalAmount;

float _VerticalAmount;

float _Speed;

struct a2v {

float4 vertex : POSITION;

float2 texcoord : TEXCOORD0;

};

struct v2f {

float4 pos : SV_POSITION;

float2 uv : TEXCOORD0;

};

v2f vert (a2v v) {

v2f o;

//將頂點坐標轉換到裁剪空間坐標系并且

o.pos = mul(UNITY_MATRIX_MVP, v.vertex);

//o.texcoord = v.texcoord.xy *_MainTex_ST.xy+_MainTex_ST.zw

//將紋理坐標映射到頂點上以及zw偏移

o.uv = TRANSFORM_TEX(v.texcoord, _MainTex);

return o;

}

fixed4 frag (v2f i) : SV_Target {

float time = floor(_Time.y * _Speed);

float row = floor(time / _HorizontalAmount); // /運算獲取當前行

float column = time - row * _HorizontalAmount; // %運算獲取當前列

//首先把原紋理坐標i.uv按行數和列數進行等分,然后使用當前的行列進行偏移

half2 uv = i.uv + half2(column, -row);

uv.x /= _HorizontalAmount;

uv.y /= _VerticalAmount;

//紋理采樣

fixed4 c = tex2D(_MainTex, uv);

c.rgb *= _Color;

return c;

}

ENDCG

}

}

//FallBack "Transparent/VertexLit"

}

效果:

滾動的背景

Shader "Unity Shaders Book/Chapter 11/Scrolling Background" {

Properties {

_MainTex ("Base Layer (RGB)", 2D) = "white" {}

_DetailTex ("2nd Layer (RGB)", 2D) = "white" {}

_ScrollX ("Base layer Scroll Speed", Float) = 1.0

_Scroll2X ("2nd layer Scroll Speed", Float) = 1.0

_Multiplier ("Layer Multiplier", Float) = 1

}

SubShader {

Tags { "RenderType"="Opaque" "Queue"="Geometry"}

Pass {

Tags { "LightMode"="ForwardBase" }

CGPROGRAM

#pragma vertex vert

#pragma fragment frag

#include "UnityCG.cginc"

sampler2D _MainTex;

sampler2D _DetailTex;

float4 _MainTex_ST;

float4 _DetailTex_ST;

float _ScrollX;

float _Scroll2X;

float _Multiplier;

struct a2v {

float4 vertex : POSITION;

float4 texcoord : TEXCOORD0;

};

struct v2f {

float4 pos : SV_POSITION;

float4 uv : TEXCOORD0;

};

v2f vert (a2v v) {

v2f o;

//將頂點坐標從模型空間轉換到裁剪空間

o.pos = mul(UNITY_MATRIX_MVP, v.vertex);

將紋理坐標映射到頂點上以及zw偏移,并用ScrollX對x軸坐標進行偏移

o.uv.xy = TRANSFORM_TEX(v.texcoord, _MainTex) + frac(float2(_ScrollX, 0.0) * _Time.y);

o.uv.zw = TRANSFORM_TEX(v.texcoord, _DetailTex) + frac(float2(_Scroll2X, 0.0) * _Time.y);

return o;

}

fixed4 frag (v2f i) : SV_Target {

//紋理采樣

fixed4 firstLayer = tex2D(_MainTex, i.uv.xy);

fixed4 secondLayer = tex2D(_DetailTex, i.uv.zw);

//紋理混合

fixed4 c = lerp(firstLayer, secondLayer, secondLayer.a);

c.rgb *= _Multiplier;

return c;

}

ENDCG

}

}

FallBack "VertexLit"

}

效果:

頂點動畫

效果:

Shader "Unity Shaders Book/Chapter 11/Water" {

Properties {

_MainTex ("Main Tex", 2D) = "white" {}

_Color ("Color Tint", Color) = (1, 1, 1, 1)

_Magnitude ("Distortion Magnitude", Float) = 1

_Frequency ("Distortion Frequency", Float) = 1

_InvWaveLength ("Distortion Inverse Wave Length", Float) = 10

_Speed ("Speed", Float) = 0.5

}

SubShader {

// Need to disable batching because of the vertex animation

Tags {"Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent" "DisableBatching"="True"}

Pass {

Tags { "LightMode"="ForwardBase" }

ZWrite Off

Blend SrcAlpha OneMinusSrcAlpha

Cull Off

CGPROGRAM

#pragma vertex vert

#pragma fragment frag

#include "UnityCG.cginc"

sampler2D _MainTex;

float4 _MainTex_ST;

fixed4 _Color;

float _Magnitude;

float _Frequency;

float _InvWaveLength;

float _Speed;

struct a2v {

float4 vertex : POSITION;

float4 texcoord : TEXCOORD0;

};

struct v2f {

float4 pos : SV_POSITION;

float2 uv : TEXCOORD0;

};

v2f vert(a2v v) {

v2f o;

//頂點偏移量,只對x偏移

float4 offset;

offset.yzw = float3(0.0, 0.0, 0.0);

//Frequency控制頻率f

//InvWaveLength控制波長L

//Magnitude控制幅度k

offset.x = sin(_Frequency * _Time.y + v.vertex.x * _InvWaveLength + v.vertex.y * _InvWaveLength + v.vertex.z * _InvWaveLength) * _Magnitude;

o.pos = mul(UNITY_MATRIX_MVP, v.vertex + offset);

//紋理采樣

o.uv = TRANSFORM_TEX(v.texcoord, _MainTex);

//對v偏移,橫向

o.uv += float2(0.0, _Time.y * _Speed);

return o;

}

fixed4 frag(v2f i) : SV_Target {

fixed4 c = tex2D(_MainTex, i.uv);

c.rgb *= _Color.rgb;

return c;

}

ENDCG

}

}

FallBack "Transparent/VertexLit"

}

效果:

總結

以上是生活随笔為你收集整理的偏移shaderuv_Unity Shader 之 uv动画的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。