How to create a twisted torus?

Hello! I am trying to build a twisted/impossible torus based on the logic outlined here (under “The Impossible Torus” section). My goal is to eventually build something similar to Chromatic ice cube 🌈🧊 : generative, but I am starting small and trying to build up to that. :slight_smile:

Here is the relevant code I am trying:

const shape = [
  new BABYLON.Vector3(0, 0, 0),
  new BABYLON.Vector3(0, 1, 0),
  new BABYLON.Vector3(1, 1, 0),
  new BABYLON.Vector3(1, 0, 0)
];

const segmentCount = 100,
  radius = 10;
const points = [];
for (let i = 0; i <= segmentCount + 1; i++) {
  const theta = (i / segmentCount) * Math.PI * 2;

  points.push(
    new BABYLON.Vector3(Math.cos(theta) * radius, Math.sin(theta) * radius, 20)
  );
}

BABYLON.MeshBuilder.ExtrudeShape("torus", {
  shape,
  path: points,
  closeShape: true,
  sideOrientation: BABYLON.Mesh.DOUBLESIDE,
  rotation: Math.PI / 3,
});

However, I can’t get the twists right:

Any advice is much appreciated! Also, if you have any tips for building towards the “chromatic ice cube” Reddit link, that would also be much appreciated.

I am a beginner to Babylon.js so might be missing something critical here.

Hio @lotus and welcome to the community.

You were close. Here is a PG

Because of the way rotation works in ExtrudeShape you will always have a seam at the start/end point, the lower the nbSteps the more noticeable the seam.

3 Likes

That’s awesome @JohnK , thanks so much for your help. Are you aware of an alternative approach (perhaps not ExtrudeShape) that would avoid the seam? No worries if not, I appreciate your time – this is very helpful!

I was able to kind of hack it by starting the index in the path-filling loop at a higher number, but not sure if there’s a more elegant approach.

I think, but not entirely sure, that when you close the path an extrude shape joins a vertex to the equivalent rotated vertex. Imagine two identical pieces of square cardboard each with corners numbered 0, 1, 2, 3, where 0 is aligned with 0, 1 with 1 etc. Join the two 0 corners with string, the two 1s, etc to form an open cuboid. Now twis one of the pieces through 180degs. The strings will twist. This is how the extruded shape is closed. In the case of 180degs what you would want is 2 to join 0, 3 to join 1, 0 to join 2 and 1 to join 3. In the case of an extruded shape for different shapes and rotations this is very difficult to work out. This is why the simplest closure was used.

Whether I have the time and expertise to do the correct calculations I doubt it but will give it some thought.

You could use a ribbon to create your own but you would still have the same twist problem.

No worries, you’ve helped me figure this out more than enough. That analogy really helped me understand what is going on as well.

For any future readers, I set the loop index to start at 2 (rather than 0), which “hides” the seam inside of the mesh. Hacky, but it works if you need a quick fix, and can be easily covered up with a material applied.