molstar
Version:
A comprehensive macromolecular library.
60 lines • 2.29 kB
JavaScript
/**
* Copyright (c) 2021 mol* contributors, licensed under MIT, See LICENSE file for more info.
*
* @author Alexander Rose <alexander.rose@weirdbyte.de>
*/
import { __assign } from "tslib";
// adapted from three.js, MIT License Copyright 2010-2021 three.js authors
import { Vec3 } from '../../mol-math/linear-algebra';
export var DefaultTorusProps = {
radius: 1,
tube: 0.4,
radialSegments: 8,
tubularSegments: 6,
arc: Math.PI * 2,
};
export function Torus(props) {
var _a = __assign(__assign({}, 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 = Vec3();
var vertex = Vec3();
var normal = 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
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
Vec3.set(center, radius * Math.cos(u), radius * Math.sin(u), 0);
Vec3.sub(normal, vertex, center);
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)
};
}
//# sourceMappingURL=torus.js.map