Flutter中是否可以直接将PNG图片转换为GLB格式?

问题描述 投票:0回答:1

我正在探索直接在 Flutter 应用程序中将 PNG 图像转换为 GLB 格式的选项。我遇到过一些在线工具,例如 Aspose 3D Conversion 和 ImageToSTL,声称可以执行此转换。

但是,我不确定是否可以直接在 Flutter 应用程序中实现这种转换。如果直接转换不可行,我有兴趣学习如何将使用 PNG 图像创建的 3D 模型转换为 GLB 格式。

有人可以指导是否可以在 Flutter 中直接将 PNG 图像转换为 GLB 格式吗?如果没有,我将不胜感激有关如何使用库或包执行此转换的任何见解。另外,如果有任何现有的解决方案或包可用,我想知道如何将它们集成到我的 Flutter 项目中。

任何正确方向的帮助或指示将不胜感激。谢谢你。

flutter dart png file-conversion 3d-modelling
1个回答
0
投票

不幸的是,无法在 flutter 应用程序中使用 Aspose.3D。

但是可以使用 Aspose.3D 编写基于 ASP.NET 的 Web 服务来提供此功能。

这是示例代码:

var imageFile = @"input.png";
var glbFile = @"output.glb";

// Read image for retrieving its size
var bytes = System.IO.File.ReadAllBytes(imageFile);
using var image = System.Drawing.Image.FromStream(new MemoryStream(bytes));

// Calculate the plane's size with the same ratio used in the image
var planeWidth = 20.0f;
var planeHeight = planeWidth / image.Height * image.Width;

// Create a plane with calculated size
var p = new Aspose.ThreeD.Entities.Plane()
{
    Width = planeWidth,
    Length = planeHeight
};

// Create a material instance with texture
var mat = new LambertMaterial();
var tex = new Texture()
{
    // Read image file into memory so we can save it to GLB later
    Content = bytes,
    FileName = Path.GetFileName(imageFile)
};
mat.SetTexture(Material.MapDiffuse, tex);

// Create a scene from the plane and attach the material to the plane's node
var scene = new Scene();
scene.RootNode.CreateChildNode(p).Material = mat;

// Save scene into a GLB file and embed the image into the GLB file
var opt = new GltfSaveOptions(FileContentType.Binary)
{
    EmbedAssets = true
};
scene.Save(glbFile, opt);
© www.soinside.com 2019 - 2024. All rights reserved.