-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathCubemapFromTextureRenderer.cs
More file actions
50 lines (40 loc) · 2.48 KB
/
CubemapFromTextureRenderer.cs
File metadata and controls
50 lines (40 loc) · 2.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// Copyright (c) .NET Foundation and Contributors (https://dotnetfoundation.org/ & https://stride3d.net) and Silicon Studio Corp. (https://www.siliconstudio.co.jp)
// Distributed under the MIT license. See the LICENSE.md file in the project root for more information.
using Stride.Core;
using Stride.Core.Mathematics;
using Stride.Graphics;
namespace Stride.Rendering.Skyboxes
{
public class CubemapFromTextureRenderer : CubemapRendererBase
{
private DynamicEffectInstance skyboxEffect;
private SpriteBatch spriteBatch;
private Texture inputTexture;
public CubemapFromTextureRenderer(IServiceRegistry services, RenderDrawContext renderDrawContext, Texture input, int outputSize, PixelFormat outputFormat)
: base(renderDrawContext.GraphicsDevice, outputSize, outputFormat, false)
{
inputTexture = input;
DrawContext = renderDrawContext;
skyboxEffect = new DynamicEffectInstance("SkyboxShaderTexture");
skyboxEffect.Initialize(services);
spriteBatch = new SpriteBatch(renderDrawContext.GraphicsDevice) { VirtualResolution = new Vector3(1) };
}
protected override void DrawImpl()
{
skyboxEffect.UpdateEffect(DrawContext.GraphicsDevice);
spriteBatch.Begin(DrawContext.GraphicsContext, depthStencilState: DepthStencilStates.None, effect: skyboxEffect);
spriteBatch.Parameters.Set(SkyboxShaderTextureKeys.Texture, inputTexture);
spriteBatch.Parameters.Set(SkyboxShaderBaseKeys.Intensity, 1);
spriteBatch.Parameters.Set(SkyboxShaderBaseKeys.ViewInverse, Matrix.Invert(Camera.ViewMatrix));
spriteBatch.Parameters.Set(SkyboxShaderBaseKeys.ProjectionInverse, Matrix.Invert(Camera.ProjectionMatrix));
spriteBatch.Parameters.Set(SkyboxShaderBaseKeys.SkyMatrix, Matrix.Identity);
spriteBatch.Draw(inputTexture, new RectangleF(0, 0, 1, 1), null, Color.White, 0, Vector2.Zero);
spriteBatch.End();
}
public static Texture GenerateCubemap(IServiceRegistry services, RenderDrawContext renderDrawContext, Texture input, int outputSize)
{
var pixelFormat = input.Format.IsHDR() ? PixelFormat.R16G16B16A16_Float : input.Format.IsSRgb() ? PixelFormat.R8G8B8A8_UNorm_SRgb : PixelFormat.R8G8B8A8_UNorm;
return GenerateCubemap(new CubemapFromTextureRenderer(services, renderDrawContext, input, outputSize, pixelFormat), Vector3.Zero);
}
}
}