Skip to content

Instantly share code, notes, and snippets.

@fwsGonzo
Last active September 22, 2024 18:15
Show Gist options
  • Save fwsGonzo/ff6934ed9007db27613c7e80606d1e2e to your computer and use it in GitHub Desktop.
Save fwsGonzo/ff6934ed9007db27613c7e80606d1e2e to your computer and use it in GitHub Desktop.
func skyDomeLookup(vertices : Array, dv : Vector3):
for idx in vertices.size():
if vertices[idx].distance_squared_to(dv) < 0.0001:
return idx
var index : int = vertices.size()
vertices.push_back(dv)
return index
func createSkyDome(r_inner : float, r_outer : float):
const numX = 16
const numY = 4
const total_points = (2 * numY + 1) * numX * 3 # triangles * 3
# total number of vertices allocated to buffer:
var vertices : Array
var indices : Array
var lookup : Array # Vector3 -> index
# vertices for triangle strip:
var va : Vector3
var vb : Vector3
var vc : Vector3
var vd : Vector3
var stepa : float = PI * 2.0 / numX
var startb : float = asin(r_inner / r_outer)
var stepb : float = (PI / 2.0 - startb) / 4.0
for y in numY:
var b : float = startb + stepb * y
for x in numX:
var a : float = stepa * x
va = Vector3( sin(a) * cos(b) * r_outer, sin(b) * r_outer, -cos(a) * cos(b) * r_outer )
vb = Vector3( sin(a + stepa) * cos(b) * r_outer, sin(b) * r_outer, -cos(a + stepa) * cos(b) * r_outer )
vc = Vector3( sin(a + stepa) * cos(b + stepb) * r_outer, sin(b + stepb) * r_outer, -cos(a + stepa) * cos(b + stepb) * r_outer )
vd = Vector3( sin(a) * cos(b + stepb) * r_outer, sin(b + stepb) * r_outer, -cos(a) * cos(b + stepb) * r_outer )
# Find va, vb, vc, vd in the lookup hashmap and add their index to the indices vector
# Triangle 1: va, vb, vc
indices.push_back(skyDomeLookup(vertices, va));
var va_idx = indices.back()
indices.push_back(skyDomeLookup(vertices, vb));
indices.push_back(skyDomeLookup(vertices, vc));
var vc_idx = indices.back()
# Triangle 2: vc, vd, va
indices.push_back(vc_idx);
indices.push_back(skyDomeLookup(vertices, vd));
indices.push_back(va_idx);
var b : float = startb + stepb * 3
for x in numX:
var a : float = stepa * x
va = Vector3 ( sin(a) * cos(b) * r_outer, sin(b) * r_outer, -cos(a) * cos(b) * r_outer )
vb = Vector3 ( sin(a + stepa) * cos(b) * r_outer, sin(b) * r_outer, -cos(a + stepa) * cos(b) * r_outer )
vc = Vector3 ( 0.0, r_outer, 0.0 )
indices.push_back(skyDomeLookup(vertices, va));
indices.push_back(skyDomeLookup(vertices, vb));
indices.push_back(skyDomeLookup(vertices, vc));
assert(indices.size() == total_points)
#print(vertices.size())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment