gpt4 book ai didi

javascript - 使用three.js有孔的圆 Angular 框

转载 作者:行者123 更新时间:2023-12-04 10:43:22 25 4
gpt4 key购买 nike

我需要创建一个在 4 个侧面但在顶部和底部具有圆 Angular 的盒子。 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 );

他们创建了一个 形状和一个 路径 然后他们添加了这个洞。结果是

enter image description here

我对圆 Angular 正方形使用了相同的逻辑

原形
 // 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 );

结果不一样。。。

enter image description here

如您所见,洞在那里,但内部的 3D 效果是错误的。如何解决?

最佳答案

您必须以相反的缠绕顺序绘制孔。由于外部矩形路径是顺时针绘制的,因此孔路径必须逆时针绘制:

let f_rect = 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 );
}

let f_rect_reverse = function roundedRect( ctx, x, y, width, height, radius ) {
ctx.moveTo( x, y + height - radius );
ctx.lineTo( x, y + radius );
ctx.quadraticCurveTo( x, y, x + radius, y );
ctx.lineTo( x + width - radius, y );
ctx.quadraticCurveTo( x + width, y, x + width, y + radius );
ctx.lineTo( x + width, y + height - radius );
ctx.quadraticCurveTo( x + width, y + height, x + width - radius, y + height );
ctx.lineTo( x + radius, y + height );
ctx.quadraticCurveTo( x, y + height, x, y + height - radius );
}

var roundedRectShape = new THREE.Shape();
f_rect( roundedRectShape, -25, -25, 50, 50, 20 );
var roundedRectShape_small = new THREE.Path();
f_rect_reverse( roundedRectShape_small, -15, -15, 30, 30, 10 );
roundedRectShape.holes.push( roundedRectShape_small );

查看完整示例:

(function onLoad() {
var container, camera, scene, renderer, controls;

init();
animate();

function init() {
container = document.getElementById('container');

renderer = new THREE.WebGLRenderer({
antialias: true
});
renderer.setPixelRatio(window.devicePixelRatio);
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.shadowMap.enabled = true;
container.appendChild(renderer.domElement);

scene = new THREE.Scene();
scene.background = new THREE.Color(0xffffff);

camera = new THREE.PerspectiveCamera(70, window.innerWidth / window.innerHeight, 1, 10000);
camera.position.set(3, 2, 7);
scene.add(camera);
window.onresize = function() {
renderer.setSize(window.innerWidth, window.innerHeight);
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
}

var ambientLight = new THREE.AmbientLight(0x404040);
scene.add(ambientLight);

var directionalLight = new THREE.DirectionalLight( 0xffffff, 0.5 );
directionalLight.position.x = 4;
directionalLight.position.y = 1;
directionalLight.position.z = 2;
scene.add( directionalLight );

controls = new THREE.OrbitControls(camera, renderer.domElement);

addGridHelper();
createModel();
}

function createModel() {

let f_rect = 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 );
}

let f_rect_reverse = function roundedRect( ctx, x, y, width, height, radius ) {
ctx.moveTo( x, y + height - radius );
ctx.lineTo( x, y + radius );
ctx.quadraticCurveTo( x, y, x + radius, y );
ctx.lineTo( x + width - radius, y );
ctx.quadraticCurveTo( x + width, y, x + width, y + radius );
ctx.lineTo( x + width, y + height - radius );
ctx.quadraticCurveTo( x + width, y + height, x + width - radius, y + height );
ctx.lineTo( x + radius, y + height );
ctx.quadraticCurveTo( x, y + height, x, y + height - radius );
}

var roundedRectShape = new THREE.Shape();
f_rect( roundedRectShape, -25, -25, 50, 50, 20 );
var roundedRectShape_small = new THREE.Path();
f_rect_reverse( roundedRectShape_small, -15, -15, 30, 30, 10 );
roundedRectShape.holes.push( roundedRectShape_small );

var material = new THREE.MeshLambertMaterial( { color: 0x00ff00, side: THREE.DoubleSide } );
var extrudeSettings = { depth: 8, bevelEnabled: true, bevelSegments: 2, steps: 3, bevelSize: 5, bevelThickness: 5 };
var geometry = new THREE.ExtrudeBufferGeometry( roundedRectShape, extrudeSettings );

let s = 0.1;
var mesh = new THREE.Mesh( geometry, material );
mesh.scale.set( s, s, s );

scene.add( mesh );
}

function addGridHelper() {

var helper = new THREE.GridHelper(10, 10);
helper.material.opacity = 0.25;
helper.material.transparent = true;
scene.add(helper);

var axis = new THREE.AxesHelper(100);
scene.add(axis);
}

function animate() {
requestAnimationFrame(animate);
render();
}

function render() {
renderer.render(scene, camera);
}
})();
<!--script src="https://threejs.org/build/three.js"></!--script-->
<script src="https://rawcdn.githack.com/mrdoob/three.js/r124/build/three.js"></script>
<script src="https://rawcdn.githack.com/mrdoob/three.js/r124/examples/js/controls/OrbitControls.js"></script></script>
<div id="container"></div>

关于javascript - 使用three.js有孔的圆 Angular 框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59828669/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com