Ooops
September 28, 2021, 18:36:36

### Author Topic: [bb] CreateCubeSphere() function by Flanker [ 6 months ago ]  (Read 1588 times)

#### BlitzBot

• Jr. Member
• Posts: 1
##### [bb] CreateCubeSphere() function by Flanker [ 6 months ago ]
« on: June 29, 2017, 00:28:43 »
Title : CreateCubeSphere() function
Author : Flanker
Posted : 6 months ago

Description : Just a function to create a "cubesphere" mesh, with proper UVs for a cubemap set of textures. It has 6 surfaces, one for each cube face :

You can set a number of subdivisions, from 1 to 180, and choose beetween 2 geometry modes. Mode 0 is just a normalisation while Mode 1 (default) limits the deformation near edges :

I put it here before it gets lost in the depth of my hard drive...

Code :
Code: BlitzBasic
1. Function CreateCubeSphere(segments#=16,mode=1)
2.
3.         mesh = CreateMesh()
4.
5.         For s = 1 To 6
6.
7.                 surf = CreateSurface(mesh)
8.
9.                 For y = 0 To segments
10.                         For x = 0 To segments
11.
12.                                 Select s
13.                                         Case 1
14.                                                 vx# = x-segments/2 : vy# = segments/2 : vz# = segments/2-y
15.                                         Case 2
16.                                                 vx = x-segments/2 : vy = segments/2-y : vz = -segments/2
17.                                         Case 3
18.                                                 vx = x-segments/2 : vy = -segments/2 : vz = y-segments/2
19.                                         Case 4
20.                                                 vx = -segments/2 : vy = segments/2-y : vz = segments/2-x
21.                                         Case 5
22.                                                 vx = segments/2 : vy = segments/2-y : vz = x-segments/2
23.                                         Case 6
24.                                                 vx = segments/2-x : vy = segments/2-y : vz = segments/2
25.                                 End Select
26.
27.                                 If mode = 0
28.                                         magnitude# = Sqr( vx*vx + vy*vy + vz*vz )
29.                                         vertX# = vx/magnitude : vertY# = vy/magnitude : vertZ# = vz/magnitude
30.                                 Else
31.                                         vx = vx/segments*2 : vy = vy/segments*2 : vz = vz/segments*2
32.                                         vertX = vx * Sqr( 1.0 - (vy*vy)/2 - (vz*vz)/2 + ((vy*vy)*(vz*vz)/3) )
33.                                         vertY = vy * Sqr( 1.0 - (vz*vz)/2 - (vx*vx)/2 + ((vz*vz)*(vx*vx)/3) )
34.                                         vertZ = vz * Sqr( 1.0 - (vx*vx)/2 - (vy*vy)/2 + ((vx*vx)*(vy*vy)/3) )
35.                                 EndIf
36.
38.                                 VertexNormal(surf,vertex,vertX,vertY,vertZ)
39.
40.                         Next
41.                 Next
42.
43.                 For y = 0 To segments-1
44.                         For x = 0 To segments-1
47.                         Next
48.                 Next
49.
50.         Next
51.
52.         Return mesh
53.
54. End Function

Rick Nasher(Posted 6 months ago)

Very cool!

BlitzSupport(Posted 6 months ago)

This is awesome, thanks for posting it!

Krischan(Posted 6 months ago)

Very, very nice and much shorter than <a href="codearcs836a.html?code=2539" target="_blank">my current solution[/url]. As this kind of cube/sphere is the best solution to create planets check out my <a href="codearcs6dbf.html?code=3193" target="_blank">Cubemap to Spheremap conversion[/url] if you don't know it already.

Flanker(Posted 6 months ago)

@KrischanI knew there were already "cubesphere" codes in the code archives, but I didn't see that yours already included the same formula to limit the deformations.