【发布时间】:2020-12-28 16:57:50
【问题描述】:
我需要创建一个在 4 个侧面但在顶部和底部具有圆角的盒子。 Three.js 有一个很好的示例webgl_geometry_shapes,我正在尝试复制它以在绿色圆角框中添加一个孔。在这个例子中为圆添加一个洞:
// Arc circle
var arcShape = new THREE.Shape()
.moveTo( 50, 10 )
.absarc( 10, 10, 40, 0, Math.PI * 2, false );
var holePath = new THREE.Path()
.moveTo( 20, 10 )
.absarc( 10, 10, 10, 0, Math.PI * 2, true )
arcShape.holes.push( holePath );
他们创建了一个shape和一个path,然后他们添加了hole。结果是
我对圆角正方形使用了相同的逻辑
原始形状
// Rounded rectangle
var roundedRectShape = new THREE.Shape();
( function roundedRect( ctx, x, y, width, height, radius ) {
ctx.moveTo( x, y + radius );
ctx.lineTo( x, y + height - radius );
ctx.quadraticCurveTo( x, y + height, x + radius, y + height );
ctx.lineTo( x + width - radius, y + height );
ctx.quadraticCurveTo( x + width, y + height, x + width, y + height - radius );
ctx.lineTo( x + width, y + radius );
ctx.quadraticCurveTo( x + width, y, x + width - radius, y );
ctx.lineTo( x + radius, y );
ctx.quadraticCurveTo( x, y, x, y + radius );
} )( roundedRectShape, 0, 0, 50, 50, 20 );
新孔形状
var roundedRectShape_small = new THREE.Path();
( function roundedRect( ctx, x, y, width, height, radius ) {
ctx.moveTo( x, y + radius );
ctx.lineTo( x, y + height - radius );
ctx.quadraticCurveTo( x, y + height, x + radius, y + height );
ctx.lineTo( x + width - radius, y + height );
ctx.quadraticCurveTo( x + width, y + height, x + width, y + height - radius );
ctx.lineTo( x + width, y + radius );
ctx.quadraticCurveTo( x + width, y, x + width - radius, y );
ctx.lineTo( x + radius, y );
ctx.quadraticCurveTo( x, y, x, y + radius );
} )( roundedRectShape_small, 10, 10, 30, 30, 20 );
然后我添加孔
roundedRectShape.holes.push( roundedRectShape_small );
结果不一样……
您可以看到孔在那里,但内部的 3D 效果是错误的。如何解决?
【问题讨论】:
-
在同一个例子中,“smileyShape”有几个孔。也许我应该以同样的方式画出我的盒子。你有什么建议吗?
-
孔的缠绕顺序必须相反
标签: javascript three.js