summaryrefslogtreecommitdiffstats
path: root/vertex.cginc
blob: 303fc008284c8f24f9d3396ebf209fb1466cc9ff (plain)
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#ifndef __VERTEX_INC
#define __VERTEX_INC

#include "globals.cginc"
#include "vertex_deformation.hlsl"

#define FOO(x) (x)

#if defined(_VERTEX_DEFORMATION_XZ_TUBE)
#define VERTEX_DEFORM_XZ_TUBE_PREAMBLE                    \
  float t = _Vertex_Deformation_XZ_Tube_t
#define VERTEX_DEFORM_XZ_TUBE_POS                         \
  objPos = plane_to_tube(objPos.xyz, t)
#define VERTEX_DEFORM_XZ_TUBE_NORM                        \
  plane_to_tube_normal(objPos, objNorm, objTan, t)
#else
#define VERTEX_DEFORM_XZ_TUBE_PREAMBLE
#define VERTEX_DEFORM_XZ_TUBE_POS
#define VERTEX_DEFORM_XZ_TUBE_NORM
#endif  // VERTEX_DEFORMATION_XZ_TUBE

#if defined(_VERTEX_DEFORMATION_YZ_TUBE)
#define VERTEX_DEFORM_YZ_TUBE_PREAMBLE                    \
  float t = _Vertex_Deformation_YZ_Tube_t
#define VERTEX_DEFORM_YZ_TUBE_POS                         \
  objPos = plane_to_tube(objPos.yxz, t);                  \
  objPos = objPos.yxz
#define VERTEX_DEFORM_YZ_TUBE_NORM                        \
  float3 yzPos = objPos.yxz;                              \
  float3 yzNorm = objNorm.yxz;                            \
  float3 yzTan = objTan.yxz;                              \
  plane_to_tube_normal(yzPos, yzNorm, yzTan, t);          \
  objPos = yzPos.yxz;                                     \
  objNorm = yzNorm.yxz;                                   \
  objTan = yzTan.yxz
#else
#define VERTEX_DEFORM_YZ_TUBE_PREAMBLE
#define VERTEX_DEFORM_YZ_TUBE_POS
#define VERTEX_DEFORM_YZ_TUBE_NORM
#endif  // VERTEX_DEFORMATION_YZ_TUBE

#if defined(_VERTEX_DEFORMATION_XY_TUBE)
#define VERTEX_DEFORM_XY_TUBE_PREAMBLE                    \
  float t = _Vertex_Deformation_XY_Tube_t
#define VERTEX_DEFORM_XY_TUBE_POS                         \
  objPos = plane_to_tube(objPos.xzy, t);                  \
  objPos = objPos.xzy
#define VERTEX_DEFORM_XY_TUBE_NORM                        \
  float3 xyPos = objPos.xzy;                              \
  float3 xyNorm = objNorm.xzy;                            \
  float3 xyTan = objTan.xzy;                              \
  plane_to_tube_normal(xyPos, xyNorm, xyTan, t);          \
  objPos = xyPos.xzy;                                     \
  objNorm = xyNorm.xzy;                                   \
  objTan = xyTan.xzy
#else
#define VERTEX_DEFORM_XY_TUBE_PREAMBLE
#define VERTEX_DEFORM_XY_TUBE_POS
#define VERTEX_DEFORM_XY_TUBE_NORM
#endif  // VERTEX_DEFORMATION_XY_TUBE

#if defined(_VERTEX_DEFORMATION_SEAL)
#define VERTEX_DEFORM_SEAL_PREAMBLE \
  float A = _Vertex_Deformation_Seal_A;                   \
  float k = _Vertex_Deformation_Seal_k;                   \
  float st = t * _Vertex_Deformation_Seal_t
#define VERTEX_DEFORM_SEAL_POS \
  objPos = seal(objPos.xyz, A, k, st)
#define VERTEX_DEFORM_SEAL_NORM \
  seal_normal(objPos, objNorm, objTan, A, k, st)
#else
#define VERTEX_DEFORM_SEAL_PREAMBLE
#define VERTEX_DEFORM_SEAL_POS
#define VERTEX_DEFORM_SEAL_NORM
#endif  // _VERTEX_DEFORMATION_SEAL

#if defined(_VERTEX_DEFORMATION_SINE_WAVES)
#define VERTEX_DEFORM_SINE_WAVES_PREAMBLE                                       \
  float st = t * 10;                                                            \
  float3 amplitude = _Vertex_Deformation_Sine_Waves_Amplitude;                  \
  float3 direction = _Vertex_Deformation_Sine_Waves_Direction;                  \
  float3 k = _Vertex_Deformation_Sine_Waves_k;                                  \
  float3 omega = _Vertex_Deformation_Sine_Waves_omega
#define VERTEX_DEFORM_SINE_WAVES_POS                                            \
  objPos = sine_wave(objPos.xyz, amplitude, direction, k, omega, st)
#define VERTEX_DEFORM_SINE_WAVES_NORM                                           \
  sine_wave_normal(objPos, objNorm, objTan, amplitude, direction, k, omega, st)
#else
#define VERTEX_DEFORM_SINE_WAVES_PREAMBLE
#define VERTEX_DEFORM_SINE_WAVES_POS
#define VERTEX_DEFORM_SINE_WAVES_NORM
#endif  // _VERTEX_DEFORMATION_SINE_WAVES

#if defined(_VERTEX_DEFORMATION_FBM)
#define VERTEX_DEFORM_FBM_PREAMBLE                                                                \
  float st = t;                                                                                   \
  float amplitude = _Vertex_Deformation_FBM_Amplitude;                                            \
  float gain = _Vertex_Deformation_FBM_Gain;                                                      \
  float lacunarity = _Vertex_Deformation_FBM_Lacunarity;                                          \
  float scale = _Vertex_Deformation_FBM_Scale;                                                    \
  float octaves = _Vertex_Deformation_FBM_Octaves;                                                \
  float3 velocity = _Vertex_Deformation_FBM_Velocity
#define VERTEX_DEFORM_FBM_POS                                                                     \
  objPos = fbm(objPos, st, amplitude, gain, lacunarity, scale, octaves, velocity)
#define VERTEX_DEFORM_FBM_NORM                                                                    \
  fbm_normal(objPos, objNorm, objTan, st, amplitude, gain, lacunarity, scale, octaves, velocity)
#else
#define VERTEX_DEFORM_FBM_PREAMBLE
#define VERTEX_DEFORM_FBM_POS
#define VERTEX_DEFORM_FBM_NORM
#endif  // _VERTEX_DEFORMATION_FBM

void deform(inout float3 objPos) {
  const float t = getTime();
  {
    VERTEX_DEFORM_XZ_TUBE_PREAMBLE;
    VERTEX_DEFORM_XZ_TUBE_POS;
  }
  {
    VERTEX_DEFORM_YZ_TUBE_PREAMBLE;
    VERTEX_DEFORM_YZ_TUBE_POS;
  }
  {
    VERTEX_DEFORM_XY_TUBE_PREAMBLE;
    VERTEX_DEFORM_XY_TUBE_POS;
  }
  {
    VERTEX_DEFORM_SEAL_PREAMBLE;
    VERTEX_DEFORM_SEAL_POS;
  }
  {
    VERTEX_DEFORM_SINE_WAVES_PREAMBLE;
    VERTEX_DEFORM_SINE_WAVES_POS;
  }
  {
    VERTEX_DEFORM_FBM_PREAMBLE;
    VERTEX_DEFORM_FBM_POS;
  }
}

void deform_normal(inout float3 objPos, inout float3 objNorm, inout float3 objTan) {
  const float t = getTime();
  {
    VERTEX_DEFORM_XZ_TUBE_PREAMBLE;
    VERTEX_DEFORM_XZ_TUBE_NORM;
  }
  {
    VERTEX_DEFORM_YZ_TUBE_PREAMBLE;
    VERTEX_DEFORM_YZ_TUBE_NORM;
  }
  {
    VERTEX_DEFORM_XY_TUBE_PREAMBLE;
    VERTEX_DEFORM_XY_TUBE_NORM;
  }
  {
    VERTEX_DEFORM_SEAL_PREAMBLE;
    VERTEX_DEFORM_SEAL_NORM;
  }
  {
    VERTEX_DEFORM_SINE_WAVES_PREAMBLE;
    VERTEX_DEFORM_SINE_WAVES_NORM;
  }
  {
    VERTEX_DEFORM_FBM_PREAMBLE;
    VERTEX_DEFORM_FBM_NORM;
  }
}

// `objPos` is in the un-deformed coordinate system, while `objNorm` and
// `objTan` are in the deformed coordinate system. The task is to map `objNorm`
// and `objTan` to the un-deformed coordinate system.
// We do this, coarsely, as follows:
//  1. Map objPos into the deformed coordinate system through stepwise
//     transformations, like `deform`. We remember each position along the way.
//  2. Map objNorm and objTan back one step using the deformed final position.
//     For the first iteration, we use the last (deformed) `objPos` that we
//     remembered from step 1.
//  3. Repeat step 2 until we're back in the un-deforme coordinate system. At
//     each step, we use the cached objPos from the forward pass (step 1), so
//     that we evaluate the normal deformation at the correct point in space.
void undeform_normal(float3 objPos, inout float3 objNorm, inout float3 objTan) {
}

void propagateObjPos(inout v2f i) {
  i.worldPos = mul(unity_ObjectToWorld, float4(i.objPos, 1));
  i.pos = UnityObjectToClipPos(i.objPos);
  i.eyeVec.xyz = i.worldPos - _WorldSpaceCameraPos;
}

#endif  // __VERTEX_INC