Пример #1
0
        public static float AngleDegree(Vector2 a, Vector2 b)
        {
            float theta = PipeNetMath.AngleRadian(a, b) * Mathf.Rad2Deg;
            float dot   = Vector2.Dot(Vector2.right, b - a);

            if (dot < 0)
            {
                theta = 180f + theta;
            }
            return(PipeNetMath.Wrap(theta, 0f, 360f));
        }
Пример #2
0
        /// <summary>
        /// flow node to generate mesh data
        /// </summary>
        /// <param name="source">source</param>
        /// <param name="meshData">mesh data</param>
        /// <param name="bBlock">is block node</param>
        /// <param name="lastIndex">last triangle index</param>
        private void FlowNodeData(Node source, ref MeshData meshData, bool bBlock, int lastIndex = -1)
        {
            source.searched = true;
            var v = meshData.v;
            var t = meshData.t;

            var node1      = source;
            var searchList = bBlock ? source.neighbors : source.downstreams;

            foreach (var nextID in searchList)
            {
                var node2 = data.GetNode(nextID);
                if (bBlock)
                {
                    if ((node2.pressure != 0 && !node2.closed) || node2.searched)
                    {
                        continue;
                    }
                }

                /*************** Calculate vertices *****************/
                Vector2 a    = node1.position.ToXZVector2();
                Vector2 b    = node2.position.ToXZVector2();
                Vector3 rght = new Vector3(0, 0, 1);
                Vector3 lft  = new Vector3(0, 0, -1);

                var theta = PipeNetMath.AngleRadian(a, b);
                meshData.thetas.Add(theta);

                // add two segments
                v.Add(node1.position + rght * lineWidth);
                v.Add(node1.position + lft * lineWidth);
                v.Add(node2.position + rght * lineWidth);
                v.Add(node2.position + lft * lineWidth);

                // apply angular rotation to points
                int l = v.Count - 4;
                v[l + 0] = v[l + 0].RotateAroundPoint(node1.position, -theta);
                v[l + 1] = v[l + 1].RotateAroundPoint(node1.position, -theta);
                v[l + 2] = v[l + 2].RotateAroundPoint(node2.position, -theta);
                v[l + 3] = v[l + 3].RotateAroundPoint(node2.position, -theta);

                // add triangles
                t.AddRange(new int[6] {
                    meshData.tri_index + 2, meshData.tri_index + 1, meshData.tri_index + 0,
                    meshData.tri_index + 2, meshData.tri_index + 3, meshData.tri_index + 1
                });
                meshData.tri_index += 4;

                /*************** Calculate edges *****************/
                if (lastIndex >= 0)
                {
                    int p0 = lastIndex;
                    int p1 = lastIndex + 1;
                    int p2 = lastIndex + 2;
                    int p3 = lastIndex + 3;
                    int p4 = meshData.tri_index - 4;
                    int p5 = meshData.tri_index - 3;
                    int p6 = meshData.tri_index - 2;
                    int p7 = meshData.tri_index - 1;

                    var leftInterceptY  = (v[p2].y + v[p3].y) / 2f;
                    var rightInterceptY = (v[p4].y + v[p5].y) / 2f;

                    Vector2 leftIntercept;
                    if (!PipeNetMath.InterceptPoint(v[p0].ToXZVector2(), v[p2].ToXZVector2(),
                                                    v[p4].ToXZVector2(), v[p6].ToXZVector2(), out leftIntercept))
                    {
                        Debug.LogWarning("Parallel pipe lines!");
                    }

                    Vector2 rightIntercept;
                    if (!PipeNetMath.InterceptPoint(v[p1].ToXZVector2(), v[p3].ToXZVector2(),
                                                    v[p5].ToXZVector2(), v[p7].ToXZVector2(), out rightIntercept))
                    {
                        Debug.LogWarning("Parallel pipe lines!");
                    }

                    v[p2] = leftIntercept.ToVector3(leftInterceptY);
                    v[p4] = leftIntercept.ToVector3(leftInterceptY);

                    v[p3] = rightIntercept.ToVector3(rightInterceptY);
                    v[p5] = rightIntercept.ToVector3(rightInterceptY);
                }
                //continue flow node
                FlowNodeData(node2, ref meshData, bBlock, meshData.tri_index - 4);
            }
        }