RoundEdgedBox
Approach 2
by Paul West
HTML
<script src="https://threejs.org/build/three.min.js"></script>
<script src="https://threejs.org/examples/js/utils/SceneUtils.js"></script>
<script src="https://threejs.org/examples/js/controls/OrbitControls.js"></script>
<script>
function RoundEdgedBox(width, height, depth, radius, widthSegments, heightSegments, depthSegments, smoothness) {
width = width || 1;
height = height || 1;
depth = depth || 1;
radius = radius || (Math.min(Math.min(width, height), depth) * .25);
widthSegments = Math.floor(widthSegments) || 1;
heightSegments = Math.floor(heightSegments) || 1;
depthSegments = Math.floor(depthSegments) || 1;
smoothness = Math.max(3, Math.floor(smoothness) || 3);
var halfWidth = width * .5 - radius;
var halfHeight = height * .5 - radius;
var halfDepth = depth * .5 - radius;
var geometry = new THREE.Geometry();
// corners - 4 eighths of a sphere
var corner1 = new THREE.SphereGeometry(radius, smoothness, smoothness, 0, Math.PI * .5, 0, Math.PI * .5);
corner1.translate(-halfWidth, halfHeight, halfDepth);
var corner2 = new THREE.SphereGeometry(radius, smoothness, smoothness, Math.PI * .5, Math.PI * .5, 0, Math.PI * .5);
corner2.translate(halfWidth, halfHeight, halfDepth);
var corner3 = new THREE.SphereGeometry(radius, smoothness, smoothness, 0, Math.PI * .5, Math.PI * .5, Math.PI * .5);
corner3.translate(-halfWidth, -halfHeight, halfDepth);
var corner4 = new THREE.SphereGeometry(radius, smoothness, smoothness, Math.PI * .5, Math.PI * .5, Math.PI * .5, Math.PI * .5);
corner4.translate(halfWidth, -halfHeight, halfDepth);
geometry.merge(corner1);
geometry.merge(corner2);
geometry.merge(corner3);
geometry.merge(corner4);
// edges - 2 fourths for each dimension
// width
var edge = new THREE.CylinderGeometry(radius, radius, width - radius * 2, smoothness, widthSegments, true, 0, Math.PI * .5);
edge.rotateZ(Math.PI * .5);
...
CSS
body {
overflow: hidden;
margin: 0;
}
JavaScript
var scene = new THREE.Scene();
var camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 1, 1000);
camera.position.set(0, 3, 4);
var renderer = new THREE.WebGLRenderer({
antialias: true
});
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setClearColor(0x808080);
document.body.appendChild(renderer.domElement);
var controls = new THREE.OrbitControls(camera, renderer.domElement);
var light = new THREE.DirectionalLight(0xffffff, .5);
light.position.setScalar(10);
scene.add(light);
scene.add(new THREE.AmbientLight(0xffffff, .5));
var box = THREE.SceneUtils.createMultiMaterialObject(RoundEdgedBox(3, 2, 1, .25, 3, 2, 1, 6), [new THREE.MeshStandardMaterial({
color: "red"
}), new THREE.MeshBasicMaterial({
color: "blue",
wireframe: true
})]);
scene.add(box);
render();
function render() {
requestAnimationFrame(render);
renderer.render(scene, camera);
}