Restore panoramic skybox path for skybox integration
This commit is contained in:
@@ -7,8 +7,6 @@
|
||||
|
||||
#include <XCEngine/Components/CameraComponent.h>
|
||||
#include <XCEngine/Components/GameObject.h>
|
||||
#include <XCEngine/Components/MeshFilterComponent.h>
|
||||
#include <XCEngine/Components/MeshRendererComponent.h>
|
||||
#include <XCEngine/Core/Asset/IResource.h>
|
||||
#include <XCEngine/Core/Math/Color.h>
|
||||
#include <XCEngine/Core/Math/Quaternion.h>
|
||||
@@ -19,7 +17,6 @@
|
||||
#include <XCEngine/Rendering/RenderSurface.h>
|
||||
#include <XCEngine/Resources/BuiltinResources.h>
|
||||
#include <XCEngine/Resources/Material/Material.h>
|
||||
#include <XCEngine/Resources/Mesh/Mesh.h>
|
||||
#include <XCEngine/Resources/Shader/Shader.h>
|
||||
#include <XCEngine/Resources/Texture/Texture.h>
|
||||
#include <XCEngine/Resources/Texture/TextureImportSettings.h>
|
||||
@@ -30,7 +27,6 @@
|
||||
#include "../../../RHI/integration/fixtures/RHIIntegrationFixture.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <cmath>
|
||||
#include <filesystem>
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
@@ -49,8 +45,6 @@ constexpr const char* kOpenGLScreenshot = "skybox_scene_opengl.ppm";
|
||||
constexpr const char* kVulkanScreenshot = "skybox_scene_vulkan.ppm";
|
||||
constexpr uint32_t kFrameWidth = 1280;
|
||||
constexpr uint32_t kFrameHeight = 720;
|
||||
constexpr uint32_t kSkyboxFaceSize = 256;
|
||||
constexpr float kPi = 3.14159265358979323846f;
|
||||
|
||||
std::filesystem::path GetExecutableDirectory() {
|
||||
char exePath[MAX_PATH] = {};
|
||||
@@ -66,57 +60,7 @@ std::filesystem::path ResolveRuntimePath(const char* relativePath) {
|
||||
return GetExecutableDirectory() / relativePath;
|
||||
}
|
||||
|
||||
Vector3 ComputeCubemapDirection(uint32_t faceIndex, float s, float t) {
|
||||
switch (faceIndex) {
|
||||
case 0:
|
||||
return Vector3(1.0f, -t, -s).Normalized();
|
||||
case 1:
|
||||
return Vector3(-1.0f, -t, s).Normalized();
|
||||
case 2:
|
||||
return Vector3(s, 1.0f, t).Normalized();
|
||||
case 3:
|
||||
return Vector3(s, -1.0f, -t).Normalized();
|
||||
case 4:
|
||||
return Vector3(s, -t, 1.0f).Normalized();
|
||||
case 5:
|
||||
default:
|
||||
return Vector3(-s, -t, -1.0f).Normalized();
|
||||
}
|
||||
}
|
||||
|
||||
void SamplePanoramaNearest(
|
||||
const Texture& panorama,
|
||||
float u,
|
||||
float v,
|
||||
unsigned char outRgba[4]) {
|
||||
const uint32_t width = panorama.GetWidth();
|
||||
const uint32_t height = panorama.GetHeight();
|
||||
const auto* pixels = static_cast<const unsigned char*>(panorama.GetPixelData());
|
||||
if (pixels == nullptr || width == 0 || height == 0) {
|
||||
outRgba[0] = 255;
|
||||
outRgba[1] = 255;
|
||||
outRgba[2] = 255;
|
||||
outRgba[3] = 255;
|
||||
return;
|
||||
}
|
||||
|
||||
u = u - std::floor(u);
|
||||
v = std::clamp(v, 0.0f, 1.0f);
|
||||
|
||||
const uint32_t x = std::min<uint32_t>(
|
||||
static_cast<uint32_t>(u * static_cast<float>(width - 1) + 0.5f),
|
||||
width - 1);
|
||||
const uint32_t y = std::min<uint32_t>(
|
||||
static_cast<uint32_t>(v * static_cast<float>(height - 1) + 0.5f),
|
||||
height - 1);
|
||||
const size_t pixelIndex = (static_cast<size_t>(y) * static_cast<size_t>(width) + x) * 4u;
|
||||
outRgba[0] = pixels[pixelIndex + 0];
|
||||
outRgba[1] = pixels[pixelIndex + 1];
|
||||
outRgba[2] = pixels[pixelIndex + 2];
|
||||
outRgba[3] = pixels[pixelIndex + 3];
|
||||
}
|
||||
|
||||
Texture* CreateCubemapSkyboxTexture(const std::filesystem::path& panoramaPath) {
|
||||
Texture* LoadPanoramicSkyboxTexture(const std::filesystem::path& panoramaPath) {
|
||||
TextureLoader loader;
|
||||
TextureImportSettings settings;
|
||||
settings.SetSRGB(true);
|
||||
@@ -126,57 +70,11 @@ Texture* CreateCubemapSkyboxTexture(const std::filesystem::path& panoramaPath) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
std::unique_ptr<Texture> panorama(static_cast<Texture*>(loadResult.resource));
|
||||
if (!panorama || panorama->GetPixelData() == nullptr || panorama->GetWidth() == 0 || panorama->GetHeight() == 0) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
auto* texture = new Texture();
|
||||
IResource::ConstructParams params = {};
|
||||
params.name = "SkyboxCubemapTexture";
|
||||
params.path = "Tests/Rendering/SkyboxScene/skybox_cubemap.texture";
|
||||
params.guid = ResourceGUID::Generate(params.path);
|
||||
texture->Initialize(params);
|
||||
|
||||
std::vector<unsigned char> pixels(
|
||||
static_cast<size_t>(kSkyboxFaceSize) * static_cast<size_t>(kSkyboxFaceSize) * 4u * 6u,
|
||||
255u);
|
||||
for (uint32_t faceIndex = 0; faceIndex < 6u; ++faceIndex) {
|
||||
for (uint32_t y = 0; y < kSkyboxFaceSize; ++y) {
|
||||
for (uint32_t x = 0; x < kSkyboxFaceSize; ++x) {
|
||||
const float s = (2.0f * (static_cast<float>(x) + 0.5f) / static_cast<float>(kSkyboxFaceSize)) - 1.0f;
|
||||
const float t = (2.0f * (static_cast<float>(y) + 0.5f) / static_cast<float>(kSkyboxFaceSize)) - 1.0f;
|
||||
const Vector3 direction = ComputeCubemapDirection(faceIndex, s, t);
|
||||
|
||||
const float u = std::atan2(direction.z, direction.x) / (2.0f * kPi) + 0.5f;
|
||||
const float v = std::acos(std::clamp(direction.y, -1.0f, 1.0f)) / kPi;
|
||||
|
||||
unsigned char rgba[4] = {};
|
||||
SamplePanoramaNearest(*panorama, u, v, rgba);
|
||||
|
||||
const size_t pixelIndex =
|
||||
((static_cast<size_t>(faceIndex) * static_cast<size_t>(kSkyboxFaceSize) *
|
||||
static_cast<size_t>(kSkyboxFaceSize)) +
|
||||
(static_cast<size_t>(y) * static_cast<size_t>(kSkyboxFaceSize) + x)) * 4u;
|
||||
pixels[pixelIndex + 0] = rgba[0];
|
||||
pixels[pixelIndex + 1] = rgba[1];
|
||||
pixels[pixelIndex + 2] = rgba[2];
|
||||
pixels[pixelIndex + 3] = 255u;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const size_t pixelDataSize = pixels.size();
|
||||
if (!texture->Create(
|
||||
kSkyboxFaceSize,
|
||||
kSkyboxFaceSize,
|
||||
1,
|
||||
1,
|
||||
XCEngine::Resources::TextureType::TextureCube,
|
||||
panorama->GetFormat(),
|
||||
pixels.data(),
|
||||
pixelDataSize,
|
||||
6)) {
|
||||
auto* texture = static_cast<Texture*>(loadResult.resource);
|
||||
if (texture == nullptr ||
|
||||
texture->GetPixelData() == nullptr ||
|
||||
texture->GetWidth() == 0 ||
|
||||
texture->GetHeight() == 0) {
|
||||
delete texture;
|
||||
return nullptr;
|
||||
}
|
||||
@@ -184,101 +82,21 @@ Texture* CreateCubemapSkyboxTexture(const std::filesystem::path& panoramaPath) {
|
||||
return texture;
|
||||
}
|
||||
|
||||
Mesh* CreateQuadMesh() {
|
||||
auto* mesh = new Mesh();
|
||||
IResource::ConstructParams params = {};
|
||||
params.name = "SkyboxSceneQuad";
|
||||
params.path = "Tests/Rendering/SkyboxScene/quad.mesh";
|
||||
params.guid = ResourceGUID::Generate(params.path);
|
||||
mesh->Initialize(params);
|
||||
|
||||
StaticMeshVertex vertices[4] = {};
|
||||
vertices[0].position = Vector3(-1.0f, -1.0f, 0.0f);
|
||||
vertices[0].normal = Vector3::Back();
|
||||
vertices[0].uv0 = Vector2(0.0f, 1.0f);
|
||||
vertices[1].position = Vector3(-1.0f, 1.0f, 0.0f);
|
||||
vertices[1].normal = Vector3::Back();
|
||||
vertices[1].uv0 = Vector2(0.0f, 0.0f);
|
||||
vertices[2].position = Vector3(1.0f, -1.0f, 0.0f);
|
||||
vertices[2].normal = Vector3::Back();
|
||||
vertices[2].uv0 = Vector2(1.0f, 1.0f);
|
||||
vertices[3].position = Vector3(1.0f, 1.0f, 0.0f);
|
||||
vertices[3].normal = Vector3::Back();
|
||||
vertices[3].uv0 = Vector2(1.0f, 0.0f);
|
||||
|
||||
const uint32_t indices[6] = { 0, 1, 2, 2, 1, 3 };
|
||||
mesh->SetVertexData(
|
||||
vertices,
|
||||
sizeof(vertices),
|
||||
4,
|
||||
sizeof(StaticMeshVertex),
|
||||
VertexAttribute::Position | VertexAttribute::Normal | VertexAttribute::UV0);
|
||||
mesh->SetIndexData(indices, sizeof(indices), 6, true);
|
||||
|
||||
MeshSection section = {};
|
||||
section.baseVertex = 0;
|
||||
section.vertexCount = 4;
|
||||
section.startIndex = 0;
|
||||
section.indexCount = 6;
|
||||
section.materialID = 0;
|
||||
mesh->AddSection(section);
|
||||
return mesh;
|
||||
}
|
||||
|
||||
Material* CreateMaterial(
|
||||
const char* name,
|
||||
const char* path,
|
||||
const Vector4& baseColor,
|
||||
MaterialRenderQueue renderQueue,
|
||||
const MaterialRenderState& renderState) {
|
||||
auto* material = new Material();
|
||||
IResource::ConstructParams params = {};
|
||||
params.name = name;
|
||||
params.path = path;
|
||||
params.guid = ResourceGUID::Generate(params.path);
|
||||
material->Initialize(params);
|
||||
material->SetShader(ResourceManager::Get().Load<Shader>(GetBuiltinUnlitShaderPath()));
|
||||
material->SetRenderQueue(renderQueue);
|
||||
material->SetRenderState(renderState);
|
||||
material->SetFloat4("_BaseColor", baseColor);
|
||||
return material;
|
||||
}
|
||||
|
||||
Material* CreateSkyboxMaterial(Texture* texture) {
|
||||
auto* material = new Material();
|
||||
IResource::ConstructParams params = {};
|
||||
params.name = "SkyboxCubemapMaterial";
|
||||
params.path = "Tests/Rendering/SkyboxScene/skybox_cubemap.mat";
|
||||
params.name = "SkyboxPanoramicMaterial";
|
||||
params.path = "Tests/Rendering/SkyboxScene/skybox_panorama.mat";
|
||||
params.guid = ResourceGUID::Generate(params.path);
|
||||
material->Initialize(params);
|
||||
material->SetShader(ResourceManager::Get().Load<Shader>(GetBuiltinSkyboxShaderPath()));
|
||||
material->SetTexture("_Tex", ResourceHandle<Texture>(texture));
|
||||
material->SetTexture("_MainTex", ResourceHandle<Texture>(texture));
|
||||
material->SetFloat4("_Tint", Vector4(1.0f, 1.0f, 1.0f, 1.0f));
|
||||
material->SetFloat("_Exposure", 1.0f);
|
||||
material->SetFloat("_Rotation", 0.0f);
|
||||
material->SetFloat("_Rotation", -90.0f);
|
||||
return material;
|
||||
}
|
||||
|
||||
GameObject* CreateRenderableObject(
|
||||
Scene& scene,
|
||||
const char* name,
|
||||
Mesh* mesh,
|
||||
Material* material,
|
||||
const Vector3& position,
|
||||
const Vector3& scale,
|
||||
const Quaternion& rotation = Quaternion::Identity()) {
|
||||
GameObject* object = scene.CreateGameObject(name);
|
||||
object->GetTransform()->SetLocalPosition(position);
|
||||
object->GetTransform()->SetLocalScale(scale);
|
||||
object->GetTransform()->SetLocalRotation(rotation);
|
||||
|
||||
auto* meshFilter = object->AddComponent<MeshFilterComponent>();
|
||||
auto* meshRenderer = object->AddComponent<MeshRendererComponent>();
|
||||
meshFilter->SetMesh(ResourceHandle<Mesh>(mesh));
|
||||
meshRenderer->SetMaterial(0, ResourceHandle<Material>(material));
|
||||
return object;
|
||||
}
|
||||
|
||||
const char* GetScreenshotFilename(RHIType backendType) {
|
||||
switch (backendType) {
|
||||
case RHIType::D3D12:
|
||||
@@ -311,7 +129,6 @@ private:
|
||||
std::vector<RHIResourceView*> mBackBufferViews;
|
||||
std::vector<Material*> mMaterials;
|
||||
std::vector<Texture*> mTextures;
|
||||
Mesh* mMesh = nullptr;
|
||||
RHITexture* mDepthTexture = nullptr;
|
||||
RHIResourceView* mDepthView = nullptr;
|
||||
};
|
||||
@@ -321,8 +138,6 @@ void SkyboxSceneTest::SetUp() {
|
||||
|
||||
mSceneRenderer = std::make_unique<SceneRenderer>();
|
||||
mScene = std::make_unique<Scene>("SkyboxScene");
|
||||
mMesh = CreateQuadMesh();
|
||||
ASSERT_NE(mMesh, nullptr);
|
||||
|
||||
BuildScene();
|
||||
|
||||
@@ -385,10 +200,6 @@ void SkyboxSceneTest::TearDown() {
|
||||
delete texture;
|
||||
}
|
||||
mTextures.clear();
|
||||
|
||||
delete mMesh;
|
||||
mMesh = nullptr;
|
||||
|
||||
RHIIntegrationFixture::TearDown();
|
||||
}
|
||||
|
||||
@@ -399,63 +210,19 @@ void SkyboxSceneTest::BuildScene() {
|
||||
auto* camera = cameraObject->AddComponent<CameraComponent>();
|
||||
camera->SetPrimary(true);
|
||||
camera->SetProjectionType(CameraProjectionType::Perspective);
|
||||
camera->SetFieldOfView(46.0f);
|
||||
camera->SetFieldOfView(90.0f);
|
||||
camera->SetNearClipPlane(0.1f);
|
||||
camera->SetFarClipPlane(20.0f);
|
||||
camera->SetClearColor(XCEngine::Math::Color(0.01f, 0.01f, 0.01f, 1.0f));
|
||||
camera->SetSkyboxEnabled(true);
|
||||
Texture* skyboxTexture = CreateCubemapSkyboxTexture(ResolveRuntimePath("Res/skyboxes/sky.png"));
|
||||
Texture* skyboxTexture = LoadPanoramicSkyboxTexture(ResolveRuntimePath("Res/skyboxes/sky.png"));
|
||||
ASSERT_NE(skyboxTexture, nullptr);
|
||||
Material* skyboxMaterial = CreateSkyboxMaterial(skyboxTexture);
|
||||
ASSERT_NE(skyboxMaterial, nullptr);
|
||||
camera->SetSkyboxMaterial(skyboxMaterial);
|
||||
|
||||
MaterialRenderState opaqueState = {};
|
||||
opaqueState.cullMode = MaterialCullMode::Back;
|
||||
|
||||
MaterialRenderState transparentState = {};
|
||||
transparentState.blendEnable = true;
|
||||
transparentState.srcBlend = MaterialBlendFactor::SrcAlpha;
|
||||
transparentState.dstBlend = MaterialBlendFactor::InvSrcAlpha;
|
||||
transparentState.srcBlendAlpha = MaterialBlendFactor::One;
|
||||
transparentState.dstBlendAlpha = MaterialBlendFactor::Zero;
|
||||
transparentState.depthTestEnable = true;
|
||||
transparentState.depthWriteEnable = false;
|
||||
transparentState.depthFunc = MaterialComparisonFunc::LessEqual;
|
||||
transparentState.cullMode = MaterialCullMode::None;
|
||||
|
||||
Material* cubeMaterial = CreateMaterial(
|
||||
"SkyboxOpaqueCubeMaterial",
|
||||
"Tests/Rendering/SkyboxScene/opaque_cube.mat",
|
||||
Vector4(0.86f, 0.18f, 0.14f, 1.0f),
|
||||
MaterialRenderQueue::Geometry,
|
||||
opaqueState);
|
||||
Material* transparentMaterial = CreateMaterial(
|
||||
"SkyboxTransparentQuadMaterial",
|
||||
"Tests/Rendering/SkyboxScene/transparent_quad.mat",
|
||||
Vector4(0.12f, 0.88f, 0.74f, 0.56f),
|
||||
MaterialRenderQueue::Transparent,
|
||||
transparentState);
|
||||
mMaterials = { skyboxMaterial, cubeMaterial, transparentMaterial };
|
||||
mMaterials = { skyboxMaterial };
|
||||
mTextures = { skyboxTexture };
|
||||
|
||||
CreateRenderableObject(
|
||||
*mScene,
|
||||
"OpaqueQuad",
|
||||
mMesh,
|
||||
cubeMaterial,
|
||||
Vector3(-0.54f, -0.10f, 3.25f),
|
||||
Vector3(1.20f, 1.20f, 1.0f),
|
||||
Quaternion::FromEulerAngles(0.0f, -0.38f, 0.0f));
|
||||
|
||||
CreateRenderableObject(
|
||||
*mScene,
|
||||
"TransparentQuad",
|
||||
mMesh,
|
||||
transparentMaterial,
|
||||
Vector3(0.82f, 0.06f, 4.1f),
|
||||
Vector3(1.48f, 1.48f, 1.0f),
|
||||
Quaternion::FromEulerAngles(0.0f, -0.34f, 0.0f));
|
||||
}
|
||||
|
||||
RHIResourceView* SkyboxSceneTest::GetCurrentBackBufferView() {
|
||||
|
||||
Reference in New Issue
Block a user