private void AddCommonReceiveComponents(EntityCommandBuffer commandBuffer) { commandBuffer.AddComponent(entity, new SetTransformToGameObjectTag()); var transformComponent = transformReader.Data; var defaultToSet = new TransformToSet { Position = transformComponent.Location.ToUnityVector() + world.GetExistingSystem <WorkerSystem>().Origin, Velocity = transformComponent.Velocity.ToUnityVector(), Orientation = transformComponent.Rotation.ToUnityQuaternion(), ApproximateRemoteTick = 0 }; var previousTransform = new DeferredUpdateTransform { Transform = transformComponent }; commandBuffer.AddComponent(entity, defaultToSet); commandBuffer.AddComponent(entity, previousTransform); commandBuffer.AddBuffer <BufferedTransform>(entity); }
protected override void OnUpdate() { interpolationGroup.SetFilter(TransformInternal.ComponentAuthority.NotAuthoritative); var interpolationConfigArray = interpolationGroup.GetSharedComponentDataArray <InterpolationConfig>(); var bufferedTransformArray = interpolationGroup.GetBufferArray <BufferedTransform>(); var spatialEntityIdArray = interpolationGroup.GetComponentDataArray <SpatialEntityId>(); var transformComponentArray = interpolationGroup.GetComponentDataArray <TransformInternal.Component>(); var lastTransformArray = interpolationGroup.GetComponentDataArray <DeferredUpdateTransform>(); for (int i = 0; i < transformComponentArray.Length; ++i) { var config = interpolationConfigArray[i]; var transformBuffer = bufferedTransformArray[i]; var lastTransformApplied = lastTransformArray[i].Transform; if (transformBuffer.Length >= config.MaxLoadMatchedBufferSize) { transformBuffer.Clear(); } if (transformBuffer.Length == 0) { var currentTransformComponent = transformComponentArray[i]; if (currentTransformComponent.PhysicsTick <= lastTransformApplied.PhysicsTick) { continue; } lastTransformArray[i] = new DeferredUpdateTransform { Transform = currentTransformComponent }; var transformToInterpolateTo = ToBufferedTransform(currentTransformComponent); uint ticksToFill = math.max((uint)config.TargetBufferSize, 1); if (ticksToFill > 1) { var transformToInterpolateFrom = ToBufferedTransformAtTick(lastTransformApplied, transformToInterpolateTo.PhysicsTick - ticksToFill + 1); transformBuffer.Add(transformToInterpolateFrom); for (uint j = 0; j < ticksToFill - 2; ++j) { transformBuffer.Add(InterpolateValues(transformToInterpolateFrom, transformToInterpolateTo, j + 1)); } } transformBuffer.Add(transformToInterpolateTo); continue; } var updates = updateSystem .GetEntityComponentUpdatesReceived <TransformInternal.Update>(spatialEntityIdArray[i].EntityId); for (int j = 0; j < updates.Count; ++j) { var update = updates[j].Update; UpdateLastTransform(ref lastTransformApplied, update); lastTransformArray[i] = new DeferredUpdateTransform { Transform = lastTransformApplied }; if (!update.PhysicsTick.HasValue) { continue; } var transformToInterpolateTo = ToBufferedTransform(lastTransformApplied); var transformToInterpolateFrom = transformBuffer[transformBuffer.Length - 1]; uint lastTickId = transformToInterpolateFrom.PhysicsTick; // This could go backwards if authority changes quickly between two workers with different loads if (lastTickId >= transformToInterpolateTo.PhysicsTick) { continue; } uint ticksToFill = math.max(transformToInterpolateTo.PhysicsTick - lastTickId, 1); for (uint k = 0; k < ticksToFill - 1; ++k) { transformBuffer.Add(InterpolateValues(transformToInterpolateFrom, transformToInterpolateTo, k + 1)); } transformBuffer.Add(transformToInterpolateTo); } } }
protected override void OnUpdate() { Entities.With(interpolationGroup).ForEach( (Entity entity, InterpolationConfig config, ref SpatialEntityId spatialEntityId, ref TransformInternal.Component transformInternal, ref DeferredUpdateTransform deferredUpdateTransform) => { var transformBuffer = EntityManager.GetBuffer <BufferedTransform>(entity); var lastTransformApplied = deferredUpdateTransform.Transform; if (transformBuffer.Length >= config.MaxLoadMatchedBufferSize) { transformBuffer.Clear(); } if (transformBuffer.Length == 0) { var currentTransformComponent = transformInternal; if (currentTransformComponent.PhysicsTick <= lastTransformApplied.PhysicsTick) { return; } deferredUpdateTransform = new DeferredUpdateTransform { Transform = currentTransformComponent }; var transformToInterpolateTo = ToBufferedTransform(currentTransformComponent); var ticksToFill = math.max((uint)config.TargetBufferSize, 1); if (ticksToFill > 1) { var transformToInterpolateFrom = ToBufferedTransformAtTick(lastTransformApplied, transformToInterpolateTo.PhysicsTick - ticksToFill + 1); transformBuffer.Add(transformToInterpolateFrom); for (uint j = 0; j < ticksToFill - 2; ++j) { transformBuffer.Add(InterpolateValues(transformToInterpolateFrom, transformToInterpolateTo, j + 1)); } } transformBuffer.Add(transformToInterpolateTo); return; } var updates = updateSystem .GetEntityComponentUpdatesReceived <TransformInternal.Update>(spatialEntityId.EntityId); for (var j = 0; j < updates.Count; ++j) { var update = updates[j].Update; UpdateLastTransform(ref lastTransformApplied, update); deferredUpdateTransform = new DeferredUpdateTransform { Transform = lastTransformApplied }; if (!update.PhysicsTick.HasValue) { continue; } var transformToInterpolateTo = ToBufferedTransform(lastTransformApplied); var transformToInterpolateFrom = transformBuffer[transformBuffer.Length - 1]; var lastTickId = transformToInterpolateFrom.PhysicsTick; // This could go backwards if authority changes quickly between two workers with different loads if (lastTickId >= transformToInterpolateTo.PhysicsTick) { continue; } var ticksToFill = math.max(transformToInterpolateTo.PhysicsTick - lastTickId, 1); for (uint k = 0; k < ticksToFill - 1; ++k) { transformBuffer.Add(InterpolateValues( transformToInterpolateFrom, transformToInterpolateTo, k + 1)); } transformBuffer.Add(transformToInterpolateTo); } }); }