public ModelRoot AddTerrainMesh(ModelRoot model, Triangulation triangulation, PBRTexture textures, bool doubleSided = true) { var indexedTriangulation = new IndexedTriangulation(triangulation); var normals = _meshService.ComputeMeshNormals(indexedTriangulation.Positions, indexedTriangulation.Indices); return(AddMesh(model, TERRAIN_NODE_NAME, indexedTriangulation, normals, textures, doubleSided)); }
public ModelRoot AddLine(ModelRoot model, string nodeName, IEnumerable <GeoPoint> gpxPointsElevated, Vector4 color, float trailWidthMeters) { var scene = model.UseScene(TERRAIN_SCENE_NAME); var rnode = scene.FindNode(n => n.Name == nodeName); if (rnode == null) { rnode = scene.CreateNode(nodeName); } var rmesh = rnode.Mesh = FindOrCreateMesh(model, string.Concat(nodeName, "Mesh")); var material = model.CreateMaterial(string.Concat(nodeName, "Material")) .WithPBRMetallicRoughness(color, null, null, 0, 0.9f) .WithDoubleSide(true); material.Alpha = SharpGLTF.Schema2.AlphaMode.BLEND; var triangulation = _meshService.GenerateTriangleMesh_Line(gpxPointsElevated, trailWidthMeters); var indexedTriangulation = new IndexedTriangulation(triangulation.positions, triangulation.indexes); var normals = _meshService.ComputeMeshNormals(indexedTriangulation.Positions, indexedTriangulation.Indices); // create mesh primitive var primitive = rmesh.CreatePrimitive() .WithVertexAccessor("POSITION", indexedTriangulation.Positions) .WithVertexAccessor("NORMAL", normals.ToList()) .WithIndicesAccessor(PrimitiveType.TRIANGLES, indexedTriangulation.Indices); primitive = primitive.WithMaterial(material); return(model); }
public ModelRoot AddLine(ModelRoot model, IEnumerable <GeoPoint> gpxPointsElevated, Vector4 vector4, float trailWidthMeters) { var scene = model.UseScene(TERRAIN_SCENE_NAME); var rnode = scene.FindNode(n => n.Name == TERRAIN_NODE_NAME); var rmesh = rnode.Mesh = FindOrCreateMesh(model, TERRAIN_MESH_NAME); var material = model.CreateMaterial("Line") .WithPBRMetallicRoughness(vector4, null, null, 1, 0.1f) .WithDoubleSide(true); var triangulation = _meshService.GenerateTriangleMesh_Line(gpxPointsElevated, trailWidthMeters); var indexedTriangulation = new IndexedTriangulation(triangulation.positions, triangulation.indexes); var normals = _meshService.ComputeNormals(indexedTriangulation.Positions, indexedTriangulation.Indices); // create mesh primitive var primitive = rmesh.CreatePrimitive() .WithVertexAccessor("POSITION", indexedTriangulation.Positions) .WithVertexAccessor("NORMAL", normals.ToList()) .WithIndicesAccessor(PrimitiveType.TRIANGLES, indexedTriangulation.Indices); primitive = primitive.WithMaterial(material); return(model); }
public ModelRoot AddMesh(ModelRoot model, string nodeName, IndexedTriangulation indexedTriangulation, IEnumerable <Vector3> normals, PBRTexture textures, bool doubleSided = true) { // create a basic scene model = model ?? CreateNewModel(); var rnode = model.LogicalScenes.First()?.FindNode(n => n.Name == nodeName); if (rnode == null) { rnode = model.LogicalScenes.First().CreateNode(nodeName); } var rmesh = rnode.Mesh = FindOrCreateMesh(model, string.Concat(rnode.Name, "Mesh")); var material = model.CreateMaterial(string.Concat(nodeName, "Material")) .WithPBRMetallicRoughness(Vector4.One, textures?.BaseColorTexture?.FilePath, null, 0, 1) .WithDoubleSide(doubleSided); if (textures != null && textures.NormalTexture != null) { material.WithChannelTexture("NORMAL", 0, textures.NormalTexture.FilePath); } // create mesh primitive MeshPrimitive primitive = rmesh.CreatePrimitive(); if (indexedTriangulation.Colors != null && indexedTriangulation.Colors.Any()) { primitive = primitive.WithVertexAccessor("POSITION", indexedTriangulation.Positions); primitive = primitive.WithVertexAccessor("COLOR_0", indexedTriangulation.Colors); } else { primitive = primitive.WithVertexAccessor("POSITION", indexedTriangulation.Positions); } if (normals != null) { primitive = primitive.WithVertexAccessor("NORMAL", normals.ToList()); } primitive = primitive.WithIndicesAccessor(PrimitiveType.TRIANGLES, indexedTriangulation.Indices); if (textures != null && textures.TextureCoordSets == null) { (Vector3 Min, Vector3 Max)coordBounds = CalculateBounds(indexedTriangulation.Positions); textures.TextureCoordSets = indexedTriangulation.Positions.Select(pos => new Vector2( MathHelper.Map(coordBounds.Min.X, coordBounds.Max.X, 0, 1, pos.X, true) , MathHelper.Map(coordBounds.Min.Z, coordBounds.Max.Z, 0, 1, pos.Z, true) )); primitive = primitive .WithVertexAccessor("TEXCOORD_0", textures.TextureCoordSets.ToList()); } primitive = primitive.WithMaterial(material); return(model); }
public ModelRoot CloneWithMesh(ModelRoot inputModel, IndexedTriangulation indexedTriangulation) { var model = CreateNewModel(); var rnode = model.LogicalScenes.First().CreateNode(TERRAIN_NODE_NAME); var rmesh = rnode.Mesh = FindOrCreateMesh(model, string.Concat(rnode.Name, "Mesh")); MaterialBuilder b = new MaterialBuilder(); inputModel.LogicalMaterials.First().CopyTo(b); var material = Toolkit.CreateMaterial(model, b); // create mesh primitive MeshPrimitive primitive = rmesh.CreatePrimitive(); if (indexedTriangulation.Colors != null && indexedTriangulation.Colors.Any()) { primitive = primitive.WithVertexAccessor("POSITION", indexedTriangulation.Positions); primitive = primitive.WithVertexAccessor("COLOR_0", indexedTriangulation.Colors); } else { primitive = primitive.WithVertexAccessor("POSITION", indexedTriangulation.Positions); } var normals = _meshService.ComputeMeshNormals(indexedTriangulation.Positions, indexedTriangulation.Indices); primitive = primitive.WithVertexAccessor("NORMAL", normals.ToList()); primitive = primitive.WithIndicesAccessor(PrimitiveType.TRIANGLES, indexedTriangulation.Indices); (Vector3 Min, Vector3 Max)coordBounds = CalculateBounds(indexedTriangulation.Positions); var coordSets = indexedTriangulation.Positions.Select(pos => new Vector2( MathHelper.Map(coordBounds.Min.X, coordBounds.Max.X, 0, 1, pos.X, true) , MathHelper.Map(coordBounds.Min.Z, coordBounds.Max.Z, 0, 1, pos.Z, true) )); primitive = primitive .WithVertexAccessor("TEXCOORD_0", coordSets.ToList()); primitive = primitive.WithMaterial(material); return(model); }
public ModelRoot CreateTerrainMesh(HeightMap heightMap, GenOptions options = GenOptions.None, Matrix4x4 vectorTransform = default, bool doubleSided = true, float meshReduceFactor = 0.5f) { Triangulation triangulation = default; if (options.HasFlag(GenOptions.BoxedBaseElevation0)) { triangulation = _meshService.GenerateTriangleMesh_Boxed(heightMap, BoxBaseThickness.FixedElevation, 0); } else if (options.HasFlag(GenOptions.BoxedBaseElevationMin)) { triangulation = _meshService.GenerateTriangleMesh_Boxed(heightMap, BoxBaseThickness.FromMinimumPoint, 5); } else { triangulation = _meshService.TriangulateHeightMap(heightMap); } triangulation = _meshReducer.Decimate(triangulation, meshReduceFactor); // create a basic scene var model = CreateNewModel(); var rnode = model.LogicalScenes.First()?.FindNode(n => n.Name == TERRAIN_NODE_NAME); var rmesh = rnode.Mesh = model.CreateMesh(TERRAIN_MESH_NAME); var material = model.CreateMaterial(string.Concat(TERRAIN_MESH_NAME, "Material")) .WithPBRMetallicRoughness() .WithDoubleSide(doubleSided); var indexedTriangulation = new IndexedTriangulation(triangulation, vectorTransform); // create mesh primitive var primitive = rmesh.CreatePrimitive() .WithVertexAccessor("POSITION", indexedTriangulation.Positions); if (options.HasFlag(GenOptions.Normals)) { var normals = _meshService.ComputeMeshNormals(indexedTriangulation.Positions, indexedTriangulation.Indices); primitive = primitive.WithVertexAccessor("NORMAL", normals.ToList()); } primitive = primitive.WithIndicesAccessor(PrimitiveType.TRIANGLES, indexedTriangulation.Indices) .WithMaterial(material); return(model); }
public ModelRoot AddTerrainMesh(ModelRoot model, Triangulation triangulation, PBRTexture textures) { // create a basic scene model = model ?? CreateNewModel(); var rnode = model.LogicalScenes.First()?.FindNode(n => n.Name == TERRAIN_NODE_NAME); var rmesh = rnode.Mesh = FindOrCreateMesh(model, TERRAIN_MESH_NAME); var material = model.CreateMaterial("Default") .WithPBRMetallicRoughness(Vector4.One, textures?.BaseColorTexture?.FilePath, null, 0, 1) .WithDoubleSide(true); if (textures != null && textures.NormalTexture != null) { material.WithChannelTexture("NORMAL", 0, textures.NormalTexture.FilePath); } var indexedTriangulation = new IndexedTriangulation(triangulation); var normals = _meshService.ComputeNormals(indexedTriangulation.Positions, indexedTriangulation.Indices); // create mesh primitive var primitive = rmesh.CreatePrimitive() .WithVertexAccessor("POSITION", indexedTriangulation.Positions) .WithVertexAccessor("NORMAL", normals.ToList()) .WithIndicesAccessor(PrimitiveType.TRIANGLES, indexedTriangulation.Indices); if (textures != null && textures.TextureCoordSets == null) { (Vector3 Min, Vector3 Max)coordBounds = CalculateBounds(indexedTriangulation.Positions); textures.TextureCoordSets = indexedTriangulation.Positions.Select(pos => new Vector2( MathHelper.Map(coordBounds.Min.X, coordBounds.Max.X, 0, 1, pos.X, true) , MathHelper.Map(coordBounds.Min.Z, coordBounds.Max.Z, 0, 1, pos.Z, true) )); primitive = primitive .WithVertexAccessor("TEXCOORD_0", textures.TextureCoordSets.ToList()); } primitive = primitive.WithMaterial(material); return(model); }