void ComputeSmoothedNormalByJob(Mesh smoothedMesh, Mesh originalMesh, int maxOverlapvertices = 20) { int svc = smoothedMesh.vertexCount, ovc = originalMesh.vertexCount; // CollectNormalJob Data NativeArray <Vector3> normals = new NativeArray <Vector3>(smoothedMesh.normals, Allocator.Persistent), vertrx = new NativeArray <Vector3>(smoothedMesh.vertices, Allocator.Persistent), smoothedNormals = new NativeArray <Vector3>(svc, Allocator.Persistent); var result = new NativeArray <UnsafeHashMap <Vector3, Vector3> >(maxOverlapvertices, Allocator.Persistent); var resultParallel = new NativeArray <UnsafeHashMap <Vector3, Vector3> .ParallelWriter>(result.Length, Allocator.Persistent); // NormalBakeJob Data NativeArray <Vector3> normalsO = new NativeArray <Vector3>(originalMesh.normals, Allocator.Persistent), vertrxO = new NativeArray <Vector3>(originalMesh.vertices, Allocator.Persistent); var tangents = new NativeArray <Vector4>(originalMesh.tangents, Allocator.Persistent); var colors = new NativeArray <Color>(ovc, Allocator.Persistent); var uv2 = new NativeArray <Vector2>(ovc, Allocator.Persistent); for (int i = 0; i < result.Length; i++) { result[i] = new UnsafeHashMap <Vector3, Vector3>(svc, Allocator.Persistent); resultParallel[i] = result[i].AsParallelWriter(); } bool existColors = originalMesh.colors.Length == ovc; bool isFace = originalMesh.name.Contains("face") || originalMesh.name.Contains("Face"); if (existColors) { colors.CopyFrom(originalMesh.colors); } CollectNormalJob collectNormalJob = new CollectNormalJob(normals, vertrx, resultParallel); BakeNormalJob normalBakeJob = new BakeNormalJob( vertrxO, normalsO, tangents, result, existColors, isFace, colors, uv2); normalBakeJob.Schedule(ovc, 100, collectNormalJob.Schedule(svc, 100)).Complete(); var c = new Color[ovc]; colors.CopyTo(c); originalMesh.colors = c; if (isFace) { var _uv2 = new Vector2[ovc]; uv2.CopyTo(_uv2); originalMesh.uv2 = _uv2; } normals.Dispose(); vertrx.Dispose(); result.Dispose(); smoothedNormals.Dispose(); resultParallel.Dispose(); normalsO.Dispose(); vertrxO.Dispose(); tangents.Dispose(); colors.Dispose(); uv2.Dispose(); }
void ComputeSmoothedNormalByJob(Mesh smoothedMesh, Mesh originalMesh, int maxOverlapvertices = 20) { int svc = smoothedMesh.vertexCount, ovc = originalMesh.vertexCount; // CollectNormalJob Data NativeArray <Vector3> normals = new NativeArray <Vector3>(smoothedMesh.normals, Allocator.Persistent), vertrx = new NativeArray <Vector3>(smoothedMesh.vertices, Allocator.Persistent), smoothedNormals = new NativeArray <Vector3>(svc, Allocator.Persistent); var result = new NativeArray <UnsafeHashMap <Vector3, Vector3> >(maxOverlapvertices, Allocator.Persistent); var resultParallel = new NativeArray <UnsafeHashMap <Vector3, Vector3> .ParallelWriter>(result.Length, Allocator.Persistent); // NormalBakeJob Data NativeArray <Vector3> normalsO = new NativeArray <Vector3>(originalMesh.normals, Allocator.Persistent), vertrxO = new NativeArray <Vector3>(originalMesh.vertices, Allocator.Persistent); var tangents = new NativeArray <Vector4>(originalMesh.tangents, Allocator.Persistent); var uv8 = new NativeArray <Vector2>(ovc, Allocator.Persistent); for (int i = 0; i < result.Length; i++) { result[i] = new UnsafeHashMap <Vector3, Vector3>(svc, Allocator.Persistent); resultParallel[i] = result[i].AsParallelWriter(); } CollectNormalJob collectNormalJob = new CollectNormalJob(normals, vertrx, resultParallel); BakeNormalJob normalBakeJob = new BakeNormalJob(vertrxO, normalsO, tangents, result, uv8); normalBakeJob.Schedule(ovc, 8, collectNormalJob.Schedule(svc, 100)).Complete(); var _uv8 = new Vector2[ovc]; uv8.CopyTo(_uv8); originalMesh.uv8 = _uv8; normals.Dispose(); vertrx.Dispose(); result.Dispose(); smoothedNormals.Dispose(); resultParallel.Dispose(); normalsO.Dispose(); vertrxO.Dispose(); tangents.Dispose(); uv8.Dispose(); }