Skip to content

Commit

Permalink
sky shader: rather use noise textures for noise lookups instead of co…
Browse files Browse the repository at this point in the history
…mpute noise in the shader
  • Loading branch information
andreasdr committed Jul 11, 2024
1 parent f8d88ff commit 5903bd8
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 8 deletions.
Binary file added resources/engine/textures/clouds_bottom.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added resources/engine/textures/clouds_middle.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added resources/engine/textures/clouds_top.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
17 changes: 11 additions & 6 deletions shader/gl3/framebuffer/sky/render_fragmentshader.frag
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ uniform vec3 LIGHT0_DIRECTION;
uniform int LIGHT1_ENABLED;
uniform vec3 LIGHT1_DIRECTION;
uniform sampler2D stars_texture;
uniform sampler2D clouds_top_texture;
uniform sampler2D clouds_middle_texture;
uniform sampler2D clouds_bottom_texture;

uniform int lightScatteringPass;
uniform float time;
Expand Down Expand Up @@ -115,6 +118,7 @@ uniform float stars_speed;
uniform float stars_speed : hint_range( 0.0, 20.0, 0.01 ) = 1.0;
*/

/*
////////////////////////////////////////////////////////////////////////////////////////////////////
// Function for clouds noises. You can replace using "gen_fractal_ping_pong" with a simple texture reading.
// I was frustrated with the repeating texture that's why I included the algorithm in the code.
Expand Down Expand Up @@ -195,6 +199,7 @@ uniform float stars_speed;
return sum * 0.5 + 0.5;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
*/

// Function needed to calculate the phase of the moon
// Source: https://kelvinvanhoorn.com/2022/03/17/skybox-tutorial-part-1/
Expand Down Expand Up @@ -325,14 +330,14 @@ void main(void)
// I using 3 levels of clouds. Top is the lightes and botom the darkest.
// The speed of movement (and direction a little) is different for the illusion of the changing shape of the clouds.
vec2 _clouds_movement = vec2( _sin_x, _cos_y ) * _clouds_speed;
// float _noise_top = texture( clouds_top_texture, ( _sky_uv + _clouds_movement ) * clouds_scale ).r;
float _noise_top = gen_fractal_ping_pong( ( _sky_uv + _clouds_movement ) * clouds_scale, 0, 0.5 );
float _noise_top = texture( clouds_top_texture, ( _sky_uv + _clouds_movement ) * clouds_scale ).r;
// float _noise_top = gen_fractal_ping_pong( ( _sky_uv + _clouds_movement ) * clouds_scale, 0, 0.5 );
_clouds_movement = vec2( _sin_x * 0.97, _cos_y * 1.07 ) * _clouds_speed * 0.89;
// float _noise_middle = texture( clouds_middle_texture, ( _sky_uv + _clouds_movement ) * clouds_scale ).r;
float _noise_middle = gen_fractal_ping_pong( ( _sky_uv + _clouds_movement ) * clouds_scale, 1, 0.75 );
float _noise_middle = texture( clouds_middle_texture, ( _sky_uv + _clouds_movement ) * clouds_scale ).r;
// float _noise_middle = gen_fractal_ping_pong( ( _sky_uv + _clouds_movement ) * clouds_scale, 1, 0.75 );
_clouds_movement = vec2( _sin_x * 1.01, _cos_y * 0.89 ) * _clouds_speed * 0.79;
// float _noise_bottom = texture( clouds_bottom_texture, ( _sky_uv + _clouds_movement ) * clouds_scale ).r;
float _noise_bottom = gen_fractal_ping_pong( ( _sky_uv + _clouds_movement ) * clouds_scale, 2, 1.0 );
float _noise_bottom = texture( clouds_bottom_texture, ( _sky_uv + _clouds_movement ) * clouds_scale ).r;
// float _noise_bottom = gen_fractal_ping_pong( ( _sky_uv + _clouds_movement ) * clouds_scale, 2, 1.0 );
// Smoothstep with the addition of a noise value from a lower level gives a nice, deep result
_noise_bottom = smoothstep( clouds_cutoff, clouds_cutoff + clouds_fuzziness, _noise_bottom );
_noise_middle = smoothstep( clouds_cutoff, clouds_cutoff + clouds_fuzziness, _noise_middle + _noise_bottom * 0.2 ) * 1.1;
Expand Down
53 changes: 51 additions & 2 deletions src/tdme/engine/subsystems/framebuffer/SkyRenderShader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,12 @@ void SkyRenderShader::initialize()
if (uniformLIGHT1_DIRECTION == -1) return;
uniformStarsTexture = renderer->getProgramUniformLocation(programId, "stars_texture");
if (uniformStarsTexture == -1) return;
uniformCloudsTopTexture = renderer->getProgramUniformLocation(programId, "clouds_top_texture");
if (uniformCloudsTopTexture == -1) return;
uniformCloudsMiddleTexture = renderer->getProgramUniformLocation(programId, "clouds_middle_texture");
if (uniformCloudsMiddleTexture == -1) return;
uniformCloudsBottomTexture = renderer->getProgramUniformLocation(programId, "clouds_bottom_texture");
if (uniformCloudsBottomTexture == -1) return;
uniformLightScatteringPass = renderer->getProgramUniformLocation(programId, "lightScatteringPass");
if (uniformLightScatteringPass == -1) return;
uniformTime = renderer->getProgramUniformLocation(programId, "time");
Expand Down Expand Up @@ -233,11 +239,32 @@ void SkyRenderShader::unloadTextures() {
starsTexture = nullptr;
starsTextureId = renderer->ID_NONE;
}
if (cloudsTopTexture != nullptr) {
Engine::getInstance()->getTextureManager()->removeTexture(cloudsTopTexture);
cloudsTopTexture->releaseReference();
cloudsTopTexture = nullptr;
cloudsTopTextureId = renderer->ID_NONE;
}
if (cloudsMiddleTexture != nullptr) {
Engine::getInstance()->getTextureManager()->removeTexture(cloudsMiddleTexture);
cloudsMiddleTexture->releaseReference();
cloudsMiddleTexture = nullptr;
cloudsMiddleTextureId = renderer->ID_NONE;
}
if (cloudsBottomTexture != nullptr) {
Engine::getInstance()->getTextureManager()->removeTexture(cloudsBottomTexture);
cloudsBottomTexture->releaseReference();
cloudsBottomTexture = nullptr;
cloudsBottomTextureId = renderer->ID_NONE;
}
}

void SkyRenderShader::loadTextures(const string& pathName) {
unloadTextures();
starsTextureId = Engine::getInstance()->getTextureManager()->addTexture(starsTexture = TextureReader::read(pathName + "/resources/engine/textures", "stars.png"), renderer->CONTEXTINDEX_DEFAULT);
cloudsTopTextureId = Engine::getInstance()->getTextureManager()->addTexture(cloudsTopTexture = TextureReader::read(pathName + "/resources/engine/textures", "clouds_top.png"), renderer->CONTEXTINDEX_DEFAULT);
cloudsMiddleTextureId = Engine::getInstance()->getTextureManager()->addTexture(cloudsMiddleTexture = TextureReader::read(pathName + "/resources/engine/textures", "clouds_middle.png"), renderer->CONTEXTINDEX_DEFAULT);
cloudsBottomTextureId = Engine::getInstance()->getTextureManager()->addTexture(cloudsBottomTexture = TextureReader::read(pathName + "/resources/engine/textures", "clouds_bottom.png"), renderer->CONTEXTINDEX_DEFAULT);
}

void SkyRenderShader::render(Engine* engine, bool lightScatteringPass, Camera* camera) {
Expand Down Expand Up @@ -282,8 +309,10 @@ void SkyRenderShader::render(Engine* engine, bool lightScatteringPass, Camera* c
renderer->setProgramUniformFloatVec3(contextIdx, uniformSideVector, camera->getSideVector().getArray());
renderer->setProgramUniformFloatVec3(contextIdx, uniformUpVector, camera->getUpVector().getArray());
//
renderer->setTextureUnit(contextIdx, 0);
renderer->bindTexture(contextIdx, starsTextureId);
renderer->setProgramUniformInteger(contextIdx, uniformStarsTexture, 0);
renderer->setProgramUniformInteger(contextIdx, uniformCloudsTopTexture, 1);
renderer->setProgramUniformInteger(contextIdx, uniformCloudsMiddleTexture, 2);
renderer->setProgramUniformInteger(contextIdx, uniformCloudsBottomTexture, 3);
// sky
renderer->setProgramUniformFloatVec3(contextIdx, uniformDayTopColor, engine->getShaderParameter("sky", "day_top_color").getColor3ValueArray());
renderer->setProgramUniformFloatVec3(contextIdx, uniformDayBottomColor, engine->getShaderParameter("sky", "day_bottom_color").getColor3ValueArray());
Expand Down Expand Up @@ -321,7 +350,20 @@ void SkyRenderShader::render(Engine* engine, bool lightScatteringPass, Camera* c
//
renderer->disableDepthBufferWriting();
renderer->disableDepthBufferTest();
//
renderer->disableCulling(contextIdx);
//
renderer->setTextureUnit(contextIdx, 0);
renderer->bindTexture(contextIdx, starsTextureId);
//
renderer->setTextureUnit(contextIdx, 1);
renderer->bindTexture(contextIdx, cloudsTopTextureId);
//
renderer->setTextureUnit(contextIdx, 2);
renderer->bindTexture(contextIdx, cloudsMiddleTextureId);
//
renderer->setTextureUnit(contextIdx, 3);
renderer->bindTexture(contextIdx, cloudsBottomTextureId);

// use frame buffer render shader
auto frameBufferRenderShader = Engine::getFrameBufferRenderShader();
Expand All @@ -336,9 +378,16 @@ void SkyRenderShader::render(Engine* engine, bool lightScatteringPass, Camera* c
// unbind buffers
renderer->unbindBufferObjects(contextIdx);

//
//
renderer->setTextureUnit(contextIdx, 0);
renderer->bindTexture(contextIdx, renderer->ID_NONE);
renderer->setTextureUnit(contextIdx, 1);
renderer->bindTexture(contextIdx, renderer->ID_NONE);
renderer->setTextureUnit(contextIdx, 2);
renderer->bindTexture(contextIdx, renderer->ID_NONE);
renderer->setTextureUnit(contextIdx, 3);
renderer->bindTexture(contextIdx, renderer->ID_NONE);

// unset
renderer->enableCulling(contextIdx);
Expand Down
9 changes: 9 additions & 0 deletions src/tdme/engine/subsystems/framebuffer/SkyRenderShader.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ class tdme::engine::subsystems::framebuffer::SkyRenderShader final
int32_t uniformLIGHT1_DIRECTION { -1 };
int32_t uniformLIGHT1_ENABLED { -1 };
int32_t uniformStarsTexture { -1 };
int32_t uniformCloudsTopTexture { -1 };
int32_t uniformCloudsMiddleTexture { -1 };
int32_t uniformCloudsBottomTexture { -1 };

// sky
int32_t uniformDayTopColor { -1 };
Expand Down Expand Up @@ -76,6 +79,12 @@ class tdme::engine::subsystems::framebuffer::SkyRenderShader final

Texture* starsTexture { nullptr };
int32_t starsTextureId { 0 };
Texture* cloudsTopTexture { nullptr };
int32_t cloudsTopTextureId { 0 };
Texture* cloudsMiddleTexture { nullptr };
int32_t cloudsMiddleTextureId { 0 };
Texture* cloudsBottomTexture { nullptr };
int32_t cloudsBottomTextureId { 0 };

bool initialized { false };

Expand Down

0 comments on commit 5903bd8

Please sign in to comment.