Exemplo n.º 1
0
 public void CaptureSnapshot(int index)
 {
     if (_spline == null)
     {
         return;
     }
     if ((channels.Length > 0 && _spline.pointCount != channels[0].points.Length && index != 0))
     {
         Debug.LogError("Point count must be the same as " + _spline.pointCount);
         return;
     }
     channels[index].points = _spline.GetPoints(space);
     UpdateMorph();
 }
Exemplo n.º 2
0
        public static double ScreenPointToSplinePercent(SplineComputer computer, Vector2 screenPoint)
        {
            SplinePoint[] points          = computer.GetPoints();
            float         closestDistance = (screenPoint - HandleUtility.WorldToGUIPoint(points[0].position)).sqrMagnitude;
            double        closestPercent  = 0.0;
            double        add             = computer.moveStep;

            if (computer.type == Spline.Type.Linear)
            {
                add /= 2f;
            }
            int count = 0;

            for (double i = add; i < 1.0; i += add)
            {
                SplineResult result = computer.Evaluate(i);
                Vector2      point  = HandleUtility.WorldToGUIPoint(result.position);
                float        dist   = (point - screenPoint).sqrMagnitude;
                if (dist < closestDistance)
                {
                    closestDistance = dist;
                    closestPercent  = i;
                }
                count++;
            }
            return(closestPercent);
        }
Exemplo n.º 3
0
 public virtual void Init(SplineComputer comp)
 {
     computer   = comp;
     lastClosed = comp.isClosed;
     lastType   = comp.type;
     lastPoints = comp.GetPoints(SplineComputer.Space.Local);
 }
Exemplo n.º 4
0
        void OnSceneGUI()
        {
            Node node = (Node)target;

            Node.Connection[] connections = node.GetConnections();

            for (int i = 0; i < connections.Length; i++)
            {
                SplineEditor.DrawSplineComputer(connections[i].computer, SceneView.currentDrawingSceneView.camera, false, false, 0.5f);
            }

            bool update = false;

            if (position != node.transform.position)
            {
                position = node.transform.position;
                update   = true;
            }
            if (scale != node.transform.localScale)
            {
                scale  = node.transform.localScale;
                update = true;
            }
            if (rotation != node.transform.rotation)
            {
                rotation = node.transform.rotation;
                update   = true;
            }
            if (update)
            {
                node.UpdateConnectedComputers();
            }

            if (addComp == null)
            {
                return;
            }
            SplinePoint[] points       = addComp.GetPoints();
            Transform     camTransform = SceneView.currentDrawingSceneView.camera.transform;

            SplineEditor.DrawSplineComputer(addComp, SceneView.currentDrawingSceneView.camera, false, false, 0.5f);
            for (int i = 0; i < availablePoints.Length; i++)
            {
                if (addComp.isClosed && i == points.Length - 1)
                {
                    break;
                }
                Handles.color = addComp.editorPathColor;
                if (Handles.Button(points[availablePoints[i]].position, Quaternion.LookRotation(-camTransform.forward, camTransform.up), HandleUtility.GetHandleSize(points[availablePoints[i]].position) * 0.1f, HandleUtility.GetHandleSize(points[availablePoints[i]].position) * 0.2f, Handles.CircleCap))
                {
                    AddConnection(addComp, availablePoints[i]);
                    break;
                }
            }
        }
Exemplo n.º 5
0
 /// <summary>
 /// Deletes columns in the spline.
 /// </summary>
 /// <param name="newCount"> Overall count of points wanted in the new spline. </param>
 /// <param name="sc"> SplineComputer of the spline that is being adjusted. </param>
 protected void DeleteColumns(int newCount, SplineComputer sc)
 {
     if (newCount < basePoints.Length)
     {
         return;
     }
     SplinePoint[] short_segment = new SplinePoint[newCount];
     SplinePoint[] old_points    = sc.GetPoints();
     Array.Copy(old_points, 0, short_segment, 0, newCount);
     sc.SetPoints(short_segment);
     currentPointCount = newCount;
 }
Exemplo n.º 6
0
            public BendProperty(Transform t, bool isParent = false)
            {
                parent           = isParent;
                transform        = new TS_Transform(t);
                originalPosition = t.localPosition;
                originalScale    = t.localScale;
                originalRotation = t.localRotation;
                parentRotation   = t.transform.rotation;
                if (t.transform.parent != null)
                {
                    parentRotation = t.transform.parent.rotation;
                }
                filter   = t.GetComponent <MeshFilter>();
                collider = t.GetComponent <MeshCollider>();
                if (filter != null && filter.sharedMesh != null)
                {
                    originalMesh = filter.sharedMesh;
                    normals      = originalMesh.normals;
                    for (int i = 0; i < normals.Length; i++)
                    {
                        normals[i] = transform.transform.TransformDirection(normals[i]).normalized;
                    }
                }

                if (collider != null && collider.sharedMesh != null)
                {
                    originalColliderMesh = collider.sharedMesh;
                    colliderNormals      = originalColliderMesh.normals;
                    for (int i = 0; i < colliderNormals.Length; i++)
                    {
                        colliderNormals[i] = transform.transform.TransformDirection(colliderNormals[i]);
                    }
                }
                if (!parent)
                {
                    splineComputer = t.GetComponent <SplineComputer>();
                }
                if (splineComputer != null)
                {
                    if (splineComputer.isClosed)
                    {
                        originalSpline.Close();
                    }
                    destinationSpline        = new Spline(originalSpline.type);
                    destinationSpline.points = new SplinePoint[originalSpline.points.Length];
                    destinationSpline.points = splineComputer.GetPoints();
                    if (splineComputer.isClosed)
                    {
                        destinationSpline.Close();
                    }
                }
            }
Exemplo n.º 7
0
        Vector3 GetOrigin(SplineComputer comp)
        {
            Vector3 avg = Vector3.zero;

            SplinePoint[] points = comp.GetPoints(SplineComputer.Space.Local);
            for (int i = 0; i < comp.pointCount; i++)
            {
                avg += points[i].position;
            }
            if (points.Length > 0)
            {
                avg /= points.Length;
            }
            return(avg);
        }
Exemplo n.º 8
0
            public void AddChannel(string name)
            {
                if (morphStates.Length > 0 && computer.pointCount != morphStates[0].points.Length)
                {
                    Debug.LogError("Point count must be the same as " + computer.pointCount);
                    return;
                }
                SplineMorphState newMorph = new SplineMorphState();

                newMorph.points = computer.GetPoints(SplineComputer.Space.Local);
                newMorph.name   = name;
                if (morphStates.Length > 0)
                {
                    newMorph.percent = 0f;
                }
                SplineMorphState[] newMorphs = new SplineMorphState[morphStates.Length + 1];
                morphStates.CopyTo(newMorphs, 0);
                morphStates = newMorphs;
                morphStates[morphStates.Length - 1] = newMorph;
            }
Exemplo n.º 9
0
        public static void DrawSplineComputer(SplineComputer comp, double fromPercent = 0.0, double toPercent = 1.0, float alpha = 1f)
        {
            if (comp == null)
            {
                return;
            }
            Color prevColor   = Handles.color;
            Color orange      = new Color(1f, 0.564f, 0f);
            Color handleColor = comp.hasMorph && !MorphWindow.editShapeMode ? orange : comp.editorPathColor;

            handleColor.a = alpha;
            Handles.color = handleColor;
            if (comp.pointCount < 2)
            {
                return;
            }
            double add = comp.moveStep;

            if (add < 0.0025)
            {
                add = 0.0025;
            }

            if (comp.type == Spline.Type.BSpline && comp.pointCount > 1)
            {
                SplinePoint[] compPoints = comp.GetPoints();
                Handles.color = new Color(handleColor.r, handleColor.g, handleColor.b, 0.5f * alpha);
                for (int i = 0; i < compPoints.Length - 1; i++)
                {
                    Handles.DrawLine(compPoints[i].position, compPoints[i + 1].position);
                }
                Handles.color = handleColor;
            }

            if (!comp.drawThinckness)
            {
                double percent = fromPercent;
                fromPos = comp.EvaluatePosition(percent);
                toPos   = Vector3.zero;
                while (true)
                {
                    percent = DMath.Move(percent, toPercent, add);
                    toPos   = comp.EvaluatePosition(percent);
                    Handles.DrawLine(fromPos, toPos);
                    if (percent == toPercent)
                    {
                        break;
                    }
                    fromPos = toPos;
                }
                return;
            }
            else
            {
                Camera editorCamera = SceneView.currentDrawingSceneView.camera;
                double percent      = fromPercent;
                comp.Evaluate(fromResult, percent);
                Vector3 fromNormal = fromResult.normal;
                if (comp.billboardThickness)
                {
                    fromNormal = (editorCamera.transform.position - fromResult.position).normalized;
                }
                Vector3 fromRight = Vector3.Cross(fromResult.direction, fromNormal).normalized *fromResult.size * 0.5f;
                while (true)
                {
                    percent  = DMath.Move(percent, toPercent, add);
                    toResult = comp.Evaluate(percent);
                    Vector3 toNormal = toResult.normal;
                    if (comp.billboardThickness)
                    {
                        toNormal = (editorCamera.transform.position - toResult.position).normalized;
                    }
                    Vector3 toRight = Vector3.Cross(toResult.direction, toNormal).normalized *toResult.size * 0.5f;

                    Handles.DrawLine(fromResult.position + fromRight, toResult.position + toRight);
                    Handles.DrawLine(fromResult.position - fromRight, toResult.position - toRight);
                    Handles.DrawLine(fromResult.position + fromRight, fromResult.position - fromRight);
                    if (percent == toPercent)
                    {
                        break;
                    }
                    fromResult = toResult;
                    fromNormal = toNormal;
                    fromRight  = toRight;
                }
            }
            Handles.color = prevColor;
        }
Exemplo n.º 10
0
        protected virtual void OnSceneGUI()
        {
            Node node = (Node)target;

            Node.Connection[] connections = node.GetConnections();

            for (int i = 0; i < connections.Length; i++)
            {
                SplineDrawer.DrawSplineComputer(connections[i].computer, 0.0, 1.0, 0.5f);
            }

            bool update = false;

            if (position != node.transform.position)
            {
                position = node.transform.position;
                update   = true;
            }
            if (scale != node.transform.localScale)
            {
                scale  = node.transform.localScale;
                update = true;
            }
            if (rotation != node.transform.rotation)
            {
                rotation = node.transform.rotation;
                update   = true;
            }
            if (update)
            {
                node.UpdateConnectedComputers();
            }

            if (addComp == null)
            {
                if (connections.Length > 0)
                {
                    bool bezier = false;
                    for (int i = 0; i < connections.Length; i++)
                    {
                        if (connections[i].computer == null)
                        {
                            continue;
                        }
                        if (connections[i].computer.type == Spline.Type.Bezier)
                        {
                            bezier = true;
                            continue;
                        }
                    }
                    if (bezier && node.type == Node.Type.Smooth)
                    {
                        if (connections[0].computer != null)
                        {
                            SplinePoint point = node.GetPoint(0);
                            Handles.DrawDottedLine(node.transform.position, point.tangent, 6f);
                            Handles.DrawDottedLine(node.transform.position, point.tangent2, 6f);
                            Vector3 lastPos  = point.tangent;
                            bool    setPoint = false;
                            point.SetTangentPosition(Handles.PositionHandle(point.tangent, node.transform.rotation));
                            if (lastPos != point.tangent)
                            {
                                setPoint = true;
                            }
                            lastPos = point.tangent2;
                            point.SetTangent2Position(Handles.PositionHandle(point.tangent2, node.transform.rotation));
                            if (lastPos != point.tangent2)
                            {
                                setPoint = true;
                            }
                            if (setPoint)
                            {
                                node.SetPoint(0, point);
                                node.UpdateConnectedComputers();
                            }
                        }
                    }
                }
                return;
            }
            SplinePoint[] points       = addComp.GetPoints();
            Transform     camTransform = SceneView.currentDrawingSceneView.camera.transform;

            SplineDrawer.DrawSplineComputer(addComp, 0.0, 1.0, 0.5f);
            TextAnchor originalAlignment = GUI.skin.label.alignment;
            Color      originalColor     = GUI.skin.label.normal.textColor;

            GUI.skin.label.alignment        = TextAnchor.MiddleCenter;
            GUI.skin.label.normal.textColor = addComp.editorPathColor;
            for (int i = 0; i < availablePoints.Length; i++)
            {
                if (addComp.isClosed && i == points.Length - 1)
                {
                    break;
                }

                Handles.Label(points[i].position + Camera.current.transform.up * HandleUtility.GetHandleSize(points[i].position) * 0.3f, (i + 1).ToString());
                if (SplineEditorHandles.CircleButton(points[availablePoints[i]].position, Quaternion.LookRotation(-camTransform.forward, camTransform.up), HandleUtility.GetHandleSize(points[availablePoints[i]].position) * 0.1f, 2f, addComp.editorPathColor))
                {
                    AddConnection(addComp, availablePoints[i]);
                    break;
                }
            }
            GUI.skin.label.alignment        = originalAlignment;
            GUI.skin.label.normal.textColor = originalColor;
        }
Exemplo n.º 11
0
        public static void DrawSplineComputer(SplineComputer comp, double fromPercent = 0.0, double toPercent = 1.0, float alpha = 1f)
        {
            if (comp == null)
            {
                return;
            }
            Color prevColor   = Handles.color;
            Color orange      = new Color(1f, 0.564f, 0f);
            Color handleColor = comp.hasMorph && !MorphWindow.editShapeMode ? orange : comp.editorPathColor;

            handleColor.a = alpha;
            Handles.color = handleColor;
            int iterations = Mathf.CeilToInt(comp.iterations * Mathf.Abs((float)(toPercent - fromPercent)));

            if (comp.pointCount < 2)
            {
                return;
            }

            if (comp.type == Spline.Type.BSpline && comp.pointCount > 1)
            {
                SplinePoint[] compPoints = comp.GetPoints();
                Handles.color = new Color(handleColor.r, handleColor.g, handleColor.b, 0.5f * alpha);
                for (int i = 0; i < compPoints.Length - 1; i++)
                {
                    Handles.DrawLine(compPoints[i].position, compPoints[i + 1].position);
                }
                Handles.color = handleColor;
            }

            if (!comp.drawThinckness)
            {
                if (positions.Length != iterations * 2)
                {
                    positions = new Vector3[iterations * 2];
                }
                Vector3 prevPoint  = comp.EvaluatePosition(fromPercent);
                int     pointIndex = 0;
                for (int i = 1; i < iterations; i++)
                {
                    double p = DMath.Lerp(fromPercent, toPercent, (double)i / (iterations - 1));
                    positions[pointIndex] = prevPoint;
                    pointIndex++;
                    positions[pointIndex] = comp.EvaluatePosition(p);
                    pointIndex++;
                    prevPoint = positions[pointIndex - 1];
                }
                Handles.DrawLines(positions);
            }
            else
            {
                Transform editorCamera = SceneView.currentDrawingSceneView.camera.transform;
                if (positions.Length != iterations * 6)
                {
                    positions = new Vector3[iterations * 6];
                }
                SplineResult prevResult = comp.Evaluate(fromPercent);
                Vector3      prevNormal = prevResult.normal;
                if (comp.billboardThickness)
                {
                    prevNormal = (editorCamera.position - prevResult.position).normalized;
                }
                Vector3 prevRight  = Vector3.Cross(prevResult.direction, prevNormal).normalized *prevResult.size * 0.5f;
                int     pointIndex = 0;
                for (int i = 1; i < iterations; i++)
                {
                    double       p         = DMath.Lerp(fromPercent, toPercent, (double)i / (iterations - 1));
                    SplineResult newResult = comp.Evaluate(p);
                    Vector3      newNormal = newResult.normal;
                    if (comp.billboardThickness)
                    {
                        newNormal = (editorCamera.position - newResult.position).normalized;
                    }
                    Vector3 newRight = Vector3.Cross(newResult.direction, newNormal).normalized *newResult.size * 0.5f;

                    positions[pointIndex] = prevResult.position + prevRight;
                    positions[pointIndex + iterations * 2] = prevResult.position - prevRight;
                    positions[pointIndex + iterations * 4] = newResult.position - newRight;
                    pointIndex++;
                    positions[pointIndex] = newResult.position + newRight;
                    positions[pointIndex + iterations * 2] = newResult.position - newRight;
                    positions[pointIndex + iterations * 4] = newResult.position + newRight;
                    pointIndex++;
                    prevResult = newResult;
                    prevRight  = newRight;
                    prevNormal = newNormal;
                }
                Handles.DrawLines(positions);
            }
            Handles.color = prevColor;
        }
Exemplo n.º 12
0
        void Presets()
        {
            if (presets == null)
            {
                return;
            }
            EditorGUILayout.BeginHorizontal();

            EditorGUILayout.BeginScrollView(scroll, GUILayout.Width(position.width * 0.35f), GUILayout.MaxHeight(10 * 22));
            if (presets.Length == 0)
            {
                EditorGUILayout.HelpBox("No saved presets available", MessageType.Info);
            }
            for (int i = 0; i < presets.Length; i++)
            {
                EditorGUILayout.BeginHorizontal();
                if (SplineEditorGUI.EditorLayoutSelectableButton(new GUIContent(presets[i].name), true, presetIndex == i))
                {
                    Undo.RecordObject(this, "PresetsWindow state");
                    Undo.RecordObject(computer, "Spline state");
                    presetIndex    = i;
                    primitiveIndex = -1;
                    computer.SetPoints(presets[i].points);
                    ((SplineEditor)editor).Refresh();
                    computer.type = presets[i].type;
                    if (presets[i].isClosed)
                    {
                        computer.Close();
                    }
                    else
                    {
                        computer.Break();
                    }
                    SplineUser[] users = computer.GetComponents <SplineUser>();
                    foreach (SplineUser user in users)
                    {
                        user.Rebuild(true);
                    }
                    computer.Rebuild();
                    ((SplineEditor)editor).Refresh();
                    SceneView.RepaintAll();
                }
                if (GUILayout.Button("X", GUILayout.MaxWidth(30)))
                {
                    if (EditorUtility.DisplayDialog("Delete preset ?", "Do you want to delete this preset ? This action cannot be undone.", "Yes", "No"))
                    {
                        SplinePreset.Delete(presets[i].filename);
                        GetPresets();
                        if (presetIndex >= presets.Length)
                        {
                            presetIndex = -1;
                        }
                        break;
                    }
                }
                EditorGUILayout.EndHorizontal();
            }
            EditorGUILayout.Space();
            EditorGUILayout.Space();
            if (GUILayout.Button("Create Preset"))
            {
                presetIndex = -1;
                newPreset   = new SplinePreset(computer.GetPoints(), computer.isClosed, computer.type);
                GetPresets();
            }
            EditorGUILayout.EndScrollView();

            if (presetIndex >= 0 && presetIndex < presets.Length)
            {
                EditorGUILayout.BeginVertical();
                EditorGUILayout.LabelField(presets[presetIndex].name);
                EditorGUILayout.LabelField(presets[presetIndex].description);
                EditorGUILayout.BeginHorizontal();
                if (GUILayout.Button("Save"))
                {
                    computer.Rebuild();
                    ((SplineEditor)editor).Refresh();
                    SceneView.RepaintAll();
                    presetIndex = -1;
                    this.Close();
                }
                if (GUILayout.Button("Cancel"))
                {
                    presets[presetIndex].Cancel();
                    presetIndex = -1;
                    SplineUser[] users = computer.GetComponents <SplineUser>();
                    foreach (SplineUser user in users)
                    {
                        user.Rebuild(true);
                    }
                    computer.Rebuild();
                    ((SplineEditor)editor).Refresh();
                    SceneView.RepaintAll();
                }
                EditorGUILayout.EndHorizontal();
                EditorGUILayout.EndVertical();
            }
            else if (computer.pointCount > 0)
            {
                EditorGUILayout.BeginVertical();
                if (newPreset != null)
                {
                    SavePresetDialog();
                }
                EditorGUILayout.EndVertical();
            }
            EditorGUILayout.EndHorizontal();
        }