-
Notifications
You must be signed in to change notification settings - Fork 2
/
Stosomethingmaterial.tres
196 lines (174 loc) · 7.08 KB
/
Stosomethingmaterial.tres
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
191
192
193
194
195
196
[gd_resource type="ShaderMaterial" load_steps=5 format=2]
[ext_resource path="res://_tests/Terrain/CloverField.jpg" type="Texture" id=1]
[sub_resource type="Shader" id=1]
code = "shader_type spatial;
render_mode blend_mix,depth_draw_opaque,cull_back,diffuse_burley,specular_schlick_ggx;
uniform vec4 albedo : hint_color;
uniform sampler2D texture_albedo : hint_albedo;
uniform sampler2D texture_albedo_input;
uniform sampler2D texture_albedo_tinv;
uniform sampler2D texture_noise : hint_albedo;
uniform float specular;
uniform float metallic;
uniform float roughness : hint_range(0,1);
uniform float point_size : hint_range(0,128);
uniform sampler2D texture_metallic : hint_white;
uniform vec4 metallic_texture_channel;
uniform sampler2D texture_roughness : hint_white;
uniform vec4 roughness_texture_channel;
uniform sampler2D texture_normal : hint_normal;
uniform float normal_scale : hint_range(-16,16);
uniform vec3 uv1_scale;
uniform vec3 uv1_offset;
uniform vec3 uv2_scale;
uniform vec3 uv2_offset;
varying vec4 posPos;
//uniform float FXAA_SUBPIX_SHIFT = (1.0/4.0);
uniform float rt_w; // GeeXLab built-in
uniform float rt_h; // GeeXLab built-in
// Compute local triangle barycentric coordinates and vertex IDs
void TriangleGrid ( vec2 uv ,
out float w1 , out float w2 , out float w3 ,
out vec2 vertex1 , out vec2 vertex2 , out vec2 vertex3 )
{
// Scaling of the input
uv *= 3.464; // 2 * sqrt (3)
// Skew input space into simplex triangle grid
const mat2 gridToSkewedGrid = mat2 (vec2(1.0 , 0.0) , vec2(-0.57735027 , 1.15470054)) ;
vec2 skewedCoord = gridToSkewedGrid * uv ;
// Compute local triangle vertex IDs and local barycentric coordinates
ivec2 baseId = ivec2 ( floor ( skewedCoord ));
vec3 temp = vec3 ( fract ( skewedCoord ) , 0) ;
temp .z = 1.0 - temp . x - temp .y;
if ( temp . z > 0.0)
{
w1 = temp .z;
w2 = temp .y;
w3 = temp .x;
vertex1 = vec2(float(baseId.x), float(baseId.y)) ;
vertex2 = vertex1 + vec2 (0 , 1) ;
vertex3 = vertex1 + vec2 (1 , 0) ;
}
else
{
w1 = - temp .z ;
w2 = 1.0 - temp .y;
w3 = 1.0 - temp .x;
vertex1 = vec2(float(baseId.x), float(baseId.y)) + vec2 (1 , 1) ;
vertex2 = vec2(float(baseId.x), float(baseId.y)) + vec2 (1 , 0) ;
vertex3 = vec2(float(baseId.x), float(baseId.y)) + vec2 (0 , 1) ;
}
}
vec2 hash2D2D (vec2 s)
{
//magic numbers
return fract(sin(mod(vec2(dot(s, vec2(127.1,311.7)), dot(s, vec2(269.5,183.3))), 3.14159))*43758.5453);
}
vec3 ProceduralTilingAndBlending (sampler2D input, sampler2D T_input, sampler2D Tinv, vec2 uv )
{
// Get triangle info
float w1 , w2 , w3 ;
vec2 vertex1 , vertex2 , vertex3 ;
TriangleGrid (uv , w1 , w2 , w3 , vertex1 , vertex2 , vertex3 );
// Assign random offset to each triangle vertex
vec2 uv1 = uv + hash2D2D ( vertex1 );
vec2 uv2 = uv + hash2D2D ( vertex2 );
vec2 uv3 = uv + hash2D2D ( vertex3 );
// Precompute UV derivatives
vec2 duvdx = dFdx ( uv ) ;
vec2 duvdy = dFdy ( uv ) ;
// Fetch input
vec3 I1 = textureGrad ( input , uv1 , duvdx , duvdy ). rgb ;
vec3 I2 = textureGrad ( input , uv2 , duvdx , duvdy ). rgb ;
vec3 I3 = textureGrad ( input , uv3 , duvdx , duvdy ). rgb ;
// Sample Gaussian values from transformed input
vec3 G1 = textureGrad ( T_input , uv1 , duvdx , duvdy ) . rgb ;
vec3 G2 = textureGrad ( T_input , uv2 , duvdx , duvdy ) . rgb ;
vec3 G3 = textureGrad ( T_input , uv3 , duvdx , duvdy ) . rgb ;
// Variance - preserving blending
vec3 G = w1 * G1 + w2 * G2 + w3 * G3 ;
G = G - vec3 (0.5) ;
G = G * inversesqrt ( w1 * w1 + w2 * w2 + w3 * w3 ) ;
G = G + vec3 (0.5) ;
// Fetch LUT
vec3 color ;
color .r = texture ( Tinv , vec2 (G.r , 0) ).r;
color .g = texture ( Tinv , vec2 (G.g , 0) ).g;
color .b = texture ( Tinv , vec2 (G.b , 0) ).b;
return color;
}
//stochastic sampling
vec4 textureStochastic(sampler2D tex, vec2 uv)
{
//triangle vertices and blend weights
//BW_vx[0...2].xyz = triangle verts
//BW_vx[3].xy = blend weights (z is unused)
mat4 BW_vx;
//uv transformed into triangular grid space with UV scaled by approximation of 2*sqrt(3)
vec2 newUV = (mat2(vec2(1.0 , 0.0) , vec2(-0.57735027 , 1.15470054))* uv * 3.464);
//vertex IDs and barycentric coords
vec2 vxID = vec2 (floor(newUV));
vec3 fracted = vec3 (fract(newUV), 0);
fracted.z = 1.0-fracted.x-fracted.y;
BW_vx = ((fracted.z>0.0) ?
mat4(vec4(vxID, 0,0), vec4(vxID + vec2(0, 1), 0,0), vec4(vxID + vec2(1, 0), 0,0), vec4(fracted,0)) :
mat4(vec4(vxID + vec2 (1, 1), 0,0), vec4(vxID + vec2 (1, 0), 0,0), vec4(vxID + vec2 (0, 1), 0,0), vec4(-fracted.z, 1.0-fracted.y, 1.0-fracted.x,0)));
//calculate derivatives to avoid triangular grid artifacts
vec2 dx = dFdx(uv);
vec2 dy = dFdy(uv);
//blend samples with calculated weights
return (textureGrad(tex, uv + hash2D2D(BW_vx[0].xy), dx, dy) * BW_vx[3].x +
textureGrad(tex, uv + hash2D2D(BW_vx[1].xy), dx, dy) * BW_vx[3].y +
textureGrad(tex, uv + hash2D2D(BW_vx[2].xy), dx, dy) * BW_vx[3].z);
}
float aastep(float threshold, float value){
float afwidth = length(vec2(dFdx(value), dFdy(value))) * 0.70710678118654757;
return smoothstep(threshold-afwidth, threshold+afwidth, value);
}
vec4 textureaa(vec4 input, float threshold){
return vec4(aastep(threshold, input.x), aastep(threshold, input.y), aastep(threshold, input.z), aastep(threshold, input.a));
}
void vertex() {
UV=UV*uv1_scale.xy+uv1_offset.xy;
}
void fragment() {
vec2 base_uv = UV;
vec4 noise = texture(texture_noise, UV);//, dFdx(UV), dFdy(UV));
//vec4 albedo_tex = mix(textureStochastic(texture_albedo,base_uv), textureStochastic(texture_albedo, base_uv+vec2(10,5)), noise);
vec4 albedo_tex = vec4(ProceduralTilingAndBlending(texture_albedo, texture_albedo_input, texture_albedo_tinv, base_uv),1);
float f = albedo_tex.x*8.0;
//albedo_tex = mix( albedo_tex_a, albedo_tex_b, smoothstep(0.2,0.8,f-0.1*(albedo_tex_a-albedo_tex_b)));
ALBEDO = albedo.rgb * albedo_tex.rgb;
float metallic_tex = dot(textureStochastic(texture_metallic,base_uv),metallic_texture_channel);
METALLIC = metallic_tex * metallic;
float roughness_tex = dot(textureStochastic(texture_roughness,base_uv),roughness_texture_channel);
ROUGHNESS = roughness_tex * roughness;
SPECULAR = specular;
NORMALMAP = mix(textureStochastic(texture_normal,base_uv),textureStochastic(texture_normal,base_uv+vec2(10,5)), noise).rgb;
NORMALMAP_DEPTH = normal_scale;
}
"
[sub_resource type="OpenSimplexNoise" id=2]
seed = 8
[sub_resource type="NoiseTexture" id=3]
noise = SubResource( 2 )
[resource]
shader = SubResource( 1 )
shader_param/albedo = Color( 1, 1, 1, 1 )
shader_param/specular = 0.5
shader_param/metallic = 0.0
shader_param/roughness = 1.0
shader_param/point_size = 1.0
shader_param/metallic_texture_channel = Plane( 1, 0, 0, 0 )
shader_param/roughness_texture_channel = Plane( 1, 0, 0, 0 )
shader_param/normal_scale = 1.0
shader_param/uv1_scale = Vector3( 3, 3, 3 )
shader_param/uv1_offset = Vector3( 0, 0, 0 )
shader_param/uv2_scale = Vector3( 1, 1, 1 )
shader_param/uv2_offset = Vector3( 0, 0, 0 )
shader_param/rt_w = 1.0
shader_param/rt_h = 1.0
shader_param/texture_albedo = ExtResource( 1 )
shader_param/texture_albedo_input = ExtResource( 1 )
shader_param/texture_albedo_tinv = ExtResource( 1 )
shader_param/texture_noise = SubResource( 3 )