Class SkyboxGenerator
Provides functionality to generate a skybox with diffuse and specular lighting parameters from a given texture.
public static class SkyboxGenerator
- Inheritance
-
SkyboxGenerator
Remarks
This class handles the conversion of a texture to a cubemap, performs spherical harmonics filtering for diffuse lighting, and prefilters the texture for specular lighting using GGX reflection. The original logic is from Stride.Assets.Skyboxes
Methods
Generate(Skybox, SkyboxGeneratorContext, Texture, int?)
Generates a skybox using the provided texture and context, applying both diffuse and specular lighting.
public static Skybox Generate(Skybox skybox, SkyboxGeneratorContext context, Texture skyboxTexture, int? specularCubeMapSize = null)
Parameters
skyboxSkyboxThe skybox instance to apply the generated parameters to.
contextSkyboxGeneratorContextThe context required for rendering, which includes services and draw context.
skyboxTextureTextureThe texture used to generate the skybox cubemap. Must be a cubemap or a 2D texture.
specularCubeMapSizeint?Size of the prefiltered specular cubemap, in pixels. Leave null to match the source cubemap, which is the default and is usually what you want - see the remarks. Any value given is rounded to the nearest power of two and floored at 64.
Returns
Remarks
The method performs the following:
- Converts the provided texture into a cubemap with a computed resolution based on the texture width.
- Applies Lambertian spherical harmonics filtering for diffuse lighting.
- Performs GGX prefiltering for specular lighting and generates a cubemap for reflection purposes.
Why the specular cubemap defaults to the source size. Stride's own asset pipeline defaults
this to 64, but that is not a free choice here.
RadiancePrefilteringGGXNoCompute only copies the sharpest mip straight across when
Log2(input.Width / output.Width) < input.MipLevelCount; otherwise it rescales through
an image scaler instead. A source cubemap with no mip chain has a level count of one, so that
condition holds only when the two widths are equal. Matching the source keeps the roughness-zero
mirror level pixel-exact; shrinking it resamples that level and visibly softens reflections.
Pass a smaller size deliberately when a large source would otherwise cost more memory than the
reflection is worth.
Ownership. The returned skybox holds a GPU cubemap that is not released automatically and does not belong to any content manager. Generating skyboxes repeatedly - once per scene load, say - leaks one each time. To release it:
var cubeMap = skybox.SpecularLightingParameters.Get(SkyboxKeys.CubeMap);
cubeMap?.Dispose();
Exceptions
- ArgumentNullException
If any argument is null.
- ArgumentException
If
skyboxTextureis neither a cubemap nor a 2D texture.