你能在three.js中使用原始的WebGL纹理吗

msf*_*ein 4 webgl three.js

我有一个相当复杂的架构,我在 Three.JS 中完成我的大部分工作,但我也有一个特殊的渲染器,可以直接渲染到原始的 WebGL 纹理。是否可以在three.js“纹理”中使用此WebGL纹理?看起来 Three.JS 纹理类只是图像、视频或画布的容器,并且在 Three.js 深处的某个地方,它会将其上传到真正的 webgl 纹理。我怎样才能让 Three.js 将我的 WebGL 纹理渲染到网格上?

gma*_*man 5

@Brendan 的回答不再有效。

不知道它什么时候变了,也懒得去查了,但从 r102 开始

const texture = new THREE.Texture();
renderer.setTexture2D(texture, 0);  // force three.js to init the texture
const texProps = renderer.properties.get(texture);
texProps.__webglTexture = glTex;
Run Code Online (Sandbox Code Playgroud)

从 r103 开始setTexture2D不再存在。你可以用这个代替

  const forceTextureInitialization = function() {
    const material = new THREE.MeshBasicMaterial();
    const geometry = new THREE.PlaneBufferGeometry();
    const scene = new THREE.Scene();
    scene.add(new THREE.Mesh(geometry, material));
    const camera = new THREE.Camera();

    return function forceTextureInitialization(texture) {
      material.map = texture;
      renderer.render(scene, camera);
    };
  }();

  const texture = new THREE.Texture();
  forceTextureInitialization(texture);  // force three.js to init the texture
  const texProps = renderer.properties.get(texture);
  texProps.__webglTexture = glTex;
Run Code Online (Sandbox Code Playgroud)

const texture = new THREE.Texture();
renderer.setTexture2D(texture, 0);  // force three.js to init the texture
const texProps = renderer.properties.get(texture);
texProps.__webglTexture = glTex;
Run Code Online (Sandbox Code Playgroud)
  const forceTextureInitialization = function() {
    const material = new THREE.MeshBasicMaterial();
    const geometry = new THREE.PlaneBufferGeometry();
    const scene = new THREE.Scene();
    scene.add(new THREE.Mesh(geometry, material));
    const camera = new THREE.Camera();

    return function forceTextureInitialization(texture) {
      material.map = texture;
      renderer.render(scene, camera);
    };
  }();

  const texture = new THREE.Texture();
  forceTextureInitialization(texture);  // force three.js to init the texture
  const texProps = renderer.properties.get(texture);
  texProps.__webglTexture = glTex;
Run Code Online (Sandbox Code Playgroud)

注意:three.js 中没有“不受支持的行为”这样的东西。Three.js 不保证您今天所做的任何事情明天都会奏效。Three.js 随时打破它想要的任何东西