@dkostenevich/tree-js
v0.6.0
Published
<p align="center"> <img src="https://github.com/dkostenevich/tree-js/assets/3814912/d42076c3-06ad-469d-bca1-f84a1a5eb2cb" width="400"> </p>
Downloads
1
Maintainers
Readme
Tree.js
A procedural tree generator built with Three.js. Over 30 tunable parameters and support for exporting to .glb.
Installation
The tree generation code is published as an npm package and can be imported into your own project.
npm i @dkostenevich/tree-js
Usage
// Create new instance
const tree = new Tree();
// Set parameters
tree.options.trunk.length = 20;
tree.options.branch.levels = 3;
// Generate tree and add to scene
tree.generate();
scene.add(tree);
Any time the tree parametes are changed, you must call generate()
to regenerate the geometry.
For more info, see the demo app for an example on how to create a tree.
Demo
The demo application is a Three.js app which allows you to tune the parameters of the tree in real-time. It also has options for exporting the generated mesh to a .glb
file, as well as a .png
export of the current view for 2D applications.
Running Demo Locally
npm install
npm run build:npm
npm run demo
Running Demo with Docker
docker compose build
docker compose up -d
Parameters
seed
- Seed for RNG
Trunk
color
- Color of the tree trunkflatShading
- Use face normals for shading instead of vertex normalslength
- Length of the base trunkradius
- Starting radius of the trunk
Branches
levels
- Number of branch recursions ( Keep under 5 )children
- Number of child branchesstart
- Defines where child branches start forming on the parent branch. A value of 0.6 means the child branches can start forming 60% of the way up the parent branchstop
- Defines where child branches stop forming on the parent branch. A value of 0.9 means the child branches stop forming 90% of the way up the parent branchangle
- Angle between child branch and parent branch (radians)angleVariance
- Variation inangle
(radians)lengthVariance
- Variation in branch lengthlengthMultiplier
- Length of child branch relative to parentradiusMultiplier
- Radius of child branch relative to parenttaper
- Radius of end of branch relative to the start of the branchgnarliness
- Max amplitude of random angle added to each section's orientationtwist
- Amount of twisting about the vertical axis
External Force
direction
- Influences the direction the tree grows towardsstrength
- Strength of the sun influence
Geometry
sections
- Number of sections that make up this branchsegments
- Number of faces around the circumference of the branchlengthVariance
- % variance in the nominal section lengthradiusVariance
- % variance in the nominal section radiusrandomization
- Randomization factor applied to vertices
Leaves
style
- Leaf billboard style (single or double)type
- Leaf type (Ash, Aspen or Oak)color
- Leaf colorcount
- Number of leaves per branchsize
- Size of leaf texturesizeVariance
- Variance in leaf size between branches