Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Mesh Rendering Border Not working.

Discussion in '2D' started by DoctorBronze, Oct 25, 2020.

  1. DoctorBronze

    DoctorBronze

    Joined:
    Oct 13, 2020
    Posts:
    13
    I've attempted to create a math based border which won't allow a mesh to render outside of it and will change the mesh's vertices if it they get outside the border so that the mesh itself won't be rendered outside the border only inside of it. But it doesn't work.the calculations are based on line intersections, there are 4 vector3 variables which create 4 lines, each line is parallel to either the x or the y axis, and so i use two functions, one which calculates the intented vector 3 if the mesh vertex is either below or above the y axis; and a second function which calculates the intented vector if the mesh vertex crosses the x axis borders.

    link to graph showing how it's suppose to calculate the vertices: https://www.desmos.com/calculator/dpvh4lki1a

    Code (CSharp):
    1. using Microsoft.Win32.SafeHandles;
    2. using Platformer.Mechanics;
    3. using System.Collections;
    4. using System.Collections.Generic;
    5. using System.Net.NetworkInformation;
    6. using System.Runtime.CompilerServices;
    7. using UnityEditor;
    8. using UnityEngine;
    9.  
    10. public class VisualArc : MonoBehaviour
    11. {
    12.     public GameObject gameobject;
    13.     private float DR;
    14.     public int rc;
    15.     private EnemyController control;
    16.     public float RemovalPrecentile;
    17.  
    18.     public Vector3 MinXMaxY;
    19.     public Vector3 MinXMinY;
    20.     public Vector3 MaxXMaxY;
    21.     public Vector3 MaxXMinY;
    22.  
    23.     // Start is called before the first frame update
    24.     void Start()
    25.     {
    26.         control = gameobject.GetComponent<EnemyController>();
    27.         DR = gameobject.GetComponent<EnemyController>().DetectionRange;
    28.     }
    29.  
    30.     private Vector3 intersectionX(Vector3 vector1, Vector3 vector2, float x)
    31.     {
    32.         float m = (vector1.y - vector2.y) / (vector1.x - vector2.x);
    33.         float y = m * x + -m * vector1.x + vector1.y;
    34.         Vector3 intersection = new Vector3(x, y);
    35.         return intersection;
    36.     }
    37.  
    38.     private Vector3 intersectionY(Vector3 vector1, Vector3 vector2, float y)
    39.     {
    40.         float m = (vector1.y - vector2.y) / (vector1.x - vector2.x);
    41.         float x = (y - vector1.y)/m + vector1.x;
    42.         Vector3 intersection = new Vector3(x, y);
    43.         return intersection;
    44.     }
    45.  
    46.     void Update()
    47.     {
    48.         transform.position = control.transform.position;
    49.         Vector3 origin = transform.position;
    50.         Vector3[] vertices = new Vector3[rc + 2];
    51.         Vector2[] uv = new Vector2[vertices.Length];
    52.         int[] triangles = new int[(rc) * 3];
    53.  
    54.         const float INITIAL_ANGLE = Mathf.PI / 8;
    55.         float angle = INITIAL_ANGLE;
    56.         int vertexIndex = 1;
    57.         int triangleIndex = 0;
    58.  
    59.         for (int i = 0; i <= rc; i++)
    60.         {
    61.             int direction = control.direction;
    62.             float vertex_x = DR * direction - (DR / 100 * RemovalPrecentile) * direction;
    63.             float vertex_y = Mathf.Sin(direction * angle) * DR;
    64.             Vector3 vertex = new Vector3(vertex_x, vertex_y);
    65.  
    66.  
    67.             if (vertex_y < MinXMinY.y)
    68.             {
    69.                 Debug.Log(vertex);
    70.                 vertex = intersectionY(vertex, origin, MinXMinY.y);
    71.                 Debug.Log(vertex);
    72.             }
    73.             if (vertex_y > MaxXMaxY.y)
    74.             {
    75.                 Debug.Log(vertex);
    76.                 vertex = intersectionY(vertex, origin, MaxXMaxY.y);
    77.                 Debug.Log(vertex);
    78.             }
    79.             if (vertex_x < MinXMinY.x)
    80.             {
    81.                 Debug.Log(vertex);
    82.                 vertex = intersectionX(vertex, origin, MinXMinY.x);
    83.                 Debug.Log(vertex);
    84.             }
    85.             if (vertex_x > MaxXMaxY.x)
    86.             {
    87.                 Debug.Log(vertex);
    88.                 vertex = intersectionX(vertex, origin, MaxXMaxY.x);
    89.                 Debug.Log(vertex);
    90.             }
    91.             vertices[vertexIndex] = vertex;
    92.  
    93.             Debug.DrawLine(origin, vertex);
    94.  
    95.             if (vertexIndex > 1)
    96.             {
    97.                 triangles[triangleIndex] = 0;
    98.                 triangles[triangleIndex + 1] = vertexIndex - 1;
    99.                 triangles[triangleIndex + 2] = vertexIndex;
    100.  
    101.                 triangleIndex += 3;
    102.             }
    103.             vertexIndex++;
    104.             angle -= INITIAL_ANGLE * 2 / rc;
    105.         }
    106.  
    107.         Mesh mesh = new Mesh();
    108.  
    109.         mesh.vertices = vertices;
    110.         mesh.triangles = triangles;
    111.         mesh.uv = uv;
    112.  
    113.         GetComponent<MeshFilter>().mesh = mesh;
    114.  
    115.     }
    116. }
    117.  
     
    Last edited: Oct 25, 2020