【问题标题】:TGALoader is loading image mirrored in vertical axisTGALoader 正在加载垂直轴镜像的图像
【发布时间】:2016-03-08 13:29:24
【问题描述】:

使用 Targa 图像作为纹理加载带有 .mtl 材质的 .obj 模型,图像被镜像加载。 可以在下面的image 中注意到,其中左侧是 .tga,右侧是 .png。

在图像编辑器中打开两张图像,它们显示为相同。而且,只要正确加载 .png 图像,问题就出在我用于 .tga 的加载程序中。

我正在使用TGALoader 类来加载纹理。该问题似乎与来自 TGALoader.js 的函数 getTgaRGBA 中的以下代码有关:

TGA_ORIGIN_MASK = 0x30,
TGA_ORIGIN_SHIFT = 0x04,
TGA_ORIGIN_BL = 0x00,
TGA_ORIGIN_BR = 0x01,
TGA_ORIGIN_UL = 0x02,
TGA_ORIGIN_UR = 0x03;

function getTgaRGBA( width, height, image, palette ) {
    var x_start,
        y_start,
        x_step,
        y_step,
        x_end,
        y_end,
        data = new Uint8Array( width * height * 4 );
    //switch ( 0x02 ) {
    switch ( ( header.flags & TGA_ORIGIN_MASK ) >> TGA_ORIGIN_SHIFT  ) {
        default:
        case TGA_ORIGIN_UL:
            x_start = 0;
            x_step = 1;
            x_end = width;
            y_start = 0;
            y_step = 1;
            y_end = height;
            break;

        case TGA_ORIGIN_BL:
            x_start = 0;
            x_step = 1;
            x_end = width;
            y_start = height - 1;
            y_step = - 1;
            y_end = - 1;
            break;

        case TGA_ORIGIN_UR:
            x_start = width - 1;
            x_step = - 1;
            x_end = - 1;
            y_start = 0;
            y_step = 1;
            y_end = height;
            break;

        case TGA_ORIGIN_BR:
            x_start = width - 1;
            x_step = - 1;
            x_end = - 1;
            y_start = height - 1;
            y_step = - 1;
            y_end = - 1;
            break;

    } 

如果 switch 的结果是 0x02,图像将被正确加载,而不是镜像。我用不同的 .tga 图像进行了测试,它们都产生了相同的镜像。

我不太明白switch的参数是什么意思...大家能帮我弄清楚吗,我该如何解决我的问题?

【问题讨论】:

    标签: javascript image three.js tga


    【解决方案1】:

    你的纹理被翻转了。使用这种模式:

    var loader = new THREE.TGALoader();
    
    var texture = loader.load( 'myTexture.tga' );
    
    texture.flipY = true;
    

    three.js r.74

    【讨论】:

    • 是的,但是 .png 版本怎么会正确加载,而 .tga 不是呢?我认为这是 TGALoader 中的一些问题。有没有一种方法可以自动检查图像是否翻转?这样我总是在使用之前纠正它?
    • 因为.tga文件被视为THREE.DataTexture,默认不翻转Y。研究源代码,或使用调试器单步调试。
    • 在 r.76dev 的 TGALoader 中修复的错误。不再需要设置flipY
    猜你喜欢
    • 2016-03-07
    • 2018-04-10
    • 1970-01-01
    • 2015-12-28
    • 1970-01-01
    • 1970-01-01
    • 2014-05-23
    • 2014-06-18
    • 1970-01-01
    相关资源
    最近更新 更多