MCPcopy Create free account
hub / github.com/microsoft/SandDance / getSphereAttributes

Function getSphereAttributes

docs/tests/v2/es6/js/sanddance.js:32247–32319  ·  view source on GitHub ↗
(nlat, nlong, radius)

Source from the content-addressed store, hash-verified

32245exports.default = SphereGeometry;
32246
32247function getSphereAttributes(nlat, nlong, radius) {
32248 var startLat = 0;
32249 var endLat = Math.PI;
32250 var latRange = endLat - startLat;
32251 var startLong = 0;
32252 var endLong = 2 * Math.PI;
32253 var longRange = endLong - startLong;
32254 var numVertices = (nlat + 1) * (nlong + 1);
32255
32256 if (typeof radius === 'number') {
32257 var value = radius;
32258
32259 radius = function radius(n1, n2, n3, u, v) {
32260 return value;
32261 };
32262 }
32263
32264 var positions = new Float32Array(numVertices * 3);
32265 var normals = new Float32Array(numVertices * 3);
32266 var texCoords = new Float32Array(numVertices * 2);
32267 var indices = new Uint16Array(nlat * nlong * 6); // Create positions, normals and texCoords
32268
32269 for (var y = 0; y <= nlat; y++) {
32270 for (var x = 0; x <= nlong; x++) {
32271 var u = x / nlong;
32272 var v = y / nlat;
32273 var index = x + y * (nlong + 1);
32274 var i2 = index * 2;
32275 var i3 = index * 3;
32276 var theta = longRange * u;
32277 var phi = latRange * v;
32278 var sinTheta = Math.sin(theta);
32279 var cosTheta = Math.cos(theta);
32280 var sinPhi = Math.sin(phi);
32281 var cosPhi = Math.cos(phi);
32282 var ux = cosTheta * sinPhi;
32283 var uy = cosPhi;
32284 var uz = sinTheta * sinPhi;
32285 var r = radius(ux, uy, uz, u, v);
32286 positions[i3 + 0] = r * ux;
32287 positions[i3 + 1] = r * uy;
32288 positions[i3 + 2] = r * uz;
32289 normals[i3 + 0] = ux;
32290 normals[i3 + 1] = uy;
32291 normals[i3 + 2] = uz;
32292 texCoords[i2 + 0] = u;
32293 texCoords[i2 + 1] = v;
32294 }
32295 } // Create indices
32296
32297
32298 var numVertsAround = nlat + 1;
32299
32300 for (var _x = 0; _x < nlat; _x++) {
32301 for (var _y = 0; _y < nlong; _y++) {
32302 var _index = (_x * nlong + _y) * 6;
32303
32304 indices[_index + 0] = _y * numVertsAround + _x;

Callers 1

SphereGeometryFunction · 0.85

Calls 1

radiusFunction · 0.70

Tested by

no test coverage detected