molstar
Version:
A comprehensive macromolecular library.
64 lines • 2.61 kB
JavaScript
/**
* Copyright (c) 2021 mol* contributors, licensed under MIT, See LICENSE file for more info.
*
* @author Alexander Rose <alexander.rose@weirdbyte.de>
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.Torus = exports.DefaultTorusProps = void 0;
var tslib_1 = require("tslib");
// adapted from three.js, MIT License Copyright 2010-2021 three.js authors
var linear_algebra_1 = require("../../mol-math/linear-algebra");
exports.DefaultTorusProps = {
radius: 1,
tube: 0.4,
radialSegments: 8,
tubularSegments: 6,
arc: Math.PI * 2,
};
function Torus(props) {
var _a = (0, tslib_1.__assign)((0, tslib_1.__assign)({}, exports.DefaultTorusProps), props), radius = _a.radius, tube = _a.tube, radialSegments = _a.radialSegments, tubularSegments = _a.tubularSegments, arc = _a.arc;
// buffers
var indices = [];
var vertices = [];
var normals = [];
// helper variables
var center = (0, linear_algebra_1.Vec3)();
var vertex = (0, linear_algebra_1.Vec3)();
var normal = (0, linear_algebra_1.Vec3)();
// generate vertices and normals
for (var j = 0; j <= radialSegments; ++j) {
for (var i = 0; i <= tubularSegments; ++i) {
var u = i / tubularSegments * arc;
var v = j / radialSegments * Math.PI * 2;
// vertex
linear_algebra_1.Vec3.set(vertex, (radius + tube * Math.cos(v)) * Math.cos(u), (radius + tube * Math.cos(v)) * Math.sin(u), tube * Math.sin(v));
vertices.push.apply(vertices, vertex);
// normal
linear_algebra_1.Vec3.set(center, radius * Math.cos(u), radius * Math.sin(u), 0);
linear_algebra_1.Vec3.sub(normal, vertex, center);
linear_algebra_1.Vec3.normalize(normal, normal);
normals.push.apply(normals, normal);
}
}
// generate indices
for (var j = 1; j <= radialSegments; ++j) {
for (var i = 1; i <= tubularSegments; ++i) {
// indices
var a = (tubularSegments + 1) * j + i - 1;
var b = (tubularSegments + 1) * (j - 1) + i - 1;
var c = (tubularSegments + 1) * (j - 1) + i;
var d = (tubularSegments + 1) * j + i;
// faces
indices.push(a, b, d);
indices.push(b, c, d);
}
}
return {
vertices: new Float32Array(vertices),
normals: new Float32Array(normals),
indices: new Uint32Array(indices)
};
}
exports.Torus = Torus;
//# sourceMappingURL=torus.js.map
;