protected mesh CreatePalletMesh(PalletProperties palletProperties) { // build pallet object Pallet pallet = new Pallet(palletProperties); // build list of boxes List<Box> listBoxes = pallet.BuildListOfBoxes(); // build list of vertices / normals / UVs ulong vertexCount = 0, normalCount = 0, uvCount = 0, triangleCount = 0, boxCount = 0; string triangle_string = string.Empty; List<double> doubleArrayPosition = new List<double>(), doubleArrayNormal = new List<double>(), doubleArrayUV = new List<double>(); foreach (Box box in listBoxes) { foreach (Vector3D p in box.Points) { doubleArrayPosition.Add(p.X); doubleArrayPosition.Add(p.Y); doubleArrayPosition.Add(p.Z); ++vertexCount; } foreach (Vector3D n in box.Normals) { doubleArrayNormal.Add(n.X); doubleArrayNormal.Add(n.Y); doubleArrayNormal.Add(n.Z); ++normalCount; } foreach (Vector2D uv in box.UVs) { doubleArrayUV.Add(uv.X); doubleArrayUV.Add(uv.Y); ++uvCount; } foreach (TriangleIndices tr in box.Triangles) { triangle_string += tr.ConvertToString(boxCount); ++triangleCount; } ++boxCount; } mesh palletMesh = new mesh(); // position source source palletPositionSource = new source() { id = "pallet_position", name = "pallet_position" }; float_array farrayPosition = new float_array { id = "pallet_position_float_array", count = (ulong)doubleArrayPosition.Count, Values = doubleArrayPosition.ToArray() }; palletPositionSource.technique_common = new sourceTechnique_common() { accessor = new accessor() { stride = 3, count = vertexCount, source = "#pallet_position_float_array", param = new param[] { new param() { name = "X", type = "float" }, new param() { name = "Y", type = "float" }, new param() { name = "Z", type = "float" } } } }; palletPositionSource.Item = farrayPosition; // normal source source palletPositionNormal = new source() { id = "pallet_normal", name = "pallet_normal" }; float_array farrayNormal = new float_array { id = "pallet_normal_float_array", count = (ulong)doubleArrayNormal.Count, Values = doubleArrayNormal.ToArray() }; palletPositionNormal.technique_common = new sourceTechnique_common() { accessor = new accessor() { stride = 3, count = normalCount, source = "#pallet_normal_float_array", param = new param[] { new param() { name = "X", type = "float" }, new param() { name = "Y", type = "float" }, new param() { name = "Z", type = "float" } } } }; palletPositionNormal.Item = farrayNormal; // uv source source palletPositionUV = new source() { id = "pallet_UV", name = "pallet_UV" }; float_array farrayUV = new float_array { id = "pallet_UV_float_array", count = (ulong)doubleArrayUV.Count, Values = doubleArrayUV.ToArray() }; palletPositionUV.technique_common = new sourceTechnique_common() { accessor = new accessor() { stride = 2, count = vertexCount, source = "#pallet_UV_float_array", param = new param[] { new param() { name = "S", type = "float" }, new param() { name = "T", type = "float" } } } }; palletPositionUV.Item = farrayUV; // insert sources palletMesh.source = new source[] { palletPositionSource, palletPositionNormal, palletPositionUV }; // vertices InputLocal verticesInput = new InputLocal() { semantic = "POSITION", source = "#pallet_position" }; palletMesh.vertices = new vertices() { id = "pallet_vertex", input = new InputLocal[] { verticesInput } }; triangles trianglesPallet = new triangles() { material = "materialPallet", count = triangleCount }; trianglesPallet.input = new InputLocalOffset[] { new InputLocalOffset() { semantic="VERTEX", source="#pallet_vertex", offset=0} , new InputLocalOffset() { semantic="NORMAL", source="#pallet_normal", offset=1} , new InputLocalOffset() { semantic="TEXCOORD", source="#pallet_UV", offset=2, set=0, setSpecified=true } }; trianglesPallet.p = triangle_string; palletMesh.Items = new object[] { trianglesPallet }; return palletMesh; }
protected mesh CreateCaseMesh(BoxProperties caseProperties) { // build box Box box = new Box(0, caseProperties); // build list of vertices / normals / UVs ulong vertexCount = 0, normalCount = 0, uvCount = 0; List<double> doubleArrayPosition = new List<double>(), doubleArrayNormal = new List<double>(), doubleArrayUV = new List<double>(); foreach (Vector3D p in box.PointsSmallOffset) { doubleArrayPosition.Add(p.X); doubleArrayPosition.Add(p.Y); doubleArrayPosition.Add(p.Z); ++vertexCount; } foreach (Vector3D n in box.Normals) { doubleArrayNormal.Add(n.X); doubleArrayNormal.Add(n.Y); doubleArrayNormal.Add(n.Z); ++normalCount; } foreach (Vector2D uv in box.UVs) { doubleArrayUV.Add(uv.X); doubleArrayUV.Add(uv.Y); ++uvCount; } mesh caseMesh = new mesh(); // position source source casePositionSource = new source() { id = "case_position", name = "case_position" }; float_array farrayPosition = new float_array { id = "case_position_float_array", count = (ulong)doubleArrayPosition.Count, Values = doubleArrayPosition.ToArray() }; casePositionSource.technique_common = new sourceTechnique_common() { accessor = new accessor() { stride = 3, count = vertexCount, source = "#case_position_float_array", param = new param[] { new param() { name = "X", type = "float" }, new param() { name = "Y", type = "float" }, new param() { name = "Z", type = "float" } } } }; casePositionSource.Item = farrayPosition; // normal source source casePositionNormal = new source() { id = "case_normal", name = "case_normal" }; float_array farrayNormal = new float_array { id = "case_normal_float_array", count = (ulong)doubleArrayNormal.Count, Values = doubleArrayNormal.ToArray() }; casePositionNormal.technique_common = new sourceTechnique_common() { accessor = new accessor() { stride = 3, count = normalCount, source = "#case_normal_float_array", param = new param[] { new param() { name = "X", type = "float" }, new param() { name = "Y", type = "float" }, new param() { name = "Z", type = "float" } } } }; casePositionNormal.Item = farrayNormal; // uv source source casePositionUV = new source() { id = "case_UV", name = "pallet_UV" }; float_array farrayUV = new float_array { id = "case_UV_float_array", count = (ulong)doubleArrayUV.Count, Values = doubleArrayUV.ToArray() }; casePositionUV.technique_common = new sourceTechnique_common() { accessor = new accessor() { stride = 2, count = vertexCount, source = "#case_UV_float_array", param = new param[] { new param() { name = "S", type = "float" }, new param() { name = "T", type = "float" } } } }; casePositionUV.Item = farrayUV; // insert sources caseMesh.source = new source[] { casePositionSource, casePositionNormal, casePositionUV }; // vertices InputLocal verticesInput = new InputLocal() { semantic = "POSITION", source = "#case_position" }; caseMesh.vertices = new vertices() { id = "case_vertex", input = new InputLocal[] { verticesInput } }; List<object> trianglesList = new List<object>(); // build list of triangles foreach (HalfAxis.HAxis axis in HalfAxis.All) { triangles trianglesCase = new triangles() { material = string.Format("materialCase{0}", (uint)axis), count = 2 }; trianglesCase.input = new InputLocalOffset[] { new InputLocalOffset() { semantic="VERTEX", source="#case_vertex", offset=0} , new InputLocalOffset() { semantic="NORMAL", source="#case_normal", offset=1} , new InputLocalOffset() { semantic="TEXCOORD", source="#case_UV", offset=2, set=0, setSpecified=true } }; string triangle_string = string.Empty; foreach (TriangleIndices tr in box.TrianglesByFace(axis)) triangle_string += tr.ConvertToString(0); trianglesCase.p = triangle_string; trianglesList.Add(trianglesCase); } // build list of lines lines linesCase = new lines() { material = "materialCaseLines", count = 12, input = new InputLocalOffset[] { new InputLocalOffset() { semantic="VERTEX", source="#case_vertex", offset=0} }, p = "0 1 1 2 2 3 3 0 4 5 5 6 6 7 7 4 0 4 1 5 2 6 3 7" }; trianglesList.Add(linesCase); caseMesh.Items = trianglesList.ToArray(); return caseMesh; }
public static geometry MeshToGeometry(Mesh inputMesh) { string meshName = "Mesh-" + inputMesh.GetInstanceID(); geometry outputGeometry = new geometry(); mesh outputMesh = new mesh(); outputGeometry.id = meshName + "-lib"; outputGeometry.name = inputMesh.name + "-mesh"; outputGeometry.Item = outputMesh; //vertex Positions List<source> sourceList = new List<source>(); var inputVertices = inputMesh.vertices; if (inputVertices.Length == 0) return null; sourceList.Add(ArrayToSource(inputMesh.vertices, meshName + "-POSITION")); vertices vertexList = new vertices(); vertexList.id = meshName + "-VERTEX"; vertexList.input = new InputLocal[1]; vertexList.input[0] = new InputLocal(); vertexList.input[0].semantic = "POSITION"; vertexList.input[0].source = "#" + sourceList[0].id; outputMesh.vertices = vertexList; List<InputLocalOffset> offsetList = new List<InputLocalOffset>(); { InputLocalOffset offset = new InputLocalOffset(); offset.semantic = "VERTEX"; offset.offset = 0; offset.source = "#" + vertexList.id; offsetList.Add(offset); } var inputNormals = inputMesh.normals; if(inputNormals.Length > 0) { var array = ArrayToSource(inputNormals, meshName + "-Normal0"); InputLocalOffset offset = new InputLocalOffset(); offset.semantic = "NORMAL"; offset.offset = (ulong)sourceList.Count; offset.source = "#" + array.id; sourceList.Add(array); offsetList.Add(offset); } var inputUV1s = inputMesh.uv; if (inputUV1s.Length > 0) { var array = ArrayToSource(inputUV1s, meshName + "-UV0"); InputLocalOffset offset = new InputLocalOffset(); offset.semantic = "TEXCOORD"; offset.offset = (ulong)sourceList.Count; offset.source = "#" + array.id; offset.set = 0; offset.setSpecified = true; sourceList.Add(array); offsetList.Add(offset); } var inputUV2s = inputMesh.uv2; if (inputUV2s.Length > 0) { var array = ArrayToSource(inputUV2s, meshName + "-UV1"); InputLocalOffset offset = new InputLocalOffset(); offset.semantic = "TEXCOORD"; offset.offset = (ulong)sourceList.Count; offset.source = "#" + array.id; offset.set = 1; offset.setSpecified = true; sourceList.Add(array); offsetList.Add(offset); } var inputColors = inputMesh.colors; if (inputColors.Length > 0) { var array = ArrayToSource(inputColors, meshName + "-VERTEX_COLOR0"); InputLocalOffset offset = new InputLocalOffset(); offset.semantic = "COLOR"; offset.offset = (ulong)sourceList.Count; offset.source = "#" + array.id; offset.set = 0; offset.setSpecified = true; sourceList.Add(array); offsetList.Add(offset); } outputMesh.source = sourceList.ToArray(); triangles triangleList = new triangles(); triangleList.input = offsetList.ToArray(); var inputTriangles = inputMesh.triangles; triangleList.count = (ulong)inputTriangles.Length / 3; if (triangleList.count == 0) return null; StringBuilder pString = new StringBuilder(); for(int i = 0; i < inputTriangles.Length; i++) { for(int j = 0; j < triangleList.input.Length; j++) { pString.Append(inputTriangles[i]).Append(" "); } if (i % 3 == 2) pString.AppendLine(); else pString.Append(" "); } triangleList.p = pString.ToString(); outputMesh.Items = new object[1]; outputMesh.Items[0] = triangleList; return outputGeometry; }
public static geometry MeshToGeometry(CPUMesh inputMesh, string id) { geometry outputGeometry = new geometry(); mesh outputMesh = new mesh(); outputGeometry.id = id + "-lib"; outputGeometry.name = inputMesh.name + "-mesh"; outputGeometry.Item = outputMesh; //vertex Positions List <source> sourceList = new List <source>(); var inputVertices = inputMesh.vertices; if (inputVertices.Length == 0) { return(null); } sourceList.Add(ArrayToSource(inputMesh.vertices, id + "-POSITION")); vertices vertexList = new vertices(); vertexList.id = id + "-VERTEX"; vertexList.input = new InputLocal[1]; vertexList.input[0] = new InputLocal(); vertexList.input[0].semantic = "POSITION"; vertexList.input[0].source = "#" + sourceList[0].id; outputMesh.vertices = vertexList; List <InputLocalOffset> offsetList = new List <InputLocalOffset>(); { InputLocalOffset offset = new InputLocalOffset(); offset.semantic = "VERTEX"; offset.offset = 0; offset.source = "#" + vertexList.id; offsetList.Add(offset); } var inputNormals = inputMesh.normals; if (inputNormals.Length > 0) { var array = ArrayToSource(inputNormals, id + "-Normal0"); InputLocalOffset offset = new InputLocalOffset(); offset.semantic = "NORMAL"; offset.offset = (ulong)sourceList.Count; offset.source = "#" + array.id; sourceList.Add(array); offsetList.Add(offset); } var inputUV1s = inputMesh.uv; if (inputUV1s.Length > 0) { var array = ArrayToSource(inputUV1s, id + "-UV0"); InputLocalOffset offset = new InputLocalOffset(); offset.semantic = "TEXCOORD"; offset.offset = (ulong)sourceList.Count; offset.source = "#" + array.id; offset.set = 0; offset.setSpecified = true; sourceList.Add(array); offsetList.Add(offset); } var inputUV2s = inputMesh.uv2; if (inputUV2s.Length > 0) { var array = ArrayToSource(inputUV2s, id + "-UV1"); InputLocalOffset offset = new InputLocalOffset(); offset.semantic = "TEXCOORD"; offset.offset = (ulong)sourceList.Count; offset.source = "#" + array.id; offset.set = 1; offset.setSpecified = true; sourceList.Add(array); offsetList.Add(offset); } var inputColors = inputMesh.colors; if (inputColors.Length > 0) { var array = ArrayToSource(inputColors, id + "-VERTEX_COLOR0"); InputLocalOffset offset = new InputLocalOffset(); offset.semantic = "COLOR"; offset.offset = (ulong)sourceList.Count; offset.source = "#" + array.id; offset.set = 0; offset.setSpecified = true; sourceList.Add(array); offsetList.Add(offset); } outputMesh.source = sourceList.ToArray(); triangles triangleList = new triangles(); triangleList.input = offsetList.ToArray(); var inputTriangles = inputMesh.triangles; triangleList.count = (ulong)inputTriangles.Length / 3; if (triangleList.count == 0) { return(null); } StringBuilder pString = new StringBuilder(); for (int i = 0; i < inputTriangles.Length; i++) { for (int j = 0; j < triangleList.input.Length; j++) { pString.Append(inputTriangles[i]).Append(" "); } if (i % 3 == 2) { pString.AppendLine(); } else { pString.Append(" "); } } triangleList.p = pString.ToString(); outputMesh.Items = new object[1]; outputMesh.Items[0] = triangleList; return(outputGeometry); }
static ExportModel ProcessModel(ModelFile mdl, ResourceManager resources) { mdl.Path = mdl.Path.Replace(' ', '_'); var ex = new ExportModel(); for (int midx = 0; midx < mdl.Levels.Length; midx++) { var lvl = mdl.Levels[midx]; var processed = ProcessRef(lvl, resources); var geo = new CL.geometry(); geo.name = geo.id = mdl.Path + "-level" + midx; var mesh = new CL.mesh(); geo.Item = mesh; CL.source positions; CL.source normals = null; CL.source colors = null; CL.source tex1 = null; CL.source tex2 = null; int idxC = 1; positions = CreateSource( geo.name + "-positions", (k) => new Vector4(processed.Vertices[k].Position, 0), 3, processed.Vertices.Length); mesh.vertices = new CL.vertices() { id = geo.name + "-vertices", input = new CL.InputLocal[] { new CL.InputLocal() { semantic = "POSITION", source = "#" + positions.id } } }; var sources = new List <CL.source>() { positions }; if ((processed.FVF & D3DFVF.NORMAL) == D3DFVF.NORMAL) { normals = CreateSource( geo.name + "-normals", (k) => new Vector4(processed.Vertices[k].Normal, 0), 3, processed.Vertices.Length); sources.Add(normals); idxC++; } if ((processed.FVF & D3DFVF.DIFFUSE) == D3DFVF.DIFFUSE) { colors = CreateSource( geo.name + "-color", (k) => { var c = Color4.FromRgba(processed.Vertices[k].Diffuse); return(new Vector4(c.R, c.G, c.B, c.A)); }, 4, processed.Vertices.Length); sources.Add(colors); idxC++; } bool doTex1, doTex2 = false; if ((processed.FVF & D3DFVF.TEX2) == D3DFVF.TEX2) { doTex1 = doTex2 = true; } else if ((processed.FVF & D3DFVF.TEX1) == D3DFVF.TEX1) { doTex1 = true; } else { doTex1 = doTex2 = false; } if (doTex1) { tex1 = CreateSource( geo.name + "-tex1", (k) => new Vector4(processed.Vertices[k].TextureCoordinate, 0, 0), 2, processed.Vertices.Length); sources.Add(tex1); idxC++; } if (doTex2) { tex2 = CreateSource( geo.name + "-tex2", (k) => new Vector4(processed.Vertices[k].TextureCoordinateTwo, 0, 0), 2, processed.Vertices.Length); sources.Add(tex2); idxC++; } mesh.source = sources.ToArray(); var items = new List <object>(); foreach (var dc in processed.Drawcalls) { if (!ex.Materials.Any((x) => x.Name == dc.Material.Name)) { ex.Materials.Add(dc.Material); } var trs = new CL.triangles(); trs.count = (ulong)(dc.Indices.Length / 3); trs.material = dc.Material.Name + "-material"; List <int> pRefs = new List <int>(dc.Indices.Length * idxC); List <CL.InputLocalOffset> inputs = new List <CL.InputLocalOffset>() { new CL.InputLocalOffset() { semantic = "VERTEX", source = "#" + geo.id + "-vertices", offset = 0 } }; ulong off = 1; if (normals != null) { inputs.Add(new CL.InputLocalOffset() { semantic = "NORMAL", source = "#" + normals.id, offset = off++ }); } if (colors != null) { inputs.Add(new CL.InputLocalOffset() { semantic = "COLOR", source = "#" + colors.id, offset = off++ }); } if (tex1 != null) { inputs.Add(new CL.InputLocalOffset() { semantic = "TEXCOORD", source = "#" + tex1.id, offset = off++ }); } if (tex2 != null) { inputs.Add(new CL.InputLocalOffset() { semantic = "TEXCOORD", source = "#" + tex2.id, offset = off++ }); } trs.input = inputs.ToArray(); for (int i = 0; i < dc.Indices.Length; i++) { for (int j = 0; j < idxC; j++) { pRefs.Add(dc.Indices[i]); } } trs.p = string.Join(" ", pRefs.ToArray()); items.Add(trs); } mesh.Items = items.ToArray(); ex.Geometries.Add(geo); } return(ex); }