Beispiel #1
0
        public void Collapse()
        {
            SplineComputer lastComputer = _elements[_elements.Length - 1].computer;
            int            lastStart    = _elements[_elements.Length - 1].startPoint;
            int            lastEnd      = _elements[_elements.Length - 1].endPoint;

            _elements               = new Element[1];
            _elements[0]            = new Element();
            _elements[0].computer   = lastComputer;
            _elements[0].startPoint = lastStart;
            _elements[0].endPoint   = lastEnd;
        }
Beispiel #2
0
 public static void UnregisterComputer(SplineComputer comp)
 {
     for (int i = 0; i < drawComputers.Count; i++)
     {
         if (drawComputers[i] == comp)
         {
             drawComputers[i].alwaysDraw = false;
             drawComputers.RemoveAt(i);
             return;
         }
     }
 }
Beispiel #3
0
        public void Collapse()
        {
            SplineComputer computer   = _elements[_elements.Length - 1].computer;
            int            startPoint = _elements[_elements.Length - 1].startPoint;
            int            endPoint   = _elements[_elements.Length - 1].endPoint;

            _elements               = new Element[1];
            _elements[0]            = new Element();
            _elements[0].computer   = computer;
            _elements[0].startPoint = startPoint;
            _elements[0].endPoint   = endPoint;
        }
            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();
                    }
                }
            }
Beispiel #5
0
        public override void OnInspectorGUI()
        {
            Node           node     = (Node)target;
            SplineComputer lastComp = addComp;

            addComp = (SplineComputer)EditorGUILayout.ObjectField("Add Computer", addComp, typeof(SplineComputer), true);
            if (lastComp != addComp)
            {
                SceneView.RepaintAll();
                if (addComp != null)
                {
                    availablePoints = GetAvailablePoints(addComp);
                }
            }
            if (addComp != null)
            {
                string[] pointNames = new string[availablePoints.Length];
                for (int i = 0; i < pointNames.Length; i++)
                {
                    pointNames[i] = "Point " + availablePoints[i];
                }
                if (availablePoints.Length > 0)
                {
                    addPoint = EditorGUILayout.Popup("Link point", addPoint, pointNames);
                }
                else
                {
                    EditorGUILayout.LabelField("No Points Available");
                }

                if (GUILayout.Button("Cancel"))
                {
                    addComp  = null;
                    addPoint = 0;
                }
                if (addPoint >= 0 && availablePoints.Length > addPoint)
                {
                    if (node.HasConnection(addComp, availablePoints[addPoint]))
                    {
                        EditorGUILayout.HelpBox("Connection already exists (" + addComp.name + "," + availablePoints[addPoint], MessageType.Error);
                    }
                    else if (GUILayout.Button("Link"))
                    {
                        AddConnection(addComp, availablePoints[addPoint]);
                    }
                }
            }
            else
            {
                RenderConnections();
            }
        }
Beispiel #6
0
 /// <summary>
 /// Adds new columns.
 /// New points of the spline are added. Their positions are calculated from their twin predecessor in the spline.
 /// </summary>
 /// <param name="newPoints"> Overall count of points wanted in the new spline. </param>
 /// <param name="sc"> SplineComputer of the spline that is being adjusted. </param>
 protected void AddColumns(int newPoints, SplineComputer sc)
 {
     for (int i = 0; i < newPoints; ++i)
     {
         int index     = currentPointCount - basePointCount + 1;
         var twinPoint = sc.GetPoint(index);
         sc.SetPointPosition(currentPointCount, new Vector3(twinPoint.position.x + width, twinPoint.position.y, twinPoint.position.z));
         sc.SetPointSize(currentPointCount, point_size);
         sc.SetPointColor(currentPointCount, Color.white);
         sc.SetPointNormal(currentPointCount, sc.GetPointNormal(index));
         currentPointCount++;
     }
 }
Beispiel #7
0
 void PaintHeightMap(Terrain terrain, SplineComputer computer, ref float[,] drawLayer, ref float[,] alphaLayer)
 {
     if (heights == null)
     {
         GetBase();
     }
     SplineResult[] results = new SplineResult[computer.iterations];
     for (int i = 0; i < results.Length; i++)
     {
         float percent = (float)i / (results.Length - 1);
         results[i] = computer.Evaluate(percent);
     }
     Draw(results, ref drawLayer, ref alphaLayer);
 }
Beispiel #8
0
        List <SplineComputer> GetSelectedSplines()
        {
            List <SplineComputer> selected = new List <SplineComputer>();

            foreach (GameObject obj in Selection.gameObjects)
            {
                SplineComputer comp = obj.GetComponent <SplineComputer>();
                if (comp != null)
                {
                    selected.Add(comp);
                }
            }
            return(selected);
        }
Beispiel #9
0
        /// <summary>
        /// Enter a junction address.
        /// </summary>
        /// <param name="element">The address element to add to the address</param>
        public virtual void AddComputer(SplineComputer computer, int connectionIndex, int connectedIndex, Spline.Direction direction = Spline.Direction.Forward)
        {
            if (sampleUser)
            {
                return;
            }
            int lastDepth = _address.depth;

            address.AddSpline(computer, connectionIndex, connectedIndex, direction);
            if (_address.depth != lastDepth)
            {
                Rebuild(true);
            }
        }
Beispiel #10
0
 public virtual bool HasConnection(SplineComputer computer, int pointIndex)
 {
     for (int num = connections.Length - 1; num >= 0; num--)
     {
         if (!connections[num].isValid)
         {
             RemoveConnection(num);
         }
         else if (connections[num].computer == computer && connections[num].pointIndex == pointIndex)
         {
             return(true);
         }
     }
     return(false);
 }
Beispiel #11
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);
        }
Beispiel #12
0
        private void GetObjects()
        {
            List <Transform> found = new List <Transform>();

            GetTransformsRecursively(transform, ref found);
            BendProperty[] newProperties = new BendProperty[found.Count];
            for (int i = 0; i < found.Count; i++)
            {
                CreateProperty(ref newProperties[i], found[i]);
            }
            bendProperties = newProperties;
            SplineComputer splineComponent = GetComponent <SplineComputer>();

            _parentIsTheSpline = splineComponent == spline;
        }
 protected override void OnSplineRemoved(SplineComputer spline)
 {
     base.OnSplineRemoved(spline);
     for (int i = 0; i < collections.Count; i++)
     {
         if (collections[i].spline == spline)
         {
             collections[i].Clear();
             collections.RemoveAt(i);
             spline.onRebuild -= Rebuild;
             Rebuild();
             return;
         }
     }
 }
Beispiel #14
0
 protected override void OnSplineRemoved(SplineComputer spline)
 {
     base.OnSplineAdded(spline);
     for (int i = 0; i < controllers.Count; i++)
     {
         if (controllers[i].computer == spline)
         {
             GameObject.DestroyImmediate(controllers[i].gameObject);
             controllers.RemoveAt(i);
             i--;
             continue;
         }
     }
     Rebuild();
 }
Beispiel #15
0
        protected ObjectController CreateController(SplineComputer computer, string name)
        {
            GameObject obj = new GameObject(name);

            obj.transform.position      = computer.transform.position;
            obj.transform.rotation      = computer.transform.rotation;
            obj.transform.localScale    = computer.transform.localScale;
            obj.transform.parent        = computer.transform;
            obj.transform.localPosition = Vector3.zero;
            obj.transform.localRotation = Quaternion.identity;
            obj.transform.localScale    = Vector3.one;
            ObjectController controller = obj.AddComponent <ObjectController>();

            controller.computer = computer;
            return(controller);
        }
Beispiel #16
0
 public virtual bool HasConnection(SplineComputer computer, int pointIndex)
 {
     for (int i = connections.Length - 1; i >= 0; i--)
     {
         if (!connections[i].isValid)
         {
             RemoveConnection(i);
             continue;
         }
         if (connections[i].computer == computer && connections[i].pointIndex == pointIndex)
         {
             return(true);
         }
     }
     return(false);
 }
        public SplineEditorToolbar(SplineEditor edit, SplineComputer comp)
        {
            editor   = edit;
            computer = comp;
            Texture2D tex = SplineEditorGUI.LoadTexture("presets.png");

            if (tex != null)
            {
                presetButtonContent.image = tex; presetButtonContent.text = "";
            }

            tex = SplineEditorGUI.LoadTexture("move.png");
            if (tex != null)
            {
                moveButtonContent.image = tex; moveButtonContent.text = "";
            }

            tex = SplineEditorGUI.LoadTexture("rotate.png");
            if (tex != null)
            {
                rotateButtonContent.image = tex; rotateButtonContent.text = "";
            }

            tex = SplineEditorGUI.LoadTexture("scale.png");
            if (tex != null)
            {
                scaleButtonContent.image = tex; scaleButtonContent.text = "";
            }

            tex = SplineEditorGUI.LoadTexture("normals.png");
            if (tex != null)
            {
                normalsButtonContent.image = tex; normalsButtonContent.text = "";
            }

            tex = SplineEditorGUI.LoadTexture("mirror.png");
            if (tex != null)
            {
                mirrorButtonContent.image = tex; mirrorButtonContent.text = "";
            }

            tex = SplineEditorGUI.LoadTexture("merge.png");
            if (tex != null)
            {
                mergeButtonContent.image = tex; mergeButtonContent.text = "";
            }
        }
Beispiel #18
0
 protected virtual void BuildObject(SplineComputer computer, string name)
 {
     if (obj != null)
     {
         GameObject.DestroyImmediate(obj);
     }
     obj = new GameObject(name);
     obj.transform.position      = computer.transform.position;
     obj.transform.rotation      = computer.transform.rotation;
     obj.transform.localScale    = computer.transform.localScale;
     obj.transform.parent        = computer.transform;
     obj.transform.localPosition = Vector3.zero;
     obj.transform.localRotation = Quaternion.identity;
     obj.transform.localScale    = Vector3.one;
     spawner          = obj.AddComponent <ObjectController>();
     spawner.computer = computer;
 }
Beispiel #19
0
        void PaintHeightMap(Terrain terrain, SplineComputer computer, ref float[,] drawLayer, ref float[,] alphaLayer)
        {
            if (heights == null)
            {
                GetBase();
            }
            SplineSample[] results = new SplineSample[computer.iterations];
            computer.Evaluate(ref results, clipFrom, clipTo);

            /*
             * for(int i = 0; i < results.Length; i++)
             * {
             *  float percent = (float)i/(results.Length-1);
             *  results[i] = computer.Evaluate(percent);
             * }
             */
            Draw(results, ref drawLayer, ref alphaLayer);
        }
Beispiel #20
0
        public virtual void RemoveConnection(SplineComputer computer, int pointIndex)
        {
            int index = -1;

            for (int i = 0; i < connections.Length; i++)
            {
                if (connections[i].pointIndex == pointIndex && connections[i].spline == computer)
                {
                    index = i;
                    break;
                }
            }
            if (index < 0)
            {
                return;
            }
            RemoveConnection(index);
        }
Beispiel #21
0
        public virtual void RemoveConnection(SplineComputer computer, int pointIndex)
        {
            int index = -1;

            for (int i = 0; i < connections.Length; i++)
            {
                if (connections[i].computer == computer && connections[i].pointIndex == pointIndex)
                {
                    index = i;
                    break;
                }
            }
            if (index < 0)
            {
                Debug.LogError("Connection not found in " + name);
                return;
            }
            RemoveConnection(index);
        }
Beispiel #22
0
        public static int PointSelectionMenu(SplineComputer computer, int selected = 0, string title = "Select point")
        {
            GUILayout.Box(title, GUILayout.Width(Screen.width - 45), GUILayout.Height(50));
            GUI.BeginGroup(GUILayoutUtility.GetLastRect());
            string[] options = new string[(computer.isClosed ? computer.pointCount - 1 : computer.pointCount) + 1];
            for (int i = 0; i < options.Length - 1; i++)
            {
                options[i + 1] = "Point " + i;
                if (computer.type == Spline.Type.Bezier)
                {
                    options[i + 1] = "Point " + i + " Bezier " + (computer.GetPoint(i, SplineComputer.Space.Local).type == SplinePoint.Type.Smooth ? "(smooth)" : "(broken)");
                }
            }
            options[0] = "- Select -";
            int selection = EditorGUI.Popup(new Rect(10, 25, Screen.width - 65, 30), selected, options) - 1;

            GUI.EndGroup();
            return(selection);
        }
Beispiel #23
0
 void MergeComputer(SplineComputer from, double mapFrom, double mapTo)
 {
     SplineUser[] subs = from.GetSubscribers();
     for (int i = 0; i < subs.Length; i++)
     {
         from.Unsubscribe(subs[i]);
         subs[i].computer = computer;
         subs[i].clipFrom = DMath.Lerp(mapFrom, mapTo, subs[i].clipFrom);
         subs[i].clipTo   = DMath.Lerp(mapFrom, mapTo, subs[i].clipTo);
     }
     if (EditorUtility.DisplayDialog("Keep merged computer's GameObject?", "Do you want to keep the merged computer's game object? This will transfer all subscribed users to the current computer.", "Yes", "No"))
     {
         GameObject.DestroyImmediate(from);
     }
     else
     {
         GameObject.DestroyImmediate(from.gameObject);
     }
 }
Beispiel #24
0
        void SplitAtPercent(double percent, ref SplinePoint[] points)
        {
            Undo.RecordObject(computer, "Split At Percent ");
            EditorUtility.SetDirty(computer);
            float pointValue     = (computer.pointCount - 1) * (float)percent;
            int   lastPointIndex = Mathf.FloorToInt(pointValue);
            int   nextPointIndex = Mathf.CeilToInt(pointValue);

            SplinePoint[] splitPoints = new SplinePoint[computer.pointCount - lastPointIndex];
            float         lerpPercent = Mathf.InverseLerp(lastPointIndex, nextPointIndex, pointValue);
            SplinePoint   splitPoint  = SplinePoint.Lerp(computer.GetPoint(lastPointIndex), computer.GetPoint(nextPointIndex), lerpPercent);

            splitPoint.SetPosition(computer.EvaluatePosition(percent));
            splitPoints[0] = splitPoint;
            for (int i = 1; i < splitPoints.Length; i++)
            {
                splitPoints[i] = computer.GetPoint(lastPointIndex + i);
            }
            SplineComputer spline = CreateNewSpline();

            spline.SetPoints(splitPoints);
            SplineUser[] users = spline.GetSubscribers();
            for (int i = 0; i < users.Length; i++)
            {
                users[i].clipFrom = DMath.InverseLerp(percent, 1.0, users[i].clipFrom);
                users[i].clipTo   = DMath.InverseLerp(percent, 1.0, users[i].clipTo);
            }
            splitPoints = new SplinePoint[lastPointIndex + 2];
            for (int i = 0; i <= lastPointIndex; i++)
            {
                splitPoints[i] = computer.GetPoint(i);
            }
            splitPoints[splitPoints.Length - 1] = splitPoint;
            points = splitPoints;
            users  = computer.GetSubscribers();
            for (int i = 0; i < users.Length; i++)
            {
                users[i].clipFrom = DMath.InverseLerp(0.0, percent, users[i].clipFrom);
                users[i].clipTo   = DMath.InverseLerp(0.0, percent, users[i].clipTo);
            }
            HandleNodes(spline, lastPointIndex);
        }
Beispiel #25
0
        void GetAvailable()
        {
            if (user == null)
            {
                return;
            }
            currentComputer = user.address.elements[user.address.depth - 1].computer;
            if (currentComputer == null)
            {
                return;
            }
            double startPercent = (double)user.address.elements[user.address.depth - 1].startPoint / (currentComputer.pointCount - 1);

            Spline.Direction dir = Spline.Direction.Forward;
            if (user.address.elements[user.address.depth - 1].startPoint > user.address.elements[user.address.depth - 1].endPoint)
            {
                dir = Spline.Direction.Backward;
            }
            int[] available = currentComputer.GetAvailableNodeLinksAtPosition(startPercent, dir);
            nodes             = new List <Node>();
            connectionIndices = new List <int>();
            pointIndices      = new List <int>();
            for (int i = 0; i < available.Length; i++)
            {
                Node node = currentComputer.nodeLinks[available[i]].node;
                if (currentComputer.nodeLinks[available[i]].pointIndex == user.address.elements[user.address.depth - 1].startPoint)
                {
                    continue;
                }
                Node.Connection[] connections = node.GetConnections();
                for (int n = 0; n < connections.Length; n++)
                {
                    if (connections[n].computer == currentComputer)
                    {
                        continue;
                    }
                    nodes.Add(node);
                    connectionIndices.Add(n);
                    pointIndices.Add(currentComputer.nodeLinks[available[i]].pointIndex);
                }
            }
        }
Beispiel #26
0
        SplineComputer CreateNewSpline()
        {
            GameObject go = GameObject.Instantiate(computer.gameObject);

            go.name = computer.name + "_split";
            SplineUser[]   users  = go.GetComponents <SplineUser>();
            SplineComputer spline = go.GetComponent <SplineComputer>();

            for (int i = 0; i < users.Length; i++)
            {
                computer.Unsubscribe(users[i]);
                users[i].computer = spline;
                spline.Subscribe(users[i]);
            }
            for (int i = go.transform.childCount - 1; i >= 0; i--)
            {
                GameObject.DestroyImmediate(go.transform.GetChild(i).gameObject);
            }
            return(spline);
        }
Beispiel #27
0
 public void UpdateConnectedComputers(SplineComputer excludeComputer = null)
 {
     for (int i = connections.Length - 1; i >= 0; i--)
     {
         if (!connections[i].isValid)
         {
             RemoveConnection(i);
             continue;
         }
         if (connections[i].computer == excludeComputer)
         {
             continue;
         }
         if (type == Type.Smooth && i != 0)
         {
             SetPoint(i, GetPoint(0));
         }
         connections[i].computer.SetPoint(connections[i].pointIndex, GetPoint(i));
     }
 }
Beispiel #28
0
        public virtual void AddConnection(SplineComputer computer, int pointIndex)
        {
            RemoveInvalidConnections();
            Node connected = computer.GetNode(pointIndex);

            if (connected != null)
            {
                Debug.LogError(computer.name + " is already connected to node " + connected.name + " at point " + pointIndex);
                return;
            }
            SplinePoint point = computer.GetPoint(pointIndex);

            point.SetPosition(transform.position);
            ArrayUtility.Add(ref connections, new Connection(computer, pointIndex, PointToLocal(point)));
            if (connections.Length == 1)
            {
                SetPoint(connections.Length - 1, point, true);
            }
            UpdateConnectedComputers();
        }
Beispiel #29
0
        /// <summary>
        /// Used to get the available junctions. Call this to update the junction list on the GUI.
        /// </summary>
        public void GetAvailableJunctions()
        {
            //Get the last SplineComputer in the junction address
            currentComputer = follower.address.elements[follower.address.depth - 1].computer;
            if (currentComputer == null)
            {
                return;
            }
            //Get the available junctions at the current address
            double startPercent = (double)follower.address.elements[follower.address.depth - 1].startPoint / (currentComputer.pointCount - 1);

            Spline.Direction dir = Spline.Direction.Forward;
            if (follower.address.elements[follower.address.depth - 1].startPoint > follower.address.elements[follower.address.depth - 1].endPoint)
            {
                dir = Spline.Direction.Backward;
            }
            int[] available = currentComputer.GetAvailableNodeLinksAtPosition(startPercent, dir);
            nodes.Clear();
            connectionIndices.Clear();
            pointIndices.Clear();
            //Make a list of the available junctions which to use in OnGUI for the buttons
            for (int i = 0; i < available.Length; i++)
            {
                Node node = currentComputer.nodeLinks[available[i]].node;
                if (currentComputer.nodeLinks[available[i]].pointIndex == follower.address.elements[follower.address.depth - 1].startPoint)
                {
                    continue;
                }
                Node.Connection[] connections = node.GetConnections();
                for (int n = 0; n < connections.Length; n++)
                {
                    if (connections[n].computer == currentComputer)
                    {
                        continue;
                    }
                    nodes.Add(node);
                    connectionIndices.Add(n);
                    pointIndices.Add(currentComputer.nodeLinks[available[i]].pointIndex);
                }
            }
        }
Beispiel #30
0
        public virtual void RemoveConnection(SplineComputer computer, int pointIndex)
        {
            int num = -1;

            for (int i = 0; i < connections.Length; i++)
            {
                if (connections[i].computer == computer && connections[i].pointIndex == pointIndex)
                {
                    num = i;
                    break;
                }
            }
            if (num < 0)
            {
                UnityEngine.Debug.LogError("Connection not found in " + base.name);
            }
            else
            {
                RemoveConnection(num);
            }
        }