Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

Setting up bone hierarchy for SkinnedMeshRenderer manually in script

Discussion in 'Scripting' started by Smidgens, Oct 22, 2018.

  1. Smidgens

    Smidgens

    Joined:
    Nov 2, 2014
    Posts:
    24
    I've been trying to find something on this topic without luck.

    What I want to do is instantiate a model through script and set up the bone hierarchy and Skinned Renderer component by hand.

    I've been wondering about the following for example:
    1) Is it possible to retrieve the bone hierarchy information from somewhere? Like say,
    to use HumanBodyBones to retrieve the transform data for a specific bone given a Mesh, Avatar definition, or both?

    2) How does the SkinnedMeshRenderer component map the mesh to the bones? Where is the vertex to bone information stored? The Mesh? The Avatar definition? ​

    To be clear: I do not want to simply instantiate a prefab. I'm curious about how much of this stuff it is possible to do procedurally through scripts.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,599
    You are in luck... it can be done!

    Unity3D is absolutely awesome for this stuff... and I had saved this in my hotlinks section a while back:

    http://docs.unity3d.com/ScriptReference/Mesh-bindposes.html

    Best of all after that sample codelet runs you can pause it and sniff around the scene and see what's going on.

    Glorious!

    NOTE: there is an annoying (infuriating?) detail about the above example code: it produces a mesh facing AWAY from the camera. Spin the object this script is on by 180 around the Y axis, or put your camera on the other side. Or just wind your triangles the opposite way so they face at you.
     
    Smidgens likes this.
  3. Smidgens

    Smidgens

    Joined:
    Nov 2, 2014
    Posts:
    24
    You magnificent hunk you - this is indeed what I was looking for, and then some. Thanks!
     
    Kurt-Dekker likes this.