Trimesh.createTorus constructor

Trimesh.createTorus(
  1. TorusGeometry torus
)

Create a Trimesh instance, shaped as a torus.

Implementation

Trimesh.createTorus(this.torus):super(type: ShapeType.trimesh){
  final List<double> vertices = [];
  List<double> normals = [];
  List<int> indices = [];
  List<double> uvs = [];

  for (int j = 0; j <= torus.radialSegments; j++) {
    for (int i = 0; i <= torus.tubularSegments; i++) {
        final center = Vec3();
        final vertex = Vec3();
        final normal = Vec3();

        final u = i / torus.tubularSegments * torus.arc;
        final v = j / torus.radialSegments * math.pi * 2;

        vertex.set(
          (torus.radius + torus.tube * math.cos(v)) * math.cos(u),
          (torus.radius + torus.tube * math.cos(v)) * math.sin(u),
          torus.tube * math.sin(v)
        );

        vertices.addAll([vertex.x, vertex.y, vertex.z]);

        center.set(torus.radius * math.cos(u),torus.radius * math.sin(u),0);
        normal.subVectors(vertex, center).normalize();
        normals.addAll([normal.x, normal.y, normal.z]);

        uvs.add(i / torus.tubularSegments);
        uvs.add(j / torus.radialSegments);

      if(i != 0 && j != 0){
        final a = (torus.tubularSegments + 1) * j + i - 1;
        final b = (torus.tubularSegments + 1) * (j - 1) + i - 1;
        final c = (torus.tubularSegments + 1) * (j - 1) + i;
        final d = (torus.tubularSegments + 1) * j + i;

        indices.addAll([a,b,d,b,c,d]);
      }
    }
  }
  this.vertices = vertices;
  this.indices = indices;
  this.uvs = uvs;
  this.normals = normals;

  updateNormals();
  updateEdges();
  updateAABB();
  updateBoundingSphereRadius();
  updateTree();
}