x


rotate bones before bindposes

please a need help with this.

I have used this code http://unity3d.com/support/documentation/ScriptReference/Mesh-bindposes.html, modify but I noticed that the bones did not rotate. I've tried in this way to see the bones:

void Update(){ 

    try{
        for ( int i = 0; i < bones.Length; i++ ){
            Debug.DrawLine (bones[i].transform.position,bones[i].parent.transform.position, Color.green);
        }

    }catch(Exception e){}
}

and this way to rorate:

bones[1] = new GameObject("Upper").transform;
    bones[1].parent = transform;
    bones[1].localRotation = Quaternion.Euler(0,  180, 0);
    bones[1].localPosition = new Vector3(0, 5, 0);

and does not work, the bones are not rotating.

What am I doing wrong?

spanish to english(spanish to english)

more ▼

asked Apr 17 '11 at 10:00 PM

montblack gravatar image

montblack
49 8 8 15

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

Do you do this as well (taken from your link):

bindPoses[1] = bones[1].worldToLocalMatrix * transform.localToWorldMatrix;

This kind of "compensates" for the rotation that you just set. You need to change position/rotation of bone after that (better in runtime to see the results).

Do you really need to create meshes from code?

more ▼

answered Apr 18 '11 at 06:10 AM

Paulius Liekis gravatar image

Paulius Liekis
7.3k 16 24 45

(comments are locked)
10|3000 characters needed characters left

yes i do that the complete code is:

using UnityEngine;
using System.Collections;
using System;
public class example : MonoBehaviour {
    Transform[] bones;
    void Start() {
        gameObject.AddComponent<Animation>();
        gameObject.AddComponent<SkinnedMeshRenderer>();
        SkinnedMeshRenderer renderer = GetComponent<SkinnedMeshRenderer>();
        Mesh mesh = new Mesh();
        mesh.vertices = new Vector3[] {new Vector3(-1, 0, 0), new Vector3(1, 0, 0), new Vector3(-1, 5, 0), new Vector3(1, 5, 0)};
        mesh.uv = new Vector2[] {new Vector2(0, 0), new Vector2(1, 0), new Vector2(0, 1), new Vector2(1, 1)};
        mesh.triangles = new int[] {0, 1, 2, 1, 3, 2};
        mesh.RecalculateNormals();
        renderer.material = new Material(Shader.Find(" Diffuse"));
        BoneWeight[] weights = new BoneWeight[4];
        weights[0].boneIndex0 = 0;
        weights[0].weight0 = 1;
        weights[1].boneIndex0 = 0;
        weights[1].weight0 = 1;
        weights[2].boneIndex0 = 1;
        weights[2].weight0 = 1;
        weights[3].boneIndex0 = 1;
        weights[3].weight0 = 1;
        mesh.boneWeights = weights;
        bones = new Transform[2];
        Matrix4x4[] bindPoses = new Matrix4x4[2];
        bones[0] = new GameObject("Lower").transform;
        bones[0].parent = transform;
        bones[0].localRotation = Quaternion.identity;
        bones[0].localPosition = Vector3.zero;
        bindPoses[0] = bones[0].worldToLocalMatrix * transform.localToWorldMatrix;
        bones[1] = new GameObject("Upper").transform;
        bones[1].parent = transform;
        bones[1].localRotation = Quaternion.Euler(0,  180, 0);
        bones[1].localPosition = new Vector3(0, 5, 0);
        bindPoses[1] = bones[1].worldToLocalMatrix * transform.localToWorldMatrix;
        mesh.bindposes = bindPoses;
        renderer.bones = bones;
        renderer.sharedMesh = mesh;
        AnimationCurve curve = new AnimationCurve();
        curve.keys = new Keyframe[] {new Keyframe(0, 0, 0, 0), new Keyframe(1, 3, 0, 0), new Keyframe(2, 0.0F, 0, 0)};
        AnimationClip clip = new AnimationClip();
        clip.SetCurve("Lower", typeof(Transform), "m_LocalPosition.z", curve);
        animation.AddClip(clip, "test");
        animation.Play("test");
    }

    void Update(){ 

    try{
        for ( int i = 0; i < bones.Length; i++ ){
        Debug.DrawLine (bones[i].transform.position,bones[i].parent.transform.position, Color.green);
    }

        }catch(Exception e){}
    }
 }

I see the inspector that applies a rotation, but graphically the object is in the same position and without rotating, try using that code and rotate in different angles, and you will see that they will always get the same result. And yes, i need to create the mesh with code, becouse i'm allready reading a xml format, and greatly reducing the number of vertices when importing.

i need a rotation like this:

alt text

please help me i really need this.

Greetings from Colombia!

more ▼

answered Apr 18 '11 at 02:57 PM

montblack gravatar image

montblack
49 8 8 15

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x721
x143
x7

asked: Apr 17 '11 at 10:00 PM

Seen: 798 times

Last Updated: Apr 17 '11 at 10:00 PM