Skip to content

Instantly share code, notes, and snippets.

@mfandl
Created November 23, 2017 09:05
Show Gist options
  • Save mfandl/c7057ec951856432d838f776f256c143 to your computer and use it in GitHub Desktop.
Save mfandl/c7057ec951856432d838f776f256c143 to your computer and use it in GitHub Desktop.
export function sphere (radius, meridians, parallels) {
const vertices = []
for (let i = 0; i < parallels; ++i) {
const polar = Math.PI * i / (parallels - 1)
for (let j = 0; j < meridians; ++j) {
const azimuth = 2.0 * Math.PI * j / meridians
vertices.push(sphericalToCartesian(radius, azimuth, polar))
}
}
const indices = []
for (let i = 0; i < parallels - 1; ++i) {
const currentRowStart = meridians * i
const nextRowStart = (meridians * (i + 1))
for (let j = 0; j < meridians; ++j) {
indices.push(
currentRowStart + j,
nextRowStart + j,
nextRowStart + (j + 1) % meridians,
currentRowStart + j,
nextRowStart + (j + 1) % meridians,
currentRowStart + (j + 1) % meridians
)
}
}
return {
vertices,
indices
}
}
function sphericalToCartesian (radius, azimuth, polar) {
return [
radius * Math.sin(polar) * Math.cos(azimuth),
radius * Math.cos(polar),
radius * Math.sin(polar) * Math.sin(azimuth)
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment