Пример #1
0
    public void Generate()
    {
        planeMesh                = new PlaneMesh();
        planeMesh.Size           = new Vector2(chunkSize, chunkSize);
        planeMesh.SubdivideDepth = Mathf.RoundToInt(chunkSize * 0.5f);
        planeMesh.SubdivideWidth = Mathf.RoundToInt(chunkSize * 0.5f);

        surfaceTool  = new SurfaceTool();
        meshDataTool = new MeshDataTool();
        surfaceTool.CreateFrom(planeMesh, 0);
        arrayPlane = surfaceTool.Commit();
        meshDataTool.CreateFromSurface(arrayPlane, 0);

        for (int i = 0; i < meshDataTool.GetVertexCount(); i++)
        {
            Vector3 vertex = meshDataTool.GetVertex(i);
            vertex.y = noise.GetNoise3d(
                vertex.x + position.x,
                vertex.y,
                vertex.z + position.z) * References.steepness;

            meshDataTool.SetVertex(i, vertex);
            avgHeight += vertex.y;
        }
        avgHeight /= meshDataTool.GetVertexCount();

        for (int i = 0; i < arrayPlane.GetSurfaceCount(); i++)
        {
            arrayPlane.SurfaceRemove(i);
        }

        for (int i = 0; i < meshDataTool.GetFaceCount(); i++)
        {
            Vector3 A      = meshDataTool.GetVertex(meshDataTool.GetFaceVertex(i, 0));
            Vector3 B      = meshDataTool.GetVertex(meshDataTool.GetFaceVertex(i, 1));
            Vector3 C      = meshDataTool.GetVertex(meshDataTool.GetFaceVertex(i, 2));
            Vector3 face   = (A + B + C) / 3 + position;
            Vector3 normal = meshDataTool.GetFaceNormal(i);
            slope += Maths.Angle(Vector3.Up, normal);
        }
        slope /= meshDataTool.GetFaceCount();

        meshDataTool.CommitToSurface(arrayPlane);
        surfaceTool.Begin(Mesh.PrimitiveType.Triangles);
        surfaceTool.CreateFrom(arrayPlane, 0);
        surfaceTool.GenerateNormals();

        meshInstance      = new MeshInstance();
        meshInstance.Mesh = surfaceTool.Commit();
        meshInstance.SetSurfaceMaterial(0, (Material)ResourceLoader.Load("res://Assets/Shader/Terrain.material"));
        meshInstance.CreateTrimeshCollision();
        meshInstance.CastShadow = GeometryInstance.ShadowCastingSetting.On;
        AddChild(meshInstance);
    }
Пример #2
0
        public static Point GetPointForDirection(Vector worldDirection)
        {
            if (worldDirection.Length == 0)
            {
                throw new InvalidOperationException("Invalid direction.");
            }
            else if (instance == null)
            {
                throw new InvalidOperationException("Instance is not initialized, cannot set click point.");
            }

            double angle = Maths.Angle(instance.upDirection, worldDirection);
            Vector rotatedScreenPoint = toRotate.Rotate(angle);
            Vector newScreenVect      = Center + rotatedScreenPoint;

            return(newScreenVect.ToPoint());
        }
Пример #3
0
        private Point GetScreenPointToMoveTo()
        {
            Vector destinationDirection = destination - currentPos;

            destinationDirection.Normalize();

            double angle = Maths.Angle(upDirection, destinationDirection);

            Vector screenVecToRotate = screenUpPosition - screenCenter;

            Vector rotatedScreenPoint = screenVecToRotate.Rotate(angle);
            Vector newScreenVect      = screenCenter + rotatedScreenPoint;

            Point newScreenPoint = newScreenVect.ToPoint();

            return(newScreenPoint);
        }
Пример #4
0
        private bool ShouldReajustPosition()
        {
            Vector fromDestination = destination - Character.Position;

            fromDestination.Normalize();
            Vector fromStartClick = Character.Position - ScreenPosition.StartPositionWorld;

            if (fromStartClick.Length >= settings.MinSamePathLengthBeforeAdjustRotation)
            {
                double angle          = Maths.Angle(fromStartClick, fromDestination);
                double angleDiff      = Math.Abs(lastAngle - angle);
                bool   shouldReadjust = angleDiff >= settings.MinAngleBeforeAdjustRotation;
                lastAngle = angle;
                return(shouldReadjust);
            }

            return(false);
        }
Пример #5
0
        private bool ShouldReajustPosition()
        {
            if (upDirection.IsZero())
            {
                return(false);
            }

            Vector directionToDestination = destination - currentPos;

            directionToDestination.Normalize();

            double distanceToLastMove = (currentPos - currentDirectionStartPoint).Length;

            double angle     = Maths.Angle(upDirection, directionToDestination);
            double angleDiff = Math.Abs(lastAngle - angle);

            Console.WriteLine($"angle  in  check: {angle}, angle diff: {angleDiff}, distance to dest: {distanceToDestination}");
            bool shouldReadjust = distanceToDestination <= distanceBeforeAlwaysAdjust ||
                                  (angleDiff >= minAngleBeforeAjdust && distanceToLastMove >= minSameDirectionLengthBeforeChangeOrientation);

            lastAngle = angle;
            return(shouldReadjust);
        }