Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Wormhole Shader with Inward Movement Effect #2131

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions resources/shaders/wormhole.shader
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
[vertex]
uniform vec4 color;
uniform mat4 view;
uniform mat4 projection;
uniform mat4 model;

attribute vec3 position;
attribute vec2 texcoords;

varying vec2 fragtexcoords;
varying vec4 fragcolor;

void main()
{
fragtexcoords = texcoords;
gl_Position = projection * ((view * model * vec4(position, 1.0)) + vec4((texcoords.x - 0.5) * color.a, (texcoords.y - 0.5) * color.a, 0.0, 0.0));
fragcolor = vec4(color.rgb, 1.0);
}

[fragment]
uniform sampler2D textureMap;
uniform float time;
varying vec4 fragcolor;
varying vec2 fragtexcoords;

// Adjusted parameters
const float spiralIntensity = 0.08;
const float spiralFrequency = 5.0;
const float spiralSpeed = 0.3;
const float inwardIntensity = 0.15;
const float inwardFrequency = 3.0;
const float inwardSpeed = 0.1;
const float colorShiftIntensity = 0.05;
const float colorShiftFrequency = 1.0;
const float effectSize = 0.6; // Adjust this value to control how large the effect grows

void main()
{
vec2 center = vec2(0.5, 0.5);
vec2 toCenter = center - fragtexcoords;
float dist = length(toCenter);

// Limit the effect to a certain radius
float normalizedDist = smoothstep(0.0, effectSize, dist);
float effectStrength = 1.0 - normalizedDist;

float angle = atan(toCenter.y, toCenter.x);
float spiral = sin(dist * spiralFrequency - time * spiralSpeed + angle) * spiralIntensity;

float inward = sin(dist * inwardFrequency - time * inwardSpeed) * inwardIntensity;

vec2 distortion = normalize(toCenter) * (spiral + inward) * effectStrength;
vec2 distortedCoords = fragtexcoords + distortion;

gl_FragColor = texture2D(textureMap, distortedCoords) * fragcolor;

float colorShift = sin(dist * colorShiftFrequency - time) * colorShiftIntensity + 1.0;
gl_FragColor.rgb *= vec3(colorShift, 1.0, 1.0/colorShift);
}
4 changes: 3 additions & 1 deletion src/shaderRegistry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ namespace ShaderRegistry
"shaders/objectShader:ILLUMINATION",
"shaders/objectShader:SPECULAR",
"shaders/objectShader:ILLUMINATION:SPECULAR",
"shaders/planet"
"shaders/planet",
"shaders/wormhole",
};

std::array<const char*, Uniforms_t(Uniforms::Count)> uniform_names{
Expand All @@ -39,6 +40,7 @@ namespace ShaderRegistry
"projection",
"model",
"view",
"time",
"camera_position",
"atmosphereColor",

Expand Down
2 changes: 2 additions & 0 deletions src/shaderRegistry.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ namespace ShaderRegistry
ObjectSpecular,
ObjectSpecularIllumination,
Planet,
Wormhole,

Count
};
Expand All @@ -47,6 +48,7 @@ namespace ShaderRegistry
Projection,
Model,
View,
Time,
CameraPosition,
AtmosphereColor,

Expand Down
5 changes: 3 additions & 2 deletions src/spaceObjects/wormHole.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <glm/gtc/type_ptr.hpp>

#include "main.h"
#include "engine.h"
#include "random.h"
#include "wormHole.h"
#include "spaceship.h"
Expand Down Expand Up @@ -66,18 +67,18 @@ void WormHole::draw3DTransparent()
};

textureManager.getTexture("wormHole3d.png")->bind();
ShaderRegistry::ScopedShader shader(ShaderRegistry::Shaders::Billboard);
ShaderRegistry::ScopedShader shader(ShaderRegistry::Shaders::Wormhole);

auto model_matrix = getModelMatrix();
auto modeldata_matrix = glm::rotate(model_matrix, glm::radians(120.f), {1.f, 0.f, 0.f});

glUniformMatrix4fv(shader.get().uniform(ShaderRegistry::Uniforms::Model), 1, GL_FALSE, glm::value_ptr(modeldata_matrix));
glUniform4f(shader.get().uniform(ShaderRegistry::Uniforms::Color), 1.f, 1.f, 1.f, 5000.f);
glUniform1f(shader.get().uniform(ShaderRegistry::Uniforms::Time), engine->getElapsedTime());
gl::ScopedVertexAttribArray positions(shader.get().attribute(ShaderRegistry::Attributes::Position));
gl::ScopedVertexAttribArray texcoords(shader.get().attribute(ShaderRegistry::Attributes::Texcoords));



glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

glVertexAttribPointer(positions.get(), 3, GL_FLOAT, GL_FALSE, sizeof(VertexAndTexCoords), (GLvoid*)quad.data());
Expand Down
Loading