Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/drupal/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 重写VertexPositionTexture以使用整数向量_C#_Xna 4.0 - Fatal编程技术网

C# 重写VertexPositionTexture以使用整数向量

C# 重写VertexPositionTexture以使用整数向量,c#,xna-4.0,C#,Xna 4.0,我目前正在从事一个项目,该项目涉及使用VertexositionTextures设置大量顶点数据。这最终会消耗大量内存,虽然我可以对内存中当前的内容以及可以卸载到硬盘的内容进行一些优化,但我希望尽一切可能进行优化 那么我的问题是:有没有一种方法可以重写VertexPositionTexture结构,使它使用我的自定义IntVector类来使用更少的空间?这是否也需要修改DrawPrimitives和DrawUserPrimitives 我这样问是因为我没有使用纹理图集,所以我的UV坐标始终是(0

我目前正在从事一个项目,该项目涉及使用VertexositionTextures设置大量顶点数据。这最终会消耗大量内存,虽然我可以对内存中当前的内容以及可以卸载到硬盘的内容进行一些优化,但我希望尽一切可能进行优化

那么我的问题是:有没有一种方法可以重写VertexPositionTexture结构,使它使用我的自定义IntVector类来使用更少的空间?这是否也需要修改DrawPrimitives和DrawUserPrimitives


我这样问是因为我没有使用纹理图集,所以我的UV坐标始终是(0,0)、(0,1)、(1,0)、(1,1),可以不使用浮点数来表示,我的顶点位置也可以很容易地用整数表示。

是的,你可以这样做。你只需要定义一个自定义顶点格式,XNA就可以完成其余的工作

需要记住的最重要的一点是,您仅限于使用可用的
VertexElementFormat
s()中的元素-注意
HalfVector
格式在
Reach
配置文件()中不可用

您可能不需要创建自己的向量类型,因为XNA已经在Microsoft.XNA.Framework.Graphics.PackedVector()或主库中提供了所有可用格式的实现。如果确实要创建自己的类型,则它需要是以相同方式打包的
结构(或顶点中的松散字段)

最后,值得指出的是,没有32位
int
数据类型。这也没有什么好处,因为一个
浮点数
(一个
单个
)在32位时大小相同。(有些GPU以单精度运行,因此无论如何都无法保持额外的精度。)


所以我们从一个
VertexPositionTexture
开始,它包含一个
Vector3
位置(96位)和一个
Vector2
纹理坐标(64位),总共160位

让我们将其转换为一个自定义的
IntVertexPositionTexture
,其中包含一个
Short4
位置(64位)和一个
NormalizedShort2
纹理坐标(32位),总共96位(大小减小40%)

(如果您在2D中工作,您可以使用
Short2
而不是
Short4
,然后再保存32位。没有
Short3

下面是此顶点类型的代码,这实际上只是了解API期望的情况:

struct IntVertexPositionTexture : IVertexType
{
    public Short4 Position;
    public NormalizedShort2 TextureCoordinate;

    public IntVertexPositionTexture(Short4 position, NormalizedShort2 textureCoordinate)
    {
        this.Position = position;
        this.TextureCoordinate = textureCoordinate;
    }

    public static readonly VertexDeclaration VertexDeclaration = new VertexDeclaration(new VertexElement[]
    {
        new VertexElement(0, VertexElementFormat.Short4, VertexElementUsage.Position, 0),
        new VertexElement(8, VertexElementFormat.NormalizedShort2, VertexElementUsage.TextureCoordinate, 0),
    });

    VertexDeclaration IVertexType.VertexDeclaration
    {
        get { return IntVertexPositionTexture.VertexDeclaration; }
    }
}
创建自定义顶点类型时,重要的一点是确保字段与您在
VertexDeclaration
()中指定的顺序和布局匹配。如何创建
VertexDeclaration
应该是不言自明的-只需看一看。每个字段的偏移量以字节为单位指定

elementUsage
usageIndex
与HLSL中的匹配。因此,在这种定制格式中,我们有
POSITION0
TEXCOORD0

IVertexType
的使用是可选的,因为您也可以将
VertexDeclaration
直接传递给(例如)一个
VertexBuffer
构造函数



这应该是所有你需要的信息,如果你想使一个不同的顶点格式,我已经提供了。如需了解更多信息,请参阅有关API的详细信息。

是的,您可以这样做。您只需定义自定义顶点格式,XNA将完成其余工作

需要记住的最重要的一点是,您仅限于使用可用的
VertexElementFormat
s()中的元素-注意
HalfVector
格式在
Reach
配置文件()中不可用

您可能不需要创建自己的向量类型,因为XNA已经在Microsoft.XNA.Framework.Graphics.PackedVector
()或主库中提供了所有可用格式的实现。如果确实要创建自己的类型,则它需要是以相同方式打包的
结构(或顶点中的松散字段)

最后,值得指出的是,没有32位
int
数据类型。这也没有什么好处,因为一个
浮点数
(一个
单个
)在32位时大小相同。(有些GPU以单精度运行,因此无论如何都无法保持额外的精度。)


所以我们从一个
VertexPositionTexture
开始,它包含一个
Vector3
位置(96位)和一个
Vector2
纹理坐标(64位),总共160位

让我们将其转换为一个自定义的
IntVertexPositionTexture
,其中包含一个
Short4
位置(64位)和一个
NormalizedShort2
纹理坐标(32位),总共96位(大小减小40%)

(如果您在2D中工作,您可以使用
Short2
而不是
Short4
,然后再保存32位。没有
Short3

下面是此顶点类型的代码,这实际上只是了解API期望的情况:

struct IntVertexPositionTexture : IVertexType
{
    public Short4 Position;
    public NormalizedShort2 TextureCoordinate;

    public IntVertexPositionTexture(Short4 position, NormalizedShort2 textureCoordinate)
    {
        this.Position = position;
        this.TextureCoordinate = textureCoordinate;
    }

    public static readonly VertexDeclaration VertexDeclaration = new VertexDeclaration(new VertexElement[]
    {
        new VertexElement(0, VertexElementFormat.Short4, VertexElementUsage.Position, 0),
        new VertexElement(8, VertexElementFormat.NormalizedShort2, VertexElementUsage.TextureCoordinate, 0),
    });

    VertexDeclaration IVertexType.VertexDeclaration
    {
        get { return IntVertexPositionTexture.VertexDeclaration; }
    }
}
创建自定义顶点类型时,重要的一点是确保字段与您在
VertexDeclaration
()中指定的顺序和布局匹配。如何创建
VertexDeclaration
应该是不言自明的-只需看一看。每个字段的偏移量以字节为单位指定

elementUsage
usageIndex
与HLSL中的匹配。因此,在这种定制格式中,我们有
POSITION0
TEXCOORD0

IVertexType
的使用是可选的,因为您也可以将
VertexDeclaration
直接传递给(例如)一个
VertexBuffer
构造函数


这应该是你需要的所有信息