Example #1
0
 public void SetSkinnedMeshRenderer(SkinnedMeshRenderer renderer, Transform parent)
 {
     Transform[] bonesTransform = new Transform[bones.Length];
     for (int i = 0; i < bones.Length; i++)
     {
         bonesTransform[i] = parent.Find(bones[i]);
     }
     renderer.bones                = bonesTransform;
     renderer.sharedMesh           = mesh.CreateMesh();
     renderer.sharedMesh.bindposes = bindposes;
     Unity.Collections.NativeArray <BoneWeight1> bw1 = new Unity.Collections.NativeArray <BoneWeight1>(boneIndexes.Length, Unity.Collections.Allocator.Temp);
     for (int j = 0; j < boneIndexes.Length; j++)
     {
         bw1[j] = new BoneWeight1 {
             boneIndex = boneIndexes[j], weight = boneWeigts[j]
         };
     }
     renderer.sharedMesh.SetBoneWeights(new Unity.Collections.NativeArray <byte>(bonesPerVert, Unity.Collections.Allocator.Temp), bw1);
     renderer.rootBone    = parent.Find(rootBone);
     renderer.localBounds = new Bounds(boundsCenter, boundsExtend);
     renderer.sharedMesh.RecalculateBounds();
 }
Example #2
0
        private void LoadObject(ObjectData data)
        {
            GameObject gobject;
            string     absoluteMeshPath;
            Transform  importedParent = null;

            // Check for import
            if (data.isImported)
            {
                try
                {
                    importedParent   = new GameObject("__VRtist_tmp_load__").transform;
                    absoluteMeshPath = data.meshPath;
                    // Don't use async import since we may reference the game object for animations or constraints
                    // and the object must be loaded before we do so
                    GlobalState.GeometryImporter.ImportObject(absoluteMeshPath, importedParent, true);
                    if (importedParent.childCount == 0)
                    {
                        return;
                    }
                    gobject = importedParent.GetChild(0).gameObject;
                }
                catch (System.Exception e)
                {
                    Debug.LogError("Failed to load external object: " + e.Message);
                    return;
                }
            }
            else
            {
                absoluteMeshPath = GetSaveFolderPath(currentProjectName) + data.meshPath;
                gobject          = new GameObject(data.name);
            }

            LoadCommonData(gobject, data);
            gobject.name = data.name;

            // Mesh
            if (null != data.meshPath && data.meshPath.Length > 0)
            {
                if (!data.isImported)
                {
                    if (!loadedMeshes.TryGetValue(absoluteMeshPath, out Mesh mesh))
                    {
                        MeshData meshData = new MeshData();
                        SerializationManager.Load(absoluteMeshPath, meshData);
                        mesh = meshData.CreateMesh();
                        loadedMeshes.Add(absoluteMeshPath, mesh);
                    }
                    gobject.AddComponent <MeshFilter>().sharedMesh  = mesh;
                    gobject.AddComponent <MeshRenderer>().materials = LoadMaterials(data);
                    gobject.AddComponent <MeshCollider>();
                }

                if (!data.visible)
                {
                    foreach (Component component in gobject.GetComponents <Component>())
                    {
                        Type componentType = component.GetType();
                        var  prop          = componentType.GetProperty("enabled");
                        if (null != prop)
                        {
                            prop.SetValue(component, data.visible);
                        }
                    }
                }
            }

            SceneManager.AddObject(gobject);

            if (data.parentPath.Length > 0)
            {
                SceneManager.SetObjectParent(gobject, rootTransform.Find(data.parentPath).gameObject);
            }

            if (data.isImported)
            {
                ParametersController controller = gobject.AddComponent <ParametersController>();
                controller.isImported = true;
                controller.importPath = data.meshPath;

                if (null != importedParent)
                {
                    Destroy(importedParent.gameObject);
                }
            }
        }