I’m new to three.js and I wanted to create a Shape (special ringsegment with some space) manually in a function. see code below. My attempt is the following:
- my function takes 4 arguments (inner radius, outer radius, the segment angle, the space)
- i create a shape (circle segment)
- i create a hole-path, that will be removed.
My function worked well when I set the angle to 25°. But when I raise the angle up to 30° -> the Hole is not removed anymore? The size of the space has the same effect… if i set the space too small , the hole is not removed as well. Has someone an idea, why this happens? Or another approach to get to my goal?
Toolbox.prototype.createCircleElement = function(inner, outer, angle, space) {
var radians = THREE.Math.degToRad(angle); //angle in radians
var spaceAngleO = Math.asin( space / outer ); //difference of
var spaceAngleI = Math.asin( space / inner );
var xCenter = space / Math.tan( radians / 2 );
var radiusDiff = Math.sqrt(Math.pow(space, 2) + Math.pow(xCenter, 2));
var yspaceO = 0.5*Math.sqrt( Math.pow(2*outer*Math.sin(spaceAngleO), 2) - Math.pow(space, 2) );
var yspaceI = 0.5*Math.sqrt( Math.pow(2*inner*Math.sin(spaceAngleI), 2) - Math.pow(space, 2) );
//fill shape
var buttonShape = new THREE.Shape();
buttonShape.moveTo(xCenter, space);
buttonShape.lineTo(outer - yspaceO, space);
buttonShape.arc(-outer + xCenter + yspaceO, 0, outer - radiusDiff, 0, radians, false);
buttonShape.lineTo(xCenter, space);
//inner ring to remove
var buttonHolePath = new THREE.Path();
buttonHolePath.moveTo(xCenter, space);
buttonHolePath.lineTo(inner - yspaceI, space);
buttonHolePath.arc(-inner + xCenter + yspaceI, 0, inner - radiusDiff, 0, radians, false);
buttonHolePath.lineTo(xCenter, space);
buttonShape.holes.push(buttonHolePath);
var geometry = new THREE.ShapeGeometry(buttonShape);
var material = new THREE.MeshBasicMaterial({ color: 0x00ff00 });
var mesh = new THREE.Mesh(geometry, material);
return mesh;
Here are three examples (two of them fail):
var butn = vrToolbox.createCircleElement(1.5, 4, 45, 0.14); //did not work... space too small
var butn2 = vrToolbox.createCircleElement(1.5, 4, 45, 0.15); //ok
var butn3 = vrToolbox.createCircleElement(1.5, 4, 30, 0.15); //did not work angle to large