Created
November 23, 2017 09:05
-
-
Save mfandl/c7057ec951856432d838f776f256c143 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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