Skip to content

Commit

Permalink
Merge pull request #7 from Tokeiburu/lub-effect-renderer
Browse files Browse the repository at this point in the history
Fixed some issues with rendering the lub effects.
  • Loading branch information
Borf authored Nov 30, 2023
2 parents 14d7efd + 44bc90c commit a5d067a
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 23 deletions.
50 changes: 38 additions & 12 deletions browedit/components/LubRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,16 @@ void LubRenderer::render()

glm::mat4 modelMatrix(1.0f);
modelMatrix = glm::scale(modelMatrix, glm::vec3(1, 1, -1));
modelMatrix = glm::translate(modelMatrix, glm::vec3(5 * gnd->width + rswObject->position.x, -rswObject->position.y, -10 - 5 * gnd->height + rswObject->position.z));
modelMatrix = glm::translate(modelMatrix, glm::vec3(5 * gnd->width + rswObject->position.x, -rswObject->position.y + 11, -9 - 5 * gnd->height + rswObject->position.z));

if (lubEffect->billboard_off) {
modelMatrix = glm::rotate(modelMatrix, -glm::radians(lubEffect->rotate_angle.z), glm::vec3(0, 0, 1));
modelMatrix = glm::rotate(modelMatrix, glm::radians(lubEffect->rotate_angle.y), glm::vec3(0, 1, 0));
modelMatrix = glm::rotate(modelMatrix, -glm::radians(lubEffect->rotate_angle.x), glm::vec3(1, 0, 0));
}

shader->setUniform(LubShader::Uniforms::modelMatrix, modelMatrix);
shader->setUniform(LubShader::Uniforms::billboard_off, (bool)lubEffect->billboard_off);
shader->setUniform(LubShader::Uniforms::color, lubEffect->color);

glBindBuffer(GL_ARRAY_BUFFER, 0);
Expand All @@ -76,22 +84,39 @@ void LubRenderer::render()
lastTime = time;
for (auto& p : particles)
{
p.position += p.speed * elapsedTime;
p.position += (p.dir * lubEffect->speed + p.speed) * elapsedTime;
p.life -= elapsedTime;
p.speed += lubEffect->gravity * elapsedTime;
p.speed += lubEffect->speed * lubEffect->gravity * glm::vec3(1, -1, 1) * elapsedTime;

if (p.start_life > 1) {
if (p.life < p.start_life / 2)
p.alpha -= elapsedTime;
else
p.alpha += elapsedTime;

p.alpha = glm::clamp(p.alpha, 0.0f, 1.0f);
}
}
if(particles.size() > 0)
particles.erase(std::remove_if(particles.begin(), particles.end(), [](const Particle& p) { return p.life < 0; }), particles.end());
emitTime -= elapsedTime;
if (emitTime < 0 && particles.size() < lubEffect->maxcount)
{
Particle p;
p.position = glm::vec3(0.0f);
p.speed.x = lubEffect->dir1.x + (rand() / (float)RAND_MAX) * (lubEffect->dir2.x - lubEffect->dir1.x);
p.speed.y = -(lubEffect->dir1.y + (rand() / (float)RAND_MAX) * (lubEffect->dir2.y - lubEffect->dir1.z));
p.speed.z = lubEffect->dir1.z + (rand() / (float)RAND_MAX) * (lubEffect->dir2.z - lubEffect->dir1.y);
p.position = glm::vec3(
-lubEffect->radius.x + (rand() / (float)RAND_MAX) * (-lubEffect->radius.x + 2 * abs(lubEffect->radius.x) + lubEffect->radius.x),
-lubEffect->radius.y + (rand() / (float)RAND_MAX) * (-lubEffect->radius.y + 2 * abs(lubEffect->radius.y) + lubEffect->radius.y),
-lubEffect->radius.z + (rand() / (float)RAND_MAX) * (-lubEffect->radius.z + 2 * abs(lubEffect->radius.z) + lubEffect->radius.z)
);
p.dir = glm::vec3(
lubEffect->dir1.x + (rand() / (float)RAND_MAX) * (lubEffect->dir2.x - lubEffect->dir1.x),
-lubEffect->dir1.y + (rand() / (float)RAND_MAX) * (-lubEffect->dir2.y + lubEffect->dir1.y),
lubEffect->dir1.z + (rand() / (float)RAND_MAX) * (lubEffect->dir2.z - lubEffect->dir1.z)
);
p.life = lubEffect->life.x + (rand() / (float)RAND_MAX) * (lubEffect->life.y - lubEffect->life.x);
p.start_life = p.life;
p.size = lubEffect->size.x + (rand() / (float)RAND_MAX) * (lubEffect->size.y - lubEffect->size.x);
p.alpha = 0;

particles.push_back(p);
emitTime = 1.0f / (lubEffect->rate.x + (rand() / (float)RAND_MAX) * (lubEffect->rate.y - lubEffect->rate.x));
Expand All @@ -107,11 +132,12 @@ void LubRenderer::render()
std::vector<VertexP3T2A1> verts;
for (const auto& p : particles)
{
float alpha = glm::clamp(p.life, 0.0f, 1.0f);
verts.push_back(VertexP3T2A1(p.position + glm::vec3(-p.size/2, 0, 0), glm::vec2(0, 0), alpha));
verts.push_back(VertexP3T2A1(p.position + glm::vec3(-p.size/2, p.size, 0), glm::vec2(0, 1), alpha));
verts.push_back(VertexP3T2A1(p.position + glm::vec3(p.size/2, p.size, 0), glm::vec2(1, 1), alpha));
verts.push_back(VertexP3T2A1(p.position + glm::vec3(p.size/2, 0, 0), glm::vec2(1, 0), alpha));
float alpha = p.alpha;
//float alpha = 1.0;
verts.push_back(VertexP3T2A1(p.position + glm::vec3(-p.size, -p.size, 0), glm::vec2(0, 0), alpha));
verts.push_back(VertexP3T2A1(p.position + glm::vec3(-p.size, p.size, 0), glm::vec2(0, 1), alpha));
verts.push_back(VertexP3T2A1(p.position + glm::vec3(p.size, p.size, 0), glm::vec2(1, 1), alpha));
verts.push_back(VertexP3T2A1(p.position + glm::vec3(p.size, -p.size, 0), glm::vec2(1, 0), alpha));
}


Expand Down
6 changes: 5 additions & 1 deletion browedit/components/LubRenderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class LubRenderer : public Renderer
modelMatrix,
s_texture,
color,
billboard,
billboard_off,
End
};
};
Expand All @@ -36,6 +36,7 @@ class LubRenderer : public Renderer
bindUniform(Uniforms::s_texture, "s_texture");
bindUniform(Uniforms::modelMatrix, "modelMatrix");
bindUniform(Uniforms::color, "color");
bindUniform(Uniforms::billboard_off, "billboard_off");
}
};
private:
Expand All @@ -51,8 +52,11 @@ class LubRenderer : public Renderer
public:
glm::vec3 position;
glm::vec3 speed;
glm::vec3 dir;
float size;
float life;
float start_life;
float alpha;
};
std::vector<Particle> particles;

Expand Down
2 changes: 1 addition & 1 deletion browedit/components/RsmRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ void RsmRenderer::renderMesh(Rsm::Mesh* mesh, const glm::mat4& matrix, bool sele
textures.push_back(util::ResourceManager<gl::Texture>::load("data\\texture\\" + textureFilename));
}

if (mesh && (!mesh->rotFrames.empty() || mesh->model->version >= 0x202 || mesh->matrixDirty))
if (mesh && (!mesh->rotFrames.empty() || mesh->matrixDirty || !mesh->scaleFrames.empty() || !mesh->posFrames.empty()))
{
mesh->matrixDirty = false;

Expand Down
6 changes: 5 additions & 1 deletion browedit/components/Rsw.Effect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,11 @@ void LubEffect::load(const json& data)
srcmode = data["srcmode"][0];
destmode = data["destmode"][0];
maxcount = data["maxcount"][0];


if (data.find("billboard_off") != data.end())
billboard_off = data["billboard_off"][0];
if (data.find("rotate_angle") != data.end())
from_json(data["rotate_angle"], rotate_angle);
}

void LubEffect::buildImGuiMulti(BrowEdit* browEdit, const std::vector<Node*>& nodes)
Expand Down
7 changes: 3 additions & 4 deletions data/shaders/lub.fs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,9 @@ out vec4 fragColor;
void main()
{
vec4 outColor = texture2D(s_texture, texCoord);
//output.a = (output.r + output.g + output.b) / 3;
outColor *= color;
outColor.rgb *= alpha;
if(outColor.a < 0.1)
discard;
outColor.a *= alpha;
//if(outColor.a < 0.1)
// discard;
fragColor = outColor;
}
15 changes: 11 additions & 4 deletions data/shaders/lub.vs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ layout (location = 2) in float a_alpha;
uniform mat4 projectionMatrix;
uniform mat4 cameraMatrix;
uniform mat4 modelMatrix;
uniform float selection;
uniform bool billboard_off;

out vec2 texCoord;
out float alpha;
Expand All @@ -16,7 +16,14 @@ void main()
{
texCoord = a_texture;
alpha = a_alpha;
vec4 billboarded = projectionMatrix * (cameraMatrix * modelMatrix) * vec4(0.0,0.0,0.0,1.0);
billboarded.xy += (projectionMatrix * vec4(a_position.x, a_position.y,0.0,1.0)).xy;
gl_Position = billboarded;

if (billboard_off) {
vec4 billboarded = projectionMatrix * (cameraMatrix * modelMatrix) * vec4(a_position,1.0);
gl_Position = billboarded;
}
else {
vec4 billboarded = projectionMatrix * (cameraMatrix * modelMatrix) * vec4(0.0,0.0,0.0,1.0);
billboarded.xy += (projectionMatrix * vec4(a_position.x, a_position.y,0.0,1.0)).xy;
gl_Position = billboarded;
}
}

0 comments on commit a5d067a

Please sign in to comment.