예제 #1
0
    private Vector3 GetNormalInternal(SegmentParameter sParam)
    {
        SplineNode n0; SplineNode n1;
        SplineNode n2; SplineNode n3;

        splineInterpolator.GetNodeData(splineNodesInternal, sParam.normalizedIndex, AutoClose, out n0, out n1, out n2, out n3);

        Vector3 normal0;
        Vector3 normal1;
        Vector3 normal2;
        Vector3 normal3;

        if (normalMode == NormalMode.UseNodeNormal)
        {
            normal0 = n0.transform.TransformDirection(n0.normal).normalized;
            normal1 = n1.transform.TransformDirection(n1.normal).normalized;
            normal2 = n2.transform.TransformDirection(n2.normal).normalized;
            normal3 = n3.transform.TransformDirection(n3.normal).normalized;
        }
        else
        {
            normal0 = n0.transform.up;
            normal1 = n1.transform.up;
            normal2 = n2.transform.up;
            normal3 = n3.transform.up;
        }

        if (splineInterpolator is HermiteInterpolator)
        {
            HermiteInterpolator hermiteInterpolator = splineInterpolator as HermiteInterpolator;
            hermiteInterpolator.RecalcVectors(this, n0, n1, ref normal2, ref normal3);
        }

        return(splineInterpolator.InterpolateVector(sParam.normalizedParam, normal0, normal1, normal2.normalized, normal3.normalized, 0).normalized);
    }
	//Draw the spline in the scene view
	void OnDrawGizmos( )
	{
		SplineInterpolator splineInterpolator = new HermiteInterpolator( );
		
		float invertedLineRes = 1f/lineResolution;
		
		for( int i = 0; i < vectorData.Length; i++ )
		{
			for( float parameter = 0; parameter <= 1-invertedLineRes; parameter += invertedLineRes )
			{
				Vector3 position1 = splineInterpolator.InterpolateVector( parameter, i, false, vectorData, 0 );
				Vector3 position2 = splineInterpolator.InterpolateVector( parameter + invertedLineRes, i, false, vectorData, 0 );
				
				Gizmos.DrawLine( position1, position2 );
			}
		}
	}
    //Draw the spline in the scene view
    void OnDrawGizmos( )
    {
        SplineInterpolator splineInterpolator = new HermiteInterpolator( );

        float invertedLineRes = 1f / lineResolution;

        for (int i = 0; i < vectorData.Length; i++)
        {
            for (float parameter = 0; parameter <= 1 - invertedLineRes; parameter += invertedLineRes)
            {
                Vector3 position1 = splineInterpolator.InterpolateVector(parameter, i, false, vectorData, 0);
                Vector3 position2 = splineInterpolator.InterpolateVector(parameter + invertedLineRes, i, false, vectorData, 0);

                Gizmos.DrawLine(position1, position2);
            }
        }
    }
	void Update( )
	{
		//Calculate a continously changing parameter in the interval 0..1
		parameter = Mathf.PingPong( Time.realtimeSinceStartup, 1 );
		
		//Calculate the node index corresponding to the current spline parameter
		int nodeIndex = Mathf.FloorToInt( (vectorData.Length-1) * parameter );
		
		//Calculate a spline segment's length, assuming that all segment have the same length
		float segmentLength = 1f / (vectorData.Length-1);
		
		//Calculate the current segment parameter
		float segmentParameter = (parameter - (nodeIndex * segmentLength)) / segmentLength;
		
		//Create a new Hermite interpolator
		SplineInterpolator splineInterpolator = new HermiteInterpolator( );
		
		//Calculate the position on the spline and assign it to the transform-component of the animated object
		Vector3 positionOnSpline = splineInterpolator.InterpolateVector( segmentParameter, nodeIndex, false, vectorData, 0 );
		
		animatedObject.transform.position = positionOnSpline;
	}
    void Update( )
    {
        //Calculate a continously changing parameter in the interval 0..1
        parameter = Mathf.PingPong(Time.realtimeSinceStartup, 1);

        //Calculate the node index corresponding to the current spline parameter
        int nodeIndex = Mathf.FloorToInt((vectorData.Length - 1) * parameter);

        //Calculate a spline segment's length, assuming that all segment have the same length
        float segmentLength = 1f / (vectorData.Length - 1);

        //Calculate the current segment parameter
        float segmentParameter = (parameter - (nodeIndex * segmentLength)) / segmentLength;

        //Create a new Hermite interpolator
        SplineInterpolator splineInterpolator = new HermiteInterpolator( );

        //Calculate the position on the spline and assign it to the transform-component of the animated object
        Vector3 positionOnSpline = splineInterpolator.InterpolateVector(segmentParameter, nodeIndex, false, vectorData, 0);

        animatedObject.transform.position = positionOnSpline;
    }
예제 #6
0
    /// <summary>
    /// This function updates the spline. It is called automatically once in a while, if updateMode isn't set to DontUpdate.
    /// </summary>
    public void UpdateSpline( )
    {
        switch (interpolationMode)
        {
        case InterpolationMode.Linear:
            if (!(splineInterpolator is LinearInterpolator))
            {
                splineInterpolator = new LinearInterpolator( );
            }
            break;

        case InterpolationMode.Bezier:
            if (!(splineInterpolator is BezierInterpolator))
            {
                splineInterpolator = new BezierInterpolator( );
            }
            break;

        case InterpolationMode.Hermite:
            if (!(splineInterpolator is HermiteInterpolator))
            {
                splineInterpolator = new HermiteInterpolator( );
            }
            break;

        case InterpolationMode.BSpline:
            if (!(splineInterpolator is BSplineInterpolator))
            {
                splineInterpolator = new BSplineInterpolator( );
            }
            break;
        }

        //Count valid spline nodes
        int validNodes = 0;

        foreach (SplineNode sNode in splineNodesArray)
        {
            if (sNode != null)
            {
                ++validNodes;
            }
        }

        //Get relevant count
        int relevantNodeCount = GetRelevantNodeCount(validNodes);

        //Initialize the internal node array
        if (splineNodesInternal == null)
        {
            splineNodesInternal = new List <SplineNode>( );
        }

        splineNodesInternal.Clear( );

        if (!EnoughNodes(relevantNodeCount))
        {
            return;
        }

        splineNodesInternal.AddRange(splineNodesArray.GetRange(0, relevantNodeCount));
        splineNodesInternal.Remove(null);

        ReparameterizeCurve( );

        updateFrame = Time.frameCount;
    }