-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathSimpleCurveModifier.shader
73 lines (60 loc) · 1.71 KB
/
SimpleCurveModifier.shader
1
2
3
4
5
6
7
8
9
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
71
72
73
Shader "Unlit/SimpleCurveModifier"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
_Speed ("Speed", Float) = 1
_Length ("Length", Float) = 1
_Radius ("Radius", Float) = 1
}
SubShader
{
Tags { "RenderType"="Opaque" }
LOD 100
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct attributes
{
float4 position : POSITION;
float2 uv : TEXCOORD0;
};
struct varying
{
float4 position : SV_POSITION;
float2 uv : TEXCOORD0;
};
uniform sampler2D _MainTex, _CurveTexture;
uniform float4 _MainTex_ST;
uniform float _Speed, _Length, _Radius;
varying vert (attributes v)
{
varying o;
o.position = mul(UNITY_MATRIX_M, v.position);
float current = fmod(o.position.y * _Length + _Time.y * _Speed, 1.);
float next = fmod(current + .01, 1.);
float3 curvePosition = tex2Dlod(_CurveTexture, float4(current,0,0,0)).xyz;
float3 curvePositionNext = tex2Dlod(_CurveTexture, float4(next,0,0,0)).xyz;
float3 forward = normalize(curvePositionNext - curvePosition);
// float3 up = normalize(cross(float3(0,1,0), forward));
float3 up = normalize(cross(normalize(curvePositionNext), normalize(curvePosition)));
float3 right = normalize(cross(forward, up));
float angle = atan2(o.position.z, o.position.x);
float radius = length(o.position.xz) * _Radius;
o.position.xyz = curvePosition + (right * cos(angle) + up * sin(angle)) * radius;
o.position = mul(UNITY_MATRIX_VP, o.position);
o.uv = TRANSFORM_TEX(v.uv, _MainTex);
return o;
}
fixed4 frag (varying i) : SV_Target
{
fixed4 color = tex2D(_MainTex, i.uv);
return color;
}
ENDCG
}
}
}