UE4 通过HTTP 接受JPG并动态 构建 UTexture2D 简单例子

void UChildBaseUserWidget::setTextureFromLoadImg(FHttpRequestPtr _request, FHttpResponsePtr _response, bool bWasSuccessful)
{
    if ( !bWasSuccessful && !_response.IsValid() )
    {
        UE_LOG(LogTemp, Warning, TEXT(" !bWasSuccessful && !_response.IsValid() "));
        return;
    }

    IImageWrapperModule& temp_img_module = FModuleManager::LoadModuleChecked<IImageWrapperModule>(FName("ImageWrapper"));    
    IImageWrapperPtr temp_imgWrapper = temp_img_module.CreateImageWrapper(EImageFormat::JPEG);

    TArray<uint8> temp_fileData = _response->GetContent();
    
    
    if ( !temp_imgWrapper.IsValid() || 
         !temp_imgWrapper->SetCompressed( temp_fileData.GetData(), temp_fileData.Num()))
    {
        GEngine->AddOnScreenDebugMessage(-1, 5.0f, FColor::Red, FString::FromInt(temp_fileData.Num()));
        UE_LOG(LogTemp, Warning, TEXT("ImageWrapper can‘t Set Compressed or ImageWrapper is InValid"));
        return;
    }

    const TArray<uint8>* temp_unCompressedRGBA = NULL;

    if ( !temp_imgWrapper->GetRaw(ERGBFormat::RGBA, 8, temp_unCompressedRGBA ))
    {
        UE_LOG(LogTemp, Warning, TEXT("can‘t get Raw temp_unCompressedRGBA"));
        return;
    }

    m_texture = UTexture2D::CreateTransient(temp_imgWrapper->GetWidth(), temp_imgWrapper->GetHeight());
    
    auto temp_dataPtr = static_cast<uint8*>(m_texture->PlatformData->Mips[0].BulkData.Lock( LOCK_READ_WRITE ));

    FColor temp_color;
    uint8 temp_colorPoint;
    TArray<FColor> temp_arr_color;
    for (int i = 0; i < temp_unCompressedRGBA->Num(); i++)
    {
        temp_colorPoint = (*temp_unCompressedRGBA)[i];
        temp_color.R = temp_colorPoint;
        i++;
        temp_colorPoint = (*temp_unCompressedRGBA)[i];
        temp_color.G = temp_colorPoint;
        i++;
        temp_colorPoint = (*temp_unCompressedRGBA)[i];
        temp_color.B = temp_colorPoint;
        i++;
        temp_colorPoint = (*temp_unCompressedRGBA)[i];
        temp_color.A = temp_colorPoint;
        temp_arr_color.Add(temp_color);
    }

    uint8* DestPtr = NULL;
    const FColor* SrcPtr = NULL;
    for (int32 y = 0; y < temp_imgWrapper->GetHeight(); y++)
    {
        DestPtr = &temp_dataPtr[(temp_imgWrapper->GetHeight() - 1 - y) * temp_imgWrapper->GetWidth() * sizeof(FColor)];
        SrcPtr = const_cast<FColor*>(&temp_arr_color[(temp_imgWrapper->GetHeight() - 1 - y) * temp_imgWrapper->GetWidth()]);
        for (int32 x = 0; x < temp_imgWrapper->GetWidth(); x++)
        {
            *DestPtr++ = SrcPtr->R;
            *DestPtr++ = SrcPtr->G;
            *DestPtr++ = SrcPtr->B;
            if ( true )
            {
                *DestPtr++ = SrcPtr->A;
            }
            else
            {
                *DestPtr++ = 0xFF;
            }
            SrcPtr++;
        }
    }

    m_texture->PlatformData->Mips[0].BulkData.Unlock();

    m_texture->UpdateResourceW();
}

 

 

参考:https://answers.unrealengine.com/questions/235086/texture-2d-shows-wrong-colors-from-jpeg-on-html5-p.html

文章来自:http://www.cnblogs.com/linqing/p/5084607.html
© 2021 jiaocheng.bubufx.com  联系我们
ICP备案:鲁ICP备09046678号-3