Chatgpt shader implementation for png transparency.

This commit is contained in:
2025-08-30 15:43:28 -05:00
parent 48788a7296
commit 79bfdba0f7
2 changed files with 40 additions and 19 deletions

57
main.c
View File

@@ -2,6 +2,7 @@
#include "rcamera.h"
#include "raymath.h"
#include <stdbool.h>
#include <stddef.h>
//----------------------------------------------------------------------------------
// Defines and Macros
@@ -65,6 +66,21 @@ typedef struct
Player player;
} Master;
// GLSL 330 core fragment shader (desktop)
// Discards fragments with alpha below a threshold so depth is only written on real pixels.
static const char *ALPHA_CLIP_FS =
"#version 330 core\n"
"in vec2 fragTexCoord;\n"
"in vec4 fragColor;\n"
"uniform sampler2D texture0;\n"
"uniform float u_alphaCutoff; // e.g., 0.3\n"
"out vec4 finalColor;\n"
"void main(){\n"
" vec4 texel = texture(texture0, fragTexCoord);\n"
" if(texel.a < u_alphaCutoff) discard;\n"
" finalColor = texel * fragColor;\n"
"}\n";
//------------------------------------------------------------------------------------
// Function Initation
//------------------------------------------------------------------------------------
@@ -159,6 +175,11 @@ int main(void)
Model mdlTile = LoadModelFromMesh(meshTile);
mdlTile.materials[0].maps[MATERIAL_MAP_ALBEDO].texture = texGrass;
Shader shAlphaClip = LoadShaderFromMemory(NULL, ALPHA_CLIP_FS);
int locCutoff = GetShaderLocation(shAlphaClip, "u_alphaCutoff");
float cutoff = 0.3f; // tweak to taste; 0.30.5 works well for tree PNGs
SetShaderValue(shAlphaClip, locCutoff, &cutoff, SHADER_UNIFORM_FLOAT);
//--------------------------------------------------------------------------------------
// Main game loop
@@ -281,26 +302,24 @@ int main(void)
BeginMode3D(g.player.camera);
DrawLevel(mdlTile);
BeginShaderMode(shAlphaClip);
{
// Draw your trees here with DrawBillboard / DrawBillboardPro
// Example using your positions:
DrawBillboard(g.player.camera, tree, treepos, treeSize, WHITE);
Vector3 p = treepos;
p.x += 3*5; p.z += 3*5; DrawBillboard(g.player.camera, tree, p, treeSize, WHITE);
p.x += 5*5; p.z += -2*5; DrawBillboard(g.player.camera, tree, p, treeSize, WHITE);
p.x += -4*5; p.z += 3*5; DrawBillboard(g.player.camera, tree, p, treeSize, WHITE);
p.x += 9*5; p.z += 10*5; DrawBillboard(g.player.camera, tree, p, treeSize, WHITE);
// Your sign as a cutout too (if it has a clean alpha)
}
EndShaderMode();
DrawBillboard(g.player.camera, sign, bbPos, bbSize, WHITE);
DrawBillboard(g.player.camera, tree, treepos, treeSize, WHITE);
treepos.x += 3*5;
treepos.z += 3*5;
DrawBillboard(g.player.camera, tree, treepos, treeSize, WHITE);
treepos.x += 5*5;
treepos.z += -2*5;
DrawBillboard(g.player.camera, tree, treepos, treeSize, WHITE);
treepos.x += -4*5;
treepos.z += 3*5;
DrawBillboard(g.player.camera, tree, treepos, treeSize, WHITE);
treepos.x += 9*5;
treepos.z += 10*5;
DrawBillboard(g.player.camera, tree, treepos, treeSize, WHITE);
EndMode3D();
// Draw info box