/// <summary> /// c-tor - generic way for collecting resources /// </summary> /// <param name="meshInfo"></param> /// assetName - just for debug output public MyMesh(MyMeshPartInfo meshInfo, string assetName) { MyMaterialDescriptor matDesc = meshInfo.m_MaterialDesc; if (matDesc != null) { string texName; matDesc.Textures.TryGetValue("DiffuseTexture", out texName); var material = new MyMeshMaterial(); material.Name = meshInfo.m_MaterialDesc.MaterialName; material.Textures = matDesc.Textures; material.DrawTechnique = meshInfo.Technique; material.GlassCW = meshInfo.m_MaterialDesc.GlassCW; material.GlassCCW = meshInfo.m_MaterialDesc.GlassCCW; material.GlassSmooth = meshInfo.m_MaterialDesc.GlassSmoothNormals; Material = material; } else { //It is OK because ie. collision meshes dont have materials Material = new MyMeshMaterial(); } AssetName = assetName; }
public void SetIndices( Assimp.Mesh sourceMesh, VRageRender.Import.Mesh mesh, int[] indices, List <Vector3> vertices, int matHash) { MyMeshPartInfo myMeshPartInfo; if (!this.m_partContainer.TryGetValue(matHash, out myMeshPartInfo)) { myMeshPartInfo = new MyMeshPartInfo(); myMeshPartInfo.m_MaterialHash = matHash; this.m_partContainer.Add(matHash, myMeshPartInfo); } mesh.StartIndex = myMeshPartInfo.m_indices.Count; mesh.IndexCount = indices.Length; int vertexOffset = mesh.VertexOffset; for (int index = 0; index < sourceMesh.FaceCount * 3; index += 3) { int num1 = indices[index] + vertexOffset; int num2 = indices[index + 1] + vertexOffset; int num3 = indices[index + 2] + vertexOffset; myMeshPartInfo.m_indices.Add(num1); myMeshPartInfo.m_indices.Add(num2); myMeshPartInfo.m_indices.Add(num3); } }
public static string GetAlphamaskTexture(MyMeshPartInfo mwmPart, string contentPath) { if (mwmPart.m_MaterialDesc == null) { return(""); } var relativePath = mwmPart.m_MaterialDesc.Textures.Get("AlphamaskTexture", ""); return(MyResourceUtils.GetTextureFullPath(relativePath, contentPath)); }
/// <summary> /// ReadMeshParts /// </summary> /// <param name="reader"></param> /// <returns></returns> private static List <MyMeshPartInfo> ReadMeshParts(BinaryReader reader) { var list = new List <MyMeshPartInfo>(); var nCount = reader.ReadInt32(); for (var i = 0; i < nCount; ++i) { var meshPart = new MyMeshPartInfo(); meshPart.Import(reader, 0); // TODO: test version detail list.Add(meshPart); } return(list); }
private static List <MyMeshPartInfo> ReadMeshParts( BinaryReader reader, int version) { var myMeshPartInfoList = new List <MyMeshPartInfo>(); var num = reader.ReadInt32(); for (var index = 0; index < num; ++index) { var myMeshPartInfo = new MyMeshPartInfo(); myMeshPartInfo.Import(reader, version); myMeshPartInfoList.Add(myMeshPartInfo); } return(myMeshPartInfoList); }
public void InitForGBuffer(MyLod parent, string name, string contentPath, MyMeshPartInfo mwmPartInfo, MyStandardMaterial standardMaterial, int startIndex, int indicesCount, int startVertex) { MyRenderProxy.Assert(StartIndex == 0 && IndicesCount == 0 && StartVertex == 0, "The part has been initialised before!"); MyRenderProxy.Assert(indicesCount != 0, "Invalid input"); bool isCmTexture = !string.IsNullOrEmpty(MyMwmUtils.GetColorMetalTexture(mwmPartInfo, contentPath)); bool isNgTexture = !string.IsNullOrEmpty(MyMwmUtils.GetNormalGlossTexture(mwmPartInfo, contentPath)); bool isExtTexture = !string.IsNullOrEmpty(MyMwmUtils.GetExtensionTexture(mwmPartInfo, contentPath)); Technique = mwmPartInfo.Technique; m_shaderBundles = new MyShaderBundle[(int)MyInstanceLodState.StatesCount]; m_shaderBundles[(int)MyInstanceLodState.Solid] = MyManagers.ShaderBundles.GetShaderBundle(MyRenderPassType.GBuffer, Technique, MyInstanceLodState.Solid, isCmTexture, isNgTexture, isExtTexture); m_shaderBundles[(int)MyInstanceLodState.Transition] = MyManagers.ShaderBundles.GetShaderBundle(MyRenderPassType.GBuffer, Technique, MyInstanceLodState.Transition, isCmTexture, isNgTexture, isExtTexture); m_shaderBundles[(int)MyInstanceLodState.Hologram] = MyManagers.ShaderBundles.GetShaderBundle(MyRenderPassType.GBuffer, Technique, MyInstanceLodState.Hologram, isCmTexture, isNgTexture, isExtTexture); m_shaderBundles[(int)MyInstanceLodState.Dithered] = MyManagers.ShaderBundles.GetShaderBundle(MyRenderPassType.GBuffer, Technique, MyInstanceLodState.Dithered, isCmTexture, isNgTexture, isExtTexture); Parent = parent; Name = name; StandardMaterial = standardMaterial; GlassMaterial = null; InstanceMaterialOffsetInLod = -1; // <- not used so far StartIndex = startIndex; IndicesCount = indicesCount; StartVertex = startVertex; }
public void SetMaterial(Material material) { int hashCode = material.Name.GetHashCode(); if (!this.m_partContainer.ContainsKey(hashCode)) { return; } MyMeshPartInfo myMeshPartInfo = this.m_partContainer[hashCode]; MyMaterialDescriptor matDesc = new MyMaterialDescriptor(material.Name); this.SetMaterialTextures(matDesc, material); if (myMeshPartInfo.m_MaterialDesc != null) { matDesc.Technique = myMeshPartInfo.m_MaterialDesc.Technique; } myMeshPartInfo.m_MaterialDesc = matDesc; }
/// <summary> /// c-tor - generic way for collecting resources /// </summary> /// <param name="meshInfo"></param> /// assetName - just for debug output public MyMesh(MyMeshPartInfo meshInfo, string assetName) { string textureName = null; MyMaterialDescriptor matDesc = meshInfo.m_MaterialDesc; if (matDesc != null) { bool hasNormalTexture = true; string texName = matDesc.m_DiffuseTextureName; if (String.IsNullOrEmpty(texName) == false) { int i = texName.LastIndexOf(C_CONTENT_ID); texName = texName.Substring(i + C_CONTENT_ID.Length, texName.Length - i - C_CONTENT_ID.Length); //@ cut extension int lastIndex = texName.LastIndexOf("."); textureName = texName.Substring(0, texName.Length - (texName.Length - lastIndex)); if (textureName.LastIndexOf(C_POSTFIX_DONT_HAVE_NORMAL) == (textureName.Length - C_POSTFIX_DONT_HAVE_NORMAL.Length)) { hasNormalTexture = false; textureName = textureName.Substring(0, textureName.Length - C_POSTFIX_DONT_HAVE_NORMAL.Length); } //@ if postfix for diffuse is _d -> trim it if (textureName.LastIndexOf(C_POSTFIX_DIFFUSE) == (textureName.Length - 2)) { textureName = textureName.Substring(0, textureName.Length - 2); } if (textureName.LastIndexOf(C_POSTFIX_DIFFUSE_EMISSIVE) == (textureName.Length - 3)) { textureName = textureName.Substring(0, textureName.Length - 3); } } var defaultMaterial = new MyMeshMaterial(matDesc.MaterialName, textureName + C_POSTFIX_DIFFUSE_EMISSIVE, textureName + C_POSTFIX_NORMAL_SPECULAR, matDesc.m_Glossiness, hasNormalTexture, ref matDesc.m_DiffuseColor, ref matDesc.m_SpecularColor); // check for alternative textures and create corresponding materials. if (!textureName.Contains(DEFAULT_DIRECTORY)) { Materials = new MyMeshMaterial[8]; for (int j = 1; j < Materials.Length; j++) { Materials[j] = defaultMaterial; } } else { int materialCount = FindMaterialCount(textureName); Materials = new MyMeshMaterial[materialCount]; // here check if corresponding textures exist in the "v02" or "v03" ... // folder. If not, fall back to default "v01" folder for (int j = 1; j < Materials.Length; j++) { string newFolder = "\\v" + String.Format("{0:00}", j + 1) + "\\"; string newNameDiffuse = textureName.Replace(DEFAULT_DIRECTORY, newFolder) + C_POSTFIX_DIFFUSE_EMISSIVE; string newNameNormal = textureName.Replace(DEFAULT_DIRECTORY, newFolder) + C_POSTFIX_NORMAL_SPECULAR; string diffusepath = Path.Combine(MyMinerGame.Static.RootDirectory, newNameDiffuse) + ".dds"; if (!File.Exists(diffusepath)) { newNameDiffuse = textureName + C_POSTFIX_DIFFUSE_EMISSIVE; } if (!File.Exists(Path.Combine(MyMinerGame.Static.RootDirectory, newNameNormal) + ".dds")) { newNameNormal = textureName + C_POSTFIX_NORMAL_SPECULAR; } Materials[j] = new MyMeshMaterial(matDesc.MaterialName, newNameDiffuse, newNameNormal, matDesc.m_Glossiness, hasNormalTexture, ref matDesc.m_DiffuseColor, ref matDesc.m_SpecularColor); } } Materials[0] = defaultMaterial; } else { //It is OK because ie. collision meshes dont have materials //MyCommonDebugUtils.AssertRelease(false, String.Format("Model {0} has bad material for mesh.", assetName)); Trace.TraceWarning("Model with null material: " + assetName); //We define at least debug material MinerWarsMath.Vector3 color = MinerWarsMath.Color.Pink.ToVector3(); Materials = new MyMeshMaterial[8]; Materials[0] = new MyMeshMaterial("", "Textures2\\Models\\Prefabs\\v01\\v01_cargo_box_de", "Textures2\\Models\\Prefabs\\v01\\v01_cargo_box_ns", 0, true, ref color, ref color); for (int j = 1; j < Materials.Length; j++) { Materials[j] = Materials[0]; } } for (int i = 0; i < Materials.Length; i++) { Materials[i].DrawTechnique = meshInfo.m_MeshRenderTechnique; } m_assetName = assetName; }
public void SetIndices(Assimp.Mesh sourceMesh, VRageRender.Import.Mesh mesh, int[] indices, List <Vector3> vertices, string materialName) { if (sourceMesh == null) { throw new System.Exception("given source mesh is null!"); } if (mesh == null) { throw new System.Exception("given target mesh is null!"); } if (indices == null) { throw new System.Exception($"Mesh '{sourceMesh.Name}': given indices array is null!"); } if (vertices == null) { throw new System.Exception($"Mesh '{sourceMesh.Name}': given vertices list is null!"); } if (materialName == null) { return; } int matHash = materialName.GetHashCode(); MyMeshPartInfo meshPart; if (!this.m_partContainer.TryGetValue(matHash, out meshPart)) { meshPart = new MyMeshPartInfo(); meshPart.m_MaterialHash = matHash; this.m_partContainer.Add(matHash, meshPart); } if (meshPart == null) { throw new System.Exception($"Mesh '{sourceMesh.Name}': Mesh part info retrieved as null for material='{materialName}' (hashCode={matHash})"); } if (meshPart.m_indices == null) { throw new System.Exception($"Mesh '{sourceMesh.Name}': Mesh part info's indices list is null."); } mesh.StartIndex = meshPart.m_indices.Count; mesh.IndexCount = indices.Length; int vertexOffset = mesh.VertexOffset; for (int index = 0; index < sourceMesh.FaceCount * 3; index += 3) { int num1 = indices[index] + vertexOffset; int num2 = indices[index + 1] + vertexOffset; int num3 = indices[index + 2] + vertexOffset; meshPart.m_indices.Add(num1); meshPart.m_indices.Add(num2); meshPart.m_indices.Add(num3); } }
/// <summary> /// c-tor - generic way for collecting resources /// </summary> /// <param name="meshInfo"></param> /// assetName - just for debug output public MyRenderMesh(MyMeshPartInfo meshInfo, string assetName) { string contentDir = ""; if (Path.IsPathRooted(assetName) && assetName.ToLower().Contains("models")) { contentDir = assetName.Substring(0, assetName.ToLower().IndexOf("models")); } MyMaterialDescriptor matDesc = meshInfo.m_MaterialDesc; if (matDesc != null) { bool hasNormalTexture = true; string normalPath = null; string diffusePath; matDesc.Textures.TryGetValue("DiffuseTexture", out diffusePath); matDesc.Textures.TryGetValue("NormalTexture", out normalPath); if (String.IsNullOrEmpty(normalPath) && !String.IsNullOrEmpty(diffusePath)) { if (String.IsNullOrEmpty(diffusePath)) { diffusePath = null; normalPath = null; } else { string ext = Path.GetExtension(diffusePath); string deMatch = C_POSTFIX_DIFFUSE_EMISSIVE + ext; string meMatch = C_POSTFIX_MASK_EMISSIVE + ext; if (diffusePath.EndsWith(deMatch)) { normalPath = diffusePath.Substring(0, diffusePath.Length - deMatch.Length) + C_POSTFIX_NORMAL_SPECULAR + ext; } else if (diffusePath.EndsWith(meMatch)) { normalPath = diffusePath.Substring(0, diffusePath.Length - meMatch.Length) + C_POSTFIX_NORMAL_SPECULAR + ext; } else { normalPath = null; } } } Material = new MyRenderMeshMaterial(matDesc.MaterialName, contentDir, diffusePath, normalPath, matDesc.SpecularPower, matDesc.SpecularIntensity, hasNormalTexture, matDesc.DiffuseColor, matDesc.ExtraData); Material.DrawTechnique = meshInfo.Technique; m_assetName = assetName; } else { //It is OK because ie. collision meshes dont have materials //MyCommonDebugUtils.AssertRelease(false, String.Format("Model {0} has bad material for mesh.", assetName)); Trace.TraceWarning("Model with null material: " + assetName); //We define at least debug material VRageMath.Vector3 color = VRageMath.Color.Pink.ToVector3(); Material = new MyRenderMeshMaterial("", "", @"Textures\Models\Debug\white_de.dds", @"Textures\Models\Debug\white_ns.dds", 0, 0, true, color, color); } }
/// <summary> /// ReadMeshParts /// </summary> /// <param name="reader"></param> /// <returns></returns> private static List<MyMeshPartInfo> ReadMeshParts(BinaryReader reader) { var list = new List<MyMeshPartInfo>(); var nCount = reader.ReadInt32(); for (var i = 0; i < nCount; ++i) { var meshPart = new MyMeshPartInfo(); meshPart.Import(reader, 0); // TODO: test version detail list.Add(meshPart); } return list; }