示例#1
0
 public void ReadFromStream(Unity.SnapshotDebugger.Buffer buffer)
 {
     startTransform = buffer.ReadAffineTransform();
     controlPoints  = buffer.ReadNativeArray <float3>(out allocator);
     navParams      = buffer.ReadBlittable <NavigationParams>();
     pathSpline.ReadFromStream(buffer);
     nextControlPoint = buffer.Read32();
     isBuilt          = buffer.ReadBlittable <BlittableBool>();
     outputTrajectory = buffer.ReadBlittable <DebugIdentifier>();
 }
        public T ReadObjectFromIdentifier <T>(DebugIdentifier identifier) where T : struct, IDebugObject
        {
            DebugReference reference = FindObjectReference(identifier);

            if (!reference.IsValid)
            {
                throw new ArgumentException("Identifier not bound to valid reference in debug memory", "identifier");
            }

            return(ReadObject <T>(reference));
        }
        public DebugReference FindObjectReference(DebugIdentifier identifier)
        {
            for (DebugReference debugRef = FirstOrDefault; debugRef.IsValid; debugRef = Next(debugRef))
            {
                if (debugRef.identifier.Equals(identifier))
                {
                    return(debugRef);
                }
            }

            return(DebugReference.Invalid);
        }
 public void ReadFromStream(Buffer buffer)
 {
     trajectory       = buffer.ReadBlittable <DebugIdentifier>();
     candidates       = buffer.ReadBlittable <DebugIdentifier>();
     samplingTime     = buffer.ReadBlittable <DebugIdentifier>();
     closestMatch     = buffer.ReadBlittable <DebugIdentifier>();
     deviationTable   = buffer.ReadBlittable <DebugIdentifier>();
     trajectoryWeight = buffer.ReadSingle();
     maxDeviation     = buffer.ReadSingle();
     deviated         = buffer.ReadBoolean();
     debugName        = buffer.ReadFixedString64();
 }
        Header CreateAndWriteHeader(DebugIdentifier identifier, bool dataOnly)
        {
            Header header = new Header()
            {
                reference = new DebugReference()
                {
                    identifier = identifier,
                    address    = buffer.Length,
                    group      = group,
                    dataOnly   = dataOnly
                },
                nextAddress = -1
            };

            buffer.WriteBlittable(header);

            return(header);
        }
 bool IsValid(DebugIdentifier identifier)
 {
     return(identifier.IsValid && identifier.version == version);
 }
示例#7
0
        public void GenerateTrajectory(ref MotionSynthesizer synthesizer, ref Trajectory trajectory)
        {
            if (GoalReached)
            {
                return;
            }

            Assert.IsTrue(trajectory.Length > 0);
            if (trajectory.Length == 0)
            {
                return;
            }

            AffineTransform rootTransform = synthesizer.WorldRootTransform;

            float maxSpeedAtCorner = navParams.desiredSpeed;

            if (nextControlPoint < pathSpline.segments.Length - 1)
            {
                float3 curSegmentDir  = pathSpline.segments[nextControlPoint].SegmentDirection;
                float3 nextSegmentDir = pathSpline.segments[nextControlPoint + 1].SegmentDirection;

                float alignment = math.max(math.dot(curSegmentDir, nextSegmentDir), 0.0f);
                maxSpeedAtCorner = math.lerp(navParams.maxSpeedAtRightAngle, navParams.desiredSpeed, alignment);
            }

            int halfTrajectoryLength = trajectory.Length / 2;

            float deltaTime = synthesizer.Binary.TimeHorizon / halfTrajectoryLength;
            float distance  = 0.0f;

            float speed = math.length(synthesizer.CurrentVelocity);
            float remainingDistOnSpline  = pathSpline.ComputeCurveLength(nextControlPoint);
            float remainingDistOnSegment = pathSpline.segments[nextControlPoint].CurveLength;

            for (int index = halfTrajectoryLength; index < trajectory.Length; ++index)
            {
                if (remainingDistOnSpline > 0.0f)
                {
                    // acceleration to reach desired speed
                    float acceleration = math.clamp((navParams.desiredSpeed - speed) / deltaTime,
                                                    -navParams.maximumDeceleration,
                                                    navParams.maximumAcceleration);

                    // decelerate if needed to reach maxSpeedAtCorner
                    float brakingDistance = 0.0f;
                    if (remainingDistOnSegment > 0.0f && speed > maxSpeedAtCorner)
                    {
                        brakingDistance = NavigationParams.ComputeDistanceToReachSpeed(speed, maxSpeedAtCorner, -navParams.maximumDeceleration);
                        if (remainingDistOnSegment <= brakingDistance)
                        {
                            acceleration = math.min(acceleration, NavigationParams.ComputeAccelerationToReachSpeed(speed, maxSpeedAtCorner, remainingDistOnSegment));
                        }
                    }

                    // decelerate if needed to stop when last control point is reached
                    brakingDistance = NavigationParams.ComputeDistanceToReachSpeed(speed, 0.0f, -navParams.maximumDeceleration);
                    if (remainingDistOnSpline <= brakingDistance)
                    {
                        acceleration = math.min(acceleration, NavigationParams.ComputeAccelerationToReachSpeed(speed, 0.0f, remainingDistOnSpline));
                    }

                    speed += acceleration * deltaTime;
                }
                else
                {
                    speed = 0.0f;
                }

                float moveDist = speed * deltaTime;
                remainingDistOnSegment -= moveDist;
                remainingDistOnSpline  -= moveDist;
                distance += moveDist;

                AffineTransform point = EvaluatePointAtDistance(distance);
                trajectory[index] = rootTransform.inverseTimes(point);
            }

            synthesizer.DebugPushGroup();

            synthesizer.DebugWriteUnblittableObject(ref trajectory);
            outputTrajectory = trajectory.debugIdentifier;

            synthesizer.DebugWriteUnblittableObject(ref this);
        }
        public void Draw(Camera camera, ref MotionSynthesizer synthesizer, DebugMemory debugMemory, SamplingTime debugSamplingTime, ref DebugDrawOptions options)
        {
            int fragmentSlot = 0;

            DebugDraw.SetMovableTextTitle(options.textWindowIdentifier, DebugWindowTitle);

            DeviationTable deviationTable = this.deviationTable.IsValid ? debugMemory.ReadObjectFromIdentifier <DeviationTable>(this.deviationTable) : DeviationTable.CreateInvalid();

            SamplingTime samplingTime = GetSamplingTime(debugMemory, this.samplingTime);

            if (samplingTime.IsValid)
            {
                if ((options.drawFlags & DebugDrawFlags.InputFragment) > 0)
                {
                    DebugDraw.DrawFragment(ref synthesizer, samplingTime, options.inputOutputFragmentColor, options.timeOffset, synthesizer.WorldRootTransform);
                    ++fragmentSlot;
                }
                DebugDraw.AddMovableTextLine(options.textWindowIdentifier, GetFragmentDebugText(ref synthesizer, "Input Clip", samplingTime, options.timeOffset, ref deviationTable), options.inputOutputFragTextColor);
            }

            SamplingTime    closestMatch         = GetSamplingTime(debugMemory, this.closestMatch);
            DebugIdentifier outputTimeIdentifier = this.closestMatch;

            if (closestMatch.IsValid)
            {
                if ((options.drawFlags & DebugDrawFlags.BestFragment) > 0)
                {
                    DebugDraw.DrawFragment(ref synthesizer, closestMatch, options.bestFragmentColor, options.timeOffset, GetAnchor(synthesizer.WorldRootTransform, fragmentSlot++ *options.distanceOffset, camera));

                    DebugDraw.AddMovableTextLine(options.textWindowIdentifier, GetFragmentDebugText(ref synthesizer, "Best Match Fragment", closestMatch, options.timeOffset, ref deviationTable), options.bestFragTextColor);
                    if (maxDeviation >= 0.0f)
                    {
                        string deviationMsg = GetDeviationText();
                        DebugDraw.AddMovableTextLine(options.textWindowIdentifier, deviationMsg, options.bestFragTextColor);
                    }

                    Nullable <TrajectoryHeuristicDebug> trajectoryHeuristic = FindTrajectoryHeuristic(debugMemory);
                    if (trajectoryHeuristic.HasValue)
                    {
                        trajectoryHeuristic.Value.DrawDebugText(ref options);

                        outputTimeIdentifier = trajectoryHeuristic.Value.outputTime;
                    }
                }
            }

            SamplingTime outputTime = GetOutputPlayTime(debugMemory, samplingTime, closestMatch, outputTimeIdentifier);

            if ((options.drawFlags & DebugDrawFlags.OutputFragment) > 0)
            {
                DebugDraw.DrawFragment(ref synthesizer, outputTime, options.inputOutputFragmentColor, options.timeOffset, GetAnchor(synthesizer.WorldRootTransform, fragmentSlot++ *options.distanceOffset, camera));
                DebugDraw.AddMovableTextLine(options.textWindowIdentifier, GetFragmentDebugText(ref synthesizer, "Output Clip", outputTime, options.timeOffset, ref deviationTable, false), options.inputOutputFragTextColor);
            }

            if (debugSamplingTime.IsValid)
            {
                if ((options.drawFlags & DebugDrawFlags.SelectedFragment) > 0)
                {
                    int slot = fragmentSlot > 0 ? -1 : 0;
                    DebugDraw.DrawFragment(ref synthesizer, debugSamplingTime, options.selectedFragmentColor, options.timeOffset, GetAnchor(synthesizer.WorldRootTransform, slot * options.distanceOffset, camera));
                    DebugDraw.AddMovableTextLine(options.textWindowIdentifier, GetFragmentDebugText(ref synthesizer, "Selected Clip", debugSamplingTime, options.timeOffset, ref deviationTable), options.selectedFragTextColor);
                }
            }

            deviationTable.Dispose();

            if ((options.drawFlags & DebugDrawFlags.Trajectory) > 0 && this.trajectory.IsValid)
            {
                using (Trajectory trajectory = debugMemory.ReadObjectFromIdentifier <Trajectory>(this.trajectory))
                {
                    Binary.TrajectoryFragmentDisplay.Options trajectoryOptions = Binary.TrajectoryFragmentDisplay.Options.Create();

                    DebugExtensions.DebugDrawTrajectory(synthesizer.WorldRootTransform,
                                                        trajectory,
                                                        synthesizer.Binary.SampleRate,
                                                        options.inputTrajectoryColor,
                                                        options.inputTrajectoryColor,
                                                        trajectoryOptions.showForward);
                }
            }
        }
 static internal SamplingTime GetSamplingTime(DebugMemory debugMemory, DebugIdentifier debugIdentifier)
 {
     return(debugIdentifier.IsValid ? debugMemory.ReadObjectFromIdentifier <SamplingTime>(debugIdentifier) : SamplingTime.Invalid);
 }
        SamplingTime GetOutputPlayTime(DebugMemory debugMemory, SamplingTime samplingTime, SamplingTime closestMatch, DebugIdentifier outputTimeIdentifier)
        {
            if (!closestMatch.IsValid)
            {
                return(samplingTime);
            }

            int playAtTimeTypeCode = BurstRuntime.GetHashCode32 <PlayAtTimeDebug>();

            for (DebugReference reference = debugMemory.FirstOrDefault; reference.IsValid; reference = debugMemory.Next(reference))
            {
                if (reference.identifier.typeHashCode != playAtTimeTypeCode)
                {
                    continue;
                }

                PlayAtTimeDebug playAtTime = debugMemory.ReadObject <PlayAtTimeDebug>(reference);
                if (playAtTime.playTime.Equals(outputTimeIdentifier))
                {
                    return(closestMatch);
                }
            }

            return(samplingTime);
        }