Skip to content

Commit

Permalink
v3.2
Browse files Browse the repository at this point in the history
  • Loading branch information
F-Mu committed Dec 12, 2022
1 parent f0a2b6a commit 7fb1752
Show file tree
Hide file tree
Showing 14 changed files with 52 additions and 50 deletions.
16 changes: 13 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ https://github.com/blurrypiano/littleVulkanEngine/tree/master

同时用来实验C++书籍中的条款

存在简单的异步执行,更多说明在v1.2分支README中

**v2.0**

+ 大重构(感谢学长给的基本架构)
Expand All @@ -28,7 +30,7 @@ https://github.com/blurrypiano/littleVulkanEngine/tree/master

**v2.1**

+ 修复BUG(大概)
+ 修复BUG(大概,原因系Window初始化时height和width还没生成,以及RenderSystem成员变量未正确初始化

**v2.2**

Expand All @@ -52,14 +54,22 @@ https://github.com/blurrypiano/littleVulkanEngine/tree/master

**v3.1**

+ 为了去除bind改用lambda(实现条款),改用C++20(C++20 deprecated了glm库某些特性,会报警告)
+ 为了去除bind改用lambda(实现条款),改用C++20(C++20 deprecated了volatile float,glm库会报警告)

+ 修改README

+ 调整ParticleComponent的参数,使其效果更佳(但还是很丑陋)

+ 模仿GetComponent调整Component的添加方式,默认实现type,使Component更有扩展性

**v3.2**

+ 使粒子效果更好看,包括粒子颜色渐变,增加粒子以抗锯齿(我真的在努力做火焰效果了,其实也许红色拖影更好看?)

+ 修复了Task乱飞的bug(大概,此bug 100个Task也不一定会出现,所以未知是否修好)

+ 修改移动逻辑

**待实现(可能也不会实现):**

+ 更好的异步执行(Job System)
Expand All @@ -70,7 +80,7 @@ https://github.com/blurrypiano/littleVulkanEngine/tree/master

+ 继续模仿Piccolo小引擎优化主循环

+ 更好的粒子系统(搞不明白)
+ 更好的粒子系统(搞不明白),更好的粒子系统tick逻辑

+ 求出多面体的凸包,而不是严格限制顶点顺序(不想写)

Expand Down
Binary file modified image/ThreadPool.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions shaders/particle_shader.frag
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ void main() {
if (dis >= 0.002) {
discard;
}
outColor = vec4(push.color.xyz*push.color.w,push.color.w);
// outColor=vec4(0.,1.,1.,0.);
outColor = push.color;
// outColor=vec4(0.,1.,1.,0.);
}
Binary file modified shaders/particle_shader.frag.spv
Binary file not shown.
7 changes: 5 additions & 2 deletions src/function/framework/component/delete_component.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,18 @@ namespace crp {
return;
}
mesh->setDirty(true);
//将坐下的点复制一个出来变为五边形
if (time == 0) {
mesh->points.emplace_back(mesh->points.back());
}
auto particleCom = m_parent_object.lock()->tryGetComponent(ParticleComponent);
//后半部分删除时,三角形中其实也保存了五个点,铺粒子
auto direction = mesh->points[4] - mesh->points[3];
auto now = mesh->points[3];
for (int i = 0; i <= time; i++) {
//使得将粒子数量翻倍,使得粒子更为稠密以达到抗锯齿的效果
for (int i = 0; i <= 2 * time; i++) {
particleCom->addParticleEmitter(now);
now += direction / static_cast<float>(time);
now += direction / static_cast<float>(2 * time);
}
float halfTime = static_cast<float>(MAX_TIME) / 2;
if (time < MAX_TIME / 2) {
Expand Down
20 changes: 6 additions & 14 deletions src/function/framework/component/move_component.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,17 @@ namespace crp {
void MoveComponent::tick() {
if (isFinished())return;
MoveToPoint(destinations[now]);
if (STRICT_EQUAL(center, destinations[now])) {
if (EQUAL(center, destinations[now])) {
center = destinations[now];
++now;
if (isFinished())return;
auto dir = destinations[now] - destinations[now - 1];
direction = glm::normalize(dir) * speed;
}
}

MoveComponent::MoveComponent(const std::weak_ptr<GameObject> &parent,const std::string &type)
: Component(parent,type),
MoveComponent::MoveComponent(const std::weak_ptr<GameObject> &parent, const std::string &type)
: Component(parent, type),
center{m_parent_object.lock()->tryGetComponent(TransformComponent)->translation} {
}

Expand All @@ -38,31 +39,22 @@ namespace crp {
}

void MoveComponent::setDestination(glm::vec3 &_destination, float _time) {
if (move)return;
if (!isFinished())return;
destinations.emplace_back(_destination);
auto dir = destinations[0] - center;
times.emplace_back(_time);
direction = dir / _time;
move = true;
}

void MoveComponent::setDestination(std::vector<glm::vec3> &_destinations, float _time) {
if (move)return;
if (!isFinished())return;
destinations = _destinations;
auto dir = destinations[0] - center;
float len = glm::length(dir);
for (int i = 1; i < destinations.size(); ++i) {
auto d = destinations[i] - destinations[i - 1];
len += glm::length(d);
}
times.resize(destinations.size());
times[0] = glm::length(dir) / len;
for (int i = 1; i < destinations.size(); ++i) {
auto d = destinations[i] - destinations[i - 1];
times[i] = glm::length(d) / len;
}
speed = len / _time;
direction = glm::normalize(dir) * speed;
move = true;
}
}
20 changes: 10 additions & 10 deletions src/function/framework/component/move_component.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,6 @@
namespace crp {
class MoveComponent : public Component {
public:
glm::vec3 direction{};
std::vector<glm::vec3> destinations;
std::vector<float> times;
bool move{};
glm::vec3 &center;

void setDestination(glm::vec3 &_destination, float _time = FRAME_TIME);

void setDestination(std::vector<glm::vec3> &_destinations, float _time = FRAME_TIME);
Expand All @@ -25,21 +19,27 @@ namespace crp {
bool isFinished() {
if (destinations.empty()) {
now = 0;
move = false;
return true;
}
if (now == destinations.size()) {
if (now >= destinations.size()) {
now = 0;
move = false;
destinations.clear();
return true;
}
if (EQUAL(center, destinations[now])) {
center = destinations[now];
now = 0;
destinations.clear();
}
return false;
};

void tick() override;

// private:
private:
glm::vec3 direction{};
std::vector<glm::vec3> destinations{};
glm::vec3 &center;
int now = 0;
float speed{};
};
Expand Down
5 changes: 2 additions & 3 deletions src/function/framework/component/particle_component.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,11 @@ namespace crp {

void Particle::renderTick() {
if (life == 0)return;
// globalContext.particleRenderPass->tick();
ParticlePushConstants push{};
push.position = position;
push.color = {color, static_cast<float>(getLife()) / MAX_LIFE};
color[1] *= (static_cast<float>(getLife()) / MAX_LIFE);//使得颜色从高温黄,变为低温红,逐渐降低G值占比
push.color = {color, 1.0};
globalContext.particleRenderPass->bind(push);

--life;
}
}
6 changes: 3 additions & 3 deletions src/function/framework/component/particle_component.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ namespace crp {

[[nodiscard]] int getLife() const { return life; }

constexpr static int MAX_LIFE = 8;
constexpr static int MAX_LIFE = 7;
private:
int life{MAX_LIFE};
glm::vec4 position;
glm::vec3 color{1., 0., 0.};
glm::vec4 velocity{0.00001, -0.00001, 0, 0};
glm::vec3 color{1., .8, .0};
glm::vec4 velocity{0, -0, 0, 0};
};

class ParticleEmitter {
Expand Down
6 changes: 3 additions & 3 deletions src/function/framework/rectangle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@ namespace crp {
return gameObject->tryGetComponentConst(TransformComponent)->translation;
}

bool Rectangle::isMove() const {
auto moveComponent = gameObject->tryGetComponentConst(MoveComponent);
return moveComponent && moveComponent->move;
bool Rectangle::isMove() {
auto moveComponent = gameObject->tryGetComponent(MoveComponent);
return moveComponent && (!moveComponent->isFinished());
}

void Rectangle::setPosition(glm::vec3 position) {
Expand Down
2 changes: 1 addition & 1 deletion src/function/framework/rectangle.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ namespace crp {

glm::vec3 getCenter() const;

bool isMove() const;
bool isMove();

std::shared_ptr<GameObject> gameObject;

Expand Down
7 changes: 3 additions & 4 deletions src/resources/systems/runtime_system.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,16 @@
namespace crp {
RuntimeSystem::RuntimeSystem() :
Rectangle{Rectangle::MakeRectangle({x, y, w, z})},
taskQueueSystem{std::make_shared<TaskQueueSystem>()},
threadPoolSystem{std::make_shared<ThreadPoolSystem>()} {
threadPoolSystem->threadsInit(taskQueueSystem->tasks);

taskQueueSystem{std::make_shared<TaskQueueSystem>()},
threadPoolSystem{std::make_shared<ThreadPoolSystem>()} {
float mid = (left + right) / 2;
points.resize(THREAD_NUM);
float hCut = (down - up) / float(THREAD_NUM);
float yFirst = (up + hCut + up) / 2;
for (int i = 0; i < THREAD_NUM; ++i) {
points[i] = {mid, yFirst + hCut * i, THREAD_LAYER};
}
threadPoolSystem->threadsInit(taskQueueSystem->tasks);
}

void RuntimeSystem::tick() {
Expand Down
3 changes: 2 additions & 1 deletion src/resources/systems/task_queue_system.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace crp {
TaskQueueSystem::TaskQueueSystem() :
Rectangle{std::move(Rectangle::MakeRectangle({x, y, w, z}))} {
Rectangle{Rectangle::MakeRectangle({x, y, w, z})} {
float mid = (left + right) / 2;
points.resize(TASK_NUM);
float hCut = (down - up) / float(TASK_NUM);
Expand Down Expand Up @@ -50,6 +50,7 @@ namespace crp {
}

bool TaskQueueSystem::isSorted() {
std::lock_guard<std::mutex> lock(this->taskMut);
if (tasks.empty())return false;
return (!tasks.front().isMove()) && (STRICT_EQUAL(tasks.front().getCenter(), points[0]));
}
Expand Down
6 changes: 2 additions & 4 deletions src/resources/systems/thread_pool_system.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,7 @@ namespace crp {
{
std::unique_lock<std::mutex> lock(runMut);
this->run.wait(lock, [this, i, &task] {
return this->stop ||
(STRICT_EQUAL(threads[i].getCenter(), globalContext.runTimeSystem->points[i])
&& STRICT_EQUAL(task->getCenter(), threads[i].getCenter()));
return this->stop || (!threads[i].isMove() && !task->isMove());
});
if (stop)return;
}
Expand All @@ -76,7 +74,7 @@ namespace crp {
{
std::unique_lock<std::mutex> lock(this->resetMut);
this->reset.wait(lock, [this, i] {
return this->stop || threads[i].getCenter() == points[i];
return this->stop || !threads[i].isMove();
});
if (stop)return;
}
Expand Down

0 comments on commit 7fb1752

Please sign in to comment.