protected virtual void DrawMesh() { int steps = Mathf.RoundToInt(MeshDensity * VisionAngle); float stepsAngle = VisionAngle / steps; List <Vector3> viewPoints = new List <Vector3>(); RaycastData oldViewCast = new RaycastData(); for (int i = 0; i <= steps; i++) { float angle = stepsAngle * i + EulerAngles.y - VisionAngle / 2f; RaycastData viewCast = RaycastAtAngle(angle); if (i > 0) { bool thresholdExceeded = Mathf.Abs(oldViewCast.Distance - viewCast.Distance) > EdgeThreshold; if ((oldViewCast.Hit != viewCast.Hit) || (oldViewCast.Hit && viewCast.Hit && thresholdExceeded)) { MeshEdgePosition edge = FindMeshEdgePosition(oldViewCast, viewCast); if (edge.PointA != Vector3.zero) { viewPoints.Add(edge.PointA); } if (edge.PointB != Vector3.zero) { viewPoints.Add(edge.PointB); } } } viewPoints.Add(viewCast.Point); oldViewCast = viewCast; } int numberOfVertices = viewPoints.Count + 1; Vector3[] vertices = new Vector3[numberOfVertices]; int[] triangles = new int[(numberOfVertices - 2) * 3]; vertices[0] = Vector3.zero + Offset; for (int i = 0; i < numberOfVertices - 1; i++) { vertices[i + 1] = this.transform.InverseTransformPoint(viewPoints[i]); if (i < numberOfVertices - 2) { triangles[i * 3] = 0; triangles[i * 3 + 1] = i + 1; triangles[i * 3 + 2] = i + 2; } } _visionMesh.Clear(); _visionMesh.vertices = vertices; _visionMesh.triangles = triangles; _visionMesh.RecalculateNormals(); }
protected virtual void DrawMesh() { if (!ShouldDrawMesh) { return; } int steps = Mathf.RoundToInt(MeshDensity * VisionAngle); float stepsAngle = VisionAngle / steps; _viewPoints.Clear(); for (int i = 0; i <= steps; i++) { float angle = stepsAngle * i + EulerAngles.y - VisionAngle / 2f; _viewCast = RaycastAtAngle(angle); if (i > 0) { bool thresholdExceeded = Mathf.Abs(_oldViewCast.Distance - _viewCast.Distance) > EdgeThreshold; if ((_oldViewCast.Hit != _viewCast.Hit) || (_oldViewCast.Hit && _viewCast.Hit && thresholdExceeded)) { MeshEdgePosition edge = FindMeshEdgePosition(_oldViewCast, _viewCast); if (edge.PointA != Vector3.zero) { _viewPoints.Add(edge.PointA); } if (edge.PointB != Vector3.zero) { _viewPoints.Add(edge.PointB); } } } _viewPoints.Add(_viewCast.Point); _oldViewCast = _viewCast; } int numberOfVertices = _viewPoints.Count + 1; if (numberOfVertices != _numberOfVerticesLastTime) { Array.Resize(ref _vertices, numberOfVertices); Array.Resize(ref _triangles, (numberOfVertices - 2) * 3); } _vertices[0].x = 0; _vertices[0].y = 0; _vertices[0].z = 0; for (int i = 0; i < numberOfVertices - 1; i++) { _vertices[i + 1] = this.transform.InverseTransformPoint(_viewPoints[i]); if (i < numberOfVertices - 2) { _triangles[i * 3] = 0; _triangles[i * 3 + 1] = i + 1; _triangles[i * 3 + 2] = i + 2; } } _visionMesh.Clear(); _visionMesh.vertices = _vertices; _visionMesh.triangles = _triangles; _visionMesh.RecalculateNormals(); _numberOfVerticesLastTime = numberOfVertices; }