Hi guys I am trying to code a class that calculates the normals of a mesh. I am trying to use the job system to enhance the performance. In my solution I am trying to use something like NativeList>. But when I compiled I got an error says that:
ArgumentException: Unity.Collections.NativeList`1[UnityEngine.Vector3Int] used in native collection is not blittable or primitive
I’ve tried other ways like using NativeList<List> (error: List is nullable type) List<NativeList> (error: It wont work with the job system.)
Is there anyway I can do this?
Thank you guys!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Unity.Collections;
using Unity.Burst;
using Unity.Jobs;
using UnityEngine.Jobs;
public class NormalCalculatorJobSystem
{
List<List> verticesAdjFaces;
List normals;
Mesh mesh = null;
NativeArray nativeIndices;
NativeList<NativeList> nativeVerticesAdjFaces;
NativeList nativeNormals;
NativeArray nativeVertices;
InitAdjacentFacesJob initAdjacentFacesJob;
JobHandle initAdjacentFacesJobHandle;
CalculateNormalsJob calculateNormalsJob;
JobHandle calculateNormalsJobHandle;
public void Init(Mesh _mesh)
{
mesh = _mesh;
verticesAdjFaces = new List<List>();
nativeIndices = new NativeArray(mesh.triangles, Allocator.Temp);
nativeVerticesAdjFaces = new NativeList<NativeList>(mesh.vertexCount,Allocator.Temp);
nativeNormals = new NativeList(mesh.normals.Length, Allocator.Temp);
nativeVertices = new NativeArray(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 indices;
public NativeList<NativeList> verticesAdjFaces;
public void Execute(int i)
{
NativeList list = new NativeList();
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(nativeNormals.ToArray());
mesh.SetNormals(normals);
}
public struct CalculateNormalsJob : IJobParallelFor {
public NativeArray vertices;
public NativeList<NativeList> verticesAdjFaces;
public NativeList normals;
public void Execute(int i)
{
NativeList adjFaces = verticesAdjFaces*;*
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);
}
}
}