Beispiel #1
0
        private void DrawSceneNodesGUI()
        {
            Camera sceneCamera = Camera.current;
            var    allNodes    = net.data.GetNodeList();
            bool   dirty       = false;

            foreach (var node in allNodes)
            {
                var nodePositon = node.position + net.transform.position;
                if (Vector3.Dot(sceneCamera.transform.forward, nodePositon - sceneCamera.transform.position) < 0)
                {
                    continue;
                }

                int selectedIndex = net.SelectedIndex;
                // Undo.RecordObject(node, "Modifying node");
                Handles.Label(nodePositon, "Node" + node.id);

                var pointHandleSize = HandleUtility.GetHandleSize(nodePositon) * HANDLE_SCALE;
                Handles.color = (node.id == selectedIndex) ? Color.red : Color.green;

                MyHandles.DragHandleResult dhResult;
                Vector3 newPosition = MyHandles.DragHandle(nodePositon, pointHandleSize, caps[(int)node.type], Color.blue, out dhResult);

                switch (dhResult)
                {
                case MyHandles.DragHandleResult.LMBClick:
                    ChangeSelectedPointIndex(node.id);
                    GUI.changed = true;
                    break;

                case MyHandles.DragHandleResult.LMBDrag:

                    if (editmode == EditMode.ConnectLine)
                    {
                        Vector3 position2  = Camera.current.WorldToScreenPoint(Handles.matrix.MultiplyPoint(newPosition));
                        var     dragNodeTo = allNodes.Where(n =>
                        {
                            return(n != node &&
                                   Vector3.Distance(Camera.current.WorldToScreenPoint(Handles.matrix.MultiplyPoint(n.position + net.transform.position)), position2) < 30);
                        }).FirstOrDefault();

                        if (dragNodeTo != null)
                        {
                            if (net.data.ConnectNode(node.id, dragNodeTo.id))
                            {
                                net.CheckLineNode(node);
                                net.CheckLineNode(dragNodeTo);
                                net.UpdateLineNode(node);
                            }
                        }
                    }
                    break;
                }

                // Move node position
                if (editmode == EditMode.Transform)
                {
                    if (node.id == selectedIndex)
                    {
                        var currentPosition = tp = node.position + net.transform.position;
                        currentPosition = Handles.DoPositionHandle(currentPosition, Quaternion.identity);

                        if (tp != currentPosition)
                        {
                            node.position = net.attachToGround ? PipeNetUtils.GroundHeight(currentPosition) : currentPosition - net.transform.position;
                            net.UpdateNodeSign(node);
                            net.UpdateLineNode(node);
                            dirty = true;
                        }
                    }
                }
            }

            if (dirty)
            {
                Undo.RecordObject(net, "Refresh");
                net.Refresh();
            }
        }
Beispiel #2
0
        /// <summary>
        /// Refresh the net
        /// </summary>
        /// <param name="includeValveState">should reset valve state also</param>
        /// <param name="refreshAll">need to refresh the whole childs</param>
        public void Refresh(bool includeValveState = false, bool refreshAll = false)
        {
            transform.localScale = Vector3.one;

            data.Reset(false, includeValveState);
            data.minGap = lineWidth * 2f;
            var nodes = data.GetNodeList();

            // if attach to ground
            if (attachToGround)
            {
                foreach (var node in nodes)
                {
                    node.position = PipeNetUtils.GroundHeight(node.position) - transform.position;
                }
            }

            // not enough nodes
            if (nodes.Length < 2)
            {
                gameObject.GetComponent <MeshFilter>().sharedMesh   = null;
                gameObject.GetComponent <MeshCollider>().sharedMesh = null;
                return;
            }

            //generate block node list
            var blockList   = new List <Node>();
            var sourceNodes = data.StartFlow();

            if (sourceNodes.Length < 1)
            {
                blockList.AddRange(nodes);
            }
            else
            {
                blockList.AddRange(nodes.Where(node => { return(node.pressure == 0); }));
            }

            //we remove all the middle connected nodes to join edges
            blockList.RemoveAll(node =>
            {
                int i = 0;
                foreach (var id in node.neighbors)
                {
                    var neighbor = data.GetNode(id);
                    if (neighbor != null && neighbor.pressure == 0)
                    {
                        i++;
                    }

                    if (i > 1)
                    {
                        return(true);
                    }
                }
                return(false);
            });

            //Mesh component initialization
            if (!gameObject.GetComponent <MeshFilter>())
            {
                gameObject.AddComponent <MeshFilter>();
            }
            else if (gameObject.GetComponent <MeshFilter>().sharedMesh != null)
            {
                DestroyImmediate(gameObject.GetComponent <MeshFilter>().sharedMesh);
            }

            if (!gameObject.GetComponent <MeshRenderer>())
            {
                gameObject.AddComponent <MeshRenderer>();
            }

            //generate meshes
            var mainMesh = GenerateMesh(sourceNodes, false);

            data.Reset(true);
            var blockMesh = GenerateMesh(blockList.ToArray(), true);

            if (mainMesh == null && blockMesh == null)
            {
                return;
            }

            var meshList = new List <Mesh>();
            var matList  = new List <Material>();

            if (mainMesh != null)
            {
                meshList.Add(mainMesh);
                matList.Add(mainMat);
            }
            if (blockMesh != null)
            {
                meshList.Add(blockMesh);
                matList.Add(blockMat);
            }

            //combine mesh
            var combinedMesh = meshList[0];

            if (meshList.Count > 1)
            {
                combinedMesh = new Mesh();
                var combines = new CombineInstance[meshList.Count];
                for (int i = 0; i < combines.Length; i++)
                {
                    combines[i].mesh      = meshList[i];
                    combines[i].transform = transform.localToWorldMatrix;
                }
                combinedMesh.CombineMeshes(combines, false);
            }

            gameObject.GetComponent <MeshFilter>().sharedMesh        = combinedMesh;
            gameObject.GetComponent <MeshRenderer>().sharedMaterials = matList.ToArray();
            gameObject.GetComponent <MeshCollider>().sharedMesh      = combinedMesh;
#if UNITY_EDITOR
            Unwrapping.GenerateSecondaryUVSet(gameObject.GetComponent <MeshFilter>().sharedMesh);
#endif
            if (refreshAll)
            {
                foreach (var node in nodes)
                {
                    UpdateLineNode(node);
                    UpdateNodeSign(node);
                }
            }
        }