Unity3d 如何使用NativeList之类的工具<;民族主义者<;矢量3int>>;?或者其他替代方法?

Unity3d 如何使用NativeList之类的工具<;民族主义者<;矢量3int>>;?或者其他替代方法?,unity3d,jobs,primitive,Unity3d,Jobs,Primitive,我正在尝试使用作业系统手动计算网格的法线,以获得更好的性能。在我的解决方案中,我尝试使用NativeList>之类的东西。但当我编译时,我得到一个错误,上面写着: ArgumentException:本机集合中使用的Unity.Collections.NativeList`1[UnityEngine.Vector3Int]不可blittable或primitive 还有什么我可以解决的吗 谢谢你们 public class NormalCalculatorJobSystem { List<

我正在尝试使用作业系统手动计算网格的法线,以获得更好的性能。在我的解决方案中,我尝试使用NativeList>之类的东西。但当我编译时,我得到一个错误,上面写着:

ArgumentException:本机集合中使用的Unity.Collections.NativeList`1[UnityEngine.Vector3Int]不可blittable或primitive

还有什么我可以解决的吗

谢谢你们

public class NormalCalculatorJobSystem
{
List<List<Vector3Int>> verticesAdjFaces;
List<Vector3> normals;
Mesh mesh = null;

NativeArray<int> nativeIndices;
NativeList<NativeList<Vector3Int>> nativeVerticesAdjFaces;
NativeList<Vector3> nativeNormals;
NativeArray<Vector3> nativeVertices;

InitAdjacentFacesJob initAdjacentFacesJob;
JobHandle initAdjacentFacesJobHandle;

CalculateNormalsJob calculateNormalsJob;
JobHandle calculateNormalsJobHandle;

public void Init(Mesh _mesh)
{
    mesh = _mesh;
    verticesAdjFaces = new List<List<Vector3Int>>();


    nativeIndices = new NativeArray<int>(mesh.triangles, Allocator.Temp);

    //ArgumentException: Unity.Collections.NativeList`1[UnityEngine.Vector3Int] used in native collection is not blittable or primitive
    nativeVerticesAdjFaces = new NativeList<NativeList<Vector3Int>>(mesh.vertexCount,Allocator.Temp);


    nativeNormals = new NativeList<Vector3>(mesh.normals.Length, Allocator.Temp);
    nativeVertices = new NativeArray<Vector3>(mesh.vertices, Allocator.Temp);

    InitAdjacentFaces();
}

public void InitAdjacentFaces() {

    initAdjacentFacesJob = new InitAdjacentFacesJob
    {
        indices = nativeIndices,
        verticesAdjFaces = nativeVerticesAdjFaces,
    };

    initAdjacentFacesJobHandle = initAdjacentFacesJob.Schedule(mesh.vertexCount, 64);
    initAdjacentFacesJobHandle.Complete();
}

public struct InitAdjacentFacesJob : IJobParallelFor
{
    public NativeArray<int> indices;
    public NativeList<NativeList<Vector3Int>> verticesAdjFaces;
    public void Execute(int i)
    {
        NativeList<Vector3Int> list = new NativeList<Vector3Int>();
        for (int j = 0; j < indices.Length; j += 3)
        {
            if (list.Length > 6)
                continue;
            if (i == indices[j] || i == indices[j + 1] || i == indices[j + 2])
            {
                Vector3Int triangle = new Vector3Int(indices[j], indices[j + 1], indices[j + 2]);
                list.Add(triangle);
            }
        }
        verticesAdjFaces.Add(list);
    }
}


public void CalculateNormals(Mesh mesh)
{
    nativeNormals.Clear();
    calculateNormalsJob = new CalculateNormalsJob
    {
        vertices = nativeVertices,
        verticesAdjFaces = nativeVerticesAdjFaces,
        normals = nativeNormals
    };
    calculateNormalsJobHandle = calculateNormalsJob.Schedule(nativeVerticesAdjFaces.Length, 64,initAdjacentFacesJobHandle);
    calculateNormalsJobHandle.Complete();
    normals = new List<Vector3>(nativeNormals.ToArray());
    mesh.SetNormals(normals);
}

public struct CalculateNormalsJob : IJobParallelFor {
    public NativeArray<Vector3> vertices;
    public NativeList<NativeList<Vector3Int>> verticesAdjFaces;
    public NativeList<Vector3> normals;
    public void Execute(int i)
    {
        NativeList<Vector3Int> adjFaces = verticesAdjFaces[i];
        Vector3 normal = Vector3.zero;
        for (int j = 0; j < adjFaces.Length; j++)
        {
            Vector3Int triangle = adjFaces[j];
            Vector3 vec0 = vertices[triangle.y] - vertices[triangle.x];
            Vector3 vec1 = vertices[triangle.z] - vertices[triangle.x];
            normal += Vector3.Cross(vec0, vec1);
        }
        normal.Normalize();
        normals.Add(normal);
    }
}
公共类NormalCalculatorJobSystem
{
列出垂直面;
列出法线;
网格=空;
民族主义民族主义;
民族主义的民族主义者;
民族主义的民族主义者;
本地阵列本地顶点;
InitAdjacentFacesJob InitAdjacentFacesJob;
JobHandle initAdjacentFacesJobHandle;
CalculateNormalsJob CalculateNormalsJob;
JobHandle计算异常JobHandle;
公共void Init(网格_网格)
{
网格=_网格;
verticesAdjFaces=新列表();
NativeIndicates=新的NativeArray(mesh.triangles、Allocator.Temp);
//ArgumentException:本机集合中使用的Unity.Collections.NativeList`1[UnityEngine.Vector3Int]不可blittable或primitive
nativeVerticesAdjFaces=新的NativeList(mesh.vertexCount、Allocator.Temp);
nativeNormals=新的NativeList(mesh.normals.Length,Allocator.Temp);
nativeVertices=新的NativeArray(mesh.vertices,Allocator.Temp);
InitAdjacentFaces();
}
公共void InitAdjacentFaces(){
initAdjacentFacesJob=新的initAdjacentFacesJob
{
指数=国家属性,
verticesAdjFaces=本地verticesAdjFaces,
};
initAdjacentFacesJobHandle=initAdjacentFacesJob.Schedule(mesh.vertexCount,64);
initAdjacentFacesJobHandle.Complete();
}
公共结构InitAdjacentFacesJob:IJobParallelFor
{
公共国家阵列指数;
公共民族主义者;
公共无效执行(int i)
{
NativeList list=新的NativeList();
对于(int j=0;j6)
继续;
如果(i==指数[j]| i==指数[j+1]| i==指数[j+2])
{
向量3int三角形=新向量3int(指数[j],指数[j+1],指数[j+2]);
列表。添加(三角形);
}
}
添加(列表);
}
}
公共空心计算器(网格)
{
nativeNormals.Clear();
calculateNormalsJob=新的calculateNormalsJob
{
顶点=原生顶点,
verticesAdjFaces=本地verticesAdjFaces,
法线=本地法线
};
calculateNormalsJobHandle=calculateNormalsJob.Schedule(nativeVerticesAdjFaces.Length,64,initAdjacentFacesJobHandle);
calculateNormalsJobHandle.Complete();
法线=新列表(nativeNormals.ToArray());
网格。设置法线(法线);
}
公共结构CalculateNormalsJob:IJobParallelFor{
国家公共阵列;
公共民族主义者;
公共民族主义常态;
公共无效执行(int i)
{
NativeList adjFaces=垂直adjFaces[i];
向量3法线=向量3.0;
对于(int j=0;j

}

可blittable类型是一种在托管和非托管代码中都具有规范表示的类型。你可以阅读更多关于他们的信息

对于圆点,Unity已经发明了自己的blittable值类型,这与您在HLSL等着色语言中看到的更为相似。对于向量,有float2、float3、float4等。对于矩阵,也有float4x4。在您的例子中,您想要的是int3而不是Vector3Int。使用这些新类型的一个好处是,它们可以利用SIMD操作(单指令、多数据),从而允许在一条指令中对所有3个值使用算术运算

这些新类型也有自己的内在操作,比如交叉和点积。你可以在网上找到数学图书馆