Boîte de ciel
Concept graphique donnant l’illusion qu’un décor est projeté dans un espace plus étendu qu’un cube/box dont le centre est la position de la caméra.
/// C++ — Skybox.hpp — update:03/11/26
#ifndef Skybox_hpp
#define Skybox_hpp
#import <Metal/Metal.hpp>
#include <memory>
#include <string>
#include <cmath>
#include "../includes/SharedGPU/Renderer_shared.h"
#include "../includes/SharedGPU/Skybox_shared.h"
class Skybox
{
public:
Skybox(MTL::Device*, MTL::Library*, MTL::PixelFormat, MTL::PixelFormat);
~Skybox();
void render(MTL::RenderCommandEncoder*);
void updateUniforms(const GlobalUniforms&);
private:
void createPipeline(MTL::Device*, MTL::Library*, MTL::PixelFormat, MTL::PixelFormat);
NS::SharedPtr<MTL::Buffer> m_vertexBuffer;
NS::SharedPtr<MTL::Buffer> m_uniformBuffer;
NS::SharedPtr<MTL::DepthStencilState> m_depthStencilState;
NS::SharedPtr<MTL::RenderPipelineState> m_pso;
uint32_t m_vertexCount;
};
#endif /* Skybox_hpp */
/// C++ — Skybox_shared.h — update:03/11/26
#ifndef Skybox_shared_h
#define Skybox_shared_h
#import <simd/simd.h>
struct SkyboxUniforms
{
simd::float4x4 invViewProjectionMatrix;
simd::float4 cameraPosition;
simd::float4 sunDirection;
float sunIntensity;
};
#endif /* Skybox_shared_h */
/// C++ — Skybox.cpp — update:03/11/26
#include "Skybox.hpp"
Skybox::Skybox(MTL::Device* device, MTL::Library* shaderLibrary,
MTL::PixelFormat colorPixelFormat, MTL::PixelFormat depthPixelFormat)
{
m_uniformBuffer = NS::TransferPtr(device->newBuffer(sizeof(SkyboxUniforms), MTL::ResourceStorageModeShared));
createPipeline(device, shaderLibrary, colorPixelFormat, depthPixelFormat);
float vertices[] = { -1.0f, -1.0f, 0.0f, 1.0f, -1.0f, 0.0f, -1.0f, 1.0f, 0.0f, 1.0f, 1.0f, 0.0f };
m_vertexCount = 4;
m_vertexBuffer = NS::TransferPtr(device->newBuffer(vertices, sizeof(vertices), MTL::ResourceStorageModeShared));
}
Skybox::~Skybox()
{}
void Skybox::createPipeline(MTL::Device* device, MTL::Library* shaderLibrary,
MTL::PixelFormat colorPixelFormat, MTL::PixelFormat depthPixelFormat)
{
auto vertexDescriptor = NS::TransferPtr(MTL::VertexDescriptor::alloc()->init());
vertexDescriptor->attributes()->object(0)->setFormat(MTL::VertexFormatFloat3);
vertexDescriptor->attributes()->object(0)->setOffset(0);
vertexDescriptor->attributes()->object(0)->setBufferIndex(0);
vertexDescriptor->layouts()->object(0)->setStride(sizeof(float) * 3);
auto vertexFunction = NS::TransferPtr(shaderLibrary->newFunction(MTLSTR("vertex_skybox")));
auto fragmentFunction = NS::TransferPtr(shaderLibrary->newFunction(MTLSTR("fragment_skybox")));
auto renderPipelineDescriptor = NS::TransferPtr(MTL::RenderPipelineDescriptor::alloc()->init());
renderPipelineDescriptor->setVertexFunction(vertexFunction.get());
renderPipelineDescriptor->setFragmentFunction(fragmentFunction.get());
renderPipelineDescriptor->setVertexDescriptor(vertexDescriptor.get());
renderPipelineDescriptor->setDepthAttachmentPixelFormat(depthPixelFormat);
renderPipelineDescriptor->colorAttachments()->object(0)->setPixelFormat(colorPixelFormat);
NS::Error* error = nullptr;
m_pso = NS::TransferPtr(device->newRenderPipelineState(renderPipelineDescriptor.get(), &error));
auto depthStencilDescriptor = NS::TransferPtr(MTL::DepthStencilDescriptor::alloc()->init());
depthStencilDescriptor->setDepthCompareFunction(MTL::CompareFunction::CompareFunctionLessEqual);
depthStencilDescriptor->setDepthWriteEnabled(false);
m_depthStencilState = NS::TransferPtr(device->newDepthStencilState(depthStencilDescriptor.get()));
}
void Skybox::updateUniforms(const GlobalUniforms& gUniforms)
{
SkyboxUniforms* uniforms = reinterpret_cast<SkyboxUniforms*>(m_uniformBuffer->contents());
uniforms->invViewProjectionMatrix = gUniforms.cameraUniforms.invViewProjectionMatrix;
uniforms->cameraPosition = gUniforms.sun.cameraPosition;
uniforms->sunDirection = gUniforms.sun.sunDirection;
uniforms->sunIntensity = 10.f; // disque du soleil : + 1.0 sur la couleur du ciel
}
void Skybox::render(MTL::RenderCommandEncoder* renderCommandEncoder)
{
renderCommandEncoder->setRenderPipelineState(m_pso.get());
renderCommandEncoder->setDepthStencilState(m_depthStencilState.get());
renderCommandEncoder->setCullMode(MTL::CullModeNone);
renderCommandEncoder->setVertexBuffer(m_vertexBuffer.get(), 0, 0);
renderCommandEncoder->setVertexBuffer(m_uniformBuffer.get(), 0, 1);
renderCommandEncoder->setFragmentBuffer(m_uniformBuffer.get(), 0, 1);
renderCommandEncoder->drawPrimitives(MTL::PrimitiveTypeTriangleStrip,
NS::UInteger(0), NS::UInteger(m_vertexCount));
}
/// MSL — Skybox.metal — update:03/11/26
#include <metal_stdlib>
#include "../includes/SharedGPU/Skybox_shared.h"
using namespace metal;
struct VertexSkybox
{
float3 position [[attribute(0)]];
};
struct VertexOut
{
float4 position [[position]];
float3 viewRay;
};
vertex VertexOut vertex_skybox(VertexSkybox in [[stage_in]],
constant SkyboxUniforms& uniforms [[buffer(1)]])
{
VertexOut out;
out.position = float4(in.position.xy, 1.0, 1.0);
float4 farPoint = uniforms.invViewProjectionMatrix * float4(in.position.xy, 1.0, 1.0);
out.viewRay = farPoint.xyz / farPoint.w - uniforms.cameraPosition.xyz;
return out;
}
viewRay est le vecteur 3D qui va de la caméra vers le point "infini" correspondant à ce pixel à l'écran. C'est la direction du rayon de vue.
/// MSL — Skybox.metal — update:03/11/26
fragment float4 fragment_skybox(VertexOut in [[stage_in]],
constant SkyboxUniforms& uniforms [[buffer(1)]])
{
float3 rayDirection = normalize(in.viewRay);
float3 sunDir = normalize(uniforms.sunDirection.xyz);
float3 color = float3(1.0, 1.0, 0.5);
float sunDot = dot(rayDirection, sunDir);
if (sunDot > 0.9995)
color += float3(1.0) * uniforms.sunIntensity * 0.1;
float sunHalo = pow(max(0.0, sunDot), 32.0);
color += float3(1.0, 0.9, 0.7) * sunHalo * 0.3;
return float4(color, 1.0);
}
Intégration
/// C++ — Renderer.hpp — update:03/11/26
#include "Skybox.hpp"
public:
Skybox m_skybox;
/// C++ — Renderer.cpp — update:03/11/26 — constructeur
m_skybox(m_device, m_shaderLibrary, m_pixelFormat, m_depthPixelFormat)
/// C++ — Renderer.cpp — update:03/11/26 — update()
m_skybox.updateUniforms(m_globalUniforms);
/// C++ — Renderer.cpp — update:03/11/26 — draw(), avant : m_text.render
m_skybox.render(renderCommandEncoder);
CheckPoint Compilation — Sans bruit
/// MSL — Skybox.metal — update:03/11/26
fragment float4 fragment_skybox(VertexOut in [[stage_in]],
constant SkyboxUniforms& uniforms [[buffer(1)]])
{
float3 rayDirection = normalize(in.viewRay);
float3 sunDir = normalize(uniforms.sunDirection.xyz);
float3 color = perlinFBMColor3D(rayDirection);
// effet pastel
color = color * 0.5 + 0.7;
// saturation
float gray = dot(color, float3(0.299, 0.587, 0.114));
color = mix(float3(gray), color, 1.3);
color = saturate(color);
// Gamma
color = pow(color, float3(1.0 / 2.5));
float sunDot = dot(rayDirection, sunDir);
if (sunDot > 0.9995)
color += float3(1.0) * uniforms.sunIntensity * 0.1;
float sunHalo = pow(max(0.0, sunDot), 32.0);
color += float3(1.0, 0.9, 0.7) * sunHalo * 0.3;
return float4(color, 1.0);
}
CheckPoint Compilation — Compile le bruit et utilise les modes de la caméra