public ModelRoot CreateTerrainMesh(HeightMap heightMap, GenOptions options = GenOptions.None, Matrix4x4 vectorTransform = default, bool doubleSided = true) { 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); } // 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("Default") .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); }
/// <summary> /// Generate a triangle mesh from supplied height map, triangulating and optionaly mapping UVs /// </summary> /// <param name="heightMap"></param> /// <param name="colors"></param> /// <param name="texture">Texture path relative from the model</param> /// <returns></returns> public MeshPrimitive GenerateTriangleMesh(HeightMap heightMap, IEnumerable <Vector4> colors = null, PBRTexture texture = null) { Triangulation triangulation = _meshService.TriangulateHeightMap(heightMap); return(GenerateTriangleMesh(triangulation, colors, texture)); }