Branches sway

This commit is contained in:
austin 2023-09-22 23:15:27 -04:00
parent fe1680b7d9
commit cfa0798b69
3 changed files with 158 additions and 12 deletions

View File

@ -60,7 +60,14 @@
"leafIncrement": 0.5,
"minLeavesToSpawnPerPoint": 3,
"maxLeavesToSpawnPerPoint": 5,
"leafDistanceFromCenter": 1.2
"leafDistanceFromCenter": 1.2,
"peelVariance": 0.2,
"peelMinimum": 0.1,
"swaySigmoidFactor": 1,
"minimumSwayTime": 500,
"swayTimeVariance": 500,
"yawVariance": 0.2,
"yawMinimum": 0.1
},
"modelPath" : "Models/proceduralTree2/proceduralTree2v2.fbx"
}

View File

@ -315,13 +315,15 @@ public class ProceduralTree {
//add behavior tree to update all child branch attachment points to new point
if(childBranches.size() > 0){
Globals.clientSceneWrapper.getScene().registerBehaviorTree(new BranchBehaviorTree(
type,
parent,
branch,
childBranches,
newScalar,
parentPeel,
peelRotation,
offsetRotation,
treeSegmentHeight
treeSegmentHeight,
0l
));
}
}
@ -747,6 +749,10 @@ public class ProceduralTree {
*/
static class BranchBehaviorTree implements BehaviorTree {
//The type of tree (controls many params of how the swaying works)
TreeModel type;
//The parent that the branch is attached to
Entity parent;
@ -759,14 +765,23 @@ public class ProceduralTree {
//The current peel for the branch
double currentPeel;
//The peel to animate towards
double targetPeel;
//the last valid peel
double lastPeel;
//The initial yaw for the branch
double initialYaw;
//the current yaw for the branch
double currentYaw;
//The height of the
float treeSegmentHeight;
//The yaw to animate towards
double targetYaw;
//the last valid yaw
double lastYaw;
//Every child branch
List<Entity> children;
@ -774,28 +789,70 @@ public class ProceduralTree {
//The current scalar of the branch
double currentScalar;
//the current time until the current sway finishes
int currentTime = 0;
//the time until the sway finishes
int maxTime = 0;
//Offset applied to the time reading for prng measurements for branch sway
long timeOffset;
/**
* Constructor
* @param branch The branch entity
* @param type The type of tree
* @param parent The parent of the current branch model
* @param branch The entity of the current branch model
* @param children The list of children attached to the end of this branch model
* @param currentScalar The current scalar of the current branch model (how wide it be)
* @param peel The initial peel of the branch
* @param yaw The initial yaw of the branch
* @param treeSegmentHeight The height of a given tree branch segment
* @param timeOffset The time offset applied to this tree branch behavior
*/
protected BranchBehaviorTree(Entity parent, Entity branch, List<Entity> children, double currentScalar, double peel, double yaw, float treeSegmentHeight){
protected BranchBehaviorTree(
TreeModel type,
Entity parent,
Entity branch,
List<Entity> children,
double currentScalar,
double peel,
double yaw,
float treeSegmentHeight,
long timeOffset
){
this.type = type;
this.parent = parent;
this.branch = branch;
this.initialPeel = peel;
this.currentPeel = peel;
this.lastPeel = peel;
this.initialYaw = yaw;
this.currentYaw = yaw;
this.treeSegmentHeight = treeSegmentHeight;
this.lastYaw = yaw;
this.children = children;
this.currentScalar = currentScalar;
this.timeOffset = timeOffset;
}
@Override
public void simulate(float deltaTime) {
System.out.println(currentYaw);
// currentYaw = currentYaw * Math.abs(currentYaw - initialYaw) + new Random().nextDouble() * (1 - Math.abs(currentYaw - initialYaw));
currentPeel = initialPeel * Math.abs(currentPeel - initialPeel) + new Random().nextDouble() * (1 - Math.abs(currentPeel - initialPeel));
if(currentTime >= maxTime - 1){
currentTime = 0;
maxTime = (int)(500 + 500 * new Random().nextDouble());
targetPeel = initialPeel + ((new Random().nextDouble() - 0.5) * type.getPeelVariance());
lastPeel = currentPeel;
targetYaw = initialYaw + ((new Random().nextDouble() - 0.5) * type.getYawVariance());
lastYaw = currentYaw;
}
double currentTimePercentage = (double)currentTime / (double)maxTime;
double sigmoid = 1.0 / (1.0 + Math.pow((currentTimePercentage / (1.0 - currentTimePercentage)),-type.getSwaySigmoidFactor()));
currentPeel = targetPeel * sigmoid + lastPeel * (1.0 - sigmoid);
currentYaw = targetYaw * sigmoid + lastYaw * (1.0 - sigmoid);
currentTime++;
//get new rotation
double pitchFactor = Math.sin(currentYaw);
@ -809,7 +866,7 @@ public class ProceduralTree {
Matrix4f boneTransform = new Matrix4f().identity().rotate(boneRotation);
//new position transform
Matrix4f newPositionTransform = new Matrix4f().rotate(boneRotation).translate(0,treeSegmentHeight,0);
Matrix4f newPositionTransform = new Matrix4f().rotate(boneRotation).translate(0,type.getBranchHeight(),0);
Vector4f newPositionRaw = newPositionTransform.transform(new Vector4f(0,0,0,1));
Matrix4f transformFromParent = new Matrix4f()

View File

@ -59,6 +59,32 @@ public class TreeModel {
//The distance from the central line of a branch to spawn a leaf at
float leafDistanceFromCenter;
//
//Tree branch sway factors
//
//How much can the peel vary hypothetically while it's swinging
double peelVariance;
//a minimum amount of peel (For instance forcing weather to cause large motions in the branches)
double peelMinimum;
//The value of the sigmoid controlling branch way speed over time (check branch btree for details)
double swaySigmoidFactor;
//The minimum number of frames that a branch should sway for
int minimumSwayTime;
//The maximum amount of frames that can be added to minimumSwayTime to increase the time of a single sway
int swayTimeVariance;
//How much can the yaw vary hypothetically while it's swinging
double yawVariance;
//a minimum amount of yaw (For instance forcing weather to cause large motions in the branches)
double yawMinimum;
/**
* how quickly do the limbs shrink
* @return
@ -203,4 +229,60 @@ public class TreeModel {
return leafDistanceFromCenter;
}
/**
* How much can the peel vary hypothetically while it's swinging
* @return
*/
public double getPeelVariance(){
return this.peelVariance;
}
/**
* a minimum amount of peel (For instance forcing weather to cause large motions in the branches)
* @return
*/
public double getPeelMinimum(){
return this.peelMinimum;
}
/**
* The value of the sigmoid controlling branch way speed over time (check branch btree for details)
* @return
*/
public double getSwaySigmoidFactor(){
return this.swaySigmoidFactor;
}
/**
* The minimum number of frames that a branch should sway for
* @return
*/
public int getMinimumSwayTime(){
return this.minimumSwayTime;
}
/**
* The maximum amount of frames that can be added to minimumSwayTime to increase the time of a single sway
* @return
*/
public int getSwayTimeVariance(){
return this.swayTimeVariance;
}
/**
* How much can the yaw vary hypothetically while it's swinging
* @return
*/
public double getYawVariance(){
return this.yawVariance;
}
/**
* a minimum amount of yaw (For instance forcing weather to cause large motions in the branches)
* @return
*/
public double getYawMinimum(){
return this.yawMinimum;
}
}