2020-10-15 08:47:55 +02:00
|
|
|
shader_type spatial;
|
|
|
|
render_mode unshaded, cull_front;
|
|
|
|
|
|
|
|
uniform sampler2D view2texture;
|
2020-11-15 18:52:39 +01:00
|
|
|
uniform sampler2D seams;
|
|
|
|
uniform float seams_multiplier = 0.06125;
|
2020-10-15 08:47:55 +02:00
|
|
|
uniform mat4 model_transform;
|
|
|
|
uniform float fovy_degrees = 45;
|
|
|
|
uniform float z_near = 0.01;
|
|
|
|
uniform float z_far = 60.0;
|
|
|
|
uniform float aspect = 1.0;
|
2021-10-22 21:29:13 +02:00
|
|
|
uniform float uv_tolerance = 0.001;
|
2020-10-15 08:47:55 +02:00
|
|
|
|
2021-10-22 21:29:13 +02:00
|
|
|
varying mat4 projection_matrix;
|
2020-10-15 08:47:55 +02:00
|
|
|
varying vec4 global_position;
|
|
|
|
varying vec3 normal;
|
|
|
|
|
|
|
|
mat4 get_projection_matrix() {
|
2021-10-22 21:29:13 +02:00
|
|
|
float rads = fovy_degrees*0.00872664625;
|
2020-10-15 08:47:55 +02:00
|
|
|
float deltaZ = z_far - z_near;
|
|
|
|
float sine = sin(rads);
|
|
|
|
if (deltaZ == 0.0 || sine == 0.0 || aspect == 0.0)
|
|
|
|
return mat4(0.0);
|
|
|
|
float cotangent = cos(rads) / sine;
|
|
|
|
mat4 matrix = mat4(1.0);
|
|
|
|
matrix[0][0] = cotangent / aspect;
|
|
|
|
matrix[1][1] = cotangent;
|
|
|
|
matrix[2][2] = (z_far + z_near) / deltaZ;
|
|
|
|
matrix[2][3] = 1.0; //try +1
|
|
|
|
matrix[3][2] = 2.0 * z_near * z_far / deltaZ;
|
|
|
|
matrix[3][3] = 0.0;
|
|
|
|
return matrix;
|
|
|
|
}
|
|
|
|
|
|
|
|
void vertex() {
|
|
|
|
global_position = model_transform*vec4(VERTEX, 1.0);
|
|
|
|
normal = (model_transform*vec4(NORMAL, 0.0)).xyz;
|
|
|
|
VERTEX=vec3(UV.x, UV.y, 0.0);
|
|
|
|
COLOR=vec4(1.0);
|
2021-10-22 21:29:13 +02:00
|
|
|
projection_matrix = get_projection_matrix();
|
2020-10-15 08:47:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
float visibility(vec2 uv, vec3 view_pos) {
|
2020-11-14 21:36:21 +01:00
|
|
|
// Compare actual UV with uv from view
|
2021-10-22 21:29:13 +02:00
|
|
|
vec4 pos = textureLod(view2texture, view_pos.xy, 0.0);
|
|
|
|
vec2 uv_delta = pos.xy-uv;
|
|
|
|
return 1.0-20.0*length(uv_delta);
|
2020-10-15 08:47:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void fragment() {
|
2021-10-22 21:29:13 +02:00
|
|
|
float box = 3.0;
|
|
|
|
vec4 position = projection_matrix*vec4(global_position.xyz, 1.0);
|
2020-10-15 08:47:55 +02:00
|
|
|
position.xyz /= position.w;
|
2021-10-22 21:29:13 +02:00
|
|
|
vec3 xyz = vec3(0.5-0.5*position.x, 0.5+0.5*position.y, 0.5+0.5*position.z);
|
2020-10-15 08:47:55 +02:00
|
|
|
float visible = 0.0;
|
|
|
|
if (position.x > -1.0 && position.x < 1.0 && position.y > -1.0 && position.y < 1.0) {
|
2020-11-15 18:52:39 +01:00
|
|
|
float visibility_multiplier = 0.0;
|
2021-10-22 21:29:13 +02:00
|
|
|
vec2 epsilon = vec2(0.0005);
|
|
|
|
for (float dx = -box*epsilon.x; dx <= box*epsilon.x; dx += epsilon.x) {
|
|
|
|
for (float dy = -box*epsilon.y; dy <= box*epsilon.y; dy += epsilon.y) {
|
|
|
|
visibility_multiplier += max(visibility_multiplier, visibility(UV.xy, xyz+vec3(dx, dy, 0.0)));
|
2020-11-15 18:52:39 +01:00
|
|
|
}
|
|
|
|
}
|
2020-12-04 22:28:54 +01:00
|
|
|
visibility_multiplier = clamp(visibility_multiplier, 0.0, 1.0);
|
2021-10-22 21:29:13 +02:00
|
|
|
float normal_multiplier = smoothstep(0.1, 0.2, normal.z);
|
2020-10-15 08:47:55 +02:00
|
|
|
visible = normal_multiplier*visibility_multiplier;
|
|
|
|
}
|
|
|
|
ALBEDO = vec3(xyz.xy, visible);
|
|
|
|
}
|