summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormattkae <mattkae@protonmail.com>2022-01-17 08:46:49 -0500
committermattkae <mattkae@protonmail.com>2022-01-17 08:46:49 -0500
commitc3c2451232bfeeaa0d5fc200e4a5a27e13c258d2 (patch)
tree2d0e8cc3f5197d1afc5472fab1f826e48ce126b2
parent1d47e3aa120539e053ffa41293f3f756b9d07844 (diff)
Generating slightly prettier snowflakes
-rw-r--r--themes/Snowflake.cpp19
-rw-r--r--themes/Snowflake.h7
-rwxr-xr-xthemes/dist/output.wasmbin82223 -> 82965 bytes
3 files changed, 20 insertions, 6 deletions
diff --git a/themes/Snowflake.cpp b/themes/Snowflake.cpp
index 3c9afa4..da42515 100644
--- a/themes/Snowflake.cpp
+++ b/themes/Snowflake.cpp
@@ -5,7 +5,7 @@
const Vector4 snowColor = Vector4(1.0, 0.98, 0.98, 1);
-inline void generateSnowflakeShape(List<Renderer2dVertex>* vertices, int32 numArms, float32 radius) {
+inline void generateSnowflakeShape(List<Renderer2dVertex>* vertices, int32 numArms, float32 radius, float32 innerRadius) {
float32 dx = ((2 * PI) / numArms) / 3.0;
for (int32 centerIdx = 0; centerIdx < (3 * numArms); centerIdx+=3) {
float32 degreeStart = dx * centerIdx;
@@ -20,8 +20,8 @@ inline void generateSnowflakeShape(List<Renderer2dVertex>* vertices, int32 numAr
Vector2 leftEnd = Vector2(radius * cosStart, radius * sinStart);
Vector2 rightEnd = Vector2(radius * cosEnd, radius * sinEnd);
Vector2 diff = (rightEnd - leftEnd) / 2.0;
- Vector2 leftStart = Vector2(-diff.x, -diff.y);
- Vector2 rightStart = diff;
+ Vector2 leftStart = Vector2(-diff.x, -diff.y) + Vector2(innerRadius * cosStart, innerRadius * sinStart);
+ Vector2 rightStart = diff + Vector2(innerRadius * cosEnd, innerRadius * sinEnd);
vertices->add({ leftStart, snowColor, Mat4x4() });
vertices->add({ leftEnd, snowColor, Mat4x4() });
@@ -34,7 +34,7 @@ inline void generateSnowflakeShape(List<Renderer2dVertex>* vertices, int32 numAr
inline void initFlake(SnowflakeParticleRenderer* renderer, SnowflakeUpdateData* ud) {
ud->vtxIdx = renderer->vertices.numElements;
- generateSnowflakeShape(&renderer->vertices, 6, randomFloatBetween(8.f, 24.f);
+ generateSnowflakeShape(&renderer->vertices, randomIntBetween(4, 16), randomFloatBetween(8.f, 16.f), randomFloatBetween(2.f, 6.f));
ud->numVertices = renderer->vertices.numElements - ud->vtxIdx;
}
@@ -113,8 +113,19 @@ void SnowflakeParticleRenderer::update(float32 dtSeconds) {
findAndSpawnNextFlake(this);
}
+ bool addWind = false;
+ timeUntilNextWindSeconds -= dtSeconds;
+ if (timeUntilNextWindSeconds < 0) {
+ timeUntilNextWindSeconds = windIntervalSeconds;
+ windSpeed = Vector2(randomFloatBetween(-10, 10), randomFloatBetween(-10, 0));
+ addWind = true;
+ }
+
for (int32 s = startIndex; s < endIndex; s++) {
SnowflakeUpdateData* ud = &updateData[s];
+
+ if (addWind) ud->velocity += windSpeed;
+
ud->position += ud->velocity * dtSeconds;
Mat4x4 m = Mat4x4().translateByVec2(ud->position);
diff --git a/themes/Snowflake.h b/themes/Snowflake.h
index 08f1c0c..d44851b 100644
--- a/themes/Snowflake.h
+++ b/themes/Snowflake.h
@@ -14,6 +14,7 @@ struct SnowflakeLoadParameters {
float32 flakeSizeDeviation = 1.f;
Vector4 snowColor = { 0.8, 0.8, 0.8, 1.0 };
float32 spawnIntervalSeconds = 0.3;
+ float32 windIntervalSeconds = 1.5;
};
struct SnowflakeUpdateData {
@@ -31,12 +32,14 @@ struct SnowflakeParticleRenderer {
float32 xMax = 0;
float32 yMax = 0;
float32 spawnIntervalSeconds = 0.3;
+ float32 windIntervalSeconds = 1.5;
int32 startIndex = 0;
int32 endIndex = 0;
int32 numSnowflakes = 0;
- Vector3 windSpeed = { 0, 0, 0 };
+ Vector2 windSpeed;
SnowflakeUpdateData* updateData;
- float32 timeUntilNextSpawnSeconds = 0;;
+ float32 timeUntilNextSpawnSeconds = 0;
+ float32 timeUntilNextWindSeconds = 0;
uint32 vao;
uint32 vbo;
diff --git a/themes/dist/output.wasm b/themes/dist/output.wasm
index 427f834..776adba 100755
--- a/themes/dist/output.wasm
+++ b/themes/dist/output.wasm
Binary files differ