Exemplo n.º 1
0
 public virtual void usdiOnLoad()
 {
     m_primPath     = usdi.usdiPrimGetPathS(m_schema);
     m_primTypeName = usdi.usdiPrimGetUsdTypeNameS(m_schema);
     m_master       = m_stream.usdiFindSchema(usdi.usdiPrimGetMaster(m_schema));
     usdiSyncVarinatSets();
     if (m_goAssigned)
     {
         var c = usdiSetupSchemaComponent();
         c.schema = this;
     }
 }
Exemplo n.º 2
0
        public void usdiSetupComponents(usdiMesh parent_mesh)
        {
            if (!m_setupRequierd)
            {
                return;
            }
            m_setupRequierd = false;

            GameObject go;

            if (m_nth == 0)
            {
                go = parent_mesh.gameObject;
            }
            else
            {
                string name   = "Submesh[" + m_nth + "]";
                var    parent = parent_mesh.GetComponent <Transform>();
                var    child  = parent.FindChild(name);
                if (child != null)
                {
                    go = child.gameObject;
                }
                else
                {
                    go = new GameObject(name);
                    go.GetComponent <Transform>().SetParent(parent_mesh.GetComponent <Transform>(), false);
                }
            }

            m_trans = go.GetComponent <Transform>();

            var  meshSummary           = parent_mesh.meshSummary;
            var  meshData              = parent_mesh.meshData;
            bool assignDefaultMaterial = false;

            if (meshSummary.num_bones > 0)
            {
                // setup SkinnedMeshRenderer

                var renderer = go.GetComponent <SkinnedMeshRenderer>();
                if (renderer == null)
                {
                    renderer = go.AddComponent <SkinnedMeshRenderer>();
                    assignDefaultMaterial = true;
                }
                {
                    var boneNames = usdi.usdiMeshGetBoneNames(parent_mesh.nativeMeshPtr, ref meshData);
                    m_bones = new Transform[boneNames.Length];
                    for (int i = 0; i < boneNames.Length; ++i)
                    {
                        var schema = m_stream.usdiFindSchema(boneNames[i]);
                        if (schema == null)
                        {
                            Debug.LogError("bone not found: " + boneNames[i]);
                            continue;
                        }
                        if (schema.gameObject == null)
                        {
                            // todo: this will happen on instanced object. need to do something
                            Debug.LogError("bone don't have GameObject: " + boneNames[i]);
                            continue;
                        }
                        m_bones[i] = schema.gameObject.GetComponent <Transform>();
                    }

                    if (meshData.root_bone != IntPtr.Zero)
                    {
                        var rootBone = m_stream.usdiFindSchema(usdi.S(meshData.root_bone));
                        m_rootBone = rootBone.gameObject.GetComponent <Transform>();
                    }
                    else
                    {
                        m_rootBone = m_bones[0]; // maybe incorrect
                    }
                }
                m_renderer = renderer;

                m_umesh = renderer.sharedMesh;
                if (m_umesh == null)
                {
                    m_umesh             = new Mesh();
                    renderer.sharedMesh = m_umesh;
                }
                m_umesh.MarkDynamic();
            }
            else
            {
                // setup MeshFilter and MeshRenderer

                var meshFilter = go.GetComponent <MeshFilter>();
                if (meshFilter == null || meshFilter.sharedMesh == null)
                {
                    m_umesh = new Mesh();
                    if (meshFilter == null)
                    {
                        meshFilter = go.AddComponent <MeshFilter>();
                    }
                    meshFilter.sharedMesh = m_umesh;
                }
                else
                {
                    m_umesh = meshFilter.sharedMesh;
                }
                m_umesh.MarkDynamic();

                m_renderer = go.GetComponent <MeshRenderer>();
                if (m_renderer == null)
                {
                    m_renderer            = go.AddComponent <MeshRenderer>();
                    assignDefaultMaterial = true;
                }
            }

#if UNITY_EDITOR
            if (assignDefaultMaterial)
            {
                Material material = UnityEngine.Object.Instantiate(GetDefaultMaterial());
                material.name             = "Material_0";
                m_renderer.sharedMaterial = material;
            }
#endif
        }