/// <summary> /// This is the method that actually does the work. /// </summary> /// <param name="DA">The DA object can be used to retrieve data from input parameters and /// to store data in output parameters.</param> protected override void SolveInstance(IGH_DataAccess DA) { IntPtr topoData = IntPtr.Zero; Mesh refMesh = new Mesh(); if (!DA.GetData(0, ref topoData)) { return; } if (!DA.GetData(1, ref refMesh)) { return; } CMesh crefMesh = new CMesh(); String errorMessage = ""; if (TopoCreator.RhinoMeshToCMesh(refMesh, ref crefMesh, true, out errorMessage)) { TopoCreator.setReferenceSurface(ref crefMesh, topoData); } else { AddRuntimeMessage(GH_RuntimeMessageLevel.Error, errorMessage); return; } Marshal.FreeHGlobal(crefMesh.points); Marshal.FreeHGlobal(crefMesh.faces); DA.SetData(0, topoData); }
public bool ExportMeshWithRig(Stream meshStream, Stream rigStream, FileInfo outfile, bool LodFilter = true, bool isGLBinary = true) { RawArmature Rig = _rig.ProcessRig(rigStream); var cr2w = _wolvenkitFileService.TryReadRED4File(meshStream); if (cr2w == null || !cr2w.Chunks.Select(_ => _.Data).OfType <CMesh>().Any()) { return(false); } if (!cr2w.Chunks.Select(_ => _.Data).OfType <rendRenderMeshBlob>().Any()) { return(false); } MemoryStream ms = GetMeshBufferStream(meshStream, cr2w); MeshesInfo meshinfo = GetMeshesinfo(cr2w); MeshBones bones = new MeshBones(); CMesh cmesh = cr2w.Chunks.Select(_ => _.Data).OfType <CMesh>().First(); if (cmesh.BoneNames.Count != 0) // for rigid meshes { bones.Names = RIG.GetboneNames(cr2w); bones.WorldPosn = GetMeshBonesPosn(cr2w); } List <RawMeshContainer> expMeshes = ContainRawMesh(ms, meshinfo, LodFilter); if (cmesh.BoneNames.Count == 0) // for rigid meshes { for (int i = 0; i < expMeshes.Count; i++) { expMeshes[i].weightcount = 0; } } UpdateMeshJoints(ref expMeshes, Rig, bones); ModelRoot model = RawMeshesToGLTF(expMeshes, Rig); if (isGLBinary) { model.SaveGLB(outfile.FullName); } else { model.SaveGLTF(outfile.FullName); } meshStream.Dispose(); meshStream.Close(); rigStream.Dispose(); rigStream.Close(); return(true); }
public void Generate() { if (WManager == null) { return; } m = new CMesh(); Vector3 tposition = transform.position; Task.Run( () => { m = Calculate(m, tposition); calculated = true; } ); }
private void addToContactGraph(Mesh mesh, bool atBoundary, IntPtr graphData) { CMesh cmesh = new CMesh(); String errorMessage; if (TopoCreator.RhinoMeshToCMesh(mesh, ref cmesh, false, out errorMessage)) { TopoCreator.addMeshesToContactGraph(graphData, ref cmesh, atBoundary); } else { AddRuntimeMessage(GH_RuntimeMessageLevel.Error, errorMessage); return; } Marshal.FreeHGlobal(cmesh.points); Marshal.FreeHGlobal(cmesh.faces); }
public CMesh Calculate(CMesh cm, Vector3 tposition) { CMesh m = new CMesh(cm); var perlin = new Perlin(0.05, 0.7, 2, 4, 2304, QualityMode.Medium); for (int i = 0; i < WManager.ChunkSize; i++) { for (int j = 0; j < WManager.ChunkSize; j++) { Vector3 p = new Vector3(i, 0, j) + tposition; int h = (int)perlin.GetValue(p.x, p.y, p.z); var r = (int)perlin.GetValue(p.x + 1, p.y, p.z) == h ? true : false; var l = (int)perlin.GetValue(p.x - 1, p.y, p.z) == h ? true : false; var f = (int)perlin.GetValue(p.x, p.y, p.z + 1) == h ? true : false; var b = (int)perlin.GetValue(p.x, p.y, p.z - 1) == h ? true : false; m.AddCMesh(Cube.CreateCube(new Vector3(i, h, j), WManager.CubeSize / 2f, new bool[] { !b, !r, !f, !l, true, false }, "__base__")); } } return(m); }
public static CMesh CreateCube(Vector3 position, float size, bool[] faces, string textureName) { if(faces.Length != 6){ throw new System.Exception("Faces array do not conains 6 elements!", new System.IndexOutOfRangeException()); } Vector3[] v = new Vector3[]{ new Vector3(-size, -size, -size) + position, new Vector3(-size, size, -size) + position, new Vector3( size, size, -size) + position, new Vector3( size, -size, -size) + position, new Vector3(-size, -size, size) + position, new Vector3(-size, size, size) + position, new Vector3( size, size, size) + position, new Vector3( size, -size, size) + position, }; CMesh m = new CMesh(); Vector2[] _uvs = null; if(TextureLoader.TextureExist(textureName)){ _uvs = (TextureLoader.GetTexture(textureName).CalculatedUV()); } for(int i = 0; i < faces.Length; i++){ if(faces[i]){ int vCount = m.vertices.Count; switch(i){ case 0: vCount = m.vertices.Count; m.AddTriangles(new int[]{0 + (vCount), 1 + (vCount), 2 + (vCount), 0 + (vCount), 2 + (vCount), 3 + (vCount)}); m.AddVertices(new Vector3[]{ v[0], v[1], v[2], v[3] }); if(_uvs != null) m.AddUVs(new Vector2[]{ _uvs[0], _uvs[1], _uvs[2], _uvs[3] }); break; case 1: vCount = m.vertices.Count; m.AddTriangles(new int[]{0 + (vCount), 1 + (vCount), 2 + (vCount), 0 + (vCount), 2 + (vCount), 3 + (vCount)}); m.AddVertices(new Vector3[]{ v[3], v[2], v[6], v[7] }); if(_uvs != null) m.AddUVs(new Vector2[]{ _uvs[4], _uvs[5], _uvs[6], _uvs[7] }); break; case 2: vCount = m.vertices.Count; m.AddTriangles(new int[]{0 + (vCount), 1 + (vCount), 2 + (vCount), 0 + (vCount), 2 + (vCount), 3 + (vCount)}); m.AddVertices(new Vector3[]{ v[7], v[6], v[5], v[4] }); if(_uvs != null) m.AddUVs(new Vector2[]{ _uvs[8], _uvs[9], _uvs[10], _uvs[11] }); break; case 3: vCount = m.vertices.Count; m.AddTriangles(new int[]{0 + (vCount), 1 + (vCount), 2 + (vCount), 0 + (vCount), 2 + (vCount), 3 + (vCount)}); m.AddVertices(new Vector3[]{ v[4], v[5], v[1], v[0] }); if(_uvs != null) m.AddUVs(new Vector2[]{ _uvs[12], _uvs[13], _uvs[14], _uvs[15] }); break; case 4: vCount = m.vertices.Count; m.AddTriangles(new int[]{0 + (vCount), 1 + (vCount), 2 + (vCount), 0 + (vCount), 2 + (vCount), 3 + (vCount)}); m.AddVertices(new Vector3[]{ v[1], v[5], v[6], v[2] }); if(_uvs != null) m.AddUVs(new Vector2[]{ _uvs[16], _uvs[17], _uvs[18], _uvs[19] }); break; case 5: vCount = m.vertices.Count; m.AddTriangles(new int[]{0 + (vCount), 1 + (vCount), 2 + (vCount), 0 + (vCount), 2 + (vCount), 3 + (vCount)}); m.AddVertices(new Vector3[]{ v[4], v[0], v[3], v[7] }); if(_uvs != null) m.AddUVs(new Vector2[]{ _uvs[20], _uvs[21], _uvs[22], _uvs[23] }); break; default: break; } } } return m; }
// Summary: // Create a new Instance of a Mesh // Parameters: // mesh: // the ModelMesh to instance // parentBone: // the bone that will give this instance its position // materialBinding // the binding between the material name and the name in the mesh parts // // Returns: // A new instance of mesh public InstanceMesh(Model.CMesh mesh, Model.Bone parentBone, Dictionary<string, string> materialBinding) { this.mesh = mesh; this.parentBone = parentBone; this.materialBinding = materialBinding; }
public void Push(CMesh mesh) { }
public void Render(CModel model, CCamera cam, CShaderSystem shader, bool transparent) { GL.Enable(EnableCap.Texture2D); GL.Enable(EnableCap.AlphaTest); GL.Enable(EnableCap.DepthTest); if (model == null) { return; } CMesh mesh = model.Mesh; if (mesh.BindVBOs() == false) { return; } // start shader if (shader.UseProgram(SupportShader.Model) == true) { UniformModel(model); foreach (CSubset subset in mesh.Subsets) { CMaterial material = null; if (model.Materials.Count > 0 && model.Materials.ContainsKey(subset.Name)) { material = model.Materials[subset.Name]; } if (material == null) { if (transparent == true) // 재질이 없으면 불투명할 대만 그린다. { continue; } // Material Routine Func Uniform int func_index = (int)FSMaterialRoutine.funcBasic; GL.UniformSubroutines(ShaderType.FragmentShader, 1, ref func_index); } else { if (transparent != material.IsTransparent()) { continue; } material.GLModeSetting(); material.Bind(); material.Uniform(); } // Draw int offset = subset.IndexOffset * sizeof(uint); int elementCount = subset.TriangleCount * 3; GL.DrawElements(PrimitiveType.Triangles, elementCount, DrawElementsType.UnsignedInt, offset); } } shader.ReleaseProgram(); GL.BindTexture(TextureTarget.Texture2D, 0); GL.Disable(EnableCap.Blend); mesh.UnbindVBOs(); GL.Disable(EnableCap.Texture2D); GL.Disable(EnableCap.AlphaTest); GL.Disable(EnableCap.DepthTest); }
public RDTMeshViewModel(CMesh data, RedDocumentViewModel file) { Header = "Preview"; File = file; _data = data; }
public bool ExportMultiMeshWithRig(List <Stream> meshStreamS, List <Stream> rigStreamS, FileInfo outfile, bool LodFilter = true, bool isGLBinary = true) { List <RawArmature> Rigs = new List <RawArmature>(); rigStreamS = rigStreamS.OrderByDescending(r => r.Length).ToList(); // not so smart hacky method to get bodybase rigs on top/ orderby descending for (int r = 0; r < rigStreamS.Count; r++) { RawArmature Rig = _rig.ProcessRig(rigStreamS[r]); Rigs.Add(Rig); } RawArmature expRig = RIG.CombineRigs(Rigs); List <RawMeshContainer> expMeshes = new List <RawMeshContainer>(); for (int m = 0; m < meshStreamS.Count; m++) { var cr2w = _wolvenkitFileService.TryReadRED4File(meshStreamS[m]); if (cr2w == null || !cr2w.Chunks.Select(_ => _.Data).OfType <CMesh>().Any() || !cr2w.Chunks.Select(_ => _.Data).OfType <rendRenderMeshBlob>().Any()) { continue; } MemoryStream ms = GetMeshBufferStream(meshStreamS[m], cr2w); MeshesInfo meshinfo = GetMeshesinfo(cr2w); MeshBones bones = new MeshBones(); CMesh cmesh = cr2w.Chunks.Select(_ => _.Data).OfType <CMesh>().First(); if (cmesh.BoneNames.Count != 0) // for rigid meshes { bones.Names = RIG.GetboneNames(cr2w); bones.WorldPosn = GetMeshBonesPosn(cr2w); } List <RawMeshContainer> Meshes = ContainRawMesh(ms, meshinfo, LodFilter); for (int i = 0; i < Meshes.Count; i++) { Meshes[i].name = m + "_" + Meshes[i].name; if (cmesh.BoneNames.Count == 0) // for rigid meshes { Meshes[i].weightcount = 0; } } UpdateMeshJoints(ref Meshes, expRig, bones); expMeshes.AddRange(Meshes); } ModelRoot model = RawMeshesToGLTF(expMeshes, expRig); if (isGLBinary) { model.SaveGLB(outfile.FullName); } else { model.SaveGLTF(outfile.FullName); } for (int i = 0; i < meshStreamS.Count; i++) { meshStreamS[i].Dispose(); meshStreamS[i].Close(); } for (int i = 0; i < rigStreamS.Count; i++) { rigStreamS[i].Dispose(); rigStreamS[i].Close(); } return(true); }
public void UpdatePerObjectLights(DeviceContext deviceContext, CMesh meshToRender) { // TODO henning do something smart here to get the best lights that affect this mesh m_perObjectLightBuffer.UpdateBuffer(deviceContext, m_positionalLights); }
public static bool TryParse(CMdlFileNavigator reader, out CMesh parsedMesh) { parsedMesh = null; if (null == reader) { return(false); } CMesh mesh = new CMesh { Name = reader.ReadText() }; string meshTag; // Data could contain end block symbol. // If errors occur, the whole process must be canceled, // because reader can be at unexpected position (segmentation fault). bool passed = true; reader.SeekBlockStart(); // begin mesh while (passed && !String.IsNullOrEmpty(meshTag = reader.ReadTag(mTags))) { reader.SeekBlockStart(); // begin data switch (meshTag) { case TagInfo.vertex: // repeatedly entering here appends more vertice points to same mesh { float[] pt3d = new float[3]; passed &= reader.TryOnDataSequence(onElement : delegate() { // basically a foreach vec3 for (int i = 0; i < 3; ++i) { pt3d[i] = reader.ReadSingle(); } reader.MoveCursor(4); // we discard the 4th dimension (4B) mesh.AddVertex(pt3d); }); break; }//VRTX case TagInfo.normal: // repeatedly entering here appends more normals to same mesh { float[] pt3d = new float[3]; passed &= reader.TryOnDataSequence(onElement : delegate() { for (int i = 0; i < 3; ++i) { pt3d[i] = reader.ReadSingle(); } mesh.AddNormal(pt3d); // stores as flipped }); break; }//NRML case TagInfo.textureUV: // repeatedly entering here appends more coordinates to same mesh { float u, v; passed &= reader.TryOnDataSequence(onElement : delegate() { u = reader.ReadSingle(); v = reader.ReadSingle(); mesh.AddTexCoord(u, v); // expect normalized values and mirrors them }); break; }//TXUV case TagInfo.face: { ushort[] idx = new ushort[3]; passed &= reader.TryOnDataSequence(onElement : delegate() { for (int i = 0; i < 3; ++i) { idx[i] = reader.ReadUInt16(); } mesh.AssignFace(idx); }); break; }//FACE case TagInfo.materials: passed &= ReadInMaterialList(); // MTRL reader.SkipWhitespace(); // \r\n while ('}' != reader.PeekChar()) { // improve parsing stability on garbage text passages reader.SeekBlockEnd(); reader.SkipWhitespace(); } break; case TagInfo.weight: while (reader.IsAtTag(mBONE_ANSI)) { reader.MoveCursor(4); // skip 'BONE' passed &= ReadInWeights(out VertexGrp influence); if (!influence.IsNullOrEmpty()) { mesh.AssignWeight(influence); } } break; case TagInfo.unsupported1: try { int valCnt = reader.ReadInt32(); reader.MoveCursor(valCnt * 24); } catch { passed = false; } break; default: // try to skip with block end (but no error message) //passed = false; break; }//switch reader.SeekBlockEnd(); // end data } reader.SeekBlockEnd(); // end mesh //if (!mesh.IsValid()) //{ // return false; //} parsedMesh = mesh; return(passed); bool ReadInMaterialList() { // Read material count and index ranges uint size = 0; uint[] iStart; uint[] iStop; try{ size = reader.ReadUInt32(); // Empty MTRL bodys might follow that are not counted! iStart = new uint[size]; iStop = new uint[size]; for (int i = 0; i < size; i++) { iStart[i] = reader.ReadUInt32(); iStop[i] = reader.ReadUInt32(); } } catch { return(false); } // Begin of material description string matNam; long currPos; int bindsCnt; for (int matN = 0; matN < size; ++matN) { reader.SkipWhitespace(); if (!reader.IsAtTag(mMTRL_ANSI)) { return(false); } reader.MoveCursor(4); // Fetch material name matNam = null; currPos = reader.ReaderPos; // right after tag CTextUtil.DoOnNewLine(reader.Data, currPos, onNewLine : delegate(long nLPos) { if (nLPos > currPos) { byte[] lineSeq = new byte[nLPos - currPos - 1]; // exclude '{' Array.Copy(reader.Data, currPos, lineSeq, 0L, lineSeq.Length); matNam = CTextUtil.AnsiToStr(lineSeq).Trim(); // remove preceeding whitespace currPos = reader.ReaderPos = nLPos + 2; // after \r\n } }); if (String.IsNullOrEmpty(matNam)) { return(false); } mesh?.AssignMaterial(matNam, iStart[matN], iStop[matN]); // Determine lenght of description part bindsCnt = 0; reader.SeekBlockStart(); // after SBST{ CTextUtil.DoOnNewLine(reader.Data, reader.ReaderPos, delegate(long nLPos) { reader.ReaderPos = nLPos + 2; // in newline if (!Int32.TryParse(CTextUtil.AnsiToStr(reader.PeekLine()).Trim(), out bindsCnt)) { bindsCnt = -1; } }); if (bindsCnt < 1) { return(false); } // Skip material description do { reader.SeekBlockEnd(); // end of binding block bindsCnt--; } while (bindsCnt > 0); reader.SeekBlockEnd(); // end of material description reader.SeekBlockEnd(); // end of material block }//for matN return(true); // may contain no materials } bool ReadInWeights(out VertexGrp boneInfluence) { boneInfluence = new VertexGrp(); // Read bone influence reader.SeekBlockStart(); string boneNam = reader.ReadText(); uint size = 0; int[] ids; float[] weights; try{ size = reader.ReadUInt32(); ids = new int[size]; weights = new float[size]; for (uint i = 0; i < size; ++i) { ids[i] = reader.ReadInt32(); } for (uint i = 0; i < size; ++i) { weights[i] = reader.ReadSingle(); } } catch { return(false); } // Read bind matrix float[] buff = new float[16]; int numVal = 0; Transform3DF mat; try { for (; (numVal < buff.Length) && reader.HasMore(4); ++numVal) // 4B float { buff[numVal] = reader.ReadSingle(); } mat = new Transform3DF { m00 = buff[0], m01 = buff[4], m02 = buff[8], m03 = buff[12], m10 = buff[1], m11 = buff[5], m12 = buff[9], m13 = buff[13], m20 = buff[2], m21 = buff[6], m22 = buff[10], m23 = buff[14] }; } catch { if (numVal < 64) { reader.MoveCursor(64 - numVal * 4); // sizeOf(mat) == 64 } mat = Transform3DF.Identity(); } reader.SeekBlockEnd(); // Assign bone weight map if (size > 0) { //var weightMap = new System.Collections.Specialized.OrderedDictionary(); var weightMap = new Dictionary <int, float>(); for (int i = 0; i < size; ++i) { weightMap[ids[i]] = weights[i]; // overwrite if exist, add if not } boneInfluence.groupName = boneNam; boneInfluence.mapping = weightMap; boneInfluence.bindMatrix = mat; } return(true); } }//tryparse
public CMesh(CMesh m) { vertices = new List <Vector3>(m.vertices); triangles = new List <int>(m.triangles); uvs = new List <Vector2>(m.uvs); }
// Ajoute les vertex, triangles et uv d'un CMesh public void AddCMesh(CMesh cmesh) { AddTriangles(RecalulateTriangles(cmesh.triangles)); AddVertices(cmesh.vertices); AddUVs(cmesh.uvs); }