Procedural Mesh and Vertex Winding problem..

Hello there! I’m building my own mesh as a Cone. The Cone start from an Enemy and End with some Length. Everythings work’s fine when my enemy look at +X or -X directions, but when it face +Z or -Z directions, the Vertex normal are inverted and i can’t see my mesh.

As you can see into the image, the image number 1 work’s fine, the image n 2 is inverted. I Can’t understand why… This is my code, anyone can give me some suggets? Thanks!

m_goVisibilityCone 		= GameObject.CreatePrimitive(PrimitiveType.Cube);
		Component.Destroy( m_goVisibilityCone.GetComponent<BoxCollider>() );
		m_goVisibilityCone.name = this.name + "_VisConeMesh";
		m_mConeMesh 			= new Mesh();
		m_goVisibilityCone.GetComponent<MeshFilter>().mesh = m_mConeMesh;
		
		m_vVertices  = new Vector3[ m_iVertMax ];
		m_iTriangles = new int[ m_iVertMax ];
		m_vNormals   = new Vector3[ m_iVertMax ];
		
		m_mConeMesh.vertices 	= m_vVertices;
		m_mConeMesh.triangles 	= m_iTriangles;
		m_mConeMesh.normals     = m_vNormals;
		
		m_vUV		 			= new Vector2[ m_mConeMesh.vertices.Length ];
		m_mConeMesh.uv 			= m_vUV;
		
		m_goVisibilityCone.renderer.material = m_matVisibilityCone;
		
		for ( int i = 0; i < m_iVertMax; ++ i ) {
			m_vNormals[ i ] = Vector3.up;
		}

                m_fStartRadians   = ( 360.0f - ( m_AngleOfView * 0.5f ) ) * Mathf.Deg2Rad;
		m_fEndRadians     = ( m_AngleOfView * 0.5f ) * Mathf.Deg2Rad;
		m_fCurrentRadians = m_fStartRadians;
		
		m_fSpan = ( m_AngleOfView * Mathf.Deg2Rad ) / m_iConeVisibilityPrecision;
private void DrawVisibilityCone() {
		
		m_fCurrentRadians = m_fStartRadians;
		int index = 0;
		
		for ( int i = 0; i < m_iConeVisibilityPrecision; ++i ) {
			Vector3 currentPoint = this.transform.position + this.transform.forward * m_ConeLenght;
			
			float s = Mathf.Sin( m_fCurrentRadians );
			float c = Mathf.Cos( m_fCurrentRadians );
			
			currentPoint.x -= this.transform.position.x;
			currentPoint.z -= this.transform.position.z;
			
			float newX = currentPoint.x * c - currentPoint.z * s;
			float newZ = currentPoint.x * s - currentPoint.z * c;
			
			currentPoint.x = newX + this.transform.position.x;
			currentPoint.z = -newZ + this.transform.position.z;
			
			/* Calcoliamo dove arriva il Ray */
			Vector3 rayDirection = currentPoint - this.transform.position;
			RaycastHit raycastInfo = new RaycastHit();
		
			bool found = false;
			if ( Physics.Raycast( this.transform.position, rayDirection, out raycastInfo ) ) {
				if ( raycastInfo.collider.tag == "level_wall"  raycastInfo.distance < m_ConeLenght ) {
					currentPoint.x = raycastInfo.point.x;
					currentPoint.z = raycastInfo.point.z;
					found = true;
				}
			}
			
			Vector3 currentStartPoint = this.transform.position;// + ( currentPoint - this.transform.position ).normalized * 2.0f;
			
			if ( found )
				Debug.DrawLine( currentStartPoint, currentPoint, Color.red );
			else 
				Debug.DrawLine( currentStartPoint, currentPoint, Color.blue );
			
			
			m_fCurrentRadians += m_fSpan;
			m_vVertices[ index     ] = currentStartPoint;
			m_vVertices[ index + 1 ] = currentPoint;
			
			index += 2;
		}
		
		int localIndex = 0;

		for ( int j = 0; j < 60 - 6; j = j + 6 ) {
			m_iTriangles[ j 	] = localIndex + 0;
			m_iTriangles[ j + 1 ] = localIndex + 1;
			m_iTriangles[ j + 2 ] = localIndex + 3;
			
			m_iTriangles[ j + 3	] = localIndex + 0;
			m_iTriangles[ j + 4 ] = localIndex + 3;
			m_iTriangles[ j + 5 ] = localIndex + 2;
	
			localIndex += 2;
		}
		
		m_mConeMesh.Clear();
		m_mConeMesh.vertices  = m_vVertices;
		m_mConeMesh.triangles = m_iTriangles;
		m_mConeMesh.normals   = m_vNormals;
		m_mConeMesh.RecalculateNormals();
		m_mConeMesh.Optimize();
	}

I think one of the problems you are facing is that you are using arbitrary angles and directions to create your mesh.

Consider using a single object that is connected to your “head” or whatever. This means that all you have to do is to come up with the vertices for the fan you are working on. It is always facing “up” relative to the head, but is always facing “up” in regards to the mesh.

This means that all of your normals are always Vector3.up. All of your face windings are CounterClockwise. UV’s are calculated from the min distance to the max distance. If a ray collided with something, then it falls short that distance.

Each ray is in a direction. You can get the angle of the ray by using Sin and Cos. Since the return is a normal, then all you have to do is multiply it by your distances. TransformPosition that, and you have a start and stop place for a Linecast.