/// <summary> /// ExportData /// </summary> /// <param name="tagName"></param> /// <param name="strArr"></param> /// <returns></returns> public bool ExportData(string tagName, MyModelInfo modelInfo) { WriteTag(tagName); m_writer.Write(modelInfo.TrianglesCount); m_writer.Write(modelInfo.VerticesCount); WriteVector(modelInfo.BoundingBoxSize); return(true); }
// Load only snappoints. public void LoadOnlyModelInfo() { if (!m_loadedData) { lock (this) { MyLog.Default.WriteLine("MyModel.LoadModelData -> START", LoggingOptions.LOADING_MODELS); using (var indent = MyLog.Default.IndentUsing(LoggingOptions.LOADING_MODELS)) { MyLog.Default.WriteLine("m_assetName: " + m_assetName, LoggingOptions.LOADING_MODELS); // Read data from model TAG parameter. There are stored vertex positions, triangle indices, vectors, ... everything we need. MyModelImporter exporter = new MyModelImporter(); MyLog.Default.WriteLine(String.Format("Importing asset {0}, path: {1}", m_assetName, AssetName), LoggingOptions.LOADING_MODELS); try { // Read only TAG_DUMMIES data exporter.ImportData(AssetName, new string[] { MyImporterConstants.TAG_MODEL_INFO }); } catch (Exception e) { MyLog.Default.WriteLine(String.Format("Importing asset failed {0}, message: {1}, stack:{2}", m_assetName, e.Message, e.StackTrace)); } Dictionary<string, object> tagData = exporter.GetTagData(); if (tagData.Count > 0) { ModelInfo = tagData[MyImporterConstants.TAG_MODEL_INFO] as MyModelInfo; } else { ModelInfo = new MyModelInfo(0, 0, Vector3.Zero); } } } } }
public void LoadAnimationData() { if (m_loadedData) return; lock (this) { VRageRender.MyRenderProxy.GetRenderProfiler().StartProfilingBlock("MyModel::LoadAnimationData"); MyLog.Default.WriteLine("MyModel.LoadData -> START", LoggingOptions.LOADING_MODELS); MyLog.Default.IncreaseIndent(LoggingOptions.LOADING_MODELS); MyLog.Default.WriteLine("m_assetName: " + m_assetName, LoggingOptions.LOADING_MODELS); // Read data from model TAG parameter. There are stored vertex positions, triangle indices, vectors, ... everything we need. VRageRender.MyRenderProxy.GetRenderProfiler().StartProfilingBlock("Model - load data - import data"); MyLog.Default.WriteLine(String.Format("Importing asset {0}, path: {1}", m_assetName, AssetName), LoggingOptions.LOADING_MODELS); try { m_importer.ImportData(AssetName); } catch { MyLog.Default.WriteLine(String.Format("Importing asset failed {0}", m_assetName)); throw; } VRageRender.MyRenderProxy.GetRenderProfiler().EndProfilingBlock(); VRageRender.MyRenderProxy.GetRenderProfiler().StartProfilingBlock("Model - load data - load tag data"); Dictionary<string, object> tagData = m_importer.GetTagData(); if (tagData.Count == 0) { throw new Exception(String.Format("Uncompleted tagData for asset: {0}, path: {1}", m_assetName, AssetName)); } DataVersion = m_importer.DataVersion; Animations = (ModelAnimations)tagData[MyImporterConstants.TAG_ANIMATIONS]; Bones = (MyModelBone[])tagData[MyImporterConstants.TAG_BONES]; BoundingBox = (BoundingBox)tagData[MyImporterConstants.TAG_BOUNDING_BOX]; BoundingSphere = (BoundingSphere)tagData[MyImporterConstants.TAG_BOUNDING_SPHERE]; BoundingBoxSize = BoundingBox.Max - BoundingBox.Min; BoundingBoxSizeHalf = BoundingBoxSize / 2.0f; Dummies = tagData[MyImporterConstants.TAG_DUMMIES] as Dictionary<string, MyModelDummy>; BoneMapping = tagData[MyImporterConstants.TAG_BONE_MAPPING] as VRageMath.Vector3I[]; if (BoneMapping.Length == 0) BoneMapping = null; VRageRender.MyRenderProxy.GetRenderProfiler().EndProfilingBlock(); ModelInfo = new MyModelInfo(GetTrianglesCount(), GetVerticesCount(), BoundingBoxSize); m_loadedData = true; MyLog.Default.DecreaseIndent(LoggingOptions.LOADING_MODELS); MyLog.Default.WriteLine("MyModel.LoadAnimationData -> END", LoggingOptions.LOADING_MODELS); VRageRender.MyRenderProxy.GetRenderProfiler().EndProfilingBlock(); } }
// Sort of lazy-load, where constructor just saves information about what this model should be, but real load is done here - and only one time. // This loads only vertex data, doesn't touch GPU // Can be called from main and background thread public void LoadData() { if (m_loadedData) return; lock (this) { VRageRender.MyRenderProxy.GetRenderProfiler().StartProfilingBlock("MyModel::LoadData"); MyLog.Default.WriteLine("MyModel.LoadData -> START", LoggingOptions.LOADING_MODELS); MyLog.Default.IncreaseIndent(LoggingOptions.LOADING_MODELS); MyLog.Default.WriteLine("m_assetName: " + m_assetName, LoggingOptions.LOADING_MODELS); // Read data from model TAG parameter. There are stored vertex positions, triangle indices, vectors, ... everything we need. VRageRender.MyRenderProxy.GetRenderProfiler().StartProfilingBlock("Model - load data - import data"); MyLog.Default.WriteLine(String.Format("Importing asset {0}, path: {1}", m_assetName, AssetName), LoggingOptions.LOADING_MODELS); string assetForImport = AssetName; var fsPath = Path.IsPathRooted(AssetName) ? AssetName : Path.Combine(MyFileSystem.ContentPath, AssetName); if (!MyFileSystem.FileExists(fsPath)) { assetForImport = @"Models\Debug\Error.mwm"; } try { m_importer.ImportData(assetForImport); } catch { MyLog.Default.WriteLine(String.Format("Importing asset failed {0}", m_assetName)); VRageRender.MyRenderProxy.GetRenderProfiler().EndProfilingBlock(); VRageRender.MyRenderProxy.GetRenderProfiler().EndProfilingBlock(); throw; } VRageRender.MyRenderProxy.GetRenderProfiler().EndProfilingBlock(); DataVersion = m_importer.DataVersion; VRageRender.MyRenderProxy.GetRenderProfiler().StartProfilingBlock("Model - load data - load tag data"); Dictionary<string, object> tagData = m_importer.GetTagData(); if (tagData.Count == 0) { VRageRender.MyRenderProxy.GetRenderProfiler().EndProfilingBlock(); VRageRender.MyRenderProxy.GetRenderProfiler().EndProfilingBlock(); throw new Exception(String.Format("Uncompleted tagData for asset: {0}, path: {1}", m_assetName, AssetName)); } VRageRender.MyRenderProxy.GetRenderProfiler().EndProfilingBlock(); VRageRender.MyRenderProxy.GetRenderProfiler().StartProfilingBlock("Model - load data - vertex, normals, texture coords"); HalfVector4[] vertices = (HalfVector4[])tagData[MyImporterConstants.TAG_VERTICES]; System.Diagnostics.Debug.Assert(vertices.Length > 0); Byte4[] normals = (Byte4[])tagData[MyImporterConstants.TAG_NORMALS]; m_vertices = new MyCompressedVertexNormal[vertices.Length]; if (normals.Length > 0) { for (int v = 0; v < vertices.Length; v++) { m_vertices[v] = new MyCompressedVertexNormal() { Position = vertices[v],// VF_Packer.PackPosition(ref vertices[v]), Normal = normals[v]//VF_Packer.PackNormalB4(ref normals[v]) }; } } else { for (int v = 0; v < vertices.Length; v++) { m_vertices[v] = new MyCompressedVertexNormal() { Position = vertices[v],// VF_Packer.PackPosition(ref vertices[v]), }; } } m_verticesCount = vertices.Length; VRageRender.MyRenderProxy.GetRenderProfiler().EndProfilingBlock(); VRageRender.MyRenderProxy.GetRenderProfiler().StartProfilingBlock("Model - load data - mesh"); m_meshContainer.Clear(); if (tagData.ContainsKey(MyImporterConstants.TAG_MESH_PARTS)) { List<int> indices = new List<int>(GetVerticesCount()); // Default capacity estimation int maxIndex = 0; List<MyMeshPartInfo> meshParts = tagData[MyImporterConstants.TAG_MESH_PARTS] as List<MyMeshPartInfo>; foreach (MyMeshPartInfo meshPart in meshParts) { MyMesh mesh = new MyMesh(meshPart, m_assetName); mesh.IndexStart = indices.Count; mesh.TriCount = meshPart.m_indices.Count / 3; if (m_loadUV && false == m_hasUV) { m_texCoords = (HalfVector2[])tagData[MyImporterConstants.TAG_TEXCOORDS0]; m_hasUV = true; m_loadUV = false; } if (meshPart.m_MaterialDesc != null && meshPart.Technique == MyMeshDrawTechnique.GLASS) { GlassData = mesh; HalfVector2[] forLoadingTexCoords0 = (HalfVector2[])tagData[MyImporterConstants.TAG_TEXCOORDS0]; List<HalfVector2> neededTexCoords = new List<HalfVector2>(); for (int t = 0; t < meshPart.m_indices.Count; t++) { int index = meshPart.m_indices[t]; neededTexCoords.Add(forLoadingTexCoords0[index]); } GlassTexCoords = neededTexCoords.ToArray(); } System.Diagnostics.Debug.Assert(mesh.TriCount > 0); if (mesh.TriCount == 0) { VRageRender.MyRenderProxy.GetRenderProfiler().EndProfilingBlock(); VRageRender.MyRenderProxy.GetRenderProfiler().EndProfilingBlock(); return; } foreach (var i in meshPart.m_indices) { indices.Add(i); if (i > maxIndex) { maxIndex = i; } } m_meshContainer.Add(mesh); } if (maxIndex <= ushort.MaxValue) { // create 16 bit indices m_Indices_16bit = new ushort[indices.Count]; for (int i = 0; i < indices.Count; i++) { m_Indices_16bit[i] = (ushort)indices[i]; } } else { // use 32bit indices m_Indices = indices.ToArray(); } } if (tagData.ContainsKey(MyImporterConstants.TAG_MODEL_BVH)) { m_bvh = new MyQuantizedBvhAdapter(tagData[MyImporterConstants.TAG_MODEL_BVH] as GImpactQuantizedBvh, this); } VRageRender.MyRenderProxy.GetRenderProfiler().EndProfilingBlock(); VRageRender.MyRenderProxy.GetRenderProfiler().StartProfilingBlock("Model - load data - other data"); Animations = (ModelAnimations)tagData[MyImporterConstants.TAG_ANIMATIONS]; Bones = (MyModelBone[])tagData[MyImporterConstants.TAG_BONES]; BoundingBox = (BoundingBox)tagData[MyImporterConstants.TAG_BOUNDING_BOX]; BoundingSphere = (BoundingSphere)tagData[MyImporterConstants.TAG_BOUNDING_SPHERE]; BoundingBoxSize = BoundingBox.Max - BoundingBox.Min; BoundingBoxSizeHalf = BoundingBoxSize / 2.0f; Dummies = tagData[MyImporterConstants.TAG_DUMMIES] as Dictionary<string, MyModelDummy>; BoneMapping = tagData[MyImporterConstants.TAG_BONE_MAPPING] as VRageMath.Vector3I[]; if (tagData.ContainsKey(MyImporterConstants.TAG_MODEL_FRACTURES)) ModelFractures = (MyModelFractures)tagData[MyImporterConstants.TAG_MODEL_FRACTURES]; object patternScale; if (tagData.TryGetValue(MyImporterConstants.TAG_PATTERN_SCALE, out patternScale)) { PatternScale = (float)patternScale; } if (BoneMapping.Length == 0) BoneMapping = null; if (tagData.ContainsKey(MyImporterConstants.TAG_HAVOK_COLLISION_GEOMETRY)) { HavokData = (byte[])tagData[MyImporterConstants.TAG_HAVOK_COLLISION_GEOMETRY]; byte[] tagCollisionData = (byte[])tagData[MyImporterConstants.TAG_HAVOK_COLLISION_GEOMETRY]; if (tagCollisionData.Length > 0 && HkBaseSystem.IsThreadInitialized) { bool containsSceneData; bool containsDestructionData; List<HkShape> shapesList = new List<HkShape>(); if (!HkShapeLoader.LoadShapesListFromBuffer(tagCollisionData, shapesList, out containsSceneData, out containsDestructionData)) { MyLog.Default.WriteLine(string.Format("Model {0} - Unable to load collision geometry", AssetName), LoggingOptions.LOADING_MODELS); //Debug.Fail("Collision model was exported in wrong way: " + m_assetName); } if (shapesList.Count > 10) MyLog.Default.WriteLine(string.Format("Model {0} - Found too many collision shapes, only the first 10 will be used", AssetName), LoggingOptions.LOADING_MODELS); if (HavokCollisionShapes != null) { Debug.Fail("Shapes already loaded"); } if (shapesList.Count > 0) { HavokCollisionShapes = shapesList.ToArray(); } else { MyLog.Default.WriteLine(string.Format("Model {0} - Unable to load collision geometry from file, default collision will be used !", AssetName)); } if (containsDestructionData) HavokDestructionData = tagCollisionData; ExportedWrong = !containsSceneData; } } if (tagData.ContainsKey(MyImporterConstants.TAG_HAVOK_DESTRUCTION)) { if (((byte[])tagData[MyImporterConstants.TAG_HAVOK_DESTRUCTION]).Length > 0) HavokDestructionData = (byte[])tagData[MyImporterConstants.TAG_HAVOK_DESTRUCTION]; } VRageRender.MyRenderProxy.GetRenderProfiler().EndProfilingBlock(); VRageRender.MyRenderProxy.GetRenderProfiler().StartProfilingBlock("Model - load data - copy triangle indices"); // Prepare data CopyTriangleIndices(); m_trianglesCount = Triangles.Count(); // Remember this numbers as list may be cleared at the end of this method VRageRender.MyRenderProxy.GetRenderProfiler().EndProfilingBlock(); MyLog.Default.WriteLine("Triangles.Length: " + Triangles.Length, LoggingOptions.LOADING_MODELS); MyLog.Default.WriteLine("Vertexes.Length: " + GetVerticesCount(), LoggingOptions.LOADING_MODELS); MyLog.Default.WriteLine("Centered: " + (bool)tagData[MyImporterConstants.TAG_CENTERED], LoggingOptions.LOADING_MODELS); MyLog.Default.WriteLine("UseChannelTextures: " + (bool)tagData[MyImporterConstants.TAG_USE_CHANNEL_TEXTURES], LoggingOptions.LOADING_MODELS); MyLog.Default.WriteLine("Length in meters: " + (float)tagData[MyImporterConstants.TAG_LENGTH_IN_METERS], LoggingOptions.LOADING_MODELS); MyLog.Default.WriteLine("Rescale to length in meters?: " + (bool)tagData[MyImporterConstants.TAG_RESCALE_TO_LENGTH_IN_METERS], LoggingOptions.LOADING_MODELS); MyLog.Default.WriteLine("BoundingBox: " + BoundingBox, LoggingOptions.LOADING_MODELS); MyLog.Default.WriteLine("BoundingSphere: " + BoundingSphere, LoggingOptions.LOADING_MODELS); Stats.PerAppLifetime.MyModelsCount++; Stats.PerAppLifetime.MyModelsMeshesCount += m_meshContainer.Count; Stats.PerAppLifetime.MyModelsVertexesCount += GetVerticesCount(); Stats.PerAppLifetime.MyModelsTrianglesCount += Triangles.Length; ModelInfo = new MyModelInfo(GetTrianglesCount(), GetVerticesCount(), BoundingBoxSize); m_loadedData = true; m_loadingErrorProcessed = false; MyLog.Default.DecreaseIndent(LoggingOptions.LOADING_MODELS); MyLog.Default.WriteLine("MyModel.LoadData -> END", LoggingOptions.LOADING_MODELS); VRageRender.MyRenderProxy.GetRenderProfiler().EndProfilingBlock(); } }
/// <summary> /// ExportData /// </summary> /// <param name="tagName"></param> /// <param name="strArr"></param> /// <returns></returns> public bool ExportData(string tagName, MyModelInfo modelInfo) { WriteTag(tagName); m_writer.Write(modelInfo.TrianglesCount); m_writer.Write(modelInfo.VerticesCount); WriteVector(modelInfo.BoundingBoxSize); return true; }
// Sort of lazy-load, where constructor just saves information about what this model should be, but real load is done here - and only one time. // This loads only vertex data, doesn't touch GPU // Can be called from main and background thread public void LoadData() { if (m_loadedData) return; if (m_loadedContent) return; lock (this) { if (m_loadedData) return; if (m_loadedContent) return; MyRender.GetRenderProfiler().StartProfilingBlock("MyModel::LoadData"); MyRender.Log.WriteLine("MyModel.LoadData -> START", LoggingOptions.LOADING_MODELS); MyRender.Log.IncreaseIndent(LoggingOptions.LOADING_MODELS); MyRender.Log.WriteLine("m_assetName: " + m_assetName, LoggingOptions.LOADING_MODELS); // Read data from model TAG parameter. There are stored vertex positions, triangle indices, vectors, ... everything we need. MyRender.GetRenderProfiler().StartProfilingBlock("Model - load data - import data"); string assetForImport = AssetName; var fsPath = Path.IsPathRooted(AssetName) ? AssetName : Path.Combine(MyFileSystem.ContentPath, AssetName); if (!MyFileSystem.FileExists(fsPath)) { MyRender.Log.WriteLine("ERROR: Asset " + AssetName + "not exists!"); assetForImport = @"Models\Debug\Error.mwm"; } MyRender.Log.WriteLine(String.Format("Importing asset {0}, path: {1}", assetForImport, AssetName), LoggingOptions.LOADING_MODELS); try { m_importer.ImportData(assetForImport); } catch { MyRender.Log.WriteLine(String.Format("Importing asset failed {0}", m_assetName)); throw; } MyRender.GetRenderProfiler().EndProfilingBlock(); MyRender.GetRenderProfiler().StartProfilingBlock("Model - load data - load tag data"); Dictionary<string, object> tagData = m_importer.GetTagData(); if (tagData.Count == 0) { throw new Exception(String.Format("Uncompleted tagData for asset: {0}, path: {1}", m_assetName, AssetName)); } MyRender.GetRenderProfiler().EndProfilingBlock(); MyRender.GetRenderProfiler().StartProfilingBlock("Model - load data - vertex, normals, texture coords"); object patternScale; if (tagData.TryGetValue(MyImporterConstants.TAG_PATTERN_SCALE, out patternScale)) { PatternScale = (float)patternScale; } HalfVector4[] vertices = (HalfVector4[])tagData[MyImporterConstants.TAG_VERTICES]; //Dont assert, it can be animation //System.Diagnostics.Debug.Assert(vertices.Length > 0); Byte4[] normals = (Byte4[])tagData[MyImporterConstants.TAG_NORMALS]; m_vertices = new MyCompressedVertexNormal[vertices.Length]; if (normals.Length > 0) { for (int v = 0; v < vertices.Length; v++) { m_vertices[v] = new MyCompressedVertexNormal() { Position = vertices[v],// VF_Packer.PackPosition(ref vertices[v]), Normal = normals[v]//VF_Packer.PackNormalB4(ref normals[v]) }; } } else { for (int v = 0; v < vertices.Length; v++) { m_vertices[v] = new MyCompressedVertexNormal() { Position = vertices[v],// VF_Packer.PackPosition(ref vertices[v]), }; } } m_verticesCount = vertices.Length; HalfVector2[] forLoadingTexCoords0 = (HalfVector2[])tagData[MyImporterConstants.TAG_TEXCOORDS0]; m_forLoadingTexCoords0 = new HalfVector2[forLoadingTexCoords0.Length]; for (int t = 0; t < forLoadingTexCoords0.Length; t++) { m_forLoadingTexCoords0[t] = forLoadingTexCoords0[t];// new HalfVector2(forLoadingTexCoords0[t]); m_forLoadingTexCoords0[t] = new HalfVector2(m_forLoadingTexCoords0[t].ToVector2() / PatternScale); } MyRender.GetRenderProfiler().EndProfilingBlock(); MyRender.GetRenderProfiler().StartProfilingBlock("Model - load data - mesh"); m_meshContainer.Clear(); if (tagData.ContainsKey(MyImporterConstants.TAG_MESH_PARTS)) { List<int> indices = new List<int>(GetVerticesCount()); // Default capacity estimation int maxIndex = 0; List<MyMeshPartInfo> meshParts = tagData[MyImporterConstants.TAG_MESH_PARTS] as List<MyMeshPartInfo>; foreach (MyMeshPartInfo meshPart in meshParts) { if (meshPart.m_MaterialDesc != null) MyRenderModels.Materials[meshPart.m_MaterialDesc.MaterialName] = meshPart.m_MaterialDesc; MyRenderMesh mesh = new MyRenderMesh(meshPart, m_assetName); mesh.IndexStart = indices.Count; mesh.TriCount = meshPart.m_indices.Count / 3; System.Diagnostics.Debug.Assert(mesh.TriCount > 0); foreach (var i in meshPart.m_indices) { indices.Add(i); if (i > maxIndex) { maxIndex = i; } } m_meshContainer.Add(mesh); if (meshPart.m_MaterialDesc != null && meshPart.Technique == MyMeshDrawTechnique.GLASS) { float minimumGlassShadow = 0.0f; if (string.IsNullOrEmpty(meshPart.m_MaterialDesc.GlassCW)) continue; if (string.IsNullOrEmpty(meshPart.m_MaterialDesc.GlassCCW)) continue; var materialCW = MyTransparentMaterials.GetMaterial(meshPart.m_MaterialDesc.GlassCW); var materialCCW = MyTransparentMaterials.GetMaterial(meshPart.m_MaterialDesc.GlassCCW); mesh.GlassDithering = System.Math.Max(materialCW.Color.W, minimumGlassShadow); MyRenderMesh glassMesh = new MyRenderMesh(meshPart, m_assetName); glassMesh.GlassDithering = System.Math.Max(materialCCW.Color.W, minimumGlassShadow); glassMesh.IndexStart = indices.Count; glassMesh.TriCount = meshPart.m_indices.Count / 3; System.Diagnostics.Debug.Assert(glassMesh.TriCount > 0); for (int i = 0; i < meshPart.m_indices.Count; i += 3) { indices.Add(meshPart.m_indices[i + 0]); indices.Add(meshPart.m_indices[i + 2]); indices.Add(meshPart.m_indices[i + 1]); } m_meshContainer.Add(glassMesh); } } if (maxIndex <= ushort.MaxValue) { // create 16 bit indices m_Indices_16bit = new ushort[indices.Count]; for (int i = 0; i < indices.Count; i++) { m_Indices_16bit[i] = (ushort)indices[i]; } } else { // use 32bit indices m_Indices = indices.ToArray(); } m_trianglesCount = indices.Count / 3; } MyRender.GetRenderProfiler().EndProfilingBlock(); MyRender.GetRenderProfiler().StartProfilingBlock("Model - load data - other data"); if (MyRenderConstants.RenderQualityProfile.UseNormals && m_forLoadingTexCoords0.Length > 0) { var verticesNum = vertices.Length; Byte4[] forLoadingTangents = (Byte4[])tagData[MyImporterConstants.TAG_TANGENTS]; Byte4[] forLoadingBitangents = (Byte4[])tagData[MyImporterConstants.TAG_BINORMALS]; m_forLoadingTangents = new Byte4[forLoadingTangents.Length]; for (int v = 0; v < forLoadingTangents.Length; v++) { var N = VF_Packer.UnpackNormal(m_vertices[v].Normal.PackedValue); var T = VF_Packer.UnpackNormal(forLoadingTangents[v].PackedValue); var B = VF_Packer.UnpackNormal(forLoadingBitangents[v].PackedValue); var tangentSign = new Vector4(T.X, T.Y, T.Z, 0); tangentSign.W = T.Cross(N).Dot(B) < 0 ? -1 : 1; m_forLoadingTangents[v] = VF_Packer.PackTangentSignB4(ref tangentSign); } } m_specularShininess = (float)tagData[MyImporterConstants.TAG_SPECULAR_SHININESS]; m_specularPower = (float)tagData[MyImporterConstants.TAG_SPECULAR_POWER]; m_rescaleFactor = (float)tagData[MyImporterConstants.TAG_RESCALE_FACTOR]; BoneIndices = (Vector4I[])tagData[MyImporterConstants.TAG_BLENDINDICES]; BoneWeights = (Vector4[])tagData[MyImporterConstants.TAG_BLENDWEIGHTS]; Animations = (ModelAnimations)tagData[MyImporterConstants.TAG_ANIMATIONS]; Bones = (MyModelBone[])tagData[MyImporterConstants.TAG_BONES]; if(BoneIndices.Length > 0 && Bones.Length > MyRenderConstants.MAX_SHADER_BONES) { List<MyMeshPartInfo> meshParts = tagData[MyImporterConstants.TAG_MESH_PARTS] as List<MyMeshPartInfo>; Dictionary<int, int> vertexChanged = new Dictionary<int,int>(); for(int p=0; p<meshParts.Count; p++) { var meshPart = meshParts[p]; Dictionary<int, int> bonesUsed = new Dictionary<int, int>(); int trianglesNum = meshPart.m_indices.Count / 3; for (int i = 0; i < trianglesNum; i++) { for (int j = 0; j < 3; j++) { int index = meshPart.m_indices[i * 3 + j]; if(BoneWeights[index].X > 0) bonesUsed[BoneIndices[index].X] = 1; if (BoneWeights[index].Y > 0) bonesUsed[BoneIndices[index].Y] = 1; if (BoneWeights[index].Z > 0) bonesUsed[BoneIndices[index].Z] = 1; if (BoneWeights[index].W > 0) bonesUsed[BoneIndices[index].W] = 1; } } var partBones = new List<int>(bonesUsed.Keys); partBones.Sort(); if (partBones.Count > 0 && partBones[partBones.Count - 1] >= MyRenderConstants.MAX_SHADER_BONES) { for(int i=0; i<partBones.Count; i++) { bonesUsed[partBones[i]] = i; } Dictionary<int, int> vertexTouched = new Dictionary<int, int>(); for (int i = 0; i < trianglesNum; i++) { for (int j = 0; j < 3; j++) { int index = meshPart.m_indices[i * 3 + j]; if(!vertexTouched.ContainsKey(index)) { if (BoneWeights[index].X > 0) BoneIndices[index].X = bonesUsed[BoneIndices[index].X]; if (BoneWeights[index].Y > 0) BoneIndices[index].Y = bonesUsed[BoneIndices[index].Y]; if (BoneWeights[index].Z > 0) BoneIndices[index].Z = bonesUsed[BoneIndices[index].Z]; if (BoneWeights[index].W > 0) BoneIndices[index].W = bonesUsed[BoneIndices[index].W]; vertexTouched[index] = 1; int changes = 0; vertexChanged.TryGetValue(index, out changes); vertexChanged[index] = changes + 1; } } } m_meshContainer[p].BonesUsed = partBones.ToArray(); } } if (vertexChanged.Values.Count > 0) Debug.Assert(vertexChanged.Values.Max() < 2, "Vertex shared between model parts, will likely result in wrong skinning"); } BoundingBox = (BoundingBox)tagData[MyImporterConstants.TAG_BOUNDING_BOX]; BoundingSphere = (BoundingSphere)tagData[MyImporterConstants.TAG_BOUNDING_SPHERE]; BoundingBoxSize = BoundingBox.Max - BoundingBox.Min; BoundingBoxSizeHalf = BoundingBoxSize / 2.0f; Dictionary<string, MyModelDummy> Dummies = tagData[MyImporterConstants.TAG_DUMMIES] as Dictionary<string, MyModelDummy>; MyRender.GetRenderProfiler().EndProfilingBlock(); if (tagData.ContainsKey(MyImporterConstants.TAG_LODS)) { var tagLODs = tagData[MyImporterConstants.TAG_LODS]; LODs.Clear(); LODs.AddArray((MyLODDescriptor[])tagLODs); foreach (var lodDesc in LODs) { if (!string.IsNullOrEmpty(lodDesc.RenderQuality)) { lodDesc.RenderQualityList = new List<int>(); string[] qualityStrings = lodDesc.RenderQuality.ToUpper().Split(new char[] {','}, StringSplitOptions.RemoveEmptyEntries); foreach (string qs in qualityStrings) { string qs2 = qs.Trim(); if (qs2 == "LOW") lodDesc.RenderQualityList.Add((int)MyRenderQualityEnum.LOW); else if (qs2 == "NORMAL") lodDesc.RenderQualityList.Add((int)MyRenderQualityEnum.NORMAL); else if (qs2 == "HIGH") lodDesc.RenderQualityList.Add((int)MyRenderQualityEnum.HIGH); else if (qs2 == "EXTREME") lodDesc.RenderQualityList.Add((int)MyRenderQualityEnum.EXTREME); } } } } MyRender.Log.WriteLine("Vertexes.Length: " + GetVerticesCount(), LoggingOptions.LOADING_MODELS); MyRender.Log.WriteLine("Centered: " + (bool)tagData[MyImporterConstants.TAG_CENTERED], LoggingOptions.LOADING_MODELS); MyRender.Log.WriteLine("UseChannelTextures: " + (bool)tagData[MyImporterConstants.TAG_USE_CHANNEL_TEXTURES], LoggingOptions.LOADING_MODELS); MyRender.Log.WriteLine("Length in meters: " + (float)tagData[MyImporterConstants.TAG_LENGTH_IN_METERS], LoggingOptions.LOADING_MODELS); MyRender.Log.WriteLine("Rescale to length in meters?: " + (bool)tagData[MyImporterConstants.TAG_RESCALE_TO_LENGTH_IN_METERS], LoggingOptions.LOADING_MODELS); MyRender.Log.WriteLine("SpecularShininess: " + m_specularShininess, LoggingOptions.LOADING_MODELS); MyRender.Log.WriteLine("SpecularPower: " + m_specularPower, LoggingOptions.LOADING_MODELS); MyRender.Log.WriteLine("RescaleFactor: " + m_rescaleFactor, LoggingOptions.LOADING_MODELS); MyRender.Log.WriteLine("BoundingBox: " + BoundingBox, LoggingOptions.LOADING_MODELS); MyRender.Log.WriteLine("BoundingSphere: " + BoundingSphere, LoggingOptions.LOADING_MODELS); MyPerformanceCounter.PerAppLifetime.MyModelsCount++; MyPerformanceCounter.PerAppLifetime.MyModelsMeshesCount += m_meshContainer.Count; MyPerformanceCounter.PerAppLifetime.MyModelsVertexesCount += GetVerticesCount(); MyPerformanceCounter.PerAppLifetime.MyModelsTrianglesCount += m_trianglesCount; ModelInfo = new MyModelInfo(GetTrianglesCount(), GetVerticesCount(), BoundingBoxSize); m_loadedData = true; m_importer.Clear(); MyRender.Log.DecreaseIndent(LoggingOptions.LOADING_MODELS); MyRender.Log.WriteLine("MyModel.LoadData -> END", LoggingOptions.LOADING_MODELS); MyRender.GetRenderProfiler().EndProfilingBlock(); } }
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; } }
private static bool ExportData(this BinaryWriter writer, string tagName, MyModelInfo modelInfo) { WriteTag(writer, tagName); writer.Write(modelInfo.TrianglesCount); writer.Write(modelInfo.VerticesCount); WriteVector3(writer, ref modelInfo.BoundingBoxSize); return true; }
public void LoadAnimationData() { if (m_loadedData) return; VRageRender.MyRenderProxy.GetRenderProfiler().StartProfilingBlock("MyModel::LoadAnimationData"); MyLog.Default.WriteLine("MyModel.LoadData -> START", LoggingOptions.LOADING_MODELS); MyLog.Default.IncreaseIndent(LoggingOptions.LOADING_MODELS); MyLog.Default.WriteLine("m_assetName: " + m_assetName, LoggingOptions.LOADING_MODELS); // Read data from model TAG parameter. There are stored vertex positions, triangle indices, vectors, ... everything we need. VRageRender.MyRenderProxy.GetRenderProfiler().StartProfilingBlock("Model - load data - import data"); MyLog.Default.WriteLine(String.Format("Importing asset {0}, path: {1}", m_assetName, AssetName), LoggingOptions.LOADING_MODELS); try { m_importer.ImportData(AssetName); } catch { MyLog.Default.WriteLine(String.Format("Importing asset failed {0}", m_assetName)); throw; } VRageRender.MyRenderProxy.GetRenderProfiler().EndProfilingBlock(); VRageRender.MyRenderProxy.GetRenderProfiler().StartProfilingBlock("Model - load data - load tag data"); Dictionary<string, object> tagData = m_importer.GetTagData(); if (tagData.Count == 0) { throw new Exception(String.Format("Uncompleted tagData for asset: {0}, path: {1}", m_assetName, AssetName)); } DataVersion = m_importer.DataVersion; Animations = (ModelAnimations)tagData[MyImporterConstants.TAG_ANIMATIONS]; Bones = (MyModelBone[])tagData[MyImporterConstants.TAG_BONES]; BoundingBox = (BoundingBox)tagData[MyImporterConstants.TAG_BOUNDING_BOX]; BoundingSphere = (BoundingSphere)tagData[MyImporterConstants.TAG_BOUNDING_SPHERE]; BoundingBoxSize = BoundingBox.Max - BoundingBox.Min; BoundingBoxSizeHalf = BoundingBoxSize / 2.0f; Dummies = tagData[MyImporterConstants.TAG_DUMMIES] as Dictionary<string, MyModelDummy>; BoneMapping = tagData[MyImporterConstants.TAG_BONE_MAPPING] as VRageMath.Vector3I[]; if (BoneMapping.Length == 0) BoneMapping = null; if (DataVersion < 01050001) { //old version of animations. Scaled and rotated if (Animations.Clips.Count > 0) { var rotationMatrix = Matrix.CreateRotationY(MathHelper.Pi); Matrix invert = Matrix.Invert(rotationMatrix); foreach (var bone in Animations.Clips[0].Bones) { foreach (var kf in bone.Keyframes) { Matrix keyMatrix = Matrix.CreateFromQuaternion(kf.Rotation); keyMatrix.Translation = kf.Translation * 100; keyMatrix = invert * keyMatrix * rotationMatrix; kf.Transform = keyMatrix; } } } } VRageRender.MyRenderProxy.GetRenderProfiler().EndProfilingBlock(); ModelInfo = new MyModelInfo(GetTrianglesCount(), GetVerticesCount(), BoundingBoxSize); m_loadedData = true; MyLog.Default.DecreaseIndent(LoggingOptions.LOADING_MODELS); MyLog.Default.WriteLine("MyModel.LoadAnimationData -> END", LoggingOptions.LOADING_MODELS); VRageRender.MyRenderProxy.GetRenderProfiler().EndProfilingBlock(); }