XNA:自定义内容处理(刷新)(译文)
By robot-v1.0
本文链接 https://www.kyfws.com/games/xna-customizing-the-content-processing-refresh-zh/
版权声明 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
- 5 分钟阅读 - 2195 个词 阅读量 0XNA:自定义内容处理(刷新)(译文)
原文地址:https://www.codeproject.com/Articles/35714/XNA-Customizing-the-Content-Processing-Refresh
原文作者:Inaki Ayucar
译文由本站 robot-v1.0 翻译
前言
In this post, I will try to explain the basic concepts of content processing inside XNA and the simplest way to customize it.
在本文中,我将尝试解释XNA内部内容处理的基本概念以及最简单的自定义方法.
介绍(Introduction)
在本文中,我将尝试解释XNA内部内容处理的基本概念以及最简单的自定义方法.(In this post, Ill try to explain the basic concepts of content processing inside XNA and the simplest way to customize it.) 如果您还不知道,那么在XNA中,所有内容都是解决方案的一部分,并且就像它们是代码一样被构建.在构建过程中,首先将它们导入为通用格式(取决于它的资源类型).在文件属性中,您可以选择哪个导入程序将加载文件:(If you don’t already know, in XNA, all the contents are part of the solution, and are built just like if they were code. In the build process, they are first imported to a common format (depending on the kind of resource it is). In the file properties, you can choose which importer will load the file:)
之后,内容由(After that, the content is processed by the) ContentProcessor
(在文件的属性中指定)(自解释名称),然后将结果存储在XNB文件中并复制到项目的((self-explanatory name) specified in the file’s properties, and then the results are stored in an XNB file and copied to the project’s)输出(output)目录.因此,应用程序加载并使用了那些XNB文件,而不是原始文件.(directory. So, the application loads and uses those XNB files, not the original).FBX(.FBX),(,).DDS(.DDS), 管他呢.(, or whatever.)
处理内容(Processing Contents)
以这种方式处理内容的最大优点之一是,您可以自定义处理阶段并将结果存储在XNB文件中.例如,假设您的编程团队与一个以英寸为单位的美国设计工作室合作,并且您想要将所有模型转换为米.(One of the biggest advantages of working this way with contents is that you can customize the processing stage and store the results in the XNB file. For example, let’s say that your programming team works with an American design studio that works in inches, and you want to transform all your models to meters.)
在XNA之前,这很繁琐.基本上,有两种解决方法:每次收到新模型并存储"转换后的版本"时都要执行此过程,或者在将模型加载到应用程序中时执行此操作(这意味着延迟).(Before XNA, this was something tedious. Basically, you had two ways of solving it: do that process every time you receive a new model and store the “transformed version”, or do it when the models are loaded in the application (with the delay that implies).)
现在,使用XNA,您可以编写自己的自定义(Now, with XNA, you can write your own custom) ContentProcessor
做出了转变.因此,每次构建Visual Studio项目时,所有内容都作为转换后的XNB文件进行处理并存储在输出目录中.除此之外,Content Build是智能的,它将检测文件是否已更改(因此,是否需要重建).(that makes that transformation. So, every time you Build your Visual Studio project, all the contents are processed and stored in the output directory as transformed XNB files. In addition to that, the Content Build is intelligent, and will detect if files have changed or not (and consequently, if they need to be rebuilt or not).)
如何编写内容处理器(How to Write a Content Processor)
内容处理器必须在单独的程序集中创建.因此,首先创建一个新(Content processors must be created in a separate assembly. So, first create a new) Library
所有XNA参考的项目(别忘了(project with all the XNA references (don’t forget) Microsoft.Xna.Framework.Content.Pipeline
).().)
然后,添加一个空类,该类继承自标准内容处理器,以处理您要处理的内容类型.例如,一个(Then, add an empty class which inherits from the standard content processor for the kind of content you want to process. For example, a) ModelProcessor
.此类应标记为属性(. This class should be marked with the attribute) [ContentProcessor]
太.在新类中,覆盖(too. Inside the new class, override the) Process
XNA框架将针对使用此处理器的每个模型调用此方法.(method, which will be called by the XNA framework for every model using this processor.)
内容处理器示例(A Content Processor Example)
[ContentProcessor]
public class VertexTaggedMesh : ModelProcessor
{
public override ModelContent Process(NodeContent input,
ContentProcessorContext context)
{
// This converts the raw loaded data
// of your model to a form that can be written to
// an instance of the model class
ModelContent model = base.Process(input, context);
foreach (ModelMeshContent mesh in model.Meshes)
{
// Put the data in the tag.
byte[] rawVertexBufferData = mesh.VertexBuffer.VertexData;
mesh.Tag = rawVertexBufferData;
}
return model;
}
}
此示例仅将网格顶点信息的副本存储在"(This example simply stores a copy of the vertex information of the mesh in the “) Tag
每个网格的"“属性.有时这很有用,因为在XNA中,如果顶点缓冲区信息是使用XNA创建的,则无法访问它.(” property of each mesh. This is useful sometimes, because in XNA, the vertex buffer information is not accessible if it was created with the) ReadOnly
标志(这是默认行为).(flag (which is the default behavior).)
在其他文章中,我将提供更多内容处理器的示例,但是现在我对如何使用它们更加感兴趣.(In other posts, I’ll include more examples of content processors, but now I’m more interested in how to use them.)
如何使用内容处理器DLL(How to Use the Content Processor DLL)
一旦有了内容处理器DLL,就可以将其集成到Visual C#Express(或Visual Studio 2008)环境中了.为此:(Once we have the content processor DLL, it’s time to integrate it into the Visual C# Express (or Visual Studio 2008) environment. To do so:)
- 如果使用(If using)XNA Game Studio 1.0(XNA Game Studio 1.0),转到要在其中使用它的项目,然后选择项目->属性->内容管道.该窗口包含用作内容处理器的程序集列表.使用按钮”(, go to the project in which you want to use it, and select Project -> Properties -> Content Pipeline. The window contains a list of assemblies used as content processors. Use the button “)加(Add)“以包含创建的DLL.(” to include the DLL created.)
- 如果使用XNA Game Studio 2.0或更高版本,则只需添加新项目的引用即可,就像其他程序集一样. Visual Studio将检测到它是内容处理器.(If using XNA Game Studio 2.0 or higher, just add a new project’s reference, as any other assembly. Visual Studio will detect it’s a content processor.) 之后,应该在内容处理器中出现一个新的条目,用于内容(在文件属性中).选择所需的文件之一,瞧!(After that, a new entry in the content processors available for contents (in the file’s properties) should appear. Select that one for the files you want, and voila!) 每次构建项目时,都会执行内容处理器.(Every time you build your project, the content processor will be executed.)
许可
本文以及所有相关的源代码和文件均已获得The Code Project Open License (CPOL)的许可。
C# .NET Dev 新闻 翻译