XNA 中用于简单 2D SpriteBatch 绘图的像素着色器应该使用什么顶点着色器代码? [英] What vertex shader code should be used for a pixel shader used for simple 2D SpriteBatch drawing in XNA?

查看:75
本文介绍了XNA 中用于简单 2D SpriteBatch 绘图的像素着色器应该使用什么顶点着色器代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,为什么 Silverlight 5 中的 SilverlightEffect(.slfx 文件)需要 顶点着色器?我正在尝试将一个简单的 2D XNA 游戏移植到 Silverlight 5 RC,并且我想使用一个基本的像素着色器.此着色器在适用于 Windows 和 Xbox 的 XNA 中运行良好,但我无法将其与 Silverlight 作为 SilverlightEffect 一起编译.

First of all, why is a vertex shader required for a SilverlightEffect (.slfx file) in Silverlight 5? I'm trying to port a simple 2D XNA game to Silverlight 5 RC, and I would like to use a basic pixel shader. This shader works great in XNA for Windows and Xbox, but I can't get it to compile with Silverlight as a SilverlightEffect.

MS 博客 说.slfx 和 .fx 之间没有区别",但显然这并不完全正确——或者至少 SpriteBatch 正在工作在常规 XNA"中为我们提供了一些魔力,而在Silverlight XNA"中却没有.

The MS blog for the Silverlight Toolkit says that "there is no difference between .slfx and .fx", but apparently this isn't quite true -- or at least SpriteBatch is working some magic for us in "regular XNA", and it isn't in "Silverlight XNA".

如果我尝试将我的像素着色器文件直接复制到 Silverlight 项目中(并将其更改为支持的Effect - Silverlight"导入器/处理器),当我尝试编译时,我会看到以下错误消息:

If I try to directly copy my pixel shader file into a Silverlight project (and change it to the supported "Effect - Silverlight" importer/processor), when I try to compile I see the following error message:

Invalid effect file. Unable to find vertex shader in pass "P0"

确实,我的像素着色器文件中没有顶点着色器.我的其他 2D XNA 应用程序不需要,因为我只是在做基本的 SpriteBatch 绘图.

Indeed, there isn't a vertex shader in my pixel shader file. I haven't needed one with my other 2D XNA apps since I'm just doing basic SpriteBatch drawing.

我尝试使用 Remi Gillig 对这篇 Shawn Hargreaves 博客文章的评论 提供指导,但它并不完全有效.着色器文件成功编译,我在屏幕上看到了我的游戏的一些外观,但它很小,扭曲,重复,而且全部混乱.很明显有些地方不太对劲.

I tried adding a vertex shader to my shader file, using Remi Gillig's comment on this Shawn Hargreaves blog post for guidance, but it doesn't quite work. The shader file successfully compiles, and I see some semblance of my game on screen, but it's tiny, twisted, repeated, and all jumbled up. So clearly something's not quite right.

这让我想到了我真正的问题:由于需要顶点着色器,是否有适用于简单 2D SpriteBatch 绘图的基本顶点着色器函数?

So that brings me to my real question: Since a vertex shader is required, is there a basic vertex shader function that works for simple 2D SpriteBatch drawing?

如果顶点着色器需要世界/视图/项目矩阵作为参数,我应该为 2D 游戏使用什么值?

And if the vertex shader requires world/view/project matricies as parameters, what values am I supposed to use for a 2D game?

任何着色器专业人士可以提供帮助吗?谢谢!

Can any shader pros help? Thanks!

推荐答案

在 XNA 中,SpriteBatch 提供了自己的顶点着色器.因为 Silverlight 5 中没有 SpriteBatch,所以您必须提供自己的.

In XNA, SpriteBatch is providing its own vertex shader. Because there is no SpriteBatch in Silverlight 5, you have to provide your own.

您可能需要查看源代码对于 SpriteBatch 使用的着色器(在 SpriteEffect.fx 中).这是它的顶点着色器,非常简单:

You may want to look at the source code for the shader that SpriteBatch uses (it's in SpriteEffect.fx). Here is its vertex shader, it's pretty simple:

void SpriteVertexShader(inout float4 color    : COLOR0,
                        inout float2 texCoord : TEXCOORD0,
                        inout float4 position : SV_Position)
{
    position = mul(position, MatrixTransform);
}

所以现在您只需要输入位置和变换矩阵(以及纹理坐标,但这些都相当简单).

So now you just need the input position and the transformation matrix (and the texture co-ordinates, but those are fairly simple).

同样的您链接的博文,最后告诉您如何设置矩阵(也在这里).又来了:

The same blog post you linked, towards the end, tells you how to set up the matrix (also here). Here it is again:

Matrix projection = Matrix.CreateOrthographicOffCenter(0, viewport.Width, viewport.Height, 0, 0, 1);
Matrix halfPixelOffset = Matrix.CreateTranslation(-0.5f, -0.5f, 0);
Matrix transformation = halfPixelOffset * projection;

(注意:我不确定 Silverlight 是否真的需要半像素偏移来保持纹素对齐(ref).可能是这样.)

(Note: I'm not sure if Silverlight actually requires the half-pixel offset to maintain texel alignment (ref). It probably does.)

棘手的一点是精灵的定位是在着色器之外的 CPU 上完成的.以下是 SpriteBatch 执行的顺序:

The tricky bit is that the positioning of the sprite is done outside of the shader, on the CPU. Here's the order of what SpriteBatch does:

  • 从矩形中的四个顶点开始,(0,0) 是左上角,纹理的 (width,height) 是右下角
  • 按原点向后翻译
  • 规模
  • 旋转
  • 按职位翻译

这会将精灵顶点放在客户空间"中,然后变换矩阵将这些顶点从客户空间转换到投影空间(用于绘图).

This places the sprite vertices in "client space", and then the transformation matrix transforms those vertices from client space into projection space (which is used for drawing).

我在 ExEn (SpriteBatch.SpriteBatchOpenGL.cs 中的 InternalDraw),您可以轻松适应您的使用.

I've got a highly-inlined version of this transformation in ExEn (SpriteBatch.InternalDraw in SpriteBatchOpenGL.cs) that you could easily adapt for your use.

这篇关于XNA 中用于简单 2D SpriteBatch 绘图的像素着色器应该使用什么顶点着色器代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆