示例#1
0
        void LookAtEntity()
        {
            var parentFromEntity = GetComponentDataFromEntity <Parent>(true);
            var lookFromEntity   = GetComponentDataFromEntity <LookAtEntity>(true);

            Entities
            .WithReadOnly(parentFromEntity)
            .WithReadOnly(lookFromEntity)
            .ForEach((Entity entity, int entityInQueryIndex, ref Rotation rotation, in LookAtEntity lookAt) => {
                if (!HasComponent <LocalToWorld>(entity) || !HasComponent <LocalToWorld>(lookAt.Value))
                {
                    return;
                }
                // Avoid multiple nested rotations
                if (TransformUtility.ExistsInHierarchy(entity, parentFromEntity, lookFromEntity))
                {
                    return;
                }
                var sourcePos = GetComponent <LocalToWorld>(entity).Value.Position();
                var targetPos = GetComponent <LocalToWorld>(lookAt.Value).Value.Position();
                var forward   = math.normalize(sourcePos - targetPos);
                var up        = math.normalize(GetComponent <LocalToWorld>(lookAt.Value).Value.Up());
                if (!forward.Equals(up) && math.lengthsq(forward + up) != 0)
                {
                    rotation.Value = quaternion.LookRotation(forward, up);
                }
            }).ScheduleParallel();
        }
            public void Execute(Entity entity, int index, [ReadOnly, ChangedFilter] ref LookAtPosition lookAt, [WriteOnly] ref LocalToWorld localToWorld)
            {
                if (!LocalToWorldFromEntity.Exists(entity))
                {
                    return;
                }
                // Avoid multiple nested rotations
                if (TransformUtility.ExistsInHierarchy(entity, ParentFromEntity, LookFromEntity))
                {
                    return;
                }
                var sourcePos = LocalToWorldFromEntity[entity].Value.Position();
                var forward   = math.normalize(sourcePos - lookAt.Value);
                var up        = forward.Up();

                localToWorld.Value = math.mul(new float4x4(quaternion.LookRotation(forward, up), localToWorld.Position),
                                              float4x4.Scale(localToWorld.Value.Scale()));
            }
            public void Execute(ArchetypeChunk chunk, int chunkIndex, int firstEntityIndex)
            {
                var localToWorldChunkChanged = chunk.DidChange(LocalToWorldType, LastSystemVersion);

                if (ChangedLookAtEntities.Length == 0 && !localToWorldChunkChanged)
                {
                    return;
                }

                var lookAtArray       = chunk.GetNativeArray(LookAtType);
                var localToWorldArray = new NativeArray <LocalToWorld>();

                for (var i = 0; i < chunk.Count; ++i)
                {
                    var lookAtEntity = lookAtArray[i].Value;
                    if (!LocalToWorldFromEntity.Exists(lookAtEntity))
                    {
                        continue;
                    }
                    // Avoid multiple nested rotations
                    if (TransformUtility.ExistsInHierarchy(lookAtEntity, ParentFromEntity, LookFromEntity))
                    {
                        continue;
                    }
                    if (!localToWorldChunkChanged && !ChangedLookAtEntities.ContainsKey(lookAtEntity))
                    {
                        continue;
                    }

                    localToWorldArray = localToWorldArray.IsCreated ? localToWorldArray : chunk.GetNativeArray(LocalToWorldType);
                    var localToWorld = localToWorldArray[i];
                    var forward      = math.normalize(LocalToWorldFromEntity[lookAtEntity].Value.Forward());
                    var up           = math.normalize(LocalToWorldFromEntity[lookAtEntity].Value.Up());
                    if (!math.cross(forward, up).Equals(float3.zero))
                    {
                        localToWorldArray[i] = new LocalToWorld {
                            Value = math.mul(new float4x4(quaternion.LookRotation(forward, up), localToWorld.Position),
                                             float4x4.Scale(localToWorld.Value.Scale()))
                        }
                    }
                    ;
                }
            }
        }
            public void Execute(Entity entity, int index, [ReadOnly] ref LookAtEntity lookAt, [WriteOnly] ref Rotation rotation)
            {
                if (!LocalToWorldFromEntity.Exists(entity) || !LocalToWorldFromEntity.Exists(lookAt.Value))
                {
                    return;
                }
                // Avoid multiple nested rotations
                if (TransformUtility.ExistsInHierarchy(entity, ParentFromEntity, LookFromEntity))
                {
                    return;
                }
                var sourcePos = LocalToWorldFromEntity[entity].Value.Position();
                var targetPos = LocalToWorldFromEntity[lookAt.Value].Value.Position();
                var forward   = math.normalize(sourcePos - targetPos);
                var up        = math.normalize(LocalToWorldFromEntity[lookAt.Value].Value.Up());

                if (!forward.Equals(up) && math.lengthsq(forward + up) != 0)
                {
                    rotation.Value = quaternion.LookRotation(forward, up);
                }
            }