Exemplo n.º 1
0
        public static SerializedObjectsLookup Create([NotNull] AssetsManager manager)
        {
            var result = new SerializedObjectsLookup();
            var dict   = result._lookup;

            foreach (var serializedFile in manager.assetsFileList)
            {
                foreach (var o in serializedFile.Objects)
                {
                    dict.Add(o.m_PathID, o);
                }
            }

            return(result);
        }
Exemplo n.º 2
0
        private static void BuildHierarchy([NotNull] GameObject root, [NotNull] SerializedObjectsLookup serializedObjectsLookup, [NotNull] Dictionary <long, TransformObject> objObjMap)
        {
            if (objObjMap.ContainsKey(root.m_PathID))
            {
                return;
            }

            var transform       = new RawTransform(root.m_Transform);
            var transformObject = new TransformObject(root.m_Name, transform);

            objObjMap.Add(root.m_PathID, transformObject);

            {
                var childTransforms = root.m_Transform.m_Children;

                if (childTransforms.Length > 0)
                {
                    var childList = transformObject.ChildList;
                    Debug.Assert(childList != null, nameof(childList) + " != null");

                    foreach (var childTransform in childTransforms)
                    {
                        Debug.Assert(childTransform != null, nameof(childTransform) + " != null");

                        var childGameObject = serializedObjectsLookup.Find <GameObject>(t => t.m_Transform.m_PathID == childTransform.m_PathID);
                        Debug.Assert(childGameObject != null, nameof(childGameObject) + " != null");

                        BuildHierarchy(childGameObject, serializedObjectsLookup, objObjMap);

                        childList.Add(objObjMap[childGameObject.m_PathID]);
                    }
                }
            }

            {
                var parentTransform = root.m_Transform.m_Father;

                if (!parentTransform.IsNull)
                {
                    var parentGameObject = serializedObjectsLookup.Find <GameObject>(t => t.m_Transform.m_PathID == parentTransform.m_PathID);
                    Debug.Assert(parentGameObject != null, nameof(parentGameObject) + " != null");

                    BuildHierarchy(parentGameObject, serializedObjectsLookup, objObjMap);

                    transformObject.Parent = objObjMap[parentGameObject.m_PathID];
                }
            }
        }
Exemplo n.º 3
0
        public static TransformHierarchies FromAssets([NotNull] AssetsManager manager)
        {
            var idObjectMap = new List <GameObject>();
            var lookup      = SerializedObjectsLookup.Create(manager);

            foreach (var serializedFile in manager.assetsFileList)
            {
                foreach (var o in serializedFile.Objects)
                {
                    if (o.type != ClassIDType.GameObject)
                    {
                        continue;
                    }

                    var go = o as GameObject;
                    Debug.Assert(go != null, nameof(go) + " != null");

                    idObjectMap.Add(go);
                }
            }

            var objObjMap = new Dictionary <long, TransformObject>();

            foreach (var gameObject in idObjectMap)
            {
                BuildHierarchy(gameObject, lookup, objObjMap);
            }

            var roots = new List <TransformObject>();

            foreach (var transformObject in objObjMap.Values)
            {
                transformObject.Seal();

                if (transformObject.Parent == null)
                {
                    roots.Add(transformObject);
                }
            }

            var result = new TransformHierarchies(roots.ToArray());

            return(result);
        }
Exemplo n.º 4
0
        public MeshWrapper([NotNull] SerializedObjectsLookup serializedObjectsLookup, [NotNull] Mesh mesh, [NotNull] TexturedMaterialExtraProperties extraMaterialProperties)
        {
            Name = mesh.m_Name;

            {
                var  subMeshCount   = mesh.m_SubMeshes.Length;
                var  subMeshes      = new PrettySubMesh[subMeshCount];
                uint meshIndexStart = 0;

                for (var i = 0; i < mesh.m_SubMeshes.Length; i += 1)
                {
                    var subMesh  = mesh.m_SubMeshes[i];
                    var material = FindMaterialInfo(serializedObjectsLookup, mesh, i, extraMaterialProperties);
                    var sm       = new PrettySubMesh(meshIndexStart, subMesh, material);
                    subMeshes[i]    = sm;
                    meshIndexStart += subMesh.indexCount;
                }

                SubMeshes = subMeshes;
            }

            Indices = mesh.m_Indices.ToArray();

            {
                const int bonePerVertex = 4;

                var skinCount = mesh.m_Skin.Length;
                var skins     = new BoneInfluence[skinCount][];

                for (var i = 0; i < skinCount; i += 1)
                {
                    var s = new BoneInfluence[bonePerVertex];

                    skins[i] = s;

                    var w = mesh.m_Skin[i];

                    for (var j = 0; j < bonePerVertex; j += 1)
                    {
                        BoneInfluence influence;

                        if (w != null)
                        {
                            influence = new BoneInfluence(w, j);
                        }
                        else
                        {
                            influence = null;
                        }

                        s[j] = influence;
                    }
                }

                Skin = skins;
            }

            BindPose    = (Matrix4x4[])mesh.m_BindPose.Clone();
            VertexCount = mesh.m_VertexCount;

            Vertices = ReadVector3Array(mesh.m_Vertices, mesh.m_VertexCount);
            Normals  = ReadVector3Array(mesh.m_Normals);

            if (mesh.m_Colors != null)
            {
                Colors = ReadVector4Array(mesh.m_Colors);
            }

            UV1 = ReadVector2Array(mesh.m_UV0);

            if (mesh.m_UV1 != null)
            {
                Debug.Assert(mesh.m_UV0.Length == mesh.m_UV1.Length, "mesh.m_UV0.Length == mesh.m_UV1.Length");
            }

            UV2 = ReadNullableVector2Array(mesh.m_UV1, mesh.m_UV0.Length / 2);

            if (mesh.m_Tangents != null)
            {
                Tangents = ReadVector3Array(mesh.m_Tangents);
            }

            BoneNameHashes = (uint[])mesh.m_BoneNameHashes.Clone();

            if (mesh.m_Shapes != null)
            {
                Shape = new BlendShapeData(mesh.m_Shapes);
            }
        }
Exemplo n.º 5
0
        private static TexturedMaterial FindMaterialInfo([NotNull] SerializedObjectsLookup serializedObjectsLookup, [NotNull] Mesh mesh, int meshIndex, [NotNull] TexturedMaterialExtraProperties extraProperties)
        {
            var meshRenderer = serializedObjectsLookup.Find <SkinnedMeshRenderer>(renderer => renderer.m_Mesh.m_PathID == mesh.m_PathID);

            if (meshRenderer == null)
            {
                throw new KeyNotFoundException($"Found no SkinnedMeshRenderer associated with this mesh ({mesh.m_Name}).");
            }

            Debug.Assert(meshRenderer.m_Materials != null);

            if (meshRenderer.m_Materials.Length <= meshIndex)
            {
                throw new FormatException("No corresponding material is associated with this SkinnedMeshRenderer.");
            }

            var materialPtr = meshRenderer.m_Materials[meshIndex];

            serializedObjectsLookup.TryGet(materialPtr, out var material);

            if (material == null)
            {
                throw new KeyNotFoundException("Main material is not found by path ID.");
            }

            Debug.Assert(material.m_SavedProperties != null);
            Debug.Assert(material.m_SavedProperties.m_TexEnvs != null);
            Debug.Assert(material.m_SavedProperties.m_TexEnvs.Length > 0);

            var         kvPairs    = material.m_SavedProperties.m_TexEnvs;
            UnityTexEnv mainTexEnv = null;
            UnityTexEnv subTexEnv  = null;

            foreach (var kv in kvPairs)
            {
                if (kv.Key.Equals("_MainTex", StringComparison.Ordinal))
                {
                    mainTexEnv = kv.Value;
                }
                else if (kv.Key.Equals("_SubTex", StringComparison.Ordinal))
                {
                    subTexEnv = kv.Value;
                }

                if (mainTexEnv != null && subTexEnv != null)
                {
                    break;
                }
            }

            var hasMainTexture = mainTexEnv != null;
            var hasSubTexture  = subTexEnv != null;

            Texture2D mainTexture = null;

            if (hasMainTexture)
            {
                serializedObjectsLookup.TryGet(mainTexEnv.m_Texture, out mainTexture);

                if (mainTexture == null)
                {
                    var pptrStr = mainTexEnv.m_Texture.GetDebugDescription();
                    Trace.WriteLine($"Warning: Main texture is not found by path ID, use default \"white\" instead. {pptrStr}");
                }
            }

            Texture2D subTexture = null;

            if (hasSubTexture)
            {
                serializedObjectsLookup.TryGet(subTexEnv.m_Texture, out subTexture);

                if (subTexture == null)
                {
                    var pptrStr = subTexEnv.m_Texture.GetDebugDescription();
                    Trace.WriteLine($"Warning: Sub texture is not found by path ID, use default \"white\" instead. {pptrStr}");
                }
            }

            return(new TexturedMaterial(material.m_Name, hasMainTexture, mainTexture, hasSubTexture, subTexture, extraProperties));
        }