I have created this system to handle animations of trees. It’s nothing new in terms of functionality, but it’s very optimized and simple to understand. These are all the trees. All of them are animated and located in the workspace of this example.
{
[1] = Maple tree,
[2] = Willow tree,
[3] = Golden Willow tree,
[4] = Birch tree,
[5] = Oak tree,
[6] = Pink Willow tree,
[7] = Banana tree,
[8] = Sakura tree,
[9] = Pine tree,
[10] = Oak tree,
[11] = Pine tree,
[12] = Pine tree,
[13] = Pine tree,
[14] = Pine tree,
[15] = Birch tree,
[16] = Maple tree,
[17] = Oak tree,
[18] = Maidenhair tree,
[19] = Ash tree,
[20] = Douglass Fir tree,
[21] = Broadleaf tree,
[22] = Redwood tree,
[23] = Maple tree,
[24] = Oak tree,
[25] = Wise old tree,
[26] = Beechwood tree,
[27] = tree,
[28] = tree,
[29] = tree,
[30] = Dogwood tree,
[31] = Thick Fir tree,
[32] = Fir Tree,
[33] = Fir Tree,
[34] = Magic Tree,
[35] = Fir tree,
[36] = Oak tree,
[37] = Birch tree,
[38] = Pine tree,
[39] = tall Birch tree,
[40] = short Birch tree
}
All of them have optimized collisions and rendering. I use this library for my own project and I will be testing a client sided version of this code soon. The good news is this code is pretty short so it should be easy to understand! You can copy and paste the server code to the client and it should work the same but provide better responsiveness.
You can copy the public place here to check it out!
ServerSided Tree Animations Example
Please grab a copy of the Client Sided Animation script for the Best Performance!
Optimized Client Tree Animations Script - Supports Streaming Enabled and Camera Frustrum Culling + Tweening
[PlaceIn_ReplicatedFirst - Creator Store (https://create.roblox.com/store/asset/94248172114274/PlaceInReplicatedFirst)
I have done testing and this method scales much better than running one script per tree.
One original script runs .1% for one tree for example while this is .5% at 40 trees .
This also animates the trunk!
It does so beautifully and optimized by using calculations from the leaves to align the trunk!
--Trunk Animation Module
local treeanim={}
local wind=workspace.GlobalWind
function treeanim.Initialize(Leaves)
local pos= Leaves.CFrame
local T = -99999
local tall = Leaves.Size.Y / 2
local rand = (math.random(0,20))/10
local px5=pos.Position.x/5
local pz6=pos.Position.z/6
local function Animate(x,z)
local x = x and x*.75 or (math.sin(T+(px5))*math.sin(T/9))/4.5
local z = z and z*.75 or (math.sin(T+(pz6))*math.sin(T/12))/6
Leaves.CFrame =pos:ToWorldSpace(CFrame.new(x,0,z) * CFrame.Angles((z)/tall, 0,(x)/-tall))* CFrame.Angles(math.rad(wind.Z/tall), 0,math.rad(wind.X/tall))
T = T + 0.12
return Leaves
end
return Animate
end
return treeanim
The example code perfectly handles Trees being rendered in an out
I tested different methods of timers and I found that while true do is best for the timing frequency of this system. I really scratch my head considering how to further optimize the code.
Originally I tested versus the original source code and did what I thought would be most efficient but turned out to not be. Then I started over and found some optimizations and that is about all there is to it.
–Features List
- Animate all the Trees with one Loop.
- Animate The Trunk and Leaves.
- 40 Formatted and Optimized Tree Models ready for Game.
- The code is short and optimized with a small footprint, compared to other methods.
- Supports Wind Direction