Formalize material-driven panoramic skybox path
This commit is contained in:
File diff suppressed because one or more lines are too long
@@ -17,11 +17,14 @@
|
||||
#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/RHI/RHITexture.h>
|
||||
#include <XCEngine/Scene/Scene.h>
|
||||
|
||||
#include "../../../RHI/integration/fixtures/RHIIntegrationFixture.h"
|
||||
|
||||
#include <cmath>
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
|
||||
@@ -40,6 +43,84 @@ constexpr const char* kVulkanScreenshot = "skybox_scene_vulkan.ppm";
|
||||
constexpr uint32_t kFrameWidth = 1280;
|
||||
constexpr uint32_t kFrameHeight = 720;
|
||||
|
||||
Texture* CreatePanoramicSkyboxTexture() {
|
||||
constexpr uint32_t kTextureWidth = 256;
|
||||
constexpr uint32_t kTextureHeight = 128;
|
||||
|
||||
auto* texture = new Texture();
|
||||
IResource::ConstructParams params = {};
|
||||
params.name = "SkyboxPanoramicTexture";
|
||||
params.path = "Tests/Rendering/SkyboxScene/skybox_panorama.texture";
|
||||
params.guid = ResourceGUID::Generate(params.path);
|
||||
texture->Initialize(params);
|
||||
|
||||
std::vector<float> pixels(static_cast<size_t>(kTextureWidth) * static_cast<size_t>(kTextureHeight) * 4u, 1.0f);
|
||||
for (uint32_t y = 0; y < kTextureHeight; ++y) {
|
||||
for (uint32_t x = 0; x < kTextureWidth; ++x) {
|
||||
const float u = static_cast<float>(x) / static_cast<float>(kTextureWidth - 1u);
|
||||
const float v = static_cast<float>(y) / static_cast<float>(kTextureHeight - 1u);
|
||||
|
||||
Vector3 color = Vector3(0.05f, 0.08f, 0.14f);
|
||||
if (v < 0.42f) {
|
||||
const float t = v / 0.42f;
|
||||
color = Vector3(
|
||||
0.08f + 0.42f * (1.0f - t),
|
||||
0.16f + 0.50f * (1.0f - t),
|
||||
0.34f + 0.58f * (1.0f - t));
|
||||
} else if (v < 0.58f) {
|
||||
const float t = (v - 0.42f) / 0.16f;
|
||||
color = Vector3(
|
||||
0.92f - 0.24f * t,
|
||||
0.74f - 0.18f * t,
|
||||
0.36f - 0.12f * t);
|
||||
} else {
|
||||
const float t = (v - 0.58f) / 0.42f;
|
||||
color = Vector3(
|
||||
0.12f - 0.06f * t,
|
||||
0.10f - 0.05f * t,
|
||||
0.09f - 0.04f * t);
|
||||
}
|
||||
|
||||
const float sunDx = u - 0.22f;
|
||||
const float sunDy = v - 0.34f;
|
||||
const float sun = std::exp(-(sunDx * sunDx + sunDy * sunDy) * 1450.0f) * 4.0f;
|
||||
color += Vector3(1.0f, 0.82f, 0.58f) * sun;
|
||||
|
||||
if (u > 0.62f && u < 0.68f && v > 0.46f && v < 0.82f) {
|
||||
color = Vector3(0.10f, 0.52f, 0.96f);
|
||||
}
|
||||
if (u > 0.34f && u < 0.39f && v > 0.50f && v < 0.86f) {
|
||||
color = Vector3(0.88f, 0.24f, 0.19f);
|
||||
}
|
||||
if (u > 0.78f && u < 0.92f && v > 0.60f && v < 0.78f) {
|
||||
color = Vector3(0.22f, 0.70f, 0.28f);
|
||||
}
|
||||
|
||||
const size_t pixelIndex = (static_cast<size_t>(y) * static_cast<size_t>(kTextureWidth) + x) * 4u;
|
||||
pixels[pixelIndex + 0] = color.x;
|
||||
pixels[pixelIndex + 1] = color.y;
|
||||
pixels[pixelIndex + 2] = color.z;
|
||||
pixels[pixelIndex + 3] = 1.0f;
|
||||
}
|
||||
}
|
||||
|
||||
const size_t pixelDataSize = pixels.size() * sizeof(float);
|
||||
if (!texture->Create(
|
||||
kTextureWidth,
|
||||
kTextureHeight,
|
||||
1,
|
||||
1,
|
||||
XCEngine::Resources::TextureType::Texture2D,
|
||||
XCEngine::Resources::TextureFormat::RGBA32_FLOAT,
|
||||
pixels.data(),
|
||||
pixelDataSize)) {
|
||||
delete texture;
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
return texture;
|
||||
}
|
||||
|
||||
Mesh* CreateQuadMesh() {
|
||||
auto* mesh = new Mesh();
|
||||
IResource::ConstructParams params = {};
|
||||
@@ -100,6 +181,21 @@ Material* CreateMaterial(
|
||||
return material;
|
||||
}
|
||||
|
||||
Material* CreateSkyboxMaterial(Texture* texture) {
|
||||
auto* material = new Material();
|
||||
IResource::ConstructParams params = {};
|
||||
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("_MainTex", ResourceHandle<Texture>(texture));
|
||||
material->SetFloat4("_Tint", Vector4(1.0f, 1.0f, 1.0f, 1.0f));
|
||||
material->SetFloat("_Exposure", 1.05f);
|
||||
material->SetFloat("_Rotation", 0.0f);
|
||||
return material;
|
||||
}
|
||||
|
||||
GameObject* CreateRenderableObject(
|
||||
Scene& scene,
|
||||
const char* name,
|
||||
@@ -151,6 +247,7 @@ private:
|
||||
std::unique_ptr<SceneRenderer> mSceneRenderer;
|
||||
std::vector<RHIResourceView*> mBackBufferViews;
|
||||
std::vector<Material*> mMaterials;
|
||||
std::vector<Texture*> mTextures;
|
||||
Mesh* mMesh = nullptr;
|
||||
RHITexture* mDepthTexture = nullptr;
|
||||
RHIResourceView* mDepthView = nullptr;
|
||||
@@ -221,6 +318,11 @@ void SkyboxSceneTest::TearDown() {
|
||||
}
|
||||
mMaterials.clear();
|
||||
|
||||
for (Texture* texture : mTextures) {
|
||||
delete texture;
|
||||
}
|
||||
mTextures.clear();
|
||||
|
||||
delete mMesh;
|
||||
mMesh = nullptr;
|
||||
|
||||
@@ -239,9 +341,11 @@ void SkyboxSceneTest::BuildScene() {
|
||||
camera->SetFarClipPlane(20.0f);
|
||||
camera->SetClearColor(XCEngine::Math::Color(0.01f, 0.01f, 0.01f, 1.0f));
|
||||
camera->SetSkyboxEnabled(true);
|
||||
camera->SetSkyboxTopColor(XCEngine::Math::Color(0.11f, 0.29f, 0.64f, 1.0f));
|
||||
camera->SetSkyboxHorizonColor(XCEngine::Math::Color(0.82f, 0.87f, 0.96f, 1.0f));
|
||||
camera->SetSkyboxBottomColor(XCEngine::Math::Color(0.96f, 0.93f, 0.86f, 1.0f));
|
||||
Texture* skyboxTexture = CreatePanoramicSkyboxTexture();
|
||||
ASSERT_NE(skyboxTexture, nullptr);
|
||||
Material* skyboxMaterial = CreateSkyboxMaterial(skyboxTexture);
|
||||
ASSERT_NE(skyboxMaterial, nullptr);
|
||||
camera->SetSkyboxMaterial(skyboxMaterial);
|
||||
|
||||
MaterialRenderState opaqueState = {};
|
||||
opaqueState.cullMode = MaterialCullMode::Back;
|
||||
@@ -269,7 +373,8 @@ void SkyboxSceneTest::BuildScene() {
|
||||
Vector4(0.12f, 0.88f, 0.74f, 0.56f),
|
||||
MaterialRenderQueue::Transparent,
|
||||
transparentState);
|
||||
mMaterials = { cubeMaterial, transparentMaterial };
|
||||
mMaterials = { skyboxMaterial, cubeMaterial, transparentMaterial };
|
||||
mTextures = { skyboxTexture };
|
||||
|
||||
CreateRenderableObject(
|
||||
*mScene,
|
||||
|
||||
Reference in New Issue
Block a user