javascript - 更改点云数据中特定点的不透明度

标签 javascript reactjs three.js point-clouds

我正在使用 PCDLoader 加载一些 PCD 数据,成功加载 PCD 数据后,我们获得了要添加到场景中的。 我在使用 Three.js 线几何创建的 PCD 点顶部有一个圆圈,Here's the image of how my circle is placed on top of PCD data

我正在尝试降低圆圈外所有点的不透明度。

这是我加载 PCD 并绘制圆的代码

this.loader = this.loader || new PCDLoader();
this.loader.load(pcdPath, (mesh: Points) => {

  mesh.name = `pcd-mesh`;
  (mesh.material as PointsMaterial).size = 1.5;
  (mesh.material as PointsMaterial).color.setHex(0xffffff);

  const circlePoints = [];
  const radius = 18;
  for (let i = 0; i <= 360; i++) {
    circlePoints.push(
      new Vector3(
        Math.sin(i * (Math.PI / 180)) * radius,
        Math.cos(i * (Math.PI / 180)) * radius,
        0
      )
    );
  }
  const circleLineGeo = new BufferGeometry().setFromPoints(circlePoints);

  const CircleLineMaterial = new LineBasicMaterial({
    color: 0xffff00,
    linewidth: 1.75,
  });

  const c = new Line(circleLineGeo, CircleLineMaterial);
  this.scene.add(c);

  this.renderView();
});

我知道我可以使用 ;(mesh.material as PointsMaterial).opacity = 0.5

更改所有点的不透明度

但我不想更改所有点的不透明度,我只想更改位于黄色圆圈之外的所有点的不透明度。

最佳答案

r127 起,three.js 支持四分量顶点颜色。这意味着您可以控制每个顶点的 alpha 值。查看以下实时示例以了解更多详细信息:

let camera, scene, renderer;

init();
render();

function init() {

  camera = new THREE.PerspectiveCamera(70, window.innerWidth / window.innerHeight, 0.01, 10);
  camera.position.z = 4;

  scene = new THREE.Scene();

  const points = [
    new THREE.Vector3(-2, 0, 0),
    new THREE.Vector3(-1, 0, 0),
    new THREE.Vector3(0, 0, 0),
    new THREE.Vector3(1, 0, 0),
    new THREE.Vector3(2, 0, 0)
  ];

  const geometry = new THREE.BufferGeometry().setFromPoints(points);
  const material = new THREE.PointsMaterial({
    vertexColors: true,
    transparent: true
  });
  const pointCloud = new THREE.Points(geometry, material);
  scene.add(pointCloud);

  // add color data

  const colors = [
    1, 0, 0, 0, // r,g,b,a
    1, 0, 0, 0.25,
    1, 0, 0, 0.5,
    1, 0, 0, 0.75,
    1, 0, 0, 1
  ];

  geometry.setAttribute('color', new THREE.Float32BufferAttribute(colors, 4));

  renderer = new THREE.WebGLRenderer({
    antialias: true
  });
  renderer.setSize(window.innerWidth, window.innerHeight);
  document.body.appendChild(renderer.domElement);

}

function render() {

  renderer.render(scene, camera);

}
body {
  margin: 0;
}
<script src="https://cdn.jsdelivr.net/npm/three@0.129.0/build/three.min.js"></script>

关于javascript - 更改点云数据中特定点的不透明度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67890243/

相关文章:

reactjs - 当一项更改时,React 会重新渲染整个列表

three.js - 如何在 Three.js 中补间 10,000 多个粒子?

javascript - 三.js球坐标公式

javascript - 如何在 Three.js 中为camera.zoom设置动画

javascript - AngularJS 和 PHP - 通知 : Undefined property: stdClass

javascript - 为什么我应该使用事件委托(delegate)而不是 queryselectorAll

javascript - 来自 Heroku 和 Webpack 的配置变量

javascript - arguments 属性可以通过 this.some_function.arguments 访问吗?其实我无法解释?

javascript - jQuery 添加 html 到原始标题?

reactjs - React-dates onDatesChange startDate 和 endDate 为空,DayPickerRangeController