首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >您可以在three.js中使用原始WebGL纹理吗

您可以在three.js中使用原始WebGL纹理吗
EN

Stack Overflow用户
提问于 2015-03-29 13:22:00
回答 2查看 1.3K关注 0票数 4

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

EN

回答 2

Stack Overflow用户

发布于 2019-03-10 12:26:26

@Brendan的答案不再有效。

不知道它是什么时候改变的,懒得去查找,但从r102开始

代码语言:javascript
复制
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;

从r103开始,setTexture2D已不复存在。你可以用这个来代替

代码语言:javascript
复制
  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;

代码语言:javascript
复制
'use strict';

/* global THREE */

function main() {
  const canvas = document.querySelector('#c');
  const renderer = new THREE.WebGLRenderer({
    canvas: canvas
  });
  
  const fov = 75;
  const aspect = 2; // the canvas default
  const near = 0.1;
  const far = 5;
  const camera = new THREE.PerspectiveCamera(fov, aspect, near, far);
  camera.position.z = 2;

  const scene = new THREE.Scene();

  const boxWidth = 1;
  const boxHeight = 1;
  const boxDepth = 1;
  const geometry = new THREE.BoxGeometry(boxWidth, boxHeight, boxDepth);
  
  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 cubes = []; // just an array we can use to rotate the cubes

  {
    const gl = renderer.getContext();
    const glTex = gl.createTexture();
    gl.bindTexture(gl.TEXTURE_2D, glTex);
    gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, 2, 2, 0,
        gl.RGBA, gl.UNSIGNED_BYTE, new Uint8Array([
          255, 0, 0, 255,
          0, 255, 0, 255,
          0, 0, 255, 255,
          255, 255, 0, 255,
        ]));
    gl.generateMipmap(gl.TEXTURE_2D);
    gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
  
    const texture = new THREE.Texture();
    forceTextureInitialization(texture);
    const texProps = renderer.properties.get(texture);
    texProps.__webglTexture = glTex;
    
    const material = new THREE.MeshBasicMaterial({
      map: texture,
    });
    const cube = new THREE.Mesh(geometry, material);
    scene.add(cube);
    cubes.push(cube); // add to our list of cubes to rotate
  }

  function render(time) {
    time *= 0.001;

    cubes.forEach((cube, ndx) => {
      const speed = .2 + ndx * .1;
      const rot = time * speed;
      cube.rotation.x = rot;
      cube.rotation.y = rot;
    });

    renderer.render(scene, camera);

    requestAnimationFrame(render);
  }

  requestAnimationFrame(render);
}

main();
代码语言:javascript
复制
<canvas id="c"></canvas>
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/103/three.min.js"></script>

注意:在three.js中没有“不支持的行为”这样的东西。Three.js不能保证你今天做的任何事情明天都会奏效。Three.js breaks whatever it wants to whenever it wants to

票数 5
EN

Stack Overflow用户

发布于 2015-03-31 00:30:53

这是完全不支持的行为,但您可以模仿WebGLRenderer并直接在纹理上设置__webglTexture属性。例如:

代码语言:javascript
复制
var texure = new THREE.Texture();
var rawTexture = gl.createTexture();
texture.__webglTexture = rawTexture;
texture.__webglInit = true;
// ... use texture as a normal three.js texture ...

同样,这是完全不受支持和未定义的行为,可能会在未来的three.js版本中崩溃,但如果您追求速度,可能会暂时工作。

相反,我建议如果可以的话,研究一下WebGLRenderTarget,或者提交一个特性请求来正确地启用这个特性。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29325906

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档