- Home /
Tree vertices never change position in wind
I am trying to access the vertices of a tree created with the unity tree creator. The tree is placed in a wind zone and moves correctly according to the wind. However, I want to use the vertices of the mesh for attaching an object their position so that is follows the movement of the tree.
But the vertices never change value in the array list, even though they move visually. How can this be?
void Awake () {
tree = GameObject.FindGameObjectWithTag("Tree");
treeMesh = tree.GetComponent<MeshFilter>().mesh;
float closestDistance = 99999;
for(int i = 0; i < treeMesh.vertices.Length; i++){
float tempDistance = Vector3.Distance(transform.position, treeMesh.vertices[i]);
if(tempDistance < closestDistance) {
closestDistance = tempDistance;
closestIndex = i;
}
}
}
void Update () {
transform.position = tree.transform.TransformPoint(treeMesh.vertices[closestIndex]);
}
Try doing this in Start() ins$$anonymous$$d of Awake() in case the WindZone stuff hasn't kicked in, just a shot in the dark.
Answer by cod · Sep 10, 2013 at 08:34 AM
I think this is because the tree animation is done via shader, so the vertices are modified not in the cpu loop, but on the GPU, as it is done by the shader as i said before. The only way to achieve this effect is to write a new shader with the same vertex animation as in the tree shader
PS u can easily find the tree shader in the builtin shader resource on the unity site
Ah thanks for the answer didn't think about that option. However, as I am new to shader program$$anonymous$$g I am not sure where to find the piece of code that animates the vertices, any hints?
Here's the vertex function
void TreeVertBark (inout appdata_full v)
{
v.vertex.xyz *= _Scale.xyz;
v.vertex = AnimateVertex(v.vertex, v.normal, float4(v.color.xy, v.texcoord1.xy));
v.vertex = Squash(v.vertex);
v.color = float4 (1, 1, 1, v.color.a);
v.normal = normalize(v.normal);
v.tangent.xyz = normalize(v.tangent.xyz);
}
and here're the docs link text
try to have a look at them and tell me if u have any problem :)
EDIT: fixed the wrong format
Thanks! Which shader script is this included in? I cant seem to find it.
Ok I got to a point where I can edit the code and see it affect the tree animation while running the game. However, I am unsure where to go from here. How do I give the positions of the vertices to my object?
I thought maybe its possible to use the same shader for my custom object as for the leaves in order to make it move, but it doesnt seem to do anything when put on something other than the unity tree.
Your answer
Follow this Question
Related Questions
WindZone and Tree direction 0 Answers
Is there an easy way to find connected vertices in Mesh.vertices? 2 Answers
Precise vertex positions 1 Answer
Why vertex positions appear (0.0, 0.0, 0.0) ? 1 Answer
tree bend factor wont work problem. 1 Answer