Skip to content

Commit 0e50842

Browse files
committed
Simple PCF for shadow
1 parent 0e72eee commit 0e50842

File tree

4 files changed

+908
-600
lines changed

4 files changed

+908
-600
lines changed

Source/Game/CMakeLists.txt

+1-3
Original file line numberDiff line numberDiff line change
@@ -172,13 +172,11 @@ IF(APPLE)
172172
SET_TARGET_PROPERTIES(perimeter PROPERTIES
173173
MACOSX_BUNDLE TRUE
174174
MACOSX_BUNDLE_INFO_PLIST "${PROJECT_SOURCE_DIR}/macos/DebugInfo.plist"
175-
BUNDLE DESTINATION ${CMAKE_SOURCE_DIR}
176175
)
177176
ELSE()
178177
SET_TARGET_PROPERTIES(perimeter PROPERTIES
179178
MACOSX_BUNDLE TRUE
180179
MACOSX_BUNDLE_INFO_PLIST "${PROJECT_SOURCE_DIR}/macos/Perimeter.app.template/Contents/Info.plist"
181-
BUNDLE DESTINATION ${CMAKE_SOURCE_DIR}
182180
)
183181
ENDIF()
184182
ENDIF()
@@ -187,4 +185,4 @@ ENDIF()
187185
target_link_libraries(perimeter PRIVATE ${perimeter_LINK_LIBS})
188186

189187
#Install rules
190-
install (TARGETS perimeter RUNTIME)
188+
install(TARGETS perimeter RUNTIME BUNDLE DESTINATION ${CMAKE_SOURCE_DIR})

Source/Render/sokol/SokolRenderPipeline.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,10 @@ void cSokolRender::RegisterPipeline(SokolPipelineContext context) {
316316
break;
317317
}
318318

319+
// render back-faces in shadow pass to prevent shadow acne on front-faces
320+
desc.cull_mode = SG_CULLMODE_FRONT;
321+
desc.sample_count = 1;
322+
319323
desc.depth.pixel_format = SG_PIXELFORMAT_DEPTH;
320324
desc.depth.compare = SG_COMPAREFUNC_LESS_EQUAL;
321325
desc.depth.write_enabled = true;

Source/Render/sokol/shaders/tile_map.glsl

+39-19
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,31 @@ in vec2 vs_texcoord0;
1515

1616
//Fragment shader outputs
1717
out vec3 fs_uv0;
18-
out vec2 fs_uv1;
19-
out vec2 fs_uv2;
18+
out vec3 fs_uv1;
19+
out vec3 fs_uv2;
20+
out vec3 fs_uv3;
21+
out vec2 fs_uv4;
22+
out vec2 fs_uv5;
23+
24+
const float offset = 1.0f / 1024.0f;
2025

2126
void main() {
2227
gl_Position = un_mvp * vec4(vs_position, 1.0f);
2328

24-
float offset = 1.0f / 1024.0f;
25-
fs_uv0 = (un_shadow * vec4(vs_position, 1.0f) + vec4(-offset, +offset*0.5f, 0, 0)).xyz;
29+
const vec4 p = un_shadow * vec4(vs_position, 1.0f);
30+
fs_uv0 = (p + vec4(-offset, +offset*0.5f, 0, 0)).xyz;
31+
fs_uv1 = (p + vec4(offset, -offset*0.5f, 0, 0)).xyz;
32+
fs_uv2 = (p + vec4(0, -offset, 0, 0)).xyz;
33+
fs_uv3 = (p + vec4(0, offset, 0, 0)).xyz;
2634
#if SOKOL_GLSL
2735
fs_uv0.y = 1.0f - fs_uv0.y;
36+
fs_uv1.y = 1.0f - fs_uv1.y;
37+
fs_uv2.y = 1.0f - fs_uv2.y;
38+
fs_uv3.y = 1.0f - fs_uv3.y;
2839
#endif
29-
fs_uv1 = vs_texcoord0;
30-
fs_uv2 = vs_position.xy * un_inv_world_size;
40+
41+
fs_uv4 = vs_texcoord0;
42+
fs_uv5 = vs_position.xy * un_inv_world_size;
3143
}
3244
@end
3345

@@ -43,36 +55,44 @@ uniform texture2D un_tex1; // diffuse
4355
uniform texture2D un_tex2; // light map
4456

4557
//Fragment shader inputs from Vertex shader
46-
in vec3 fs_uv0; // shadow map
47-
in vec2 fs_uv1; // diffuse
48-
in vec2 fs_uv2; // light map
58+
in vec3 fs_uv0; // shadow map 1
59+
in vec3 fs_uv1; // shadow map 2
60+
in vec3 fs_uv2; // shadow map 3
61+
in vec3 fs_uv3; // shadow map 4
62+
in vec2 fs_uv4; // diffuse
63+
in vec2 fs_uv5; // light map
4964

5065
//Fragment shader outputs
5166
out vec4 frag_color;
5267

53-
//SHADE - коэффициэнт, не который умножается цвет объекта в тени.
68+
//SHADE - коэффициент, на который умножается цвет объекта в тени.
5469
#define SHADE 0.5f
5570

5671
//MSHADE=1-SHADE
5772
#define MSHADE 0.5f
5873

59-
void main() {
60-
frag_color = texture(sampler2DShadow(un_tex0, un_sampler1), fs_uv0).rrrr;
74+
// ambient color
75+
const vec3 c0 = vec3(SHADE, MSHADE, 0.25f);
76+
const vec3 c4 = vec3(0.25f, 0.25f, 0.25f);
6177

62-
// ambient color
63-
vec4 c0 = vec4(SHADE, MSHADE, 0, 1.0f);
64-
frag_color = frag_color * c0.y + c0.x;
78+
void main() {
79+
// shadow
80+
float shadow = texture(sampler2DShadow(un_tex0, un_sampler1), fs_uv0).r;
81+
shadow += texture(sampler2DShadow(un_tex0, un_sampler1), fs_uv1).r;
82+
shadow += texture(sampler2DShadow(un_tex0, un_sampler1), fs_uv2).r;
83+
shadow += texture(sampler2DShadow(un_tex0, un_sampler1), fs_uv3).r;
84+
vec3 result = c4 * shadow * c0.y + c0.x;
6585

6686
// diffuse
67-
frag_color = frag_color * texture(sampler2D(un_tex1, un_sampler0), fs_uv1);
87+
result *= texture(sampler2D(un_tex1, un_sampler0), fs_uv4).rgb;
6888

6989
// tile color
70-
frag_color = frag_color * un_tile_color;
90+
result *= un_tile_color.rgb;
7191

7292
// light map
73-
frag_color = frag_color * texture(sampler2D(un_tex2, un_sampler0), fs_uv2);
93+
result *= texture(sampler2D(un_tex2, un_sampler0), fs_uv5).rgb;
7494

75-
frag_color.a = un_tile_color.a;
95+
frag_color = vec4(result, un_tile_color.a);
7696
}
7797
@end
7898

0 commit comments

Comments
 (0)