Auto load correct sun shadder

This commit is contained in:
nikola
2024-12-01 19:44:58 +01:00
parent ed9d049fa3
commit bbc8b33224
4 changed files with 148 additions and 38 deletions

View File

@@ -44,7 +44,7 @@ void BackGround::draw()
{
DrawRectangleGradientV(0, 0, size, size, ColorAdd(BackGroundColors::backGroundColor, 60), BackGroundColors::backGroundColor);
}
drawSun();
drawMounten(20, (int)(mounten1min * size), (int)(mounten1max * size), BackGroundColors::MountenColor1);
drawMounten(21, (int)(mounten2min * size), (int)(mounten2max * size), BackGroundColors::MountenColor2);
@@ -97,11 +97,13 @@ void BackGround::drawSun()
int xpos = Lerp(size * moonXOffset, size - size * moonXOffset, m_moon.x);
int ypos = Lerp(size * moonXOffset, maxYPosOfMoon * size, m_moon.y);
if (colorSet == 3) {
if (colorSet == 3)
{
sun.setColor(BackGroundColors::moonColor);
r = ((m_moon.size * (maxSizeOfMoon - minSizeOfMoon)) + minSizeOfMoon) * size;
}
else {
else
{
Color color = {0};
color.r = 255;
color.g = std::lerp(200, 50, m_moon.y);

View File

@@ -1,41 +1,18 @@
#include "canvas/Sun.hpp"
#include "sunShader.hpp"
#include "Math.hpp"
#include <raylib.h>
#include <rlgl.h>
const char* sun_shader_100 = "#version 100\n\
precision mediump float;\
varying vec2 fragTexCoord;\
varying vec4 fragColor;\
uniform vec3 color;\
uniform float sun_radius;\
uniform float start_transperency;\
vec2 offset = vec2(1.0, 1.0);\
float sun_end = 1.0;\
void main()\
{\
offset.x -= fragTexCoord.x * 2.0;\
offset.y -= fragTexCoord.y * 2.0;\
float radius = length(offset);\
if (radius < sun_radius) {\
gl_FragColor = vec4(color, 1.0);\
}\
else if (radius < sun_end) {\
float gradient = radius;\
gradient -= sun_radius;\
gradient = gradient / (sun_end - sun_radius) * start_transperency;\
gl_FragColor = vec4(color, start_transperency - gradient);\
}\
else {\
gl_FragColor = vec4(color, 0.0);\
}\
}";
void Sun::init()
{
shader = LoadShaderFromMemory(0, sun_shader_100);
#if defined(PLATFORM_ANDROID) || defined(PLATFORM_WEB)
shader = LoadShaderFromMemory(0, (const char *)shaders_sun_100_fs);
#else
shader = LoadShaderFromMemory(0, (const char *)shaders_sun_330_fs);
#endif
target = LoadRenderTexture(sizeTexute, sizeTexute);
sun_radius_loc = GetShaderLocation(shader, "sun_radius");
@@ -47,7 +24,7 @@ void Sun::init()
colorLoc = GetShaderLocation(shader, "color");
SetShaderValue(shader, colorLoc, c, SHADER_UNIFORM_VEC3);
// resitev da ne postane tekstura okoli sonca transparentna in da se ne vidi nasledna slika
// resitev da ne postane tekstura okoli sonca transparentna in da se ne vidi nasledna slika
// https://github.com/raysan5/raylib/issues/3820
rlSetBlendFactorsSeparate(RL_SRC_ALPHA, RL_ONE_MINUS_SRC_ALPHA, RL_ONE, RL_ONE, RL_FUNC_ADD, RL_MAX);
}
@@ -69,11 +46,11 @@ void Sun::setColor(Color color)
void Sun::draw(float x, float y, float size)
{
Rectangle dest = { x-size, y - size, size * 2, size * 2};
Rectangle dest = {x - size, y - size, size * 2, size * 2};
// zgorni komentar da se mesanje barv oklopi pravilno
BeginBlendMode(RL_BLEND_CUSTOM_SEPARATE);
BeginShaderMode(shader);
drawTexureWithRotation(target, dest, 0.0f);
EndShaderMode();
BeginShaderMode(shader);
drawTexureWithRotation(target, dest, 0.0f);
EndShaderMode();
EndBlendMode();
}