/// <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.DiffuseTexture = texName; material.Textures = matDesc.Textures; material.SpecularIntensity = meshInfo.m_MaterialDesc.SpecularIntensity; material.SpecularPower = meshInfo.m_MaterialDesc.SpecularPower; 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; }
/// <summary> /// ReadMeshParts /// </summary> /// <param name="reader"></param> /// <returns></returns> static List <MyMeshPartInfo> ReadMeshParts(BinaryReader reader, int version) { List <MyMeshPartInfo> list = new List <MyMeshPartInfo>(); int nCount = reader.ReadInt32(); for (int i = 0; i < nCount; ++i) { MyMeshPartInfo meshPart = new MyMeshPartInfo(); meshPart.Import(reader, version); list.Add(meshPart); } return(list); }
public void LoadBuffers(MyModelData modelData, string assetName = null) { System.Diagnostics.Debug.Assert(modelData.Sections.Count > 0, "Invalid object"); if (modelData.Sections.Count == 0) return; // create index buffer { m_trianglesCount = modelData.Indices.Count / 3; m_Indices_16bit = new ushort[modelData.Indices.Count]; for (int i = 0; i < modelData.Indices.Count; ++i) m_Indices_16bit[i] = (ushort)modelData.Indices[i]; m_indexBuffer = new IndexBuffer(MyRender.GraphicsDevice, m_Indices_16bit.Length * sizeof(short), Usage.WriteOnly, Pool.Default, true); m_indexBuffer.SetData(m_Indices_16bit); m_indexBuffer.Tag = this; m_indexBufferSize = m_Indices_16bit.Length * sizeof(short); SignResource(m_indexBuffer); } // create vertex buffer { m_verticesCount = modelData.Positions.Count; m_vertices = new MyCompressedVertexNormal[m_verticesCount]; var vertexArray = new MyVertexFormatPositionNormalTextureTangent[m_verticesCount]; for (int i = 0; i < modelData.Positions.Count; ++i) { vertexArray[i].Position = modelData.Positions[i]; vertexArray[i].Normal = modelData.Normals[i]; vertexArray[i].Tangent = modelData.Tangents[i]; vertexArray[i].TexCoord = modelData.TexCoords[i]; m_vertices[i] = new MyCompressedVertexNormal() { Position = vertexArray[i].PositionPacked, Normal = vertexArray[i].NormalPacked }; } m_vertexDeclaration = MyVertexFormatPositionNormalTextureTangent.VertexDeclaration; m_vertexStride = MyVertexFormatPositionNormalTextureTangent.Stride; m_vertexBufferSize = vertexArray.Length * m_vertexStride; m_vertexBuffer = new VertexBuffer(MyRender.GraphicsDevice, m_vertexBufferSize, Usage.WriteOnly, VertexFormat.None, Pool.Default); m_vertexBuffer.SetData(vertexArray); m_vertexBuffer.Tag = this; SignResource(m_vertexBuffer); } m_meshContainer.Clear(); // apply materials here for (int s = 0; s < modelData.Sections.Count; ++s) { var mpi = new MyMeshPartInfo(); mpi.Technique = m_drawTechnique; // Disabled, because it assert always when models are loaded before materials //System.Diagnostics.Debug.Assert(MyRenderModels.Materials.ContainsKey(modelData.Sections[s].MaterialName), "Mesh material not present!"); if (MyRenderModels.Materials.ContainsKey(modelData.Sections[s].MaterialName)) mpi.m_MaterialDesc = MyRenderModels.Materials[modelData.Sections[s].MaterialName]; var start = modelData.Sections[s].IndexStart; var end = start + modelData.Sections[s].TriCount*3; for (int i = start; i < end; ++i) mpi.m_indices.Add(modelData.Indices[i]); m_meshContainer.Add(new MyRenderMesh(mpi, null) { IndexStart = modelData.Sections[s].IndexStart, TriCount = modelData.Sections[s].TriCount }); } // store properties of this model { BoundingBox = modelData.AABB; BoundingSphere = new BoundingSphere(modelData.AABB.Center, modelData.AABB.HalfExtents.Length()); BoundingBoxSize = BoundingBox.Size; BoundingBoxSizeHalf = BoundingBox.HalfExtents; ModelInfo = new MyModelInfo(m_trianglesCount, m_verticesCount, BoundingBoxSize); PreloadTextures(LoadingMode.Immediate); LoadState = Textures.LoadState.Loaded; m_loadedContent = true; m_loadedData = true; } }
/// <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; }