gpt4 book ai didi

javascript - 使用 WebGL 纹理作为 Three.js 纹理贴图

转载 作者:行者123 更新时间:2023-12-03 00:03:23 29 4
gpt4 key购买 nike

我有一个带有平面的场景,应该使用 WebGL 纹理(使用 gl.createTexture() 创建)作为 Material 贴图。基本上,如何使用该纹理并不重要,我只需要找到一种方法将其传递给具有一定制服的 ShaderMaterial 即可。 WebGL 纹理更新每一帧。

纹理在其他 Canvas 元素的上下文中渲染,并且可以在该上下文中使用,如下所示:

var imageLocation = gl.getUniformLocation(program, "u_image");
gl.uniform1i(imageLocation, 0);
gl.activeTexture(gl.TEXTURE0);
gl.bindTexture(gl.TEXTURE_2D, targetTexture1);

我已经尝试过 this solution 。但似乎 THREE.Texture() 没有从原始 WebGL 纹理中获取数据。

最佳答案

如果你深入挖掘the source这将从 r103 开始​​起作用。当然,不能保证它将来会起作用,但是nothing in three is guaranteed to work in the future .

首先创建一个Texture,然后调用forceTextureInitialization来强制Three.js初始化纹理。

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

然后将 Texture 的 WebGL 纹理替换为您自己的纹理,如下所示

const texProps = renderer.properties.get(someTexture);
texProps.__webglTexture = someGLTexture;

示例:

'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>

请注意,当然,您可以获取 Texture 已在使用的纹理,然后使用 WebGL 对其进行操作,而不是使用自己的纹理

关于javascript - 使用 WebGL 纹理作为 Three.js 纹理贴图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55082573/

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