I can set a video texture as a source of the texture of material, but it appears, that the play() function does not work.
I guess, this is due to chrome on mobile blocking the autoplay.
Is there a way to start a video or change a frame? Maybe any hack to implement a video as a texture?
I’ve spent hours on this issue…
My code:
var Videotexture = pc.createScript('videotexture');
Videotexture.attributes.add('materials', {
type: 'asset',
assetType: 'material',
array: true
});
Videotexture.attributes.add('videoUrl', {
type: 'string'
});
// initialize code called once per entity
Videotexture.prototype.initialize = function() {
var app = this.app;
// Create a texture to hold the video frame data
var videoTexture = new pc.Texture(app.graphicsDevice, {
format: pc.PIXELFORMAT_R5_G6_B5,
autoMipmap: false
});
videoTexture.minFilter = pc.FILTER_LINEAR;
videoTexture.magFilter = pc.FILTER_LINEAR;
videoTexture.addressU = pc.ADDRESS_CLAMP_TO_EDGE;
videoTexture.addressV = pc.ADDRESS_CLAMP_TO_EDGE;
// Create HTML Video Element to play the video
var video = document.createElement('video');
video.setAttribute('autoplay', '');
video.setAttribute('muted', '');
// This is critical for iOS or the video initially goes fullscreen
video.setAttribute('playsinline', '');
video.src = this.videoUrl;
video.crossOrigin = 'anonymous';
video.loop = true;
videoTexture.srcObject = video;
// iOS needs a user action to start the video
if (pc.platform.mobile) {
window.addEventListener('touchstart', function (e) {
e.preventDefault();
});
}
video.addEventListener('canplay', function (e) {
videoTexture.setSource(video);
});
video.play();
// Get the material that we want to play the video on and assign the new video
// texture to it.
for (var i = 0; i < this.materials.length; i++) {
var material = this.materials[i].resource;
material.emissiveMap = videoTexture;
material.update();
}
this.videoTexture = videoTexture;
this.upload = true;
};
// update code called every frame
Videotexture.prototype.update = function(dt) {
this.videoTexture.upload();
};