열차 목업의 내부 확인용 프로젝트
smchoi
2024-06-14 3d45944b4b2abc384e2539fa72cd7925bbcf2543
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
// Upgrade NOTE: replaced '_Object2World' with 'unity_ObjectToWorld'
 
Shader "FX/Water" {
Properties {
    _WaveScale ("Wave scale", Range (0.02,0.15)) = 0.063
    _ReflDistort ("Reflection distort", Range (0,1.5)) = 0.44
    _RefrDistort ("Refraction distort", Range (0,1.5)) = 0.40
    _RefrColor ("Refraction color", COLOR)  = ( .34, .85, .92, 1)
    [NoScaleOffset] _Fresnel ("Fresnel (A) ", 2D) = "gray" {}
    [NoScaleOffset] _BumpMap ("Normalmap ", 2D) = "bump" {}
    WaveSpeed ("Wave speed (map1 x,y; map2 x,y)", Vector) = (19,9,-16,-7)
    [NoScaleOffset] _ReflectiveColor ("Reflective color (RGB) fresnel (A) ", 2D) = "" {}
    _HorizonColor ("Simple water horizon color", COLOR)  = ( .172, .463, .435, 1)
    [HideInInspector] _ReflectionTex ("Internal Reflection", 2D) = "" {}
    [HideInInspector] _RefractionTex ("Internal Refraction", 2D) = "" {}
}
 
 
// -----------------------------------------------------------
// Fragment program cards
 
 
Subshader {
    Tags { "WaterMode"="Refractive" "RenderType"="Opaque" }
    Pass {
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma multi_compile_fog
#pragma multi_compile WATER_REFRACTIVE WATER_REFLECTIVE WATER_SIMPLE
 
#if defined (WATER_REFLECTIVE) || defined (WATER_REFRACTIVE)
#define HAS_REFLECTION 1
#endif
#if defined (WATER_REFRACTIVE)
#define HAS_REFRACTION 1
#endif
 
 
#include "UnityCG.cginc"
 
uniform float4 _WaveScale4;
uniform float4 _WaveOffset;
 
#if HAS_REFLECTION
uniform float _ReflDistort;
#endif
#if HAS_REFRACTION
uniform float _RefrDistort;
#endif
 
struct appdata {
    float4 vertex : POSITION;
    float3 normal : NORMAL;
};
 
struct v2f {
    float4 pos : SV_POSITION;
    #if defined(HAS_REFLECTION) || defined(HAS_REFRACTION)
        float4 ref : TEXCOORD0;
        float2 bumpuv0 : TEXCOORD1;
        float2 bumpuv1 : TEXCOORD2;
        float3 viewDir : TEXCOORD3;
    #else
        float2 bumpuv0 : TEXCOORD0;
        float2 bumpuv1 : TEXCOORD1;
        float3 viewDir : TEXCOORD2;
    #endif
    UNITY_FOG_COORDS(4)
};
 
v2f vert(appdata v)
{
    v2f o;
    o.pos = UnityObjectToClipPos(v.vertex);
    
 
    // scroll bump waves
    float4 temp;
    float4 wpos = mul (unity_ObjectToWorld, v.vertex);
    temp.xyzw = wpos.xzxz * _WaveScale4 + _WaveOffset;
    o.bumpuv0 = temp.xy;
    o.bumpuv1 = temp.wz;
    
    // object space view direction (will normalize per pixel)
    o.viewDir.xzy = WorldSpaceViewDir(v.vertex);
    
    #if defined(HAS_REFLECTION) || defined(HAS_REFRACTION)
    o.ref = ComputeNonStereoScreenPos(o.pos);
    #endif
 
    UNITY_TRANSFER_FOG(o,o.pos);
    return o;
}
 
#if defined (WATER_REFLECTIVE) || defined (WATER_REFRACTIVE)
sampler2D _ReflectionTex;
#endif
#if defined (WATER_REFLECTIVE) || defined (WATER_SIMPLE)
sampler2D _ReflectiveColor;
#endif
#if defined (WATER_REFRACTIVE)
sampler2D _Fresnel;
sampler2D _RefractionTex;
uniform float4 _RefrColor;
#endif
#if defined (WATER_SIMPLE)
uniform float4 _HorizonColor;
#endif
sampler2D _BumpMap;
 
half4 frag( v2f i ) : SV_Target
{
    i.viewDir = normalize(i.viewDir);
    
    // combine two scrolling bumpmaps into one
    half3 bump1 = UnpackNormal(tex2D( _BumpMap, i.bumpuv0 )).rgb;
    half3 bump2 = UnpackNormal(tex2D( _BumpMap, i.bumpuv1 )).rgb;
    half3 bump = (bump1 + bump2) * 0.5;
    
    // fresnel factor
    half fresnelFac = dot( i.viewDir, bump );
    
    // perturb reflection/refraction UVs by bumpmap, and lookup colors
    
    #if HAS_REFLECTION
    float4 uv1 = i.ref; uv1.xy += bump * _ReflDistort;
    half4 refl = tex2Dproj( _ReflectionTex, UNITY_PROJ_COORD(uv1) );
    #endif
    #if HAS_REFRACTION
    float4 uv2 = i.ref; uv2.xy -= bump * _RefrDistort;
    half4 refr = tex2Dproj( _RefractionTex, UNITY_PROJ_COORD(uv2) ) * _RefrColor;
    #endif
    
    // final color is between refracted and reflected based on fresnel
    half4 color;
    
    #if defined(WATER_REFRACTIVE)
    half fresnel = UNITY_SAMPLE_1CHANNEL( _Fresnel, float2(fresnelFac,fresnelFac) );
    color = lerp( refr, refl, fresnel );
    #endif
    
    #if defined(WATER_REFLECTIVE)
    half4 water = tex2D( _ReflectiveColor, float2(fresnelFac,fresnelFac) );
    color.rgb = lerp( water.rgb, refl.rgb, water.a );
    color.a = refl.a * water.a;
    #endif
    
    #if defined(WATER_SIMPLE)
    half4 water = tex2D( _ReflectiveColor, float2(fresnelFac,fresnelFac) );
    color.rgb = lerp( water.rgb, _HorizonColor.rgb, water.a );
    color.a = _HorizonColor.a;
    #endif
 
    UNITY_APPLY_FOG(i.fogCoord, color);
    return color;
}
ENDCG
 
    }
}
 
}