It looks good in the STL, but the gltf is not showing it as solid. I have not tried sending it off to be 3D printed.
internal class CreateFrustum
{
private string _outputPath;
public CreateFrustum(string outputPath)
{
_outputPath = outputPath;
}
public void CreateAndSave()
{
Scene scene = new Scene();
var mesh = GenerateFrustumMesh(45, 45, 10, 20);
Node frustum = scene.RootNode.CreateChildNode("frustum", mesh);
scene.Save(_outputPath, FileFormat.STLASCII);
scene.Save(Utilities.GetOutputPath(_outputPath, ".gltf"), Utilities.GetGLTFSaveOptions());
}
// <summary>
/// Generate a Frustum facing +Z axis
/// </summary>
/// <param name="fovX">Field of view in X axis, measured in degrees</param>
/// <param name="fovY">Field of view in Y axis, measured in degrees</param>
/// <param name="near">Distance from eye to near plane</param>
/// <param name="far">Distance from eye to far plane</param>
/// <returns></returns>
private Mesh GenerateFrustumMesh(float fovX, float fovY, float near, float far)
{
var cosX = Math.Cos(fovX / 2 / 180 * Math.PI);
var cosY = Math.Cos(fovY / 2 / 180 * Math.PI);
var nx = near / cosX;
var ny = near / cosY;
var fx = far / cosX;
var fy = far / cosY;
var mesh = new Mesh();
//control points of near plane corners
mesh.ControlPoints.Add(new Vector4(nx, ny, near));
mesh.ControlPoints.Add(new Vector4(-nx, ny, near));
mesh.ControlPoints.Add(new Vector4(-nx, -ny, near));
mesh.ControlPoints.Add(new Vector4(nx, -ny, near));
//control points of far plane corners
mesh.ControlPoints.Add(new Vector4(fx, fy, far));
mesh.ControlPoints.Add(new Vector4(-fx, fy, far));
mesh.ControlPoints.Add(new Vector4(-fx, -fy, far));
mesh.ControlPoints.Add(new Vector4(fx, -fy, far));
mesh.CreatePolygon(0, 1, 2, 3);//near plane
mesh.CreatePolygon(4, 5, 6, 7);//far plane
mesh.CreatePolygon(0, 4, 5, 1);//top plane
mesh.CreatePolygon(3, 2, 6, 7);//bottom plane
mesh.CreatePolygon(1, 5, 6, 2);//left plane
mesh.CreatePolygon(0, 3, 7, 4);//right plane
return mesh;
}
private void FrustumTest()
{
var f = GenerateFrustumMesh(45, 45, 10, 20); //Generate a frustum solid geometry with field of view 45degree on both x/y axis and near plane is 10, far plane is 20
new Scene(f).Save("frustum.stl");
}
}
internal static GltfSaveOptions GetGLTFSaveOptions()
{
GltfSaveOptions opt = new GltfSaveOptions(FileContentType.ASCII);
opt.EmbedAssets = true;
opt.UseCommonMaterials = true;
opt.BufferFile = "gltfBuffer.bin";
return opt;
}