javascript - 在 React-Three-Fiber 中渲染索引缓冲区几何

标签 javascript reactjs three.js react-three-fiber

我目前正在尝试从 https://threejs.org/examples/?q=buffer#webgl_buffergeometry_indexed 中获取示例在 React-Three-Fiber 环境中工作。

我目前在我的代码中发现了几个问题。

首先,特别是在 Chrome 中,我反复收到以下警告:

[.WebGL-0x26d30384f700] GL_INVALID_ENUM: Enum is not currently supported.

此外,我目前看不到任何颜色应用于网格。

这是我目前使用的代码:

import React, { useMemo } from "react";
import { Canvas } from "@react-three/fiber";
import { DoubleSide } from "three";

const App = () => {
  const size = 20;
  const segments = 10;

  const [colors, normals, positions] = useMemo(() => {
    const colorsArr = [];
    const normalsArr = [];
    const positionsArr = [];

    const halfSize = size / 2;
    const segmentSize = size / segments;

    // generate vertices, normals and color data for a simple grid geometry

    for (let i = 0; i <= segments; i++) {
      const y = i * segmentSize - halfSize;

      for (let j = 0; j <= segments; j++) {
        const x = j * segmentSize - halfSize;

        positionsArr.push(x, -y, 0);
        normalsArr.push(0, 0, 1);

        const r = x / size + 0.5;
        const g = y / size + 0.5;

        colorsArr.push(r, g, 1);
      }
    }

    return [colorsArr, normalsArr, positionsArr];
  }, []);

  const indices = useMemo(() => {
    const indicesArr = [];

    // generate indices (data for element array buffer)

    for (let i = 0; i < segments; i++) {
      for (let j = 0; j < segments; j++) {
        const a = i * (segments + 1) + (j + 1);
        const b = i * (segments + 1) + j;
        const c = (i + 1) * (segments + 1) + j;
        const d = (i + 1) * (segments + 1) + (j + 1);

        // generate two faces (triangles) per iteration

        indicesArr.push(a, b, d); // face one
        indicesArr.push(b, c, d); // face two
      }
    }

    return indicesArr;
  }, []);

  return (
    <Canvas
      camera={{
        fov: 27,
        near: 1,
        far: 3500
      }}
      position-z={64}
    >
      <color attach="background" args={["#050505"]} />
      <mesh>
        <bufferGeometry attach="geometry">
          <bufferAttribute
            array={indices}
            attach="index"
            count={indices.length}
            itemSize={1}
          />
          <bufferAttribute
            attachObject={["attributes", "position"]}
            count={positions.length / 3}
            array={positions}
            itemSize={3}
          />
          <bufferAttribute
            attachObject={["attributes", "color"]}
            count={colors.length / 3}
            array={colors}
            itemSize={3}
          />
          <bufferAttribute
            attachObject={["attributes", "normal"]}
            count={normals.length / 3}
            array={normals}
            itemSize={3}
          />
        </bufferGeometry>
        <meshPhongMaterial attach="material" side={DoubleSide} vertexColors />
      </mesh>
      <hemisphereLight />
    </Canvas>
  );
};

export default App;

Three.js 中的示例代码:https://github.com/mrdoob/three.js/blob/master/examples/webgl_buffergeometry_indexed.html

最佳答案

终于解决了这个问题。主要问题是我需要将位置、颜色和法线数组转换为 Float32Array() 并将索引转换为 Uint32Array()。

例如,在索引数组的情况下,以下对我有用:

const indices = useMemo(() => {
    const indicesArr = [];

    // generate indices (data for element array buffer)

    for (let i = 0; i < segments; i++) {
      for (let j = 0; j < segments; j++) {
        const a = i * (segments + 1) + (j + 1);
        const b = i * (segments + 1) + j;
        const c = (i + 1) * (segments + 1) + j;
        const d = (i + 1) * (segments + 1) + (j + 1);

        // generate two faces (triangles) per iteration

        indicesArr.push(a, b, d); // face one
        indicesArr.push(b, c, d); // face two
      }
    }

    return new Uint32Array(indicesArr);
  }, []);

关于javascript - 在 React-Three-Fiber 中渲染索引缓冲区几何,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68087468/

相关文章:

javascript - Facebook SDK 权限

javascript - parcel serve 为 SourceMap 请求和不存在的 url 返回 index.html

d3.js - 如何使用我的 geo json 文件以 3D 形式渲染城市

javascript - 使用控件拖动和单击对象

javascript - innerHTML 和

javascript - 在字符串中搜索整个单词

reactjs - 如何正确设置 React 路由?

three.js - 使用从 Blender 导入的模型进行镜面高光?

javascript - 要么将根组件包装在 <Provider> 中,要么将 "store"作为 prop 显式传递给“Connect(CharacterList)

javascript - React 条件返回中预期的属性分配