리소판 C#에 PNG 이미지

PNG 형식은 투명한 그래픽을 포함할 수 있어 널리 사용됩니다. 반면에 lithophane은 매우 얇은 재료로 에칭되거나 몰딩된 작품으로 이러한 모델 뒤에 광원을 배치하면 볼 수 있습니다. 이 문서에서는 C#에서 PNG 이미지를 리소판으로 변환하는 방법을 다룹니다.

PNG 이미지 to Lithophane Converter – C# API 설치

Aspose.3D for .NET API를 사용하여 다양한 3D 모델 및 장면을 작업할 수 있습니다. New Releases 페이지에서 참조 DLL 파일을 다운로드하거나 다음 NuGet 설치 명령을 사용하여 API를 쉽게 구성할 수 있습니다.

PM> Install-Package Aspose.3D

C#에서 PNG 이미지를 리소판으로 변환

다음 단계에 따라 PNG 이미지 파일을 Lithophane 형식으로 변환할 수 있습니다.

  • 입력 PNG 이미지를 로드합니다.
  • Mesh 개체에 대한 계산 작업을 수행합니다.
  • Save 방법으로 3D 장면을 생성하고 객체를 저장합니다.

아래 코드 조각은 C#에서 프로그래밍 방식으로 PNG 이미지를 Lithophane으로 변환하는 방법을 설명합니다.

string file = "template.png";
string output = "file.fbx";

// 몇 가지 새 매개변수 생성
Aspose.ThreeD.Render.TextureData td = Aspose.ThreeD.Render.TextureData.FromFile(file);
const float nozzleSize = 0.9f;
const float layerHeight = 0.2f;
var grayscale = ToGrayscale(td);
const float width = 120.0f;
float height = width / td.Width * td.Height;
float thickness = 10.0f;
float layers = thickness / layerHeight;
int widthSegs = (int)Math.Floor(width / nozzleSize);
int heightSegs = (int)Math.Floor(height / nozzleSize);

// 메쉬 개체에 대한 계산 작업 수행
Aspose.ThreeD.Entities.Mesh mesh = new Aspose.ThreeD.Entities.Mesh();
for (int y = 0; y < heightSegs; y++)
{
    float dy = (float)y / heightSegs;
    for (int x = 0; x < widthSegs; x++)
    {
        float dx = (float)x / widthSegs;
        float gray = Sample(grayscale, td.Width, td.Height, dx, dy);
        float v = (1 - gray) * thickness;
        mesh.ControlPoints.Add(new Aspose.ThreeD.Utilities.Vector4(dx * width, dy * height, v));
    }
}

for (int y = 0; y < heightSegs - 1; y++)
{
    int row = (y * heightSegs);
    int ptr = row;
    for (int x = 0; x < widthSegs - 1; x++)
    {
        mesh.CreatePolygon(ptr, ptr + widthSegs, ptr + 1);
        mesh.CreatePolygon(ptr + 1, ptr + widthSegs, ptr + widthSegs + 1);
        ptr++;
    }
}

// 3D 장면 생성 및 개체 저장
Aspose.ThreeD.Scene scene = new Aspose.ThreeD.Scene(mesh);
scene.Save(output, Aspose.ThreeD.FileFormat.FBX7400ASCII);

// 호출할 샘플 메서드
static float Sample(float[,] data, int w, int h, float x, float y)
{
    return data[(int)(x * w), (int)(y * h)];
}

// 호출할 ToGrayscale 메서드
static float[,] ToGrayscale(Aspose.ThreeD.Render.TextureData td)
{
    var ret = new float[td.Width, td.Height];
    var stride = td.Stride;
    var data = td.Data;
    var bytesPerPixel = td.BytesPerPixel;
    for (int y = 0; y < td.Height; y++)
    {
        int ptr = y * stride;
        for (int x = 0; x < td.Width; x++)
        {
            var v = (data[ptr] * 0.21f + data[ptr + 1] * 0.72f + data[ptr + 2] * 0.07f) / 255.0f;
            ret[x, y] = v;
            ptr += bytesPerPixel;
        }
    }
    return ret;
}

무료 임시 라이센스 받기

제한 없이 API 평가를 위해 무료 임시 라이선스를 요청할 수 있습니다.

온라인 데모

이 API를 사용하여 개발된 PNG Image to Lithophane Converter 웹 앱을 사용해 보십시오.

결론

이 기사에서는 PNG 이미지를 리소판으로 변환하는 방법을 살펴보았습니다. 또한 문서 공간에서 API의 다른 기능을 배울 수 있습니다. 질문이나 질문이 있는 경우 포럼에 글을 남겨주세요.

또한보십시오

C#에서 USDZ를 GLB 또는 GLTF 파일로 변환