From 79bfdba0f7b4e9d1668617ac463d18b1b26f10ce Mon Sep 17 00:00:00 2001 From: cwj3rcb Date: Sat, 30 Aug 2025 15:43:28 -0500 Subject: [PATCH] Chatgpt shader implementation for png transparency. --- README.md | 2 ++ main.c | 57 ++++++++++++++++++++++++++++++++++++------------------- 2 files changed, 40 insertions(+), 19 deletions(-) diff --git a/README.md b/README.md index e69de29..e9b35d1 100644 --- a/README.md +++ b/README.md @@ -0,0 +1,2 @@ +My first game project. +Made using Raylib. \ No newline at end of file diff --git a/main.c b/main.c index d85e5b2..878d146 100644 --- a/main.c +++ b/main.c @@ -2,6 +2,7 @@ #include "rcamera.h" #include "raymath.h" #include +#include //---------------------------------------------------------------------------------- // 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.3–0.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