//Match what Wes's compiler does
        public static void FixUVScales(this GenericRCOLResource rcolResource, MLOD.Mesh mesh)
        {
            MATD matd = GetMATDforMesh(rcolResource, mesh.MaterialIndex);

            if (matd == null)
            {
                throw new ArgumentException("No MATD found for requested mesh");
            }

            foreach (FieldType ft in new FieldType[] { FieldType.UVScales, FieldType.DiffuseUVSelector, FieldType.SpecularUVSelector, })
            {
                ShaderData data = (matd.Version < 0x0103 ? matd.Mtrl.SData : matd.Mtnf.SData).Find(x => x.Field == ft);
                if (data == null)
                {
                    continue;
                }

                if (!(data is ElementFloat3))
                {
                    throw new InvalidOperationException(String.Format("Found " + ft + " of type '{0}'; expected 'ElementFloat3'.", data.GetType().Name));
                }

                ElementFloat3 e = data as ElementFloat3;
                e.Data0 = 1f / short.MaxValue;
                e.Data1 = 0f;
                e.Data2 = 0f;
            }
        }
    public static Material ChangeMaterial(MATD from, Material baseMat)
    {
        baseMat.name = from.Name;

        if (!string.IsNullOrEmpty(from.Texture))
        {
            string    texPath    = "Textures/" + from.Texture.Replace(".tga", "");
            string    normalPath = "Textures/" + from.Texture.Replace(".tga", "") + NORMAL_MAP_SUFFIX;
            Texture2D texture    = Resources.Load(texPath) as Texture2D;
            Texture2D normal     = Resources.Load(normalPath) as Texture2D;

            if (texture != null)
            {
                baseMat.SetTexture("_MainTex", texture);
            }
            else
            {
                Debug.LogWarning("Could not find " + texPath);
            }

            if (normal != null)
            {
                baseMat.SetTexture("_BumpMap", normal);
            }
            else
            {
                Debug.LogWarning("Could not find " + normalPath);
            }
        }

        return(baseMat);
    }
    public static Material Material2Unity(MATD from)
    {
        Material material = new Material(Shader.Find(SHADER));

        material.EnableKeyword("_METALLICGLOSSMAP");
        material.EnableKeyword("_SPECGLOSSMAP");
        material.name = from.Name;
        material.SetColor("_Color", Color2Unity(from.Diffuse));
        material.SetFloat("_Glossiness", 1.0f);
        material.SetFloat("_Metallic", 0.0f);


        if (!string.IsNullOrEmpty(from.Texture))
        {
            string    texPath    = "Textures/" + from.Texture.Replace(".tga", "");
            string    normalPath = "Textures/" + (from.Texture + "_normal").Replace(".tga", "");
            Texture2D texture    = Resources.Load(texPath) as Texture2D;
            Texture2D normal     = Resources.Load(normalPath) as Texture2D;

            if (texture != null)
            {
                material.SetTexture("_MainTex", texture);
                if (!File.Exists(@"Assets\Resources\Materials\" + from.Texture.Replace(".tga", ".mat")))
                {
                    AssetDatabase.CreateAsset(material, "Assets/Resources/Materials/" + from.Texture.Replace(".tga", ".mat"));
                    material = Resources.Load("Materials/" + from.Texture.Replace(".tga", "")) as Material;
                }
                else
                {
                    material = Resources.Load("Materials/" + from.Texture.Replace(".tga", "")) as Material;
                }
            }
            else
            {
                Debug.LogWarning(File.Exists(from.Texture));
                Debug.LogWarning("Could not find " + texPath);
            }

            if (normal != null)
            {
                material.EnableKeyword("_NORMALMAP");
                material.SetTexture("_BumpMap", normal);
                //material.SetFloat("_BumpScale", NormalScale);
            }
            else
            {
                Debug.LogWarning("Could not find " + normalPath);
            }
        }

        return(material);
    }
Exemple #4
0
        private void removeMaterialMenuItem_Click(object sender, EventArgs e)
        {
            if (selectedChunk.GetType() == typeof(MATD))
            {
                MATD material = (MATD)selectedChunk;

                if (MessageBox.Show("Do you really want to delete the Material '" + material.Name + "' ?", "Confirm", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    msh.Materials.Remove(material);
                    RefreshTreeView();
                }
            }
            else
            {
                MessageBox.Show("Selected Item is not a Material!", "No Material Selected", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        public static float[] GetUVScales(this GenericRCOLResource rcolResource, MLOD.Mesh mesh)
        {
            MATD matd = GetMATDforMesh(rcolResource, mesh.MaterialIndex);

            if (matd != null)
            {
                ShaderData data = (matd.Version < 0x0103 ? matd.Mtrl.SData : matd.Mtnf.SData).Find(x => x.Field == FieldType.UVScales);
                if (data != null)
                {
                    if (data is ElementFloat3)
                    {
                        ElementFloat3 e = data as ElementFloat3; return(new float[] { e.Data0, e.Data1, e.Data2, });
                    }
                    else
                    {
                        throw new InvalidOperationException(String.Format("Found UVScales of type '{0}'; expected 'ElementFloat3'.", data.GetType().Name));
                    }
                }
            }
            return(new float[] { 1f / 32767f, 0f, 0f, });
        }
Exemple #6
0
        private static float[] GetUvScales(MATD matd)
        {
            var param = GetMATDParam <ElementFloat3>(matd, FieldType.UVScales);

            return(param != null ? new[] { param.Data0, param.Data1, param.Data2 } : new[] { 1f / short.MaxValue, 1f / short.MaxValue, 1f / short.MaxValue });
        }
Exemple #7
0
 private static T GetMATDParam <T>(MATD matd, FieldType type) where T : class
 {
     return(matd == null ? null : (matd.Mtrl != null ? matd.Mtrl.SData : matd.Mtrl.SData).FirstOrDefault(x => x.Field == type) as T);
 }
Exemple #8
0
        private void InitScene()
        {
            GeostatesPanel.Visibility = Visibility.Collapsed;
            GenericRCOLResource.ChunkEntry chunk = rcol.ChunkEntries.FirstOrDefault(x => x.RCOLBlock is MLOD);

            int polyCount = 0;
            int vertCount = 0;


            if (chunk != null)
            {
                var mlod = chunk.RCOLBlock as MLOD;
                foreach (MLOD.Mesh m in mlod.Meshes)
                {
                    try
                    {
                        vertCount += m.VertexCount;
                        polyCount += m.PrimitiveCount;
                        var        vbuf     = (VBUF)GenericRCOLResource.ChunkReference.GetBlock(rcol, m.VertexBufferIndex);
                        var        ibuf     = (IBUF)GenericRCOLResource.ChunkReference.GetBlock(rcol, m.IndexBufferIndex);
                        VRTF       vrtf     = (VRTF)GenericRCOLResource.ChunkReference.GetBlock(rcol, m.VertexFormatIndex) ?? VRTF.CreateDefaultForMesh(m);
                        IRCOLBlock material = GenericRCOLResource.ChunkReference.GetBlock(rcol, m.MaterialIndex);

                        MATD matd = FindMainMATD(rcol, material);

                        float[] uvscale = GetUvScales(matd);
                        if (uvscale != null)
                        {
                            Debug.WriteLine(string.Format("{0} - {1} - {2}", uvscale[0], uvscale[2], uvscale[2]));
                        }
                        else
                        {
                            Debug.WriteLine("No scales");
                        }

                        GeometryModel3D model = DrawModel(vbuf.GetVertices(m, vrtf, uvscale), ibuf.GetIndices(m), mNonSelectedMaterial);

                        var sceneMesh = new SceneMlodMesh(m, model);
                        if (matd != null)
                        {
                            sceneMesh.Shader = matd.Shader;
                            switch (matd.Shader)
                            {
                            case ShaderType.ShadowMap:
                            case ShaderType.DropShadow:
                                break;

                            default:
                                var maskWidth  = GetMATDParam <ElementInt>(matd, FieldType.MaskWidth);
                                var maskHeight = GetMATDParam <ElementInt>(matd, FieldType.MaskHeight);
                                if (maskWidth != null && maskHeight != null)
                                {
                                    float scalar = Math.Max(maskWidth.Data, maskHeight.Data);
                                    mCheckerBrush.Transform = new ScaleTransform(maskHeight.Data / scalar, maskWidth.Data / scalar);
                                }
                                break;
                            }
                        }
                        try
                        {
                            var sceneGeostates = new SceneGeostate[m.GeometryStates.Count];
                            for (int i = 0; i < sceneGeostates.Length; i++)
                            {
                                GeometryModel3D state = DrawModel(vbuf.GetVertices(m, vrtf, m.GeometryStates[i], uvscale),
                                                                  ibuf.GetIndices(m, m.GeometryStates[i]), mHiddenMaterial);
                                mGroupMeshes.Children.Add(state);
                                sceneGeostates[i] = new SceneGeostate(sceneMesh, m.GeometryStates[i], state);
                            }
                            sceneMesh.States = sceneGeostates;
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show("Unable to load Geostates.  You may have some corrupted data: " + ex.ToString(),
                                            "Unable to load Geostates...");
                        }
                        mGroupMeshes.Children.Add(model);
                        mSceneMeshes.Add(sceneMesh);
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(String.Format("Unable to load mesh id 0x{0:X8}", m.Name));
                    }
                }
            }
            else
            {
                GenericRCOLResource.ChunkEntry geomChunk = rcol.ChunkEntries.FirstOrDefault();
                var geom  = new GEOM(0, null, geomChunk.RCOLBlock.Stream);
                var verts = new List <Vertex>();
                polyCount = geom.Faces.Count;
                vertCount = geom.VertexData.Count;
                foreach (GEOM.VertexDataElement vd in geom.VertexData)
                {
                    var v = new Vertex();

                    var pos = (GEOM.PositionElement)vd.Vertex.FirstOrDefault(e => e is GEOM.PositionElement);
                    if (pos != null)
                    {
                        v.Position = new[] { pos.X, pos.Y, pos.Z };
                    }


                    var norm = (GEOM.NormalElement)vd.Vertex.FirstOrDefault(e => e is GEOM.NormalElement);
                    if (norm != null)
                    {
                        v.Normal = new[] { norm.X, norm.Y, norm.Z };
                    }


                    var uv = (GEOM.UVElement)vd.Vertex.FirstOrDefault(e => e is GEOM.UVElement);
                    if (uv != null)
                    {
                        v.UV = new[] { new[] { uv.U, uv.V } };
                    }
                    verts.Add(v);
                }
                var facepoints = new List <int>();
                foreach (GEOM.Face face in geom.Faces)
                {
                    facepoints.Add(face.VertexDataIndex0);
                    facepoints.Add(face.VertexDataIndex1);
                    facepoints.Add(face.VertexDataIndex2);
                }

                GeometryModel3D model     = DrawModel(verts.ToArray(), facepoints.ToArray(), mNonSelectedMaterial);
                var             sceneMesh = new SceneGeomMesh(geom, model);
                mGroupMeshes.Children.Add(model);
                mSceneMeshes.Add(sceneMesh);
            }
            foreach (SceneMesh s in mSceneMeshes)
            {
                mMeshListView.Items.Add(s);
            }
            if (mSceneMeshes.Count <= 1)
            {
                MeshesPanel.Visibility = Visibility.Collapsed;
            }
            VertexCount.Text  = String.Format("Vertices: {0}", vertCount);
            PolygonCount.Text = String.Format("Polygons: {0}", polyCount);
        }
Exemple #9
0
        private List <IResourceConnection> MATD_GetDiffuseSpecular()
        {
            string rootStr = this.GetContentPathRootName();
            List <IResourceConnection> results = new List <IResourceConnection>();
            GenericRCOLResource        rcol    = base.Resource as GenericRCOLResource;

            if (rcol == null)
            {
                return(results);
            }
            string absolutePath, absoluteName;

            Diagnostics.Log("MATD_GetDiffuseSpecular: " +
                            ResourceGraph.PrintRKTag(base.originalKey, rootStr));
            for (int i = 0; i < rcol.ChunkEntries.Count; i++)
            {
                MATD matd = rcol.ChunkEntries[i].RCOLBlock as MATD;
                if (matd == null)
                {
                    continue;
                }

                ShaderDataList sdataList;
                if (matd.ContentFields.Contains("Mtnf"))
                {
                    sdataList    = matd.Mtnf.SData;
                    absolutePath = rootStr + ".ChunkEntries[" + i + "].RCOLBlock.Mtnf.SData[";
                }
                else
                {
                    sdataList    = matd.Mtrl.SData;
                    absolutePath = rootStr + ".ChunkEntries[" + i + "].RCOLBlock.Mtrl.SData[";
                }

                for (int j = 0; j < sdataList.Count; j++)
                {
                    ElementTextureRef texRef = sdataList[i] as ElementTextureRef;
                    if (texRef != null && IsWantedImage(texRef.Field))
                    {
                        TGIBlock tgiblock = rcol.Resources[texRef.Data.TGIBlockIndex];
                        if (ResourceGraph.IsDDS(tgiblock.ResourceType))
                        {
                            absoluteName = rootStr + ".Resources[" + texRef.Data.TGIBlockIndex + "]";
                            results.Add(new DefaultConnection(tgiblock, tgiblock,
                                                              ResourceDataActions.FindWrite, absoluteName));
                        }
                    }
                    else
                    {
                        ElementTextureKey texKey = sdataList[i] as ElementTextureKey;
                        if (texKey != null && IsWantedImage(texKey.Field))
                        {
                            if (ResourceGraph.IsDDS(texKey.Data.ResourceType))
                            {
                                absoluteName = absolutePath + j + "].Data";
                                results.Add(new DefaultConnection(texKey.Data, texKey,
                                                                  ResourceDataActions.FindWrite, absoluteName, rootStr + ".Data"));
                            }
                        }
                    }
                }
            }
            return(results);
        }
Exemple #10
0
    public static Material Material2Unity(string[] textureSearchDirs, string mshObjName, MATD from, Material baseMat, string[] additionalTextureSearchPaths = null)
    {
        Material material = new Material(baseMat);

        //material.EnableKeyword("_METALLICGLOSSMAP");
        //material.EnableKeyword("_SPECGLOSSMAP");
        material.name = from.Name;
        material.SetColor("_Color", Color2Unity(from.Diffuse));

        if (!string.IsNullOrEmpty(from.Texture) && IMPORT_TEXTURES)
        {
            Texture2D albedo = ImportTexture(textureSearchDirs, from.Texture);
            //Texture2D normal = ImportTexture(mshSourceDir, from.Texture);

            if (albedo != null)
            {
                material.SetTexture(DEFAULT_MATERIAL_ALBEDO, albedo);
            }

            //if (normal != null)
            //{
            //    material.EnableKeyword("_NORMALMAP");
            //    material.SetTexture(DEFAULT_MATERIAL_NORMAL, normal);
            //}
        }

        if (CREATE_ASSETS)
        {
            if (!AssetDatabase.IsValidFolder("Assets" + MODELS_FOLDER + "/" + mshObjName + "/Materials"))
            {
                AssetDatabase.CreateFolder("Assets" + MODELS_FOLDER + "/" + mshObjName, "Materials");
            }
            AssetDatabase.CreateAsset(material, "Assets" + MODELS_FOLDER + "/" + mshObjName + "/Materials/" + material.name + ".mat");
        }

        return(material);
    }