From 5ee0e7a8dbd647b79073beee88e82870e181b363 Mon Sep 17 00:00:00 2001 From: Matthew Kosarek Date: Sat, 16 Oct 2021 07:45:45 -0400 Subject: Tree that moves around more believably --- themes/TreeShape.cpp | 49 ++++++++++++++++++++++++++++++++++-------------- themes/TreeShape.h | 7 +++++-- themes/dist/output.js | 11 ++++++++++- themes/dist/output.wasm | Bin 56705 -> 57898 bytes 4 files changed, 50 insertions(+), 17 deletions(-) diff --git a/themes/TreeShape.cpp b/themes/TreeShape.cpp index 8532fdc..49f6a99 100644 --- a/themes/TreeShape.cpp +++ b/themes/TreeShape.cpp @@ -3,6 +3,7 @@ #include #include #include +#include void TreeBranchLoadData::fillVertices(Renderer2dVertex* vertices, int branchTier) { bottomLeft = Vector2 { position.x - width / 2.f, position.y }.rotateAbout(rotation, position); @@ -21,6 +22,8 @@ void TreeBranchLoadData::fillVertices(Renderer2dVertex* vertices, int branchTier }; TreeShapeLoadResult TreeShape::load(Renderer2d* renderer) { + srand ( time(NULL) ); + timeElapsedSeconds = 0; TreeLoadData ld; @@ -39,7 +42,7 @@ TreeShapeLoadResult TreeShape::load(Renderer2d* renderer) { lr.updateData = updateData; lr.numBranches = numBranches; int32 branchIndex = 0; - createBranch(&ld, generationData, numBranches, &branchIndex, 0, ld.trunkWidth, ld.trunkHeight, Vector2 { 300.f, 50.f }, 0, vertices, &lr); + createBranch(&ld, generationData, numBranches, &branchIndex, 0, ld.trunkWidth, ld.trunkHeight, Vector2 { 300.f, 50.f }, 0, NULL, vertices, &lr); useShader(renderer->shader); @@ -66,7 +69,9 @@ TreeShapeLoadResult TreeShape::load(Renderer2d* renderer) { const float32 ninetyDegreeRotation = PI / 2.f; -void TreeShape::createBranch(TreeLoadData* ld, TreeBranchLoadData* generationData, int32 numBranches, int32* branchIndex, int32 branchLevel, float32 width, float32 height, Vector2 position, float32 rotation, Renderer2dVertex* vertices, TreeShapeLoadResult* lr) { +void TreeShape::createBranch(TreeLoadData* ld, TreeBranchLoadData* generationData, int32 numBranches, int32* branchIndex, + int32 branchLevel, float32 width, float32 height, Vector2 position, float32 rotation, + TreeBranchUpdateData* parent, Renderer2dVertex* vertices, TreeShapeLoadResult* lr) { TreeBranchLoadData* branchLoadData = &generationData[*branchIndex]; branchLoadData->width = width; branchLoadData->height = height; @@ -90,7 +95,9 @@ void TreeShape::createBranch(TreeLoadData* ld, TreeBranchLoadData* generationDat TreeBranchUpdateData* branchUpdateData = &updateData[*branchIndex]; branchUpdateData->tier = branchLevel; - branchUpdateData->randomOffset = randomFloatBetween(-1.f, 1.f); + branchUpdateData->period = randomFloatBetween(3.f, 5.f); + branchUpdateData->amplitude = randomFloatBetween(0.01f, 0.05f); + branchUpdateData->branchToFollow = parent; branchUpdateData->vertices = &vertices[(*branchIndex) * 6]; if (branchLevel == ld->numBranchLevels) { @@ -121,7 +128,7 @@ void TreeShape::createBranch(TreeLoadData* ld, TreeBranchLoadData* generationDat Vector2 branchPosition = branchLoadData->topLeft + ((branchLoadData->topRight - branchLoadData->topLeft) * weight) - branchOffsetVertical; // Position of branch along the top of the parent branch (*branchIndex)++; - createBranch(ld, generationData, numBranches, branchIndex, branchLevel + 1, branchWidth, branchHeight, branchPosition, branchRotation, vertices, lr); + createBranch(ld, generationData, numBranches, branchIndex, branchLevel + 1, branchWidth, branchHeight, branchPosition, branchRotation, branchUpdateData, vertices, lr); } } @@ -147,20 +154,34 @@ void TreeShape::update(float32 dtSeconds) { } int32 startParentIndex = bIdx * 6; - float32 xOffset = ((branchUpdataData->randomOffset + branchUpdataData->tier) * 0.01f) * sinf(timeElapsedSeconds); - branchUpdataData->vertices[0].color.w = alpha; - branchUpdataData->vertices[0].position.x += xOffset; - branchUpdataData->vertices[1].color.w = alpha; - branchUpdataData->vertices[1].position.x += xOffset; + branchUpdataData->currentOffset.x = branchUpdataData->amplitude * sinf(branchUpdataData->period * timeElapsedSeconds); + branchUpdataData->currentOffset.y = branchUpdataData->amplitude * sinf(branchUpdataData->period * timeElapsedSeconds); + + if (branchUpdataData->branchToFollow != NULL) { + branchUpdataData->currentOffset += branchUpdataData->branchToFollow->currentOffset; + + // The root of the branch only moves according to the change of the end of the parent. + branchUpdataData->vertices[0].color.w = alpha; + branchUpdataData->vertices[0].position += branchUpdataData->branchToFollow->currentOffset; + branchUpdataData->vertices[1].color.w = alpha; + branchUpdataData->vertices[1].position += branchUpdataData->branchToFollow->currentOffset; + branchUpdataData->vertices[5].color.w = alpha; + branchUpdataData->vertices[5].position += branchUpdataData->branchToFollow->currentOffset; + } + else { + branchUpdataData->vertices[0].color.w = alpha; + branchUpdataData->vertices[1].color.w = alpha; + branchUpdataData->vertices[5].color.w = alpha; + } + + branchUpdataData->vertices[2].color.w = alpha; - branchUpdataData->vertices[2].position.x += xOffset; + branchUpdataData->vertices[2].position += branchUpdataData->currentOffset; branchUpdataData->vertices[3].color.w = alpha; - branchUpdataData->vertices[3].position.x += xOffset; + branchUpdataData->vertices[3].position += branchUpdataData->currentOffset; branchUpdataData->vertices[4].color.w = alpha; - branchUpdataData->vertices[4].position.x += xOffset; - branchUpdataData->vertices[5].color.w = alpha; - branchUpdataData->vertices[5].position.x += xOffset; + branchUpdataData->vertices[4].position += branchUpdataData->currentOffset; } } diff --git a/themes/TreeShape.h b/themes/TreeShape.h index 06a8f81..987295a 100644 --- a/themes/TreeShape.h +++ b/themes/TreeShape.h @@ -32,8 +32,11 @@ struct TreeBranchLoadData { struct TreeBranchUpdateData { int32 tier = 0; - float32 randomOffset = 0; + float32 period = 0; + float32 amplitude = 0; + Vector2 currentOffset; Renderer2dVertex* vertices = NULL; + TreeBranchUpdateData* branchToFollow = NULL; }; struct TreeShapeLoadResult { @@ -62,7 +65,7 @@ struct TreeShape { TreeShapeLoadResult load(Renderer2d* renderer); void createBranch(TreeLoadData* ld, TreeBranchLoadData* branchList, int32 numBranches, int32* branchIndex, int32 branchLevel, float32 width, float32 height, - Vector2 position, float32 rotation, Renderer2dVertex* vertices, TreeShapeLoadResult* lr); + Vector2 position, float32 rotation, TreeBranchUpdateData* parent, Renderer2dVertex* vertices, TreeShapeLoadResult* lr); void update(float32 dtSeconds); void render(Renderer2d* renderer); void unload(); diff --git a/themes/dist/output.js b/themes/dist/output.js index 2dd2a7d..79b5f23 100644 --- a/themes/dist/output.js +++ b/themes/dist/output.js @@ -2969,6 +2969,14 @@ var ASM_CONSTS = { function _setTempRet0(val) { setTempRet0(val); } + + function _time(ptr) { + var ret = (Date.now()/1000)|0; + if (ptr) { + HEAP32[((ptr)>>2)] = ret; + } + return ret; + } var GLctx;; var miniTempWebGLFloatBuffersStorage = new Float32Array(288); for (/**@suppress{duplicate}*/var i = 0; i < 288; ++i) { @@ -3051,7 +3059,8 @@ var asmLibraryArg = { "glUniformMatrix4fv": _glUniformMatrix4fv, "glUseProgram": _glUseProgram, "glVertexAttribPointer": _glVertexAttribPointer, - "setTempRet0": _setTempRet0 + "setTempRet0": _setTempRet0, + "time": _time }; var asm = createWasm(); /** @type {function(...*):?} */ diff --git a/themes/dist/output.wasm b/themes/dist/output.wasm index f994bca..8319928 100755 Binary files a/themes/dist/output.wasm and b/themes/dist/output.wasm differ -- cgit v1.2.1