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];
                }
            }
        }
Exemple #2
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));
        }