Skip to content

Commit 6ba05d1

Browse files
committed
cleanup
1 parent 5cdd403 commit 6ba05d1

10 files changed

Lines changed: 52 additions & 55 deletions

File tree

.gitattributes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
*.comp linguist-language=HLSL
22
*.frag linguist-language=HLSL
3+
*.inc linguist-language=C
34
*.vert linguist-language=HLSL
45
lib/** linguist-vendored

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ cd bin
4444

4545
#### Shaders
4646

47-
Shaders are prebuilt.
48-
To build locally, install [SDL_shadercross](https://github.com/libsdl-org/SDL_shadercross) to your path
47+
Shaders are precompiled.
48+
To build locally, add [SDL_shadercross](https://github.com/libsdl-org/SDL_shadercross) to your path
4949

5050
### Controls
5151

shaders/bin/transparent.frag.msl

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -160,18 +160,17 @@ fragment main0_out main0(main0_in in [[stage_in]], constant type_UniformBuffer&
160160
}
161161
break; // unreachable workaround
162162
} while(false);
163-
float3 _250 = in.in_var_TEXCOORD0.xyz - UniformBuffer_2.PlayerPosition;
164-
float4 _272 = positionTexture.sample(positionSampler, in.in_var_TEXCOORD4);
165-
float _284;
163+
float3 _246 = in.in_var_TEXCOORD0.xyz - UniformBuffer_2.PlayerPosition;
164+
float _278;
166165
if (((in.in_var_TEXCOORD3 >> 26u) & 63u) == 16u)
167166
{
168-
_284 = _108 + ((in.in_var_TEXCOORD0.y - _272.y) * 0.100000001490116119384765625);
167+
_278 = _108 + ((in.in_var_TEXCOORD0.y - positionTexture.sample(positionSampler, in.in_var_TEXCOORD4).y) * 0.100000001490116119384765625);
169168
}
170169
else
171170
{
172-
_284 = _108;
171+
_278 = _108;
173172
}
174-
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(_250.y, length(float2(_250.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))), _284);
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);
175174
return out;
176175
}
177176

shaders/bin/transparent.frag.spv

0 Bytes
Binary file not shown.

shaders/composite.comp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ void main(uint3 threadID : SV_DispatchThreadID)
4141
float sun = GetSunLight(shadowTexture, shadowSampler, ShadowTransform, position, normal, voxel);
4242
float ssao = ssaoTexture[threadID.xy] * kSSAO;
4343
float3 color;
44-
color = albedo;
4544
if (GetOcclusion(voxel))
4645
{
4746
color = albedo * (diffuse + ambient + sun - ssao);
@@ -56,9 +55,9 @@ void main(uint3 threadID : SV_DispatchThreadID)
5655
}
5756
if (!IsSky(voxel))
5857
{
59-
float3 skyColor = GetSkyColor(position - PlayerPosition);
58+
float3 sky = GetSkyColor(position - PlayerPosition);
6059
float fog = GetFog(distance(position.xz, PlayerPosition.xz));
61-
color = lerp(color, skyColor, fog);
60+
color = lerp(color, sky, fog);
6261
}
6362
compositeTexture[threadID.xy] = float4(color, 1.0f);
6463
}

shaders/shader.hlsl

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -84,14 +84,14 @@ float3 GetCubePosition(uint vertexID)
8484
return kPositions[kIndices[vertexID]];
8585
}
8686

87-
bool IsCloud(float3 color)
87+
bool IsSky(uint voxel)
8888
{
89-
return length(color) > (1.0f - kEpsilon);
89+
return voxel == 0;
9090
}
9191

92-
bool IsSky(uint voxel)
92+
bool IsCloud(float3 color)
9393
{
94-
return voxel == 0;
94+
return length(color) > (1.0f - kEpsilon);
9595
}
9696

9797
struct Light
@@ -102,11 +102,6 @@ struct Light
102102
int Z;
103103
};
104104

105-
float3 GetAmbientLight()
106-
{
107-
return float3(0.5f, 0.5f, 0.5f);
108-
}
109-
110105
float3 GetDiffuseLight(StructuredBuffer<Light> lights, uint lightCount, float4 position, float3 normal)
111106
{
112107
static const float kBias = 0.1f;
@@ -184,22 +179,26 @@ float GetSunLight(Texture2D<float> texture, SamplerState sampler, float4x4 trans
184179
}
185180
}
186181

182+
float3 GetAmbientLight()
183+
{
184+
return float3(0.5f, 0.5f, 0.5f);
185+
}
186+
187+
float GetFog(float x)
188+
{
189+
return min(pow(x / 250.0f, 2.5f), 1.0f);
190+
}
191+
187192
float3 GetSkyColor(float3 position)
188193
{
189194
static const float3 kTop = float3(0.212f, 0.773f, 0.957f);
190195
static const float3 kBottom = float3(0.220f, 0.349f, 0.702f);
191196
float dy = position.y;
192197
float dx = length(float2(position.x, position.z));
193-
float pitch = atan2(dy, dx);
194-
float alpha = (pitch + kPi / 2.0f) / kPi;
198+
float alpha = (atan2(dy, dx) + kPi / 2.0f) / kPi;
195199
return lerp(kBottom, kTop, alpha);
196200
}
197201

198-
float GetFog(const float x)
199-
{
200-
return min(pow(x / 250.0f, 2.5f), 1.0f);
201-
}
202-
203202
float2 GetRandom2(float2 position)
204203
{
205204
float2 k1 = float2(127.1f, 311.7f);

shaders/transparent.frag

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,13 @@ float4 main(Input input) : SV_Target0
4343
float3 diffuse = GetDiffuseLight(lightBuffer, LightCount, position, input.Normal);
4444
float3 ambient = GetAmbientLight();
4545
float sun = GetSunLight(shadowTexture, shadowSampler, ShadowTransform, position.xyz, input.Normal, input.Voxel);
46-
float3 finalColor = albedo * (diffuse + ambient + sun);
47-
float3 skyColor = GetSkyColor(input.WorldPosition.xyz - PlayerPosition);
46+
float3 sky = GetSkyColor(input.WorldPosition.xyz - PlayerPosition);
4847
float fog = GetFog(distance(position.xz, PlayerPosition.xz));
49-
finalColor = lerp(finalColor, skyColor, fog);
50-
float3 groundPosition = positionTexture.Sample(positionSampler, input.Fragment).xyz;
5148
if (GetIndex(input.Voxel) == kWater)
5249
{
53-
// TODO: Causing bug where alpha is 0 or 1 as camera approaches water
50+
// TODO: Causes bug where alpha is 0 or 1 as camera approaches water
51+
float3 groundPosition = positionTexture.Sample(positionSampler, input.Fragment).xyz;
5452
alpha += (input.WorldPosition.y - groundPosition.y) / 10.0f;
5553
}
56-
return float4(finalColor, alpha);
54+
return float4(lerp(albedo * (diffuse + ambient + sun), sky, fog), alpha);
5755
}

src/camera.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,7 @@ void camera_move(camera_t* camera, float x, float y, float z)
202202
camera->x += cp * (sy * z) + cy * x;
203203
camera->y += y + z * sp;
204204
camera->z -= cp * (cy * z) - sy * x;
205+
camera->y = SDL_clamp(camera->y, -camera->far, camera->far);
205206
}
206207

207208
void camera_resize(camera_t* camera, int width, int height)

src/rand.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ void rand_get_blocks(void* userdata, int cx, int cz, rand_set_block_t function)
99
for (int a = 0; a < CHUNK_WIDTH; a++)
1010
for (int b = 0; b < CHUNK_WIDTH; b++)
1111
{
12-
const int x = cx + a;
13-
const int z = cz + b;
12+
int x = cx + a;
13+
int z = cz + b;
1414
bool low = false;
1515
bool grass = false;
1616
float height = stb_perlin_fbm_noise3(x * 0.005f, 0.0f, z * 0.005f, 2.0f, 0.5f, 6);
@@ -64,10 +64,10 @@ void rand_get_blocks(void* userdata, int cx, int cz, rand_set_block_t function)
6464
}
6565
if (low && grass)
6666
{
67-
const float plant = stb_perlin_fbm_noise3(x * 0.2f, 0.0f, z * 0.2f, 2.0f, 0.5f, 3) * 0.5f + 0.5f;
67+
float plant = stb_perlin_fbm_noise3(x * 0.2f, 0.0f, z * 0.2f, 2.0f, 0.5f, 3) * 0.5f + 0.5f;
6868
if (plant > 0.8f && a > 2 && a < CHUNK_WIDTH - 2 && b > 2 && b < CHUNK_WIDTH - 2)
6969
{
70-
const int log = 3 + plant * 2.0f;
70+
int log = 3 + plant * 2.0f;
7171
for (int dy = 0; dy < log; dy++)
7272
{
7373
function(userdata, x, y + dy + 1, z, BLOCK_LOG);
@@ -88,16 +88,16 @@ void rand_get_blocks(void* userdata, int cx, int cz, rand_set_block_t function)
8888
}
8989
else if (plant > 0.52f)
9090
{
91-
const int value = SDL_max(((int) (plant * 1000.0f)) % 4, 0);
92-
const block_t flowers[] = {BLOCK_BLUEBELL, BLOCK_GARDENIA, BLOCK_LAVENDER, BLOCK_ROSE};
91+
int value = SDL_max(((int) (plant * 1000.0f)) % 4, 0);
92+
block_t flowers[] = {BLOCK_BLUEBELL, BLOCK_GARDENIA, BLOCK_LAVENDER, BLOCK_ROSE};
9393
function(userdata, x, y + 1, z, flowers[value]);
9494
}
9595
}
9696
if (height > 130)
9797
{
9898
continue;
9999
}
100-
const float cloud = stb_perlin_turbulence_noise3(x * 0.015f, 0.0f, z * 0.015f, 2.0f, 0.5f, 6);
100+
float cloud = stb_perlin_turbulence_noise3(x * 0.015f, 0.0f, z * 0.015f, 2.0f, 0.5f, 6);
101101
int scale = -1;
102102
if (cloud > 0.9f)
103103
{

src/world.c

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -340,8 +340,8 @@ static void gen_chunk_blocks(chunk_t* chunk)
340340
map_clear(&chunk->lights);
341341
rand_get_blocks(chunk, chunk->x, chunk->z, set_chunk_block_function);
342342
save_get_blocks(chunk, chunk->x, chunk->z, set_chunk_block_function);
343+
CHECK(SDL_GetAtomicInt(&chunk->block_state) == JOB_STATE_RUNNING);
343344
SDL_SetAtomicInt(&chunk->block_state, JOB_STATE_COMPLETED);
344-
// Schedule because neighbors might have lights
345345
if (SDL_GetAtomicInt(&chunk->voxel_state) == JOB_STATE_REQUESTED)
346346
{
347347
SDL_SetAtomicInt(&chunk->light_state, JOB_STATE_REQUESTED);
@@ -393,6 +393,15 @@ static void gen_chunk_voxels(chunk_t* chunks[3][3], cpu_buffer_t voxels[MESH_TYP
393393
SDL_SetAtomicInt(&chunk->voxel_state, JOB_STATE_COMPLETED);
394394
}
395395

396+
static void regen_chunk_voxels(int x, int z)
397+
{
398+
CHECK(!is_chunk_on_border(x, z));
399+
chunk_t* chunks[3][3] = {0};
400+
get_neighborhood(x, z, chunks);
401+
SDL_SetAtomicInt(&chunks[1][1]->voxel_state, JOB_STATE_RUNNING);
402+
gen_chunk_voxels(chunks, cpu_voxels);
403+
}
404+
396405
static void gen_chunk_lights(chunk_t* chunks[3][3], cpu_buffer_t* lights)
397406
{
398407
chunk_t* chunk = chunks[1][1];
@@ -962,15 +971,6 @@ block_t world_get_block(const int position[3])
962971
}
963972
}
964973

965-
static void gen_voxels_sync(int x, int z)
966-
{
967-
CHECK(!is_chunk_on_border(x, z));
968-
chunk_t* chunks[3][3] = {0};
969-
get_neighborhood(x, z, chunks);
970-
SDL_SetAtomicInt(&chunks[1][1]->voxel_state, JOB_STATE_RUNNING);
971-
gen_chunk_voxels(chunks, cpu_voxels);
972-
}
973-
974974
void world_set_block(const int position[3], block_t block)
975975
{
976976
if (position[1] < 0 || position[1] >= CHUNK_HEIGHT)
@@ -1002,22 +1002,22 @@ void world_set_block(const int position[3], block_t block)
10021002
int local_z = position[2];
10031003
world_to_chunk(chunk, &local_x, &local_y, &local_z);
10041004
block_t old_block = set_chunk_block(chunk, position[0], position[1], position[2], block);
1005-
gen_voxels_sync(chunk_x, chunk_z);
1005+
regen_chunk_voxels(chunk_x, chunk_z);
10061006
if (local_x == 0)
10071007
{
1008-
gen_voxels_sync(chunk_x - 1, chunk_z);
1008+
regen_chunk_voxels(chunk_x - 1, chunk_z);
10091009
}
10101010
else if (local_x == CHUNK_WIDTH - 1)
10111011
{
1012-
gen_voxels_sync(chunk_x + 1, chunk_z);
1012+
regen_chunk_voxels(chunk_x + 1, chunk_z);
10131013
}
10141014
if (local_z == 0)
10151015
{
1016-
gen_voxels_sync(chunk_x, chunk_z - 1);
1016+
regen_chunk_voxels(chunk_x, chunk_z - 1);
10171017
}
10181018
else if (local_z == CHUNK_WIDTH - 1)
10191019
{
1020-
gen_voxels_sync(chunk_x, chunk_z + 1);
1020+
regen_chunk_voxels(chunk_x, chunk_z + 1);
10211021
}
10221022
chunk_t* neighborhood[3][3] = {0};
10231023
get_neighborhood(chunk_x, chunk_z, neighborhood);

0 commit comments

Comments
 (0)