yum/3ner

A toon shader for Unity's BIRP.

git clone https://git.yummers.dev/yum/3ner

yumDedupe fourier water texture tapsc729b8e

master
2.0 KiB57 linesraw
1#ifndef _FOURIER_WATER_INC
2#define _FOURIER_WATER_INC
3
4// Shared displacement derivatives for the surface normal and foam Jacobian.
5struct FourierWaterData {
6    float dxx;
7    float2 derivatives; // dxy, dyy
8};
9
10FourierWaterData sample_fourier_water_data(float2 uv) {
11    FourierWaterData data = (FourierWaterData) 0;
12#if defined(_FOURIER_WATER)
13    data.dxx = _Fourier_Water_Height.SampleLevel(bilinear_repeat_s, uv, 0).b * -1;
14    data.derivatives = _Fourier_Water_Displacement_Derivatives.SampleLevel(
15        bilinear_repeat_s, uv, 0).rb * -1;
16#endif
17    return data;
18}
19
20void fourier_water_deform(float2 uv, inout float3 objPos) {
21#if defined(_FOURIER_WATER)
22    float h = _Fourier_Water_Height.SampleLevel(bilinear_repeat_s, uv, 0).r * -1;
23    float scale = _Fourier_Water_Mesh_Size / _Fourier_Water_Patch_Size;
24    float2 displacement = _Fourier_Water_Displacement.SampleLevel(
25        bilinear_repeat_s, uv, 0).rb * -1;
26    objPos.xy += displacement * scale * _Fourier_Water_Chop;
27    objPos.z = -h * scale;
28#endif
29}
30
31void fourier_water_normals(float2 uv, FourierWaterData water,
32    inout float3 normal, inout float3 tangent) {
33#if defined(_FOURIER_WATER)
34    float4 data = _Fourier_Water_Normals.SampleLevel(bilinear_repeat_s, uv, 0);
35    float2 slopes = data.rb * -1;
36    float chop = _Fourier_Water_Chop;
37
38    // UV 0..1 spans object-space XY; the original normal points along -Z.
39    float3 tx = float3(1.0 + chop * water.dxx, chop * water.derivatives.x, -slopes.x);
40    float3 ty = float3(chop * water.derivatives.x, 1.0 + chop * water.derivatives.y, -slopes.y);
41    normal = normalize(cross(ty, tx));
42    tangent = normalize(tx);
43#endif
44}
45
46float fourier_water_foam(FourierWaterData water) {
47#if defined(_FOURIER_WATER)
48    float chop = _Fourier_Water_Chop;
49    float dxy = chop * water.derivatives.x;
50    float J = (1.0 + chop * water.dxx) * (1.0 + chop * water.derivatives.y) - dxy * dxy;
51    return 1.0 - smoothstep(_Fourier_Water_Foam_Full, _Fourier_Water_Foam_Start, J);
52#else
53    return 0;
54#endif
55}
56
57#endif  // _FOURIER_WATER_INC