Ejemplo n.º 1
0
        /// <summary>
        /// Move edge vertices
        /// </summary>
        protected void UpdateEdgeVertex()
        {
            foreach (int index in _groundMesh.indexes.edgeVertexIndex)
            {
                foreach (Cell fneighbor in _neighbors)
                {
                    foreach (int fIndex in fneighbor.Indexes.edgeVertexIndex)
                    {
                        Vector3 nVertex1 = _groundMesh.smoothVertex[index].normalized;
                        Vector3 nVertex2 = fneighbor.GroundMesh.smoothVertex[fIndex].normalized;

                        if (nVertex1 == nVertex2)
                        {
                            List <int> myLinkedInternIndex = GetLinkedInternVertexIndexes(_groundMesh, index);
                            List <int> linkedInternIndex   = GetLinkedInternVertexIndexes(fneighbor.GroundMesh, fIndex);

                            Vector3 internVertex         = GetNearestTypedVertex(_groundMesh.indexes.internVertexIndex, nVertex1, _groundMesh);
                            Vector3 internNeighborVertex = GetNearestTypedVertex(fneighbor.GroundMesh.indexes.internVertexIndex, nVertex2, fneighbor.GroundMesh);

                            Vector3[] cloudOfPoint = new Vector3[] { internVertex, internNeighborVertex };
                            Vector3   G            = MathCustom.GetBarycenter(cloudOfPoint);

                            _groundMesh.smoothVertex[index] = G;
                            fneighbor.UpdateVertexPos(fIndex, G);
                        }
                    }
                }
            }
        }
Ejemplo n.º 2
0
        public void TransformCameraTarget(Transform targetTransform, out Vector3 outPosition, out Quaternion outRotation, ECameraTargetType view)
        {
            if (view == ECameraTargetType.MAP)
            {
                Vector3 lpos = targetTransform.position;
                outPosition = lpos + (lpos.normalized * MapViewDistance);

                Vector3 upVector      = lpos.normalized;
                Vector3 rightVector   = Vector3.Cross(upVector, Vector3.up).normalized;
                Vector3 forwardVector = MathCustom.GetFaceNormalVector(Vector3.zero, rightVector, upVector);
                outRotation = Quaternion.LookRotation(-upVector, forwardVector);
            }
            else if (view == ECameraTargetType.ZOOM)
            {
                Vector3 lpos = targetTransform.position;

                Vector3 newPosition;
                MathCustom.SphericalToCartesian(DistanceToplayer, -Mathf.PI / 2, Elevation, out newPosition);
                outPosition = targetTransform.TransformPoint(newPosition + Vector3.up * StepHeight);

                Vector3 newPos = targetTransform.TransformPoint(newPosition);
                Vector3 cross  = Vector3.Cross(newPos, targetTransform.forward);
                outRotation = Quaternion.LookRotation(lpos - newPos, Vector3.Cross(cross, -(lpos - newPos)));
            }
            else
            {
                outPosition = Vector3.zero;
                outRotation = Quaternion.identity;
            }
        }
Ejemplo n.º 3
0
    /// <summary>
    /// Recalculate peak to make ground flat
    /// </summary>
    /// <param name="groundMesh">GroundMesh</param>
    protected void RecalculatePeak(GroundMesh groundMesh, Vector3 origin)
    {
        int nbVertex = groundMesh.computedVertex.Length;

        Vector3[] planPoints = new Vector3[3];
        int       ite        = 0;

        for (int i = 0; i < nbVertex; i++)
        {
            if (groundMesh.computedVertex[i] != groundMesh.centerPosition)
            {
                planPoints[ite] = groundMesh.computedVertex[i];
                ite++;
                if (ite >= 3)
                {
                    break;
                }
            }
        }

        Vector4 groundPlan        = MathCustom.GetPlaneValues(planPoints[0], planPoints[1], planPoints[2]);
        Vector3 newCenterPosition = MathCustom.LineCutPlaneCoordinates(origin, groundMesh.centerPosition * int.MaxValue, groundPlan);

        for (int i = 0; i < nbVertex; i++)
        {
            if (groundMesh.computedVertex[i] == groundMesh.centerPosition)
            {
                groundMesh.centerPosition    = newCenterPosition;
                groundMesh.computedVertex[i] = newCenterPosition;
            }
        }
    }
Ejemplo n.º 4
0
        protected IEnumerator MoveToTargetCell(Vector3 basePosition, Quaternion baseRotation)
        {
            _controlEnable = false;
            float t = 0;

            Vector3    targetPos;
            Quaternion targetRot;

            MathCustom.SphericalToCartesian(movingDistance, ActualPolar, Elevation, out targetPos);
            targetPos = baseTransform.TransformPoint(targetPos);
            Vector3 cross = Vector3.Cross(baseTransform.position - targetPos, baseTransform.up);

            targetRot = Quaternion.LookRotation(baseTransform.position - targetPos, Vector3.Cross(cross, baseTransform.position - targetPos));

            while (t < 1)
            {
                if (t > 1)
                {
                    t = 1;
                }
                else
                {
                    t += Time.deltaTime * 1.25f;
                }
                transform.position = Vector3.Slerp(basePosition, targetPos, t);
                transform.rotation = Quaternion.Slerp(baseRotation, targetRot, t);
                yield return(null);
            }
            _controlEnable = true;
            StopAllCoroutines();
        }
Ejemplo n.º 5
0
        protected void Reoriente()
        {
            Vector3 forwardVector = -transform.position.normalized;
            Vector3 rightVector   = Vector3.Cross(forwardVector, Vector3.up).normalized;
            Vector3 upVector      = MathCustom.GetFaceNormalVector(transform.position, transform.position + rightVector, transform.position + forwardVector);

            transform.rotation = Quaternion.LookRotation(forwardVector, -upVector);
        }
Ejemplo n.º 6
0
 public void SetPos(float x)
 {
     if (relative)
     {
         _targetTransform.localPosition = MathCustom.LerpUnClampVector(_startPos, targetPos, x);
     }
     else
     {
         _targetTransform.position = MathCustom.LerpUnClampVector(_startPos, targetPos, x);
     }
 }
Ejemplo n.º 7
0
 protected void Update()
 {
     if (_controlEnable)
     {
         Vector3 newPosition;
         MathCustom.SphericalToCartesian(movingDistance, ActualPolar, Elevation, out newPosition);
         transform.position = baseTransform.TransformPoint(newPosition);
         Vector3 cross = Vector3.Cross(baseTransform.position - transform.position, baseTransform.up);
         transform.rotation = Quaternion.LookRotation(baseTransform.position - transform.position, Vector3.Cross(cross, baseTransform.position - transform.position));
     }
 }
Ejemplo n.º 8
0
    private void Pop()
    {
        popUpDirection = transform.position.normalized;
        float   randomAngle = Random.Range(0f, 360f);
        Vector3 groundVec   = (_player.transform.position - transform.position).normalized;

        popDirection = MathCustom.RotateDirectionAround(Vector3.Cross(popUpDirection, groundVec), randomAngle, popUpDirection);
        Cell       cell     = GetComponentInParent <Cell>();
        GameObject newProps = EarthManager.Instance.CreateProps(popItem.prefab, transform.position, cell);

        StartCoroutine(PopJumpCoroutine(newProps.transform));
    }
Ejemplo n.º 9
0
    private Vector3 GetRosacePosition(float deltaTime)
    {
        for (int i = 0; i < angularSpeeds.Count; i++)
        {
            Vector3 nVector;
            nVector    = MathCustom.RotateDirectionAround(vectors[i], deltaTime * angularSpeeds[i], Vector3.forward);
            nVector    = nVector.normalized * nTime * 2f;
            vectors[i] = nVector;
            nVector.z  = 0f;
            activeVectors.Add(nVector);
        }
        Vector3 pos = MathCustom.GetBarycenter(activeVectors.ToArray());

        activeVectors.Clear();
        return(pos);
    }
Ejemplo n.º 10
0
    private void GenerateRoomsConnections()
    {
        // R3 rooms
        List <Room> rooms = roomsList.FindAll(r => portalCouples.Find(pc => pc.exRoomID == r.ID) != null);

        foreach (Room r in rooms)
        {
            r.layoutType = ELayoutType.L3;
        }

        // R1 rooms
        rooms = roomsList.FindAll(r => portalCouples.Find(pc => pc.exRoomID == r.ID) == null);
        foreach (Room r in rooms)
        {
            r.layoutType = ELayoutType.L1;
        }

        // R2 rooms
        rooms = roomsList.FindAll(r => roomsList.Find(ir => ir.gridPos.x == r.gridPos.x && ir.gridPos.y == r.gridPos.y + 1) != null);
        foreach (Room r in rooms)
        {
            r.layoutType = MathCustom.RandomBool() ? ELayoutType.L2 : r.layoutType;
            if (r.layoutType == ELayoutType.L2)
            {
                _rooms[r.gridPos.x, r.gridPos.y + 1].openSides.Add(ERoomDirection.Bottom);
                r.openSides.Add(ERoomDirection.Top);
            }
        }

        foreach (Room r in roomsList)
        {
            if (portalCouples.Find(pc => pc.enRoomID == r.ID) == null)
            {
                if (r.exDirection == ERoomDirection.Right)
                {
                    r.openSides.Add(ERoomDirection.Right);
                    _rooms[r.gridPos.x + 1, r.gridPos.y].openSides.Add(ERoomDirection.Left);
                }
                else if (r.exDirection == ERoomDirection.Left)
                {
                    r.openSides.Add(ERoomDirection.Left);
                    _rooms[r.gridPos.x - 1, r.gridPos.y].openSides.Add(ERoomDirection.Right);
                }
            }
        }
    }
Ejemplo n.º 11
0
        /// <summary>
        /// Move corner vertices
        /// </summary>
        protected void UpdateCornerHeight()
        {
            foreach (int myindex in _groundMesh.indexes.cornerVertexIndex)
            {
                int    iterator            = 0;
                int[]  internVertexIndexes = new int[2];
                int[]  cornerVertexIndexes = new int[2];
                Cell[] linkedCells         = new Cell[2];

                foreach (Cell fneighbor in _neighbors)
                {
                    foreach (int index in fneighbor.Indexes.cornerVertexIndex)
                    {
                        Vector3 nVertex1 = _groundMesh.smoothVertex[myindex].normalized;
                        Vector3 nVertex2 = fneighbor.GroundMesh.smoothVertex[index].normalized;

                        if (nVertex1 == nVertex2)
                        {
                            List <int> internVertexIndex = GetLinkedInternVertexIndexes(fneighbor.GroundMesh, index);
                            linkedCells[iterator]         = fneighbor;
                            internVertexIndexes[iterator] = internVertexIndex[0];
                            cornerVertexIndexes[iterator] = index;
                            iterator++;
                        }
                    }
                }

                if (iterator == 2)
                {
                    List <int> myInternVertexIndex = GetLinkedInternVertexIndexes(_groundMesh, myindex);

                    Vector3[] cloudOfPoint = new Vector3[3]
                    {
                        _groundMesh.smoothVertex[myInternVertexIndex[0]],
                        linkedCells[0].GroundMesh.smoothVertex[internVertexIndexes[0]],
                        linkedCells[1].GroundMesh.smoothVertex[internVertexIndexes[1]]
                    };

                    Vector3 G = MathCustom.GetBarycenter(cloudOfPoint);
                    _groundMesh.smoothVertex[myindex] = G;

                    linkedCells[0].UpdateVertexPos(cornerVertexIndexes[0], G);
                    linkedCells[1].UpdateVertexPos(cornerVertexIndexes[1], G);
                }
            }
        }
Ejemplo n.º 12
0
        protected void AddRoadPart(RoadEdge startEdge, RoadEdge targetEdge)
        {
            SortPoints(startEdge, targetEdge);

            List <Vector3[]> preTris = new List <Vector3[]>();

            preTris.Add(new Vector3[3] {
                startEdge.meshPoints[1], startEdge.meshPoints[0], targetEdge.meshPoints[0]
            });
            preTris.Add(new Vector3[3] {
                targetEdge.meshPoints[0], targetEdge.meshPoints[1], startEdge.meshPoints[1]
            });

            for (int i = 0; i < 2; i++)
            {
                if (Vector3.Angle(MathCustom.GetFaceNormalVector(preTris[i][0], preTris[i][1], preTris[i][2]).normalized, MathCustom.GetBarycenter(preTris[i]).normalized) < 90f)
                {
                    roadVertice.AddTriangle(new Vector3[3] {
                        preTris[i][2], preTris[i][1], preTris[i][0]
                    });
                }
                else
                {
                    roadVertice.AddTriangle(preTris[i]);
                }
            }
            preTris.Clear();

            int[] lTriangles = new int[roadVertice.triangles.Count * 3];
            for (int i = 0; i < lTriangles.Length / 3; i++)
            {
                lTriangles[i * 3]       = roadVertice.triangles[i].verticesindex[0];
                lTriangles[(i * 3) + 1] = roadVertice.triangles[i].verticesindex[1];
                lTriangles[(i * 3) + 2] = roadVertice.triangles[i].verticesindex[2];
            }

            roadMeshObject.mesh.vertices  = roadVertice.vertex.ToArray();
            roadMeshObject.mesh.triangles = lTriangles;
            roadMeshObject.mesh.normals   = roadVertice.normals.ToArray();

            SetUV();
            roadMeshObject.mesh.uv      = roadVertice.UV.ToArray();
            roadMeshCollider.sharedMesh = roadMeshObject.mesh;
        }
Ejemplo n.º 13
0
    /// <summary>
    /// Generate UV mapping
    /// </summary>
    /// <param name="groundMesh"></param>
    protected void GenerateTextureMap(GroundMesh groundMesh)
    {
        Vector3 cross    = Vector3.Cross(Vector3.up, groundMesh.centerPosition);
        Vector3 py       = groundMesh.centerPosition + cross;
        Vector3 pz       = groundMesh.centerPosition + Vector3.Cross(cross, groundMesh.centerPosition);
        Vector4 cellPlan = MathCustom.GetPlaneValues(groundMesh.centerPosition, py, pz);

        for (int i = 0; i < groundMesh.smoothVertex.Length; i++)
        {
            Vector3 vertex = groundMesh.smoothVertex[i];
            if (vertex == groundMesh.centerPosition)
            {
                groundMesh.UVs[i] = new Vector2(0.5f, 0.5f);
            }
            else
            {
                Vector3 planeCoord   = MathCustom.LineCutPlaneCoordinates(Vector3.zero, vertex, cellPlan);
                Vector3 cartesianPos = Quaternion.FromToRotation(groundMesh.centerPosition, Vector3.up) * planeCoord;
                float   lRadius;
                float   lPolar;
                float   lElevation;
                MathCustom.CartesianToSpherical(cartesianPos, out lRadius, out lPolar, out lElevation);
                float xAngle = Vector3.SignedAngle(groundMesh.centerPosition, vertex, groundMesh.centerPosition);
                float yAngle = Vector3.SignedAngle(Vector3.Cross(groundMesh.centerPosition, -Vector3.up), vertex, groundMesh.centerPosition);

                if (CustomGeneric.IntArrayContain(groundMesh.indexes.internVertexIndex, i) != -1)
                {
                    groundMesh.UVs[i] = new Vector2(0.5f + (Mathf.Cos(lPolar) * 0.9f), 0.5f + (Mathf.Sin(lPolar) * 0.9f));
                }
                else
                {
                    groundMesh.UVs[i] = new Vector2(0.5f + Mathf.Cos(lPolar), 0.5f + Mathf.Sin(lPolar));
                }
            }
        }
    }
Ejemplo n.º 14
0
 public void SetScale(float x)
 {
     _targetTransform.localScale = MathCustom.LerpUnClampVector(startScale, targetScale, x);
 }
Ejemplo n.º 15
0
    /// <summary>
    /// Get the dual transformation of a GroundMesh
    /// </summary>
    /// <param name="groundMesh">GroundMesh</param>
    protected void DualTransformation(GroundMesh groundMesh)
    {
        Vector3[] computedVertices = new Vector3[groundMesh.computedVertex.Length];
        Vector3[] computedNormals  = new Vector3[groundMesh.computedVertex.Length];

        for (int i = 0; i < groundMesh.triangles.Length / 3; i++)
        {
            Vector3 addVector = Vector3.zero + groundMesh.centerPosition;
            if (groundMesh.vertex[groundMesh.triangles[i * 3]] != groundMesh.centerPosition)
            {
                addVector += groundMesh.vertex[groundMesh.triangles[i * 3]];
            }
            if (groundMesh.vertex[groundMesh.triangles[i * 3 + 1]] != groundMesh.centerPosition)
            {
                addVector += groundMesh.vertex[groundMesh.triangles[i * 3 + 1]];
            }
            if (groundMesh.vertex[groundMesh.triangles[i * 3 + 2]] != groundMesh.centerPosition)
            {
                addVector += groundMesh.vertex[groundMesh.triangles[i * 3 + 2]];
            }

            computedVertices[i] = addVector.normalized * radius;
            computedNormals[i]  = computedVertices[i].normalized;
        }

        computedVertices[computedVertices.Length - 2] = Vector3.zero;
        computedNormals[computedNormals.Length - 2]   = -groundMesh.centerPosition.normalized;

        computedVertices[computedVertices.Length - 1] = groundMesh.centerPosition;
        computedNormals[computedNormals.Length - 1]   = groundMesh.centerPosition.normalized;

        groundMesh.computedVertex  = computedVertices;
        groundMesh.computedNormals = computedNormals;

        // Creation of the dual triangles array

        int nbTriangle = groundMesh.triangles.Length / 3;

        VertexCouple[] couples = new VertexCouple[nbTriangle];

        for (int i = 0; i < nbTriangle; i++)
        {
            couples[i] = new VertexCouple(Vector3.zero, Vector3.zero);
        }

        int ite = 0;

        for (int j = 0; j < groundMesh.computedVertex.Length; j++)
        {
            for (int k = 0; k < groundMesh.computedVertex.Length; k++)
            {
                if (groundMesh.computedVertex[j] != groundMesh.centerPosition &&
                    groundMesh.computedVertex[k] != groundMesh.centerPosition &&
                    groundMesh.computedVertex[j] != Vector3.zero &&
                    groundMesh.computedVertex[k] != Vector3.zero)
                {
                    float angle = Vector3.Angle(groundMesh.centerPosition - groundMesh.computedVertex[j], groundMesh.centerPosition - groundMesh.computedVertex[k]);
                    if (angle > 2 && angle < 70)
                    {
                        VertexCouple newCouple = new VertexCouple(groundMesh.computedVertex[j], groundMesh.computedVertex[k]);
                        if (!CoupleExist(couples, newCouple))
                        {
                            couples[ite] = newCouple;
                            ite++;
                        }
                    }
                }
            }
        }

        int[] lTriangles = new int[groundMesh.triangles.Length * 2];
        for (int i = 0; i < couples.Length; i++)
        {
            Vector3 surfaceNormal = Vector3.Cross(groundMesh.centerPosition + couples[i].vertices[0], groundMesh.centerPosition + couples[i].vertices[1]);
            float   angle         = Vector3.Angle(gameObject.transform.position + groundMesh.centerPosition, surfaceNormal);

            if (angle < 90)
            {
                lTriangles[i * 3]     = ArrayContain(groundMesh.computedVertex, groundMesh.centerPosition);
                lTriangles[i * 3 + 1] = ArrayContain(groundMesh.computedVertex, couples[i].vertices[0]);
                lTriangles[i * 3 + 2] = ArrayContain(groundMesh.computedVertex, couples[i].vertices[1]);
            }
            else
            {
                lTriangles[i * 3]     = ArrayContain(groundMesh.computedVertex, groundMesh.centerPosition);
                lTriangles[i * 3 + 1] = ArrayContain(groundMesh.computedVertex, couples[i].vertices[1]);
                lTriangles[i * 3 + 2] = ArrayContain(groundMesh.computedVertex, couples[i].vertices[0]);
            }

            Vector4 face = MathCustom.GetPlaneValues(Vector3.zero, couples[i].vertices[1], couples[i].vertices[0]);
            if (MathCustom.GetDistanceToPlane(groundMesh.centerPosition, face) > 0)
            {
                lTriangles[groundMesh.triangles.Length + i * 3]     = ArrayContain(groundMesh.computedVertex, Vector3.zero);
                lTriangles[groundMesh.triangles.Length + i * 3 + 1] = ArrayContain(groundMesh.computedVertex, couples[i].vertices[0]);
                lTriangles[groundMesh.triangles.Length + i * 3 + 2] = ArrayContain(groundMesh.computedVertex, couples[i].vertices[1]);
            }
            else
            {
                lTriangles[groundMesh.triangles.Length + i * 3]     = ArrayContain(groundMesh.computedVertex, Vector3.zero);
                lTriangles[groundMesh.triangles.Length + i * 3 + 1] = ArrayContain(groundMesh.computedVertex, couples[i].vertices[1]);
                lTriangles[groundMesh.triangles.Length + i * 3 + 2] = ArrayContain(groundMesh.computedVertex, couples[i].vertices[0]);
            }
        }

        groundMesh.computedTriangles = lTriangles;
    }