Skip to content

Commit

Permalink
add background to glass tubes
Browse files Browse the repository at this point in the history
  • Loading branch information
fallahn committed Oct 28, 2024
1 parent 435639d commit fc53aa0
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 98 deletions.
4 changes: 2 additions & 2 deletions samples/golf/src/scrub/ScrubConsts.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ source distribution.

#include <cstdint>

#define HIDE_BACKGROUND
//#define HIDE_BACKGROUND

static constexpr inline float MaxSpriteScale = 4.f; //when view scale returns this (~2kHD) sprites on the HUD are 1:1
static constexpr cro::Colour SoapMeterColour = cro::Colour(0xadd9b7ff);
static constexpr cro::Colour SoapMeterColour = cro::Colour(0x0207ffff);// cro::Colour(0xadd9b7ff);

//xbox
static constexpr std::uint32_t ButtonLB = 0x2198;
Expand Down
83 changes: 44 additions & 39 deletions samples/golf/src/scrub/ScrubGameState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -786,10 +786,10 @@ void ScrubGameState::createScene()
resize(cam);

cam.shadowMapBuffer.create(2048, 2048);
cam.setMaxShadowDistance(2.f);
cam.setMaxShadowDistance(0.4f);
cam.setShadowExpansion(0.5f);

//callback is set up in resetCamera();
//callback is set up in resetCamera();
camera.addComponent<cro::Callback>();

m_gameScene.getSunlight().getComponent<cro::Transform>().rotate(cro::Transform::X_AXIS, -1.2f);
Expand Down Expand Up @@ -1071,6 +1071,9 @@ void ScrubGameState::createUI()
static constexpr float ProgBarHeight = 180.f;
static constexpr float ProgBarWidth = 800.f;

auto& noiseTex = m_resources.textures.get("assets/golf/images/wind.png");
noiseTex.setRepeated(true);

entity = m_uiScene.createEntity();
entity.addComponent<cro::Transform>();
entity.addComponent<cro::Drawable2D>().setVertexData(
Expand All @@ -1081,7 +1084,7 @@ void ScrubGameState::createUI()
cro::Vertex2D(glm::vec2(ProgBarWidth, 0.f), glm::vec2(1.f, 0.2f), cro::Colour::White)
});
m_resources.textures.setFallbackColour(cro::Colour::White);
entity.getComponent<cro::Drawable2D>().setTexture(&m_resources.textures.get("fallback"));
entity.getComponent<cro::Drawable2D>().setTexture(&noiseTex);
entity.getComponent<cro::Drawable2D>().setShader(&m_resources.shaders.get(sc::ShaderID::Fire));
entity.getComponent<cro::Drawable2D>().setBlendMode(cro::Material::BlendMode::Additive);
entity.addComponent<cro::CommandTarget>().ID = CommandID::UI::UIElement;
Expand Down Expand Up @@ -1115,23 +1118,34 @@ void ScrubGameState::createUI()
m_spriteRoot.getComponent<cro::Transform>().addChild(entity.getComponent<cro::Transform>());


//soap level
entity = m_uiScene.createEntity();
entity.addComponent<cro::Transform>();
entity.addComponent<cro::Drawable2D>().setVertexData(
const auto createLevelMeter =
[&](cro::Colour c)
{
cro::Vertex2D(glm::vec2(0.f, BarHeight), SoapMeterColour),
cro::Vertex2D(glm::vec2(0.f), SoapMeterColour),
cro::Vertex2D(glm::vec2(BarWidth, BarHeight), SoapMeterColour),
cro::Vertex2D(glm::vec2(BarWidth, 0.f), SoapMeterColour)
}
);
entity.getComponent<cro::Drawable2D>().setShader(&m_resources.shaders.get(sc::ShaderID::LevelMeter));
entity.getComponent<cro::Drawable2D>().setTexture(&bgTex);
entity.addComponent<cro::CommandTarget>().ID = CommandID::UI::UIElement;
entity.addComponent<UIElement>().relativePosition = glm::vec2(1.f, 0.f);
entity.getComponent<UIElement>().absolutePosition = { -(BarWidth + 12.f), 12.f};
entity.getComponent<UIElement>().resizeCallback = std::bind(&ScrubGameState::levelMeterCallback, this, std::placeholders::_1);
auto ent = m_uiScene.createEntity();
ent.addComponent<cro::Transform>();
ent.addComponent<cro::Drawable2D>().setVertexData(
{
cro::Vertex2D(glm::vec2(0.f, BarHeight), c),
cro::Vertex2D(glm::vec2(0.f), c),
cro::Vertex2D(glm::vec2(BarWidth, BarHeight), c),
cro::Vertex2D(glm::vec2(BarWidth, 0.f), c)
}
);
ent.getComponent<cro::Drawable2D>().setShader(&m_resources.shaders.get(sc::ShaderID::LevelMeter));
ent.getComponent<cro::Drawable2D>().setTexture(&bgTex);
ent.addComponent<cro::CommandTarget>().ID = CommandID::UI::UIElement;
ent.addComponent<UIElement>().depth = -2.f;
ent.getComponent<UIElement>().resizeCallback = std::bind(&ScrubGameState::levelMeterCallback, this, std::placeholders::_1);
m_spriteRoot.getComponent<cro::Transform>().addChild(ent.getComponent<cro::Transform>());

return ent;
};


//soap level
entity = createLevelMeter(SoapMeterColour);
entity.getComponent<UIElement>().relativePosition = glm::vec2(1.f, 0.f);
entity.getComponent<UIElement>().absolutePosition = { -(BarWidth + 12.f), 12.f };
entity.addComponent<cro::Callback>().active = true;
entity.getComponent<cro::Callback>().setUserData<float>(1.f);
entity.getComponent<cro::Callback>().function =
Expand Down Expand Up @@ -1162,27 +1176,15 @@ void ScrubGameState::createUI()
cro::FloatRect cropping = { 0.f, 0.f, BarWidth, BarHeight * currPos };
e.getComponent<cro::Drawable2D>().setCroppingArea(cropping);
};
m_spriteRoot.getComponent<cro::Transform>().addChild(entity.getComponent<cro::Transform>());


auto tubeEnt = createLevelMeter(cro::Colour::White);
tubeEnt.getComponent<UIElement>().depth = -0.1f;
entity.getComponent<cro::Transform>().addChild(tubeEnt.getComponent<cro::Transform>());

//current ball cleanliness - TODO make this a generic level meter so we can
//recycle it with other levels on screen (and use the glass tube effect)
entity = m_uiScene.createEntity();
entity.addComponent<cro::Transform>();
entity.addComponent<cro::Drawable2D>().setVertexData(
{
cro::Vertex2D(glm::vec2(0.f, BarHeight), cro::Colour::Blue),
cro::Vertex2D(glm::vec2(0.f), cro::Colour::Blue),
cro::Vertex2D(glm::vec2(BarWidth, BarHeight), cro::Colour::Blue),
cro::Vertex2D(glm::vec2(BarWidth, 0.f), cro::Colour::Blue)
}
);
entity.getComponent<cro::Drawable2D>().setShader(&m_resources.shaders.get(sc::ShaderID::LevelMeter));
entity.getComponent<cro::Drawable2D>().setTexture(&bgTex);
entity.addComponent<cro::CommandTarget>().ID = CommandID::UI::UIElement;
entity.addComponent<UIElement>().relativePosition = glm::vec2(1.f, 0.f);
//current ball cleanliness
entity = createLevelMeter(cro::Colour::Blue);
entity.getComponent<UIElement>().relativePosition = glm::vec2(1.f, 0.f);
entity.getComponent<UIElement>().absolutePosition = { -((BarWidth + 12.f) * 2.f), 12.f };
entity.getComponent<UIElement>().resizeCallback = std::bind(&ScrubGameState::levelMeterCallback, this, std::placeholders::_1);
entity.addComponent<cro::Callback>().active = true;
entity.getComponent<cro::Callback>().function =
[&](cro::Entity e, float dt)
Expand All @@ -1200,7 +1202,10 @@ void ScrubGameState::createUI()
v.colour = c;
}
};
m_spriteRoot.getComponent<cro::Transform>().addChild(entity.getComponent<cro::Transform>());

tubeEnt = createLevelMeter(cro::Colour::White);
tubeEnt.getComponent<UIElement>().depth = -0.1f;
entity.getComponent<cro::Transform>().addChild(tubeEnt.getComponent<cro::Transform>());

auto resize = [&](cro::Camera& cam) mutable
{
Expand Down
71 changes: 14 additions & 57 deletions samples/golf/src/scrub/Shaders.inl
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ void main()
u = pow(((u * 2.0) - 1.0), 3.0);
coord.x += u * DistortionAmount;
FRAG_OUT = mix(TEXTURE(u_texture, coord), v_colour, 0.5 + (abs(u) * 0.3)) + vec4(highlight * HighlightAmount);
FRAG_OUT = mix(TEXTURE(u_texture, coord), v_colour, 0.25 + (abs(u) * 0.3)) + vec4(highlight * HighlightAmount);
})";

/*
Expand All @@ -74,79 +74,36 @@ VARYING_IN vec4 v_colour;
OUTPUT
const vec4 brighterColour = vec4(0.316, 0.766, 1.0, 0.25);
const vec4 darkerColour = vec4(0.1, 0.309, 0.687, 0.0625);
float rand(vec2 co)
{
return fract(sin(dot(co.xy ,vec2(12.9898,78.233))) * 43758.5453);
}
float hermite(float t)
{
return t * t * (3.0 - 2.0 * t);
}
float noise(vec2 co, float frequency)
{
vec2 v = vec2(co.x * frequency, co.y * frequency);
float ix1 = floor(v.x);
float iy1 = floor(v.y);
float ix2 = floor(v.x + 1.0);
float iy2 = floor(v.y + 1.0);
float fx = hermite(fract(v.x));
float fy = hermite(fract(v.y));
float fade1 = mix(rand(vec2(ix1, iy1)), rand(vec2(ix2, iy1)), fx);
float fade2 = mix(rand(vec2(ix1, iy2)), rand(vec2(ix2, iy2)), fx);
return mix(fade1, fade2, fy);
}
float pnoise(vec2 co, float freq, int steps, float persistence)
{
float value = 0.0;
float ampl = 1.0;
float sum = 0.0;
for(int i=0 ; i<steps ; i++)
{
sum += ampl;
value += noise(co, freq) * ampl;
freq *= 2.0;
ampl *= persistence;
}
return value / sum;
}
//const vec4 brighterColour = vec4(1.0, 0.65, 0.1, 0.25);
//const vec4 darkerColour = vec4(1.0, 0.0, 0.15, 0.0625);
void main()
{
vec2 uv = v_texCoord;
float gradient = 1.0 - uv.y;
float gradientStep = 0.2;
vec2 pos = v_texCoord * 0.5;
pos.y -= u_time * 0.03125;
//TODO this is the ratio of the drawable
//tho we may as well just use one of the existing noise textures
//(as the drawable in this instance is textured with a dummy anyway)
//then just sample that instead of creating a noise value with a function
vec2 pos = vec2(uv.x, uv.x * 0.225) * 0.5;
pos.y -= u_time * 0.3125;
vec4 brighterColour = vec4(1.0, 0.65, 0.1, 0.25);
vec4 darkerColour = vec4(1.0, 0.0, 0.15, 0.0625);
vec4 middleColour = mix(brighterColour, darkerColour, 0.5);
float noiseTexel = pnoise(pos, 10.0, 5, 0.5);
float noiseTexel = TEXTURE(u_texture, pos).r;
float firstStep = smoothstep(0.0, noiseTexel, gradient);
float darkerColourStep = smoothstep(0.0, noiseTexel, gradient - gradientStep);
float darkerColourPath = firstStep - darkerColourStep;
vec4 colour = mix(brighterColour, darkerColour, darkerColourPath);
vec4 colour = mix(darkerColour, brighterColour, darkerColourPath);
float middleColourStep = smoothstep(0.0, noiseTexel, gradient - 0.2 * 2.0);
colour = mix(colour, middleColour, darkerColourStep - middleColourStep);
colour = mix(vec4(0.0), colour, firstStep);
colour = mix(vec4(0.0), colour, firstStep) * 0.75;
//colour.rgb *= colour.a;
colour.a = 1.0;
FRAG_OUT = colour * v_colour * TEXTURE(u_texture, v_texCoord);
colour.a = 1.0;
FRAG_OUT = colour * v_colour;// * TEXTURE(u_texture, v_texCoord);
})";

0 comments on commit fc53aa0

Please sign in to comment.