Code archives/3D Graphics - Mesh/Primitives By PolyCount

This code has been declared by its author to be Public Domain code.

Download source code

Primitives By PolyCount by _PJ_2013
Rather than rely on the "Segments" value, These simple functions do the math to allow for one to specify a maximum polycount when creating primitives.
The function will automatically select the highest possible quality without exceeding the given value*.

NOTES:
CreateCube() has no counterpart, since cube primitives ALWAYS have 12 triangles.

*Entering values less than the necessary minimum or greater than the allowed maximum will automatically default to min or max to prevent error.

I couldn't work out how to figure the sphere polys per segments in reverse, so a quick iteration (max 97 counts) shouldn't be too much of a worry I hope!
Function CreatePolySphere(MaxPolys, Parent=False)
	MaxPolys=(MaxPolys*(MaxPolys<=39600 And MaxPolys >=8 ))+(39600*(MaxPolys>39600))+(8*(MaxPolys<8))
	Local Iterate
	Local Segments
	For Iterate=3 To 100
		Segments=4*(Iterate*Iterate)
		Segments=Segments * (Iterate - 1)
		If (Segments>MaxPolys)
			Iterate=Iterate-1
			Exit
		End If
	Next
	Return CreateSphere(Iterate,Parent)
End Function

Function CreatePolyCylinder(MaxPolys, Closed=True, Parent=False)
	MaxPolys=(MaxPolys*(MaxPolys<=200 And MaxPolys >=8 ))+(200*(MaxPolys>200))+(8*(MaxPolys<8))
	MaxPolys=Int(Floor(MaxPolys*0.5))
	
	Return  CreateCylinder(MaxPolys,Closed,Parent)
End Function

Function CreatePolyCone(MaxPolys, Closed=True, Parent=False)
	MaxPolys=(MaxPolys*(MaxPolys<=100 And MaxPolys >=8 ))+(100*(MaxPolys>100))+(8*(MaxPolys<8))
	Return  CreateCone(MaxPolys,Closed,Parent)
End Function

Comments

Bobysait2015
Polycount for sphere is :

CountTriangle = (segment-1)*(segment*2) *2


Code Archives Forum