使用Three.js的LatheGeometry画一个胶囊

核心是使用LatheGeometry将一个2D的线扫描成3D物体

 function createObj() {
    // 创建的单个粒子的尺寸为(radius * 2, height + radius * 2, radius * 2)
    const radius = 4;
    const height = 192;
    itemHeight = radius * 2 + height;
    // 存放样条曲线的点集
    const points = [];
    //上半部分四分之一圆弧
    for (let i = Math.PI / 2; i > 0; i -= 0.3) {
      points.push(
        new THREE.Vector2(
          Math.cos(i) * radius,
          Math.sin(i) * radius + height / 2
        )
      );
    }
    //中间直线
    for (let i = height / 2; i > -height / 2; i -= height) {
      points.push(new THREE.Vector2(radius, i));
    }
    //下半部分四分之一圆弧
    for (let i = 0; i <= Math.PI / 2; i += 0.3) {
      points.push(
        new THREE.Vector2(
          Math.cos(i) * radius,
          -Math.sin(i) * radius - height / 2
        )
      );
    }

    // 补充一个点,去掉底部的小洞
    points.push(new THREE.Vector2(0, -radius - height / 2));
    console.log(points);
    const geometry = new THREE.LatheGeometry(points);
    const pngec = textureLoader.load(pngUrl);
    // 围绕中心点旋转180度
    // pngec.center = new THREE.Vector2(0.5, 0.5);
    // pngec.rotation = Math.PI;
    // pngec.mapping = THREE.UVMapping;
    const material = new THREE.MeshBasicMaterial({
      transparent: true,
      opacity: 0.6,
      vertexColors: false,
      map: pngec,
      blending: THREE.AdditiveBlending,
      color: new THREE.Color(0xffffff),
    });
    const mesh = new THREE.Mesh(geometry, material);

   return mesh;
  }
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容