我正在编写前几个着色器,通常编写着色器来完成功能,因为我意识到主 XNA 库不支持它们。
我遇到的麻烦是,并非特定场景中的所有模型都包含纹理数据,而且我不知道如何处理它。主要的 XNA 库似乎通过使用 BasicEffect 的包装类来处理它,通过内容管理器加载它并相应地有选择地启用或禁用纹理处理。
对于自定义着色器来说,实现这一点有多困难?我正在编写的是通用的“色调偏移”效果,也就是说,我希望使用此技术绘制的任何内容都使其纹理颜色(如果有)及其顶点颜色色调发生一定程度的偏移。我是否需要编写单独的着色器,一个带有纹理,一个没有?如果是这样,当我循环遍历网格部件时,是否有任何方法可以检测给定部件是否具有纹理坐标,以便我可以应用正确的效果?
I'm in the process of writing my first few shaders, usually writing a shader to accomplish features as I realize that the main XNA library doesn't support them.
The trouble I'm running into is that not all of my models in a particular scene have texture data in them, and I can't figure out how to handle that. The main XNA libraries seem to handle it by using a wrapper class for BasicEffect, loading it through the content manager and selectively enabling or disabling texture processing accordingly.
How difficult is it to accomplish this for a custom shader? What I'm writing is an generic "hue shift" effect, that is, I want whatever gets drawn with this technique to have its texture colors (if any) and its vertex color hue shifted by a certain degree. Do I need to write separate shaders, one with textures and one without? If so, when I'm looping through my MeshParts, is there any way to detect if a given part has texture coordinates so that I can apply the correct effect?
发布评论
评论(1)
是的,您将需要单独的着色器,或者更确切地说不同的“技术” - 它仍然可以是相同的效果并使用大部分相同的代码。您可以了解
BasicEffect
(至少是 XNA 4.0 之前的版本)是如何实现的通过阅读源代码。要检测模型网格部件是否具有纹理坐标,请尝试以下操作:
内容管道设置其
BasicEffect
的方式是通过BasicMaterialContent
。如果设置了Texture
,则只需打开BasicEffect.TextureEnabled
属性。Yes, you will need separate shaders, or rather different "techniques" - it can still be the same effect and use much of the same code. You can see how
BasicEffect
(at least the pre-XNA 4.0 version) does it by reading the source code.To detect whether or not a model mesh part has texture coordinates, try this:
The way the content pipeline sets up its
BasicEffect
is viaBasicMaterialContent
. TheBasicEffect.TextureEnabled
property is simply turned on ifTexture
is set.