Skip to content

Commit f6ffaf6

Browse files
committed
fix a few bugs
- allow placing non-solid blocks in player box - fix ssao precision issues - add offset to light position to avoid artifacts
1 parent 3052e4d commit f6ffaf6

10 files changed

Lines changed: 81 additions & 103 deletions

File tree

README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ To build locally, add [SDL_shadercross](https://github.com/libsdl-org/SDL_shader
4949

5050
### Controls
5151

52-
- `WASD` to move
52+
- `WASDEQ` to move
5353
- `Space` to jump
5454
- `F5` to toggle fly
5555
- `Escape` to unfocus
@@ -59,7 +59,6 @@ To build locally, add [SDL_shadercross](https://github.com/libsdl-org/SDL_shader
5959
- `Scroll` to change blocks
6060
- `F11` to toggle fullscreen
6161
- `LControl` to sprint
62-
- `EQ` to move up and down (fly only)
6362

6463
### Passes
6564

shaders/bin/opaque.frag.msl

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ fragment main0_out main0(main0_in in [[stage_in]], constant type_UniformBuffer&
5252
for (uint _87 = 0u; _87 < _82; _84 = _85, _87++)
5353
{
5454
float _100 = float((lightBuffer._m0[_87].Color & 4278190080u) >> 24u);
55-
float3 _107 = (float3(float(lightBuffer._m0[_87].X), float(lightBuffer._m0[_87].Y), float(lightBuffer._m0[_87].Z)) + float3(0.5)) - in.in_var_TEXCOORD0.xyz;
55+
float3 _107 = (float3(float(lightBuffer._m0[_87].X), float(lightBuffer._m0[_87].Y), float(lightBuffer._m0[_87].Z)) + float3(0.5, 0.75, 0.5)) - in.in_var_TEXCOORD0.xyz;
5656
float _108 = length(_107);
5757
bool _114;
5858
if (!(_108 >= _100))
@@ -68,28 +68,19 @@ fragment main0_out main0(main0_in in [[stage_in]], constant type_UniformBuffer&
6868
_85 = _84;
6969
continue;
7070
}
71-
float _128;
72-
if (_108 > 0.100000001490116119384765625)
71+
float _120 = fast::clamp(dot(in.in_var_TEXCOORD1, _107 / float3(_108)), 0.0, 1.0);
72+
if (_120 <= 0.0)
7373
{
74-
float _124 = fast::clamp(dot(in.in_var_TEXCOORD1, _107 / float3(_108)), 0.0, 1.0);
75-
if (_124 <= 0.0)
76-
{
77-
_85 = _84;
78-
continue;
79-
}
80-
_128 = _124;
81-
}
82-
else
83-
{
84-
_128 = 1.0;
74+
_85 = _84;
75+
continue;
8576
}
86-
float _131 = fast::clamp(1.0 - (_108 / _100), 0.0, 1.0);
87-
_85 = _84 + ((float3(float((lightBuffer._m0[_87].Color & 255u) >> 0u) * 0.0039215688593685626983642578125, float((lightBuffer._m0[_87].Color & 65280u) >> 8u) * 0.0039215688593685626983642578125, float((lightBuffer._m0[_87].Color & 16711680u) >> 16u) * 0.0039215688593685626983642578125) * _128) * (_131 * _131));
77+
float _126 = fast::clamp(1.0 - (_108 / _100), 0.0, 1.0);
78+
_85 = _84 + ((float3(float((lightBuffer._m0[_87].Color & 255u) >> 0u) * 0.0039215688593685626983642578125, float((lightBuffer._m0[_87].Color & 65280u) >> 8u) * 0.0039215688593685626983642578125, float((lightBuffer._m0[_87].Color & 16711680u) >> 16u) * 0.0039215688593685626983642578125) * _120) * (_126 * _126));
8879
}
89-
float3 _149 = _84 * 2.0;
80+
float3 _144 = _84 * 2.0;
9081
out.out_var_SV_Target0 = _71;
9182
out.out_var_SV_Target1 = in.in_var_TEXCOORD0;
92-
out.out_var_SV_Target2 = float4(_149.x, _149.y, _149.z, float4(0.0).w);
83+
out.out_var_SV_Target2 = float4(_144.x, _144.y, _144.z, float4(0.0).w);
9384
out.out_var_SV_Target3 = (0u | (in.in_var_TEXCOORD3 & 1u)) | (in.in_var_TEXCOORD3 & 30u);
9485
return out;
9586
}

shaders/bin/opaque.frag.spv

-116 Bytes
Binary file not shown.

shaders/bin/ssao.comp.msl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ kernel void main0(texture2d<uint> voxelTexture [[texture(0)]], texture2d<float>
143143
float _191 = _97.y;
144144
_57[4] = _190 - _191;
145145
_57[5] = _191 - _190;
146-
_199 = _57[_95] > 0.00999999977648258209228515625;
146+
_199 = _57[_95] > 0.0500000007450580596923828125;
147147
break;
148148
} while(false);
149149
_114 = _106 + (float(_199) / precise::max(distance(_88, float2(_139)), 1.0));

shaders/bin/ssao.comp.spv

0 Bytes
Binary file not shown.

shaders/bin/transparent.frag.msl

Lines changed: 59 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -48,129 +48,120 @@ struct main0_in
4848
fragment main0_out main0(main0_in in [[stage_in]], constant type_UniformBuffer& UniformBuffer [[buffer(0)]], constant type_UniformBuffer_1& UniformBuffer_1 [[buffer(1)]], constant type_UniformBuffer_2& UniformBuffer_2 [[buffer(2)]], const device type_StructuredBuffer_Light& lightBuffer [[buffer(3)]], texture2d_array<float> atlasTexture [[texture(0)]], texture2d<float> shadowTexture [[texture(1)]], texture2d<float> positionTexture [[texture(2)]], sampler atlasSampler [[sampler(0)]], sampler shadowSampler [[sampler(1)]], sampler positionSampler [[sampler(2)]])
4949
{
5050
main0_out out = {};
51-
float4 _106 = atlasTexture.sample(atlasSampler, in.in_var_TEXCOORD2.xy, uint(rint(in.in_var_TEXCOORD2.z)));
52-
float _108 = _106.w;
53-
uint _111 = uint(UniformBuffer.LightCount);
54-
float3 _113;
55-
_113 = float3(0.0);
56-
float3 _114;
57-
for (uint _116 = 0u; _116 < _111; _113 = _114, _116++)
51+
float4 _108 = atlasTexture.sample(atlasSampler, in.in_var_TEXCOORD2.xy, uint(rint(in.in_var_TEXCOORD2.z)));
52+
float _110 = _108.w;
53+
uint _113 = uint(UniformBuffer.LightCount);
54+
float3 _115;
55+
_115 = float3(0.0);
56+
float3 _116;
57+
for (uint _118 = 0u; _118 < _113; _115 = _116, _118++)
5858
{
59-
float _129 = float((lightBuffer._m0[_116].Color & 4278190080u) >> 24u);
60-
float3 _136 = (float3(float(lightBuffer._m0[_116].X), float(lightBuffer._m0[_116].Y), float(lightBuffer._m0[_116].Z)) + float3(0.5)) - in.in_var_TEXCOORD0.xyz;
61-
float _137 = length(_136);
62-
bool _143;
63-
if (!(_137 >= _129))
59+
float _131 = float((lightBuffer._m0[_118].Color & 4278190080u) >> 24u);
60+
float3 _138 = (float3(float(lightBuffer._m0[_118].X), float(lightBuffer._m0[_118].Y), float(lightBuffer._m0[_118].Z)) + float3(0.5, 0.75, 0.5)) - in.in_var_TEXCOORD0.xyz;
61+
float _139 = length(_138);
62+
bool _145;
63+
if (!(_139 >= _131))
6464
{
65-
_143 = _129 <= 0.0;
65+
_145 = _131 <= 0.0;
6666
}
6767
else
6868
{
69-
_143 = true;
69+
_145 = true;
7070
}
71-
if (_143)
71+
if (_145)
7272
{
73-
_114 = _113;
73+
_116 = _115;
7474
continue;
7575
}
76-
float _157;
77-
if (_137 > 0.100000001490116119384765625)
76+
float _151 = fast::clamp(dot(in.in_var_TEXCOORD1, _138 / float3(_139)), 0.0, 1.0);
77+
if (_151 <= 0.0)
7878
{
79-
float _153 = fast::clamp(dot(in.in_var_TEXCOORD1, _136 / float3(_137)), 0.0, 1.0);
80-
if (_153 <= 0.0)
81-
{
82-
_114 = _113;
83-
continue;
84-
}
85-
_157 = _153;
86-
}
87-
else
88-
{
89-
_157 = 1.0;
79+
_116 = _115;
80+
continue;
9081
}
91-
float _160 = fast::clamp(1.0 - (_137 / _129), 0.0, 1.0);
92-
_114 = _113 + ((float3(float((lightBuffer._m0[_116].Color & 255u) >> 0u) * 0.0039215688593685626983642578125, float((lightBuffer._m0[_116].Color & 65280u) >> 8u) * 0.0039215688593685626983642578125, float((lightBuffer._m0[_116].Color & 16711680u) >> 16u) * 0.0039215688593685626983642578125) * _157) * (_160 * _160));
82+
float _157 = fast::clamp(1.0 - (_139 / _131), 0.0, 1.0);
83+
_116 = _115 + ((float3(float((lightBuffer._m0[_118].Color & 255u) >> 0u) * 0.0039215688593685626983642578125, float((lightBuffer._m0[_118].Color & 65280u) >> 8u) * 0.0039215688593685626983642578125, float((lightBuffer._m0[_118].Color & 16711680u) >> 16u) * 0.0039215688593685626983642578125) * _151) * (_157 * _157));
9384
}
94-
float _242;
85+
float _239;
9586
do
9687
{
97-
float4 _189 = UniformBuffer_1.ShadowTransform * float4(in.in_var_TEXCOORD0.xyz, 1.0);
98-
float3 _193 = _189.xyz / float3(_189.w);
99-
float2 _196 = (_193.xy * 0.5) + float2(0.5);
100-
float _198 = 1.0 - _196.y;
101-
float2 _199 = _196;
102-
_199.y = _198;
103-
float _200 = _196.x;
104-
bool _206;
105-
if (!(_200 < 0.0))
106-
{
107-
_206 = _200 > 1.0;
88+
float4 _186 = UniformBuffer_1.ShadowTransform * float4(in.in_var_TEXCOORD0.xyz, 1.0);
89+
float3 _190 = _186.xyz / float3(_186.w);
90+
float2 _193 = (_190.xy * 0.5) + float2(0.5);
91+
float _195 = 1.0 - _193.y;
92+
float2 _196 = _193;
93+
_196.y = _195;
94+
float _197 = _193.x;
95+
bool _203;
96+
if (!(_197 < 0.0))
97+
{
98+
_203 = _197 > 1.0;
10899
}
109100
else
110101
{
111-
_206 = true;
102+
_203 = true;
112103
}
113-
bool _211;
114-
if (!_206)
104+
bool _208;
105+
if (!_203)
115106
{
116-
_211 = _198 < 0.0;
107+
_208 = _195 < 0.0;
117108
}
118109
else
119110
{
120-
_211 = true;
111+
_208 = true;
121112
}
122-
bool _216;
123-
if (!_211)
113+
bool _213;
114+
if (!_208)
124115
{
125-
_216 = _198 > 1.0;
116+
_213 = _195 > 1.0;
126117
}
127118
else
128119
{
129-
_216 = true;
120+
_213 = true;
130121
}
131-
if (_216)
122+
if (_213)
132123
{
133-
_242 = 0.4000000059604644775390625;
124+
_239 = 0.4000000059604644775390625;
134125
break;
135126
}
136-
float _231;
127+
float _228;
137128
if (((in.in_var_TEXCOORD3 >> 0u) & 1u) != 0u)
138129
{
139-
float _227 = dot(in.in_var_TEXCOORD1, fast::normalize(float4(UniformBuffer_1.ShadowTransform[0][2], UniformBuffer_1.ShadowTransform[1][2], UniformBuffer_1.ShadowTransform[2][2], UniformBuffer_1.ShadowTransform[3][2]).xyz));
140-
if (_227 > 0.0)
130+
float _224 = dot(in.in_var_TEXCOORD1, fast::normalize(float4(UniformBuffer_1.ShadowTransform[0][2], UniformBuffer_1.ShadowTransform[1][2], UniformBuffer_1.ShadowTransform[2][2], UniformBuffer_1.ShadowTransform[3][2]).xyz));
131+
if (_224 > 0.0)
141132
{
142-
_242 = 0.0;
133+
_239 = 0.0;
143134
break;
144135
}
145-
_231 = _227;
136+
_228 = _224;
146137
}
147138
else
148139
{
149-
_231 = -0.7070000171661376953125;
140+
_228 = -0.7070000171661376953125;
150141
}
151-
if (_193.z < (shadowTexture.sample(shadowSampler, _199, level(0.0)).x + 0.001000000047497451305389404296875))
142+
if (_190.z < (shadowTexture.sample(shadowSampler, _196, level(0.0)).x + 0.001000000047497451305389404296875))
152143
{
153-
_242 = _231 * (-0.4000000059604644775390625);
144+
_239 = _228 * (-0.4000000059604644775390625);
154145
break;
155146
}
156147
else
157148
{
158-
_242 = 0.0;
149+
_239 = 0.0;
159150
break;
160151
}
161152
break; // unreachable workaround
162153
} while(false);
163-
float3 _246 = in.in_var_TEXCOORD0.xyz - UniformBuffer_2.PlayerPosition;
164-
float _278;
154+
float3 _243 = in.in_var_TEXCOORD0.xyz - UniformBuffer_2.PlayerPosition;
155+
float _275;
165156
if (((in.in_var_TEXCOORD3 >> 26u) & 63u) == 16u)
166157
{
167-
_278 = _108 + ((in.in_var_TEXCOORD0.y - positionTexture.sample(positionSampler, in.in_var_TEXCOORD4).y) * 0.100000001490116119384765625);
158+
_275 = _110 + ((in.in_var_TEXCOORD0.y - positionTexture.sample(positionSampler, in.in_var_TEXCOORD4).y) * 0.100000001490116119384765625);
168159
}
169160
else
170161
{
171-
_278 = _108;
162+
_275 = _110;
172163
}
173-
out.out_var_SV_Target0 = float4(mix(_106.xyz * (((_113 * 2.0) + float3(0.5)) + float3(_242)), mix(float3(0.2199999988079071044921875, 0.3490000069141387939453125, 0.70200002193450927734375), float3(0.21199999749660491943359375, 0.7730000019073486328125, 0.9570000171661376953125), float3((precise::atan2(_246.y, length(float2(_246.xz))) + 1.57079637050628662109375) * 0.3183098733425140380859375)), float3(precise::min(powr(distance(in.in_var_TEXCOORD0.xz, UniformBuffer_2.PlayerPosition.xz) * 0.0040000001899898052215576171875, 2.5), 1.0))), _278);
164+
out.out_var_SV_Target0 = float4(mix(_108.xyz * (((_115 * 2.0) + float3(0.5)) + float3(_239)), mix(float3(0.2199999988079071044921875, 0.3490000069141387939453125, 0.70200002193450927734375), float3(0.21199999749660491943359375, 0.7730000019073486328125, 0.9570000171661376953125), float3((precise::atan2(_243.y, length(float2(_243.xz))) + 1.57079637050628662109375) * 0.3183098733425140380859375)), float3(precise::min(powr(distance(in.in_var_TEXCOORD0.xz, UniformBuffer_2.PlayerPosition.xz) * 0.0040000001899898052215576171875, 2.5), 1.0))), _275);
174165
return out;
175166
}
176167

shaders/bin/transparent.frag.spv

-76 Bytes
Binary file not shown.

shaders/shader.hlsl

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -104,33 +104,25 @@ struct Light
104104

105105
float3 GetDiffuseLight(StructuredBuffer<Light> lights, uint lightCount, float4 position, float3 normal)
106106
{
107-
static const float kBias = 0.1f;
107+
static const float3 kOffset = float3(0.0f, 0.25f, 0.0f);
108108
static const float kLight = 2.0f;
109109
float3 finalColor = float3(0.0f, 0.0f, 0.0f);
110110
for (uint i = 0; i < lightCount; i++)
111111
{
112112
Light light = lights[i];
113113
float radius = (light.Color & 0xFF000000) >> 24;
114-
float3 lightPosition = float3(light.X, light.Y, light.Z) + 0.5f;
114+
float3 lightPosition = float3(light.X, light.Y, light.Z) + 0.5f + kOffset;
115115
float3 offset = lightPosition - position.xyz;
116116
float distance = length(offset);
117117
if (distance >= radius || radius <= 0.0f)
118118
{
119119
continue;
120120
}
121121
float3 lightDirection = offset / distance;
122-
float NdotL;
123-
if (distance > kBias)
122+
float NdotL = saturate(dot(normal, lightDirection));
123+
if (NdotL <= 0.0f)
124124
{
125-
NdotL = saturate(dot(normal, lightDirection));
126-
if (NdotL <= 0.0f)
127-
{
128-
continue;
129-
}
130-
}
131-
else
132-
{
133-
NdotL = 1.0f;
125+
continue;
134126
}
135127
float attenuation = 1.0f - (distance / radius);
136128
attenuation = saturate(attenuation);

shaders/ssao.comp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Texture2D<float4> positionTexture : register(t1, space0);
55
[[vk::image_format("r8")]]
66
RWTexture2D<unorm float> ssaoTexture : register(u0, space1);
77

8-
static const float kThreshold = 0.01f;
8+
static const float kThreshold = 0.05f;
99
static const float kScale = 75.0f;
1010
static const int kKernel = 2;
1111

src/player.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,11 @@ void player_place_block(const player_t* player)
245245
{
246246
return;
247247
}
248+
if (!block_is_solid(player->block))
249+
{
250+
world_set_block(player->query.previous, player->block);
251+
return;
252+
}
248253
const aabb_t aabb = get_aabb();
249254
for (int i = 0; i < 3; i++)
250255
{

0 commit comments

Comments
 (0)