0

I'm trying to create a torus graph where there are different coloured segments of different widths (depending on the data size), basically a pie graph that looks nicer.

Is it possible to do this using torus geometry or would it be best to create the segments as shapes and use exctrudegeometry?

In wither case, what's the best way of doing this?

Edit

I have implemented this via torus geometry like:

var prevAngle = 0;
for(var i = 0; i < data.length; i++){
    var mat = new THREE.MeshPhongmaterial({materialOptions});
    var angle = 2* Math.PI * data[i].size //size is decimal
    var geo = new THREE.TorusGeometry(500, 200, 8, 6, angle);
    var mesh = new THREE.Mesh(geo, mat);
    mesh.rotation.z = prevAngle;
    scene.add(mesh);
    prevAngle = angle;
}

But when I render this (with 4 objects in data with size 0.25) I only get the top half of the torus (semi-torus?).

is the rotation correct?

2 Answers 2

1

You can look at the example at https://github.com/josdirksen/learning-threejs/blob/master/chapter-05/07-basic-3d-geometries-torus.html

As you can see, the last argument to the TorusGeometry is the arc which you can use to directly map the segments of your pie graph to segments of a torus geometry.

Sign up to request clarification or add additional context in comments.

5 Comments

So say I had 4 segments of 25% each, would I need to create 4 meshes with torus geometry and set the arc of each geometry to pi/2, pi, 3*pi/2, 2pi?
you would have to create 4 segments with an arc of pi/2 and rotate the meshes appropriately (1st one 0 degrees, 2nd one pi/2 degrees, 3rd one pi degrees and 4th one 3*pi/2 degrees) to have them fall on the right place.
in your code you should be doing: prevAngle += angle; otherwise all geometry subsection will fall on top of each other.
in addition, if materialOptions is the same for each i then you can move the call to THREE.MeshPhongmaterial outside the for-loop for improved performance.
I noticed the missing += as soon as I'd posted this.
1

Working solution

var prevAngle = 0;
var material = new THREE.MeshPhongmaterial({materialOptions});
var graphContainer = new THREE.Object3D();
graphContainer.castShadow = false;

for(var i = 0; i < data.length; i++){
    var mat = material.clone();
    mat.color = new THREE.Color(this.coloursArray[i]); //colorsArray = ['#46adad', ...]

    var angle = 2* Math.PI * data[i].size //size is decimal
    var geo = new THREE.TorusGeometry(500, 200, 8, 6, angle);
    var mesh = new THREE.Mesh(geo, mat);

    mesh.rotation.z = prevAngle;
    graphContainer.add(mesh);
    prevAngle += angle;
}
this.scene.add(graphContainer);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.