Hello,
I try to render a VideoTexture on a 3D Object. This is for a AR application.
It works with local video but external https video do not work.
Maybe have you an idea?
<!DOCTYPE html>
<html lang="en">
<head>
<title id="bb">My Header</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
<link type="text/css" rel="stylesheet" href="main.css">
</head>
<body>
<video id="video" loop crossOrigin="anonymous" webkit-playsinline style="display:none">
-->
<source src="video.mp4" type="video/mp4">
</video>
// and set JS variable:
//
<div id="info">
<p id="exciting">The most exciting paragraph on the page. One of a kind!</p>
</div>
<script src="three.js"></script>
<script src="OBJLoader.js"></script>
<script src="MTLLoader.js"></script>
<script src="utils.js"></script>
<script src="appAR.js"></script>
<script type="module">
import * as THREE from './build/three.module.js';
import { ARButton } from './jsm/webxr/ARButton.js';
let container;
let camera, scene, renderer;
let controller;
let reticle;
let mesh, video, texture;
let hitTestSource = null;
let hitTestSourceRequested = false;
let videoTexture;
let videoImageContext ;
init();
animate();
function init() {
container = document.createElement( 'div' );
document.body.appendChild( container );
scene = new THREE.Scene();
camera = new THREE.PerspectiveCamera();
camera.matrixAutoUpdate = false;
var updateFcts = [];
//
renderer = new THREE.WebGLRenderer( { antialias: true } );
renderer.autoClear = false;
renderer.setPixelRatio( window.devicePixelRatio );
renderer.setSize( window.innerWidth, window.innerHeight );
renderer.xr.enabled = true;
container.appendChild( renderer.domElement );
//
document.body.appendChild( ARButton.createButton( renderer, { requiredFeatures: [ 'hit-test' ] } ) );
//
function onSelect() {
if ( reticle.visible ) {
// video.id = 'video';
// video.type = ' video/ogg; codecs="theora, vorbis" ';
video = document.getElementById( 'video' );
texture = new THREE.VideoTexture( video );
texture.minFilter = THREE.LinearFilter;
texture.magFilter = THREE.LinearFilter;
texture.format = THREE.RGBFormat;
var geometry = new THREE.PlaneBufferGeometry( 2, 1);
var materialVideo = new THREE.MeshBasicMaterial( { map: texture , side: THREE.DoubleSide } );
mesh = new THREE.Mesh( geometry, materialVideo);
mesh.position.setFromMatrixPosition( reticle.matrix );
scene.add( mesh );
video.play();
}
}
controller = renderer.xr.getController( 0 );
controller.addEventListener( 'select', onSelect );
scene.add( controller );
reticle = new THREE.Mesh(
new THREE.RingGeometry( 0.15, 0.2, 32 ).rotateX( - Math.PI / 2 ),
new THREE.MeshBasicMaterial()
);
reticle.matrixAutoUpdate = false;
reticle.visible = false;
scene.add( reticle );
//
window.addEventListener( 'resize', onWindowResize );
}
function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize( window.innerWidth, window.innerHeight );
}
//
function animate() {
renderer.setAnimationLoop( render );
}
function render( timestamp, frame ) {
if ( frame ) {
const referenceSpace = renderer.xr.getReferenceSpace();
const session = renderer.xr.getSession();
if ( hitTestSourceRequested === false ) {
session.requestReferenceSpace( 'viewer' ).then( function ( referenceSpace ) {
session.requestHitTestSource( { space: referenceSpace } ).then( function ( source ) {
hitTestSource = source;
} );
} );
session.addEventListener( 'end', function () {
hitTestSourceRequested = false;
hitTestSource = null;
} );
hitTestSourceRequested = true;
}
if ( hitTestSource ) {
const hitTestResults = frame.getHitTestResults( hitTestSource );
if ( hitTestResults.length ) {
const hit = hitTestResults[ 0 ];
reticle.visible = true;
reticle.matrix.fromArray( hit.getPose( referenceSpace ).transform.matrix );
} else {
reticle.visible = false;
}
}
}
if(video!=null)
{
if ( video.readyState === video.HAVE_ENOUGH_DATA )
{
if ( texture )
texture.needsUpdate = true;
}
}
renderer.render( scene, camera );
}
</script>
<script src="js/app.js"></script>
</body>
</html>