gpt4 book ai didi

three.js - 您可以将原始 WebGL 纹理与 Three.js 一起使用吗

转载 作者:行者123 更新时间:2023-12-02 22:12:52 24 4
gpt4 key购买 nike

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

最佳答案

@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;

自 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;

'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();
<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

关于three.js - 您可以将原始 WebGL 纹理与 Three.js 一起使用吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29325906/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com