在 three.js 中添加视频作为纹理
Adding video as Texture in three.js
我正在研究 Three.js 的这个例子:http://threejs.org/examples/#canvas_geometry_panorama_fisheye
在这个例子中,我没有使用 6 张图像,而是使用了 5 张图像和一个视频作为纹理(视频格式为 .ogv)。我对上面的例子进行了如下编辑以实现我想要的:
video = document.createElement('video');
video.autoplay = true;
video.src = "textures/videos/Row1Col1.ogv";
var videoTexture = new THREE.Texture(video);
videoTexture.needsUpdate = true;
var materials = [
videoTexture, // right
loadTexture( 'textures/cube/Park2/negx.jpg' ), // left
loadTexture( 'textures/cube/Park2/posy.jpg' ), // top
loadTexture( 'textures/cube/Park2/negy.jpg' ), // bottom
loadTexture( 'textures/cube/Park2/posz.jpg' ), // back
loadTexture( 'textures/cube/Park2/negz.jpg' ) // front
];
mesh = new THREE.Mesh(
new THREE.BoxGeometry( 300, 300, 300, 32, 32, 32 ),
new THREE.MultiMaterial( materials )
);
其余代码与上述示例完全相同。
我没有得到想要的结果(五张图像渲染到球体上,一个视频在其中一个 'side' 上播放),我得到的是:
图像渲染良好,但我看不到正在播放的视频。它的位置只有白色文本。没有其他的。
我是 Three.js 的新手,我第一次尝试将视频用作纹理。请帮助我,让我知道如何在指定区域播放视频。
检查 THIS 页面的来源(Three.js 版本 60)。
你可以看到他正在做更多的工作来让他的视频正常工作。
具体来说,将视频绘制到 canvas 上并使用 canvas 作为 material.
的纹理
// create the video element
video = document.createElement( 'video' );
video.src = "textures/videos/Row1Col1.ogv";
video.load(); // must call after setting/changing source
video.play();
videoImage = document.createElement( 'canvas' );
videoImage.width = 480;
videoImage.height = 204;
videoImageContext = videoImage.getContext( '2d' );
// background color if no video present
videoImageContext.fillStyle = '#000000';
videoImageContext.fillRect( 0, 0, videoImage.width, videoImage.height );
videoTexture = new THREE.Texture( videoImage );
videoTexture.minFilter = THREE.LinearFilter;
videoTexture.magFilter = THREE.LinearFilter;
这个答案现在是旧的(three.js v60),查看其他答案以获得替代方法
自从 r83 version of three.js, you have now a VideoTexture.js class.
参见下面的示例(来自文档):
//assuming you have created a HTML video element with id="video"
var video = document.getElementById( 'video' );
var texture = new THREE.VideoTexture( video );
texture.minFilter = THREE.LinearFilter;
texture.magFilter = THREE.LinearFilter;
texture.format = THREE.RGBFormat;
我正在研究 Three.js 的这个例子:http://threejs.org/examples/#canvas_geometry_panorama_fisheye
在这个例子中,我没有使用 6 张图像,而是使用了 5 张图像和一个视频作为纹理(视频格式为 .ogv)。我对上面的例子进行了如下编辑以实现我想要的:
video = document.createElement('video');
video.autoplay = true;
video.src = "textures/videos/Row1Col1.ogv";
var videoTexture = new THREE.Texture(video);
videoTexture.needsUpdate = true;
var materials = [
videoTexture, // right
loadTexture( 'textures/cube/Park2/negx.jpg' ), // left
loadTexture( 'textures/cube/Park2/posy.jpg' ), // top
loadTexture( 'textures/cube/Park2/negy.jpg' ), // bottom
loadTexture( 'textures/cube/Park2/posz.jpg' ), // back
loadTexture( 'textures/cube/Park2/negz.jpg' ) // front
];
mesh = new THREE.Mesh(
new THREE.BoxGeometry( 300, 300, 300, 32, 32, 32 ),
new THREE.MultiMaterial( materials )
);
其余代码与上述示例完全相同。
我没有得到想要的结果(五张图像渲染到球体上,一个视频在其中一个 'side' 上播放),我得到的是:
图像渲染良好,但我看不到正在播放的视频。它的位置只有白色文本。没有其他的。
我是 Three.js 的新手,我第一次尝试将视频用作纹理。请帮助我,让我知道如何在指定区域播放视频。
检查 THIS 页面的来源(Three.js 版本 60)。
你可以看到他正在做更多的工作来让他的视频正常工作。
具体来说,将视频绘制到 canvas 上并使用 canvas 作为 material.
// create the video element
video = document.createElement( 'video' );
video.src = "textures/videos/Row1Col1.ogv";
video.load(); // must call after setting/changing source
video.play();
videoImage = document.createElement( 'canvas' );
videoImage.width = 480;
videoImage.height = 204;
videoImageContext = videoImage.getContext( '2d' );
// background color if no video present
videoImageContext.fillStyle = '#000000';
videoImageContext.fillRect( 0, 0, videoImage.width, videoImage.height );
videoTexture = new THREE.Texture( videoImage );
videoTexture.minFilter = THREE.LinearFilter;
videoTexture.magFilter = THREE.LinearFilter;
这个答案现在是旧的(three.js v60),查看其他答案以获得替代方法
自从 r83 version of three.js, you have now a VideoTexture.js class.
参见下面的示例(来自文档):
//assuming you have created a HTML video element with id="video"
var video = document.getElementById( 'video' );
var texture = new THREE.VideoTexture( video );
texture.minFilter = THREE.LinearFilter;
texture.magFilter = THREE.LinearFilter;
texture.format = THREE.RGBFormat;