public void AddGeometryData(MeshPartGeometryData data, MeshMaterial material, Matrix transform) { if (!parts.ContainsKey(material)) { throw new InvalidOperationException("Material not created by the meshbuilder"); } var p = parts[material]; var xTransform = transform.xna(); var sPositions = data.GetSourceVector3(MeshPartGeometryData.Semantic.Position); var sNormals = data.GetSourceVector3(MeshPartGeometryData.Semantic.Normal); var sTexcoords = data.GetSourceVector2(MeshPartGeometryData.Semantic.Texcoord); var nPositions = new XnaVector3[sPositions.Length]; var nNormals = new XnaVector3[sNormals.Length]; var nTexcoords = sTexcoords; XnaVector3.Transform(sPositions, ref xTransform, nPositions); XnaVector3.TransformNormal(sNormals, ref xTransform, nNormals); p.Positions.AddRange(nPositions); p.Normals.AddRange(nNormals); p.Texcoords.AddRange(nTexcoords); }
public IMesh CreateMesh() { var mesh = new RAMMesh(); foreach (var pair in parts) { var mat = pair.Key; var data = pair.Value; if (data.Positions.Count == 0) { continue; } var geom = new MeshPartGeometryData(); geom.Sources.Add(new MeshPartGeometryData.Source { DataVector3 = data.Positions.ToArray(), Number = 0, Semantic = MeshPartGeometryData.Semantic.Position }); geom.Sources.Add(new MeshPartGeometryData.Source { DataVector3 = data.Normals.ToArray(), Number = 0, Semantic = MeshPartGeometryData.Semantic.Normal }); geom.Sources.Add(new MeshPartGeometryData.Source { DataVector2 = data.Texcoords.ToArray(), Number = 0, Semantic = MeshPartGeometryData.Semantic.Texcoord }); geom.Sources.Add(new MeshPartGeometryData.Source { DataVector3 = calculateTangents(data), Number = 0, Semantic = MeshPartGeometryData.Semantic.Tangent }); var part = new RAMMeshPart(); part.SetGeometryData(geom); mesh.GetCoreData().Parts.Add(new MeshCoreData.Part { MeshMaterial = mat.ToMeshCoreDataMaterial(), MeshPart = part, ObjectMatrix = Matrix.Identity.xna() }); } return(mesh); }
public static MeshPartGeometryData CreateTestSquare() { MeshPartGeometryData.Source positions = new MeshPartGeometryData.Source(); MeshPartGeometryData.Source normals = new MeshPartGeometryData.Source(); var texcoords = new Source(); positions.Semantic = MeshPartGeometryData.Semantic.Position; positions.DataVector3 = new Vector3[6]; positions.DataVector3[0] = Vector3.Zero; positions.DataVector3[1] = Vector3.UnitY; positions.DataVector3[2] = Vector3.UnitX + Vector3.UnitY; positions.DataVector3[3] = Vector3.Zero; positions.DataVector3[4] = Vector3.UnitX + Vector3.UnitY; positions.DataVector3[5] = Vector3.UnitX; normals.Semantic = MeshPartGeometryData.Semantic.Normal; normals.DataVector3 = new Vector3[6]; normals.DataVector3[0] = Vector3.UnitZ; normals.DataVector3[1] = Vector3.UnitZ; normals.DataVector3[2] = Vector3.UnitZ; normals.DataVector3[3] = Vector3.UnitZ; normals.DataVector3[4] = Vector3.UnitZ; normals.DataVector3[5] = Vector3.UnitZ; texcoords.Semantic = Semantic.Texcoord; texcoords.DataVector2 = new Vector2[6]; texcoords.DataVector2[0] = new Vector2(0, 0); texcoords.DataVector2[1] = new Vector2(0, 1); texcoords.DataVector2[2] = new Vector2(1, 1); texcoords.DataVector2[3] = new Vector2(0, 0); texcoords.DataVector2[4] = new Vector2(1, 1); texcoords.DataVector2[5] = new Vector2(1, 0); MeshPartGeometryData data = new MeshPartGeometryData(); data.Sources.Add(positions); data.Sources.Add(normals); data.Sources.Add(texcoords); return(data); }
public static IMesh CreateSimpleTestMesh() { IMesh mesh; mesh = new RAMMesh(); var part = new MeshCoreData.Part(); part.ObjectMatrix = Matrix.Identity; part.MeshPart = new RAMMeshPart(); ((RAMMeshPart)part.MeshPart).SetGeometryData(MeshPartGeometryData.CreateTestSquare()); var mat = new MeshCoreData.Material(); mat.DiffuseMap = GetTestTexture(); part.MeshMaterial = mat; mesh.GetCoreData().Parts.Add(part); return(mesh); }
public void SetSourcesFromTangentVertices(short[] indices, TangentVertex[] vertices) { MeshPartGeometryData geom = this; Source source; var positions = new Vector3[indices.Length]; var normals = new Vector3[indices.Length]; var uvs = new Vector2[indices.Length]; var tangents = new Vector3[indices.Length]; for (int i = 0; i < indices.Length; i++) { positions[i] = vertices[indices[i]].pos; normals[i] = vertices[indices[i]].normal; uvs[i] = vertices[indices[i]].uv; tangents[i] = vertices[indices[i]].tangent; } source = new Source(); geom.Sources.Add(source); source.Semantic = Semantic.Position; source.DataVector3 = positions; source = new Source(); geom.Sources.Add(source); source.Semantic = Semantic.Normal; source.DataVector3 = normals; source = new Source(); geom.Sources.Add(source); source.Semantic = Semantic.Texcoord; source.DataVector2 = uvs; source = new Source(); geom.Sources.Add(source); source.Semantic = Semantic.Tangent; source.DataVector3 = tangents; }
public void SetGeometryData(MeshPartGeometryData data) { geometryData = data; }