예제 #1
0
    // Prepare key and control point game objects and set them inactive
    private void ResetControlPoints()
    {
        int maxControlPointNum = CurvePlugin.GetVecKeyNum(m_id) * 4 + 2;

        while (m_controlPointList.Count < maxControlPointNum)
        {
            GameObject controlPoint = Instantiate(m_controlPointPrefab, m_controlPointParent.transform);
            m_controlPointList.Add(controlPoint);
            // controlPoint.SetActive(false);
        }

        // Hide all control points
        foreach (GameObject obj in m_controlPointList)
        {
            obj.SetActive(false);
        }
    }
예제 #2
0
    // Reset all line renderer
    private void ResetLineRenderer()
    {
        // Clear line renderers
        m_curveLineRenderer.positionCount        = 0;
        m_controlPointLineRenderer.positionCount = 0;
        int maxKeyNum = CurvePlugin.GetVecKeyNum(m_id);

        // Each key has a hermite control point line
        while (m_hermiteControlPointLineRenderers.Count < maxKeyNum)
        {
            GameObject lineObj = Instantiate(m_controlPointLinePrefab, m_lineRendererParent.transform);
            m_hermiteControlPointLineRenderers.Add(lineObj.GetComponent <LineRenderer>());
            // lineObj.SetActive(false);
        }
        // Hide all lines
        foreach (LineRenderer lineRenderer in m_hermiteControlPointLineRenderers)
        {
            lineRenderer.positionCount = 0;
        }
    }
예제 #3
0
    public void ClearCurve()
    {
        CurvePlugin.ResetCurve(m_id);
        if (CurvePlugin.GetVecKeyNum(m_id) != 0)
        {
            Debug.LogError("Clear Error");
        }
        foreach (GameObject obj in m_keyPointList)
        {
            if (obj)
            {
                Destroy(obj);
            }
        }
        foreach (GameObject obj in m_controlPointList)
        {
            if (obj)
            {
                Destroy(obj);
            }
        }
        m_keyPointList.Clear();
        m_controlPointList.Clear();
        m_rotationKeyPointList.Clear();

        // Clear line renderers
        m_curveLineRenderer.positionCount        = 0;
        m_controlPointLineRenderer.positionCount = 0;
        foreach (LineRenderer line in m_hermiteControlPointLineRenderers)
        {
            Destroy(line.gameObject);
        }
        m_hermiteControlPointLineRenderers.Clear();

        m_curveChanged = true;
    }
예제 #4
0
    public void DrawCurve()
    {
        ResetControlPoints();
        ResetLineRenderer();
        if (CurvePlugin.GetVecKeyNum(m_id) == 0)
        {
            return;
        }

        // Get cached curve points
        int    cachedPointNum = 0;
        IntPtr cachedPointPtr = IntPtr.Zero;

        CurvePlugin.GetCachedCurve(m_id, ref cachedPointNum, ref cachedPointPtr);
        if (cachedPointNum < 2)
        {
            return;
        }
        List <Vector3> cachedPoints = PluginHelpFunction.DoubleArrayPointerToVector3List(cachedPointNum, cachedPointPtr);

        // Reset LineRenderer to draw the curve
        m_curveLineRenderer.positionCount = cachedPointNum;
        m_curveLineRenderer.SetPositions(cachedPoints.ToArray());

        // Get Control Points
        if (!m_showControlPoint || m_vecInterpolationType == VecInterpolationType.LINEAR)
        {
            return;
        }
        int    controlPointNum = 0;
        IntPtr controlPointPtr = IntPtr.Zero;

        double[] startPoint = new double[3];
        double[] endPoint   = new double[3];
        CurvePlugin.GetControlPoints(m_id, startPoint, endPoint, ref controlPointNum, ref controlPointPtr);
        // The start point and the end point are not included, so the size is controlPointNum - 2
        List <Vector3> controlPoints = PluginHelpFunction.DoubleArrayPointerToVector3List(controlPointNum - 2, controlPointPtr);

        // Draw control point lines and set the position of control points
        switch (m_vecInterpolationType)
        {
        case VecInterpolationType.CUBIC_BERNSTEIN:
        case VecInterpolationType.CUBIC_CASTELJAU:
        case VecInterpolationType.CUBIC_MATRIX:
            m_controlPointLineRenderer.positionCount = controlPointNum;
            // Add the start point and the end point
            controlPoints.Insert(0, PluginHelpFunction.DoubleArrayToVector3(startPoint));
            controlPoints.Add(PluginHelpFunction.DoubleArrayToVector3(endPoint));
            m_controlPointLineRenderer.SetPositions(controlPoints.ToArray());
            // Set control points
            for (int i = 0; i < controlPoints.Count; ++i)
            {
                m_controlPointList[i].transform.position = controlPoints[i];
                // Skip key points
                if (i == 0 || (i - 1) % 4 == 1 || (i - 1) % 4 == 2 || i == controlPointNum - 1)
                {
                    m_controlPointList[i].SetActive(true);
                }
            }
            break;

        case VecInterpolationType.CUBIC_HERMITE:
            // Each key has a hermite control point line
            for (int i = 0; i < m_keyPointList.Count; ++i)
            {
                // Set the line renderer
                Vector3      slope        = controlPoints[i];
                Vector3      pos0         = m_keyPointList[i].transform.position;
                Vector3      pos1         = pos0 + slope;
                LineRenderer lineRenderer = m_hermiteControlPointLineRenderers[i];
                lineRenderer.positionCount = 2;
                lineRenderer.SetPosition(0, pos0);
                lineRenderer.SetPosition(1, pos1);
                // Set the control point
                m_controlPointList[i + 1].transform.position = pos1;
                m_controlPointList[i + 1].SetActive(true);
            }
            break;

        case VecInterpolationType.CUBIC_BSPLINE:
            // No start point and end point
            m_controlPointLineRenderer.positionCount = controlPointNum - 2;
            m_controlPointLineRenderer.SetPositions(controlPoints.ToArray());
            // Set control points
            for (int i = 0; i < controlPoints.Count; ++i)
            {
                m_controlPointList[i + 1].transform.position = controlPoints[i];
                // Skip the first and the last control points because they are at the same position as the key points
                if (!(i == 1 || i == controlPointNum - 4))
                {
                    m_controlPointList[i + 1].SetActive(true);
                }
            }
            break;

        default:
            break;
        }
    }