Aug
7
2013

Procedural Tree Mesh Generator (wip)

tree_mesh_generator_unity_wip

Small test for doing procedural trees.. (screenshot showing trunk with resolution 8 and 5 sides..)

Current features:
– Trunk creation: adjustable height, height segments, resolution (amount of “sides”)
– Using quads
– Perlin noise rotations for trunk direction
– Adjustable root segment radius multiplier (so root part is thicker)
– Branches: length, size, amount
– Leaves: texture, plane width

TODO:
– lots of things Oo..


Ideas:

– For destruction mode..could encode values into vertex colors! So when some branch is clicked, can search all vertices that are in the same branch! (or do soft selection on those vertices..)


Notes:

Error: “error CS0019: Operator `*’ cannot be applied to operands of type `UnityEngine.Vector3′ and `UnityEngine.Quaternion'”
Code: myVector = myVector*myRotation;
Solution: myVector = myRotation*myVector; // uh
–  Used this ReMap function for remapping values from different ranges..
– Huh took several hours to align branches to vertex normal direction, tried everything: transform.LookAt(), Quaternion.LookRotation(), Vector3.RotateTowards().. finally this did it: transform.rotation = Quaternion.FromToRotation(Vector3.up, normalDir);
– Texture used for leaves: http://www.cgtextures.com/texview.php?id=106971


Webplayer:

http://unitycoder.com/upload/demos/ProceduralTreeGenerator/ (v1.0)
(quite a messy trees..maybe because branches are added randomly and no scaling to smaller size towards top yet..)

Inspiration:
Procedural trees (vimeo)
CanTree (online tree generator)

Image#2: Rotation on creation now works.. Vertices are not shared, so had to get them connected by rotating with 2 different values (lower part with currentRot and upper with nextRot)

tree_mesh_generator_unity_wip_bending

Image#3: Doing branches creation in 2nd pass..(Debug.LineDraw here for testing, using vertex normal to go away from the center..)

tree_mesh_generator_unity_wip_branches

Image#4: Branches..currently separate gameobjects because “mesh.CombineMeshes” keeps giving some error ” totalNumIndices <= (maxIndices+1)”..

tree_mesh_generator_unity_wip_branches2

Image#5: Random branches not yet matching to the center position..(as they were generated after trunk was done, would have to know the rotated trunk center position..uh..)

procedural_tree_generator_branches_unity_3

Image#6: Fixed ^that by saving segment center positions to a list..then Vector3.Lerp() on the branch y position between 2 segments..
procedural_tree_generator_branches_unity_4

Image#7: Recursive branch function gone wrong..

procedural_tree_generator_recursive_gone_bad_1

Image#8: Skipped recursive function for now.. duplicated makeBranch() function and adjusted few variables there instead.. starting to look like a tree almost.

procedural_tree_generator_unity3d_v1

Image#9: Ok its ready!…not. Got some leaves working, just simple quad planes from start of the branch to the end.. Used shader “AlphaTest-Diffuse.shader”, modified it to be 2 sided by adding line “Cull Off” before line “LOD 200” (built in shader sources)

procedural_tree_generator_unity3d_leaves_1

Image#10: Adjusted branch generation..more realistic, but not yet using any of that fibonacci stuff

unity_treemaker_plugin_1


6 Comments + Add Comment

Leave a comment

Connect

Twitter View LinkedIn profile Youtube Youtube Join Discord Twitch

UnityLauncherPro

Get UnityLauncherPRO and work faster with Unity Projects!
*free unity hub alternative

@unitycoder_com

Subscribe to Blog via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.