From e7b2d569924018ba097c83baec93c55355125dcd Mon Sep 17 00:00:00 2001 From: devZoGok Date: Fri, 25 Mar 2022 20:24:36 +0200 Subject: [PATCH] spread fix start and end size merge --- particle.frag | 10 +++++++++- particle.vert | 5 ++--- particleEmitter.cpp | 29 ++++++++++++++--------------- particleEmitter.h | 9 ++++----- 4 files changed, 29 insertions(+), 24 deletions(-) diff --git a/particle.frag b/particle.frag index 044f44f..78fdfaa 100755 --- a/particle.frag +++ b/particle.frag @@ -9,12 +9,19 @@ const int numParticles = 1; layout (location = 0) out vec4 FragColor; layout (location = 1) out vec4 BrightColor; -uniform sampler2D tex; +struct Texture{ + float mixRatio; + sampler2D pastTexture, nextTexture; + bool animated; +}; + +uniform Texture tex; uniform float lifePercentage[500]; uniform vec4 startColor, endColor; void main(){ float alpha = texture(tex.pastTexture, texCoords).r; + if(tex.animated) alpha = mix(alpha, texture(tex.nextTexture, texCoords).r, tex.mixRatio); @@ -22,6 +29,7 @@ void main(){ FragColor = c; float brightness = dot(c.rgb, vec3(0.2126, 0.7152, 0.0722)); + if(brightness > 1) BrightColor = vec4(c.rgb, 1); } diff --git a/particle.vert b/particle.vert index d5580d5..588bc7f 100755 --- a/particle.vert +++ b/particle.vert @@ -13,15 +13,14 @@ const int numParticles = 1; uniform mat4 proj, view; uniform float lifePercentage[numParticles]; uniform vec3 trans[numParticles]; -uniform vec2 startSize, endSize; +uniform vec2 size; void main(){ vec3 camLeft = vec3(view[0][0], view[1][0], view[2][0]); vec3 camUp = vec3(view[0][1], view[1][1], view[2][1]); id = gl_InstanceID; - vec2 s = mix(startSize, endSize, lifePercentage[id]); - vec3 v = camLeft * s.x * aPos.x + camUp * s.y * aPos.y; + vec3 v = camLeft * size.x * aPos.x + camUp * size.y * aPos.y; mat4 model = aInstancedModel; model[3][0] = trans[id].x; model[3][1] = trans[id].y; diff --git a/particleEmitter.cpp b/particleEmitter.cpp index 9791f4e..718fe2c 100755 --- a/particleEmitter.cpp +++ b/particleEmitter.cpp @@ -102,8 +102,10 @@ namespace vb01{ void ParticleEmitter::makeHeap(int offset){ bool heap = false; + while(!heap){ bool end = true; + for(int i = 0; 2 * i + 1 + offset < numParticles; i++){ if(2 * i + 2 + offset < numParticles && (particles[i + offset]->distToCamPlane < particles[2 * i + 1 + offset]->distToCamPlane || particles[i + offset]->distToCamPlane < particles[2 * i + 2 + offset]->distToCamPlane)) @@ -114,6 +116,7 @@ namespace vb01{ else if(2 * i + 2 + offset == numParticles && particles[i + offset]->distToCamPlane < particles[2 * i + 1 + offset]->distToCamPlane) swap(particles[i + offset], particles[2 * i + 1 + offset]); } + for(int i = 0; 2 * i + 1 < numParticles; i++){ if(2 * i + 2 + offset < numParticles && (particles[i + offset]->distToCamPlane < particles[2 * i + 1 + offset]->distToCamPlane || particles[i + offset]->distToCamPlane < particles[2 * i + 2 + offset]->distToCamPlane)) @@ -123,6 +126,7 @@ namespace vb01{ else if(2 * i + 2 + offset == numParticles && particles[i + offset]->distToCamPlane < particles[2 * i + 1 + offset]->distToCamPlane) end = false; } + if(end) heap = true; } @@ -157,8 +161,7 @@ namespace vb01{ shader->setMat4(proj, "proj"); shader->setVec4(startColor, "startColor"); shader->setVec4(endColor, "endColor"); - shader->setVec2(startSize, "startSize"); - shader->setVec2(endSize, "endSize"); + shader->setVec2(size, "size"); heapSort(); render(); @@ -172,24 +175,26 @@ namespace vb01{ Vector3 nodeLeft = node->getGlobalAxis(0); for(int i = 0; i < numParticles; i++){ - Vector3 dir = nodeDir; if(getTime() - particles[i]->time >= particles[i]->timeToLive){ + Vector3 dir = nodeDir; float factor = (float)(rand() % 100) / 100; particles[i]->time = getTime(); particles[i]->timeToLive = s64(1000 * ((highLife - lowLife) * factor + lowLife)); float spr1 = float(rand() % (int)spread) / 180 * PI, spr2 = float(rand() % 360) / 180 * PI; Vector3 ra1 = (nodeUp.cross(dir)).norm(), ra2 = dir.norm(); + if(ra1 == Vector3::VEC_ZERO) ra1 = Vector3::VEC_I; + dir = Quaternion(spr1, ra1) * dir; dir = Quaternion(spr2, ra2) * dir; + particles[i]->dir = dir.norm() * speed + gravity; particles[i]->trans = nodePos; } - dir = dir.norm() * speed + gravity; - particles[i]->trans = particles[i]->trans + dir; + particles[i]->trans = particles[i]->trans + particles[i]->dir; particles[i]->distToCamPlane = cos(camDir.getAngleBetween((particles[i]->trans - camPos).norm())) * camPos.getDistanceFrom(particles[i]->trans); float lifePercentage = (float)(getTime() - particles[i]->time) / particles[i]->timeToLive; @@ -237,17 +242,11 @@ namespace vb01{ case KeyframeChannel::PARTICLE_EMITTER_END_COLOR_Z: endColor.z = value; break; - case KeyframeChannel::PARTICLE_EMITTER_START_SIZE_X: - startSize.x = value; + case KeyframeChannel::PARTICLE_EMITTER_SIZE_X: + size.x = value; break; - case KeyframeChannel::PARTICLE_EMITTER_START_SIZE_Y: - startSize.y = value; - break; - case KeyframeChannel::PARTICLE_EMITTER_END_SIZE_X: - endSize.x = value; - break; - case KeyframeChannel::PARTICLE_EMITTER_END_SIZE_Y: - endSize.y = value; + case KeyframeChannel::PARTICLE_EMITTER_SIZE_Y: + size.y = value; break; case KeyframeChannel::PARTICLE_EMITTER_LOW_LIFE: lowLife = value; diff --git a/particleEmitter.h b/particleEmitter.h index 59bcab4..d3c9e39 100755 --- a/particleEmitter.h +++ b/particleEmitter.h @@ -20,8 +20,7 @@ namespace vb01{ void update(); inline void setMaterial(Material *mat){this->material = mat;} inline void setNode(Node *node){this->node = node;} - inline void setStartSize(Vector2 size){this->startSize = size;} - inline void setEndSize(Vector2 size){this->endSize = size;} + inline void setSize(Vector2 size){this->size = size;} inline void setStartColor(Vector4 color){this->startColor = color;} inline void setEndColor(Vector4 color){this->endColor = color;} inline void setSpread(float s){this->spread = s;} @@ -38,7 +37,7 @@ namespace vb01{ struct Particle{ int VAO; s64 time = 0, timeToLive = 0; - Vector3 trans; + Vector3 trans, dir; float distToCamPlane; }; @@ -56,9 +55,9 @@ namespace vb01{ Particle **particles; Material *material = nullptr; Node *node = nullptr; - Vector2 startSize = Vector2::VEC_IJ, endSize = Vector2::VEC_IJ; + Vector2 size = Vector2::VEC_IJ; Vector3 gravity = Vector3::VEC_ZERO; - Vector4 startColor, endColor; + Vector4 startColor = Vector4::VEC_IJKL, endColor = Vector4::VEC_IJKL; float speed = .2, spread = 1, lowLife = 1, highLife = 2; friend class ParticleEmitterTest;