gpt4 book ai didi

javascript - 使用 THREE.js 任何形状的可缩放轮廓

转载 作者:行者123 更新时间:2023-11-29 20:48:39 27 4
gpt4 key购买 nike

首先我应该说我是 WebGL 和三个库的新手。我正在努力实现:

  • 向场景添加 2D 形状(正方形、三 Angular 形、圆形等)
  • 形状可以有任何大小和位置
  • 形状应该没有填充和根据形状大小缩放的轮廓

我尝试使用线框:

const geometry = new THREE.BoxGeometry(40, 40, 0);

const material = new THREE.MeshBasicMaterial({
color: 0xff0000,
wireframe: true,
});

const mesh = new THREE.Mesh(geometry, material);
scene.add(mesh);

以上呈现如下:

  • 没有深度的 3D 盒子
  • 设置宽度为 1 的不可缩放轮廓
  • 禁区内传中

我也尝试了相同的方法,但绘制了二维形状的点:

const shapeSize = 100;
const x = -shapeSize / 2;
const y = -shapeSize / 2;

const square = new THREE.Shape();

square.moveTo(x, y);
square.lineTo(x + shapeSize, y);
square.lineTo(x + shapeSize, y + shapeSize);
square.lineTo(x, y + shapeSize);
square.lineTo(x, y);

const geometry = new THREE.ShapeGeometry(square);

const material = new THREE.MeshBasicMaterial({
color: 0xff0000,
wireframe: true,
});

const mesh = new THREE.Mesh(geometry, material);
this.scene.add(mesh);

上面的渲染图:

  • 一个二维盒子
  • 设置宽度为 1 的不可缩放轮廓
  • 一条穿过盒子的对 Angular 线

谁能演示如何实现二维形状的可缩放轮廓?我需要使用着色器吗?

最佳答案

我刚刚使用了粗线示例中的代码:

var scene = new THREE.Scene();
var camera = new THREE.PerspectiveCamera(60, window.innerWidth / window.innerHeight, 1, 1000);
camera.position.set(0, 0, 10);
renderer = new THREE.WebGLRenderer({
antialias: true
});
renderer.setPixelRatio(window.devicePixelRatio);
renderer.setClearColor(0x000000, 0.0);
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);

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

var points = [
-5, 4, 0, 5, 4, 0, 5, -4, 0, -5, -4, 0, -5, 4, 0
]

var geometry = new THREE.LineGeometry();
geometry.setPositions(points);

matLine = new THREE.LineMaterial({

color: 0x00ffff,
linewidth: 5, // in pixels
dashed: false

});

line = new THREE.Line2(geometry, matLine);
line.computeLineDistances();
line.scale.set(1, 1, 1);
scene.add(line);

render();

function render() {
requestAnimationFrame(render);
matLine.resolution.set(window.innerWidth, window.innerHeight);
renderer.render(scene, camera);
}
body {
overflow: hidden;
margin: 0;
}
<script src="https://threejs.org/build/three.min.js"></script>
<script src="https://threejs.org/examples/js/controls/OrbitControls.js"></script>

<script src="https://threejs.org/examples/js/WebGL.js"></script>

<script src='https://threejs.org/examples/js/lines/LineSegmentsGeometry.js'></script>
<script src='https://threejs.org/examples/js/lines/LineGeometry.js'></script>

<script src='https://threejs.org/examples/js/lines/LineMaterial.js'></script>

<script src='https://threejs.org/examples/js/lines/LineSegments2.js'></script>
<script src='https://threejs.org/examples/js/lines/Line2.js'></script>
<script src='https://threejs.org/examples/js/lines/Wireframe.js'></script>

关于javascript - 使用 THREE.js 任何形状的可缩放轮廓,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53098863/

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