Beispiel #1
0
 internal TexturedMaterial([NotNull] string materialName, [NotNull] Texture2D mainTexture, [CanBeNull] Texture2D subTexture, [NotNull] TexturedMaterialExtraProperties extraProperties)
 {
     MaterialName    = materialName;
     MainTexture     = mainTexture;
     SubTexture      = subTexture;
     ExtraProperties = extraProperties;
 }
Beispiel #2
0
        public MeshWrapper([NotNull] IReadOnlyList <SerializedFile> assetFiles, [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(assetFiles, 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_Tangents != null)
            {
                Tangents = ReadVector3Array(mesh.m_Tangents);
            }

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

            if (mesh.m_Shapes != null)
            {
                Shape = new BlendShapeData(mesh.m_Shapes);
            }
        }
Beispiel #3
0
        private static TexturedMaterial FindMaterialInfo([NotNull] IReadOnlyList <SerializedFile> assetFiles, [NotNull] Mesh mesh, int meshIndex, [NotNull] TexturedMaterialExtraProperties extraProperties)
        {
            SkinnedMeshRenderer meshRenderer = null;

            foreach (var assetFile in assetFiles)
            {
                foreach (var obj in assetFile.Objects)
                {
                    if (obj.type != ClassIDType.SkinnedMeshRenderer)
                    {
                        continue;
                    }

                    var renderer = obj as SkinnedMeshRenderer;

                    Debug.Assert(renderer != null);

                    if (renderer.m_Mesh.m_PathID == mesh.m_PathID)
                    {
                        meshRenderer = renderer;
                        break;
                    }
                }
            }

            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];
            Material material    = null;

            foreach (var assetFile in assetFiles)
            {
                foreach (var obj in assetFile.Objects)
                {
                    if (obj.type != ClassIDType.Material)
                    {
                        continue;
                    }

                    var mat = obj as Material;

                    Debug.Assert(mat != null);

                    if (mat.m_PathID == materialPtr.m_PathID)
                    {
                        material = mat;
                        break;
                    }
                }
            }

            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)
            {
                throw new KeyNotFoundException("Main texture is missing.");
            }

            Texture2D mainTexture = null;

            foreach (var assetFile in assetFiles)
            {
                foreach (var obj in assetFile.Objects)
                {
                    if (obj.type != ClassIDType.Texture2D)
                    {
                        continue;
                    }

                    var tex = obj as Texture2D;

                    Debug.Assert(tex != null);

                    if (tex.m_PathID == mainTexEnv.m_Texture.m_PathID)
                    {
                        mainTexture = tex;
                        break;
                    }
                }
            }

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

            Texture2D subTexture = null;

            if (subTexEnv != null)
            {
                foreach (var assetFile in assetFiles)
                {
                    foreach (var obj in assetFile.Objects)
                    {
                        if (obj.type != ClassIDType.Texture2D)
                        {
                            continue;
                        }

                        var tex = obj as Texture2D;

                        Debug.Assert(tex != null);

                        if (tex.m_PathID == subTexEnv.m_Texture.m_PathID)
                        {
                            subTexture = tex;
                            break;
                        }
                    }
                }

                if (subTexture == null)
                {
                    throw new KeyNotFoundException("Sub texture is not found by path ID.");
                }
            }

            return(new TexturedMaterial(material.m_Name, mainTexture, subTexture, extraProperties));
        }
Beispiel #4
0
 static TexturedMaterialExtraProperties()
 {
     Head = new TexturedMaterialExtraProperties(false);
     Body = new TexturedMaterialExtraProperties(true);
 }
Beispiel #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));
        }