#Struggling with spawning objects that align to surface normal

1 messages · Page 1 of 1 (latest)

golden sparrow
#

WorldPoints and WorldNormals is two Vector3 arrays that contain a world point and it's surface normal direction. I know this is correct because the Debug.DrawLine is outputting at the proper location pointing in the correct direction.

The first screenshot is the floor blobs, the second screenshot is on a wall surface. I can't figure out how to spawn these blobs with the correct rotation so the worldNormals are respected.

Can anyone point me in the right direction (pun intended)

    [GenerateTestsForBurstCompatibility]
    public struct SpawnJob : IJobParallelFor
    {
        public Entity Prototype;
        public NativeArray<Vector3> WorldPoints;
        public NativeArray<Vector3> WorldNormals;
        public bool singleMat;
        public EntityCommandBuffer.ParallelWriter Ecb;

        public void Execute(int index)
        {
            // Render the blob
            var e = Ecb.Instantiate(index, Prototype);
            int matIndex = singleMat ? 0 : index;
            Ecb.SetComponent(index, e, MaterialMeshInfo.FromRenderMeshArrayIndices(matIndex, 0));
            Debug.DrawLine(WorldPoints[index], WorldPoints[index] + WorldNormals[index], Color.red, 300);
            quaternion rotation = quaternion.LookRotation((WorldPoints[index] - (WorldPoints[index] + WorldNormals[index]).normalized), math.up());
            Ecb.SetComponent(index, e, new LocalToWorld
            {
                Value = float4x4.TRS(WorldPoints[index], rotation, 1f)
            });
        }
    }