纹理与渲染纹理
高级 程序员
Stride 使用 @'Stride.Graphics.Texture' 类在代码中操作纹理对象。
有关渲染到纹理的更多信息,请参阅渲染纹理。
加载纹理
要从 Stride 中的资产加载纹理,请调用以下函数:
// 加载名为 duck.dds(或 .png 等)的纹理
var myTexture = Content.Load<Texture>("duck");
这会自动生成一个纹理对象,并正确填充其所有字段。
创建纹理
你也可以在没有任何资产的情况下创建纹理(例如用作渲染目标)。为此,请调用 @'Stride.Graphics.Texture' 类的构造函数。请参阅 @'Stride.Graphics.Texture' 类参考以获取可用选项和参数的完整列表。某些纹理格式可能并非在所有平台上都可用。
代码:创建纹理
var myTexture = Texture.New2D(GraphicsDevice, 512, 512, false, PixelFormat.R8G8B8A8_UNorm, TextureFlags.ShaderResource);
渲染纹理
创建渲染目标
@'Stride.Graphics.GraphicsPresenter' 类始终提供一个默认渲染目标和一个深度缓冲区。它们可以通过 @'Stride.Graphics.GraphicsPresenter.BackBuffer' 和 @'Stride.Graphics.GraphicsPresenter.DepthStencilBuffer' 属性访问。presenter 由 @'Stride.Graphics.GraphicsDevice' 的 @'Stride.Graphics.GraphicsDevice.Presenter' 属性暴露。不过,你可能希望使用自己的缓冲区来执行离屏渲染或后处理。因此,Stride 提供了一种简单的方式来创建可用作渲染纹理和深度缓冲区的纹理。
代码:创建自定义渲染目标和深度缓冲区
// 渲染目标
var myRenderTarget = Texture.New2D(GraphicsDevice, 512, 512, false, PixelFormat.R8G8B8A8_UNorm, TextureFlags.ShaderResource | TextureFlags.RenderTarget);
// 可写深度缓冲区
var myDepthBuffer = Texture.New2D(GraphicsDevice, 512, 512, false, PixelFormat.D16_UNorm, TextureFlags.DepthStencil);
Note
不要忘记标志 @'Stride.Graphics.TextureFlags.RenderTarget',以启用渲染目标行为。
确保 PixelFormat 正确,尤其是深度缓冲区。请注意目标平台上可用的格式。
使用渲染目标
创建这些缓冲区后,你可以轻松地将它们设置为当前渲染纹理。
代码:使用渲染目标
// 设置渲染纹理
CommandList.SetRenderTargetAndViewport(myDepthBuffer, myRenderTarget);
// 设置默认渲染目标
CommandList.SetRenderTargetAndViewport(GraphicsDevice.Presenter.DepthStencilBuffer, GraphicsDevice.Presenter.BackBuffer);
Note
确保渲染目标和深度缓冲区的大小相同。否则,深度缓冲区不会被使用。
你可以同时设置多个渲染纹理。请参阅 @'Stride.Graphics.CommandList.SetRenderTargets(Stride.Graphics.Texture,Stride.Graphics.Texture[])' 和 @'Stride.Graphics.CommandList.SetRenderTargetsAndViewport(Stride.Graphics.Texture,Stride.Graphics.Texture[])' 方法的重载。
Note
只有 @'Stride.Graphics.GraphicsPresenter.BackBuffer' 会显示在屏幕上,因此你需要渲染它才能显示内容。
清除渲染目标
要清除渲染纹理,请调用 @'Stride.Graphics.CommandList.Clear(Stride.Graphics.Texture,Stride.Core.Mathematics.Color4)' 和 @'Stride.Graphics.CommandList.Clear(Stride.Graphics.Texture,Stride.Graphics.DepthStencilClearOptions,System.Single,System.Byte)' 方法。
代码:清除目标
CommandList.Clear(GraphicsDevice.Presenter.BackBuffer, Color.Black);
CommandList.Clear(GraphicsDevice.Presenter.DepthStencilBuffer, DepthStencilClearOptions.DepthBuffer); // 仅清除深度缓冲区
Note
别忘了每帧清除 @'Stride.Graphics.GraphicsPresenter.BackBuffer' 和 @'Stride.Graphics.GraphicsPresenter.DepthStencilBuffer'。如果不这样做,根据设备不同可能会出现意外行为。如果你想保留某一帧的内容,请使用中间渲染目标。
视口
@'Stride.Graphics.CommandList.SetRenderTargetAndViewport(Stride.Graphics.Texture,Stride.Graphics.Texture)' 会将当前的 @'Stride.Graphics.Viewport' 调整为渲染目标的完整大小。
如果你只想渲染到纹理的一部分,请分别使用 @'Stride.Graphics.CommandList.SetRenderTarget(Stride.Graphics.Texture,Stride.Graphics.Texture)' 和 @'Stride.Graphics.CommandList.SetViewport(Stride.Graphics.Viewport)' 设置渲染目标和视口。
你可以使用 @'Stride.Graphics.CommandList.SetViewports(Stride.Graphics.Viewport[])' 和 @'Stride.Graphics.CommandList.SetViewports(System.Int32,Stride.Graphics.Viewport[])' 重载绑定多个视口,供几何着色器使用。
代码:设置视口
// 全高清缓冲区示例
CommandList.SetRenderTarget(GraphicsDevice.Presenter.DepthStencilBuffer, GraphicsDevice.Presenter.BackBuffer); // 未设置视口
// 在全高清缓冲区(1920x1080)中将视口设置为图像四周留出 10 像素边框的示例
var viewport = new Viewport(10, 10, 1900, 1060);
CommandList.SetViewport(viewport);
裁剪
@'Stride.Graphics.CommandList.SetScissorRectangle(Stride.Core.Mathematics.Rectangle)' 和 @'Stride.Graphics.CommandList.SetScissorRectangles(Stride.Core.Mathematics.Rectangle[])' 方法用于设置裁剪矩形。与视口不同,你需要提供定义裁剪区域的顶点位置坐标,而不是它的尺寸。
代码:设置裁剪
// 在全高清缓冲区(1920x1080)中将裁剪设置为四周裁掉 10 像素的示例
var rectangle = new Rectangle(10, 10, 1910, 1070);
CommandList.SetScissorRectangles(rectangle);
var rectangles = new[] { new Rectangle(10, 10, 1900, 1060), new Rectangle(0, 0, 256, 256) };
CommandList.SetScissorRectangles(rectangles);