长不大的小不 2022-08-12 10:05 采纳率: 0%
浏览 264
已结题

Three.js视频纹理无法播放

使用IDEA运行教材<Three.js开发指南>的有关视频纹理贴图的例程,没有报错,但无法播放给的例程视频,能够加载网页界面和对应的mesh,但是无法播放视频。
<!DOCTYPE html>

<html>

<head>
    <title>Video texture - non canvas</title>
    <script type="text/javascript" src="../libs/three.js"></script>

    <script type="text/javascript" src="../libs/stats.js"></script>
    <script type="text/javascript" src="../libs/dat.gui.js"></script>
    <style>
        body {
            /* set margin to 0 and overflow to hidden, to go fullscreen */
            margin: 0;
            overflow: hidden;
        }
    </style>
</head>
<body>

<div id="Stats-output">
</div>
<video id="video" style="display: none; position: absolute; left: 15px; top: 75px;"
       src="../assets/movies/Big_Buck_Bunny_small.ogv" controls="true" autoplay="true"></video>

<!-- Div which will hold the Output -->
<div id="WebGL-output">
</div>

<!-- Javascript code that runs our Three.js examples -->
<script type="text/javascript">

    var texture;

    // once everything is loaded, we run our Three.js stuff.
    function init() {

        var stats = initStats();

        // create a scene, that will hold all our elements such as objects, cameras and lights.
        var scene = new THREE.Scene();

        // create a camera, which defines where we're looking at.
        var camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.1, 1000);

        // create a render and set the size
        var webGLRenderer = new THREE.WebGLRenderer();
        webGLRenderer.setClearColor(new THREE.Color(0xEEEEEE, 1.0));
        webGLRenderer.setSize(window.innerWidth, window.innerHeight);
        webGLRenderer.shadowMapEnabled = true;

        var video = document.getElementById('video');
        texture = new THREE.VideoTexture(video);
        var cube;


        cube = createMesh(new THREE.BoxGeometry(22, 16, 0.2), "floor-wood.jpg");
        cube.position.y = 2;
        scene.add(cube);


        // position and point the camera to the center of the scene
        camera.position.x = 0;
        camera.position.y = 1;
        camera.position.z = 28;
        camera.lookAt(new THREE.Vector3(0, 0, 0));

        var ambiLight = new THREE.AmbientLight(0x141414);
        scene.add(ambiLight);

        var light = new THREE.DirectionalLight();
        light.position.set(0, 30, 20);
        scene.add(light);

        // add the output of the renderer to the html element
        document.getElementById("WebGL-output").appendChild(webGLRenderer.domElement);

        // call the render function
        var step = 0;

//        var polyhedron = createMesh(new THREE.PolyhedronGeometry(vertices, faces, controls.radius, controls.detail));

        // setup the control gui
        var controls = new function () {

            this.showVideo = false;
            this.rotate = false;

            this.showCanvas = function () {
                if (controls.showVideo) {
                    $('#video').show();
                } else {
                    $('#video').hide();
                }
            }

        };


        var gui = new dat.GUI();
        gui.add(controls, "rotate");
        gui.add(controls, "showVideo").onChange(controls.showCanvas);


        render();

        function createMesh(geom) {

            var materialArray = [];
            materialArray.push(new THREE.MeshBasicMaterial({color: 0x0051ba}));
            materialArray.push(new THREE.MeshBasicMaterial({color: 0x0051ba}));
            materialArray.push(new THREE.MeshBasicMaterial({color: 0x0051ba}));
            materialArray.push(new THREE.MeshBasicMaterial({color: 0x0051ba}));
            materialArray.push(new THREE.MeshBasicMaterial({map: texture}));
            materialArray.push(new THREE.MeshBasicMaterial({color: 0xff51ba}));
            var faceMaterial = new THREE.MeshFaceMaterial(materialArray);


            // create a multimaterial
            var mesh = new THREE.Mesh(geom, faceMaterial);

            return mesh;
        }

        function render() {
            stats.update();

            if (controls.rotate) {
                cube.rotation.x += -0.01;
                cube.rotation.y += -0.01;
                cube.rotation.z += -0.01;
            }


            // render using requestAnimationFrame
            requestAnimationFrame(render);
            webGLRenderer.render(scene, camera);
        }

        function initStats() {

            var stats = new Stats();
            stats.setMode(0); // 0: fps, 1: ms

            // Align top-left
            stats.domElement.style.position = 'absolute';
            stats.domElement.style.left = '0px';
            stats.domElement.style.top = '0px';

            document.getElementById("Stats-output").appendChild(stats.domElement);

            return stats;
        }
    }
    window.onload = init;
</script>

</body>
</html>
尝试过更改视频源,更改three.js版本,使用其他网上教程的代码,更换电脑,均无法将视频作为纹理。

img

希望能实现加载视频纹理到物体表面的功能
  • 写回答

1条回答 默认 最新

  • 拾梦逅 2022-08-12 11:38
    关注

    你下载的three.js版本是多少的?我的102,可以正常显示和播放

    img

    评论

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 8月19日
  • 修改了问题 8月12日
  • 赞助了问题酬金10元 8月12日
  • 创建了问题 8月12日

悬赏问题

  • ¥20 谁刷目标页面的uv记录器上数据,数据只记录跳转的数值
  • ¥30 数据库软件的安装方法
  • ¥15 一道以太网数据传输题
  • ¥15 python 下载群辉文件
  • ¥50 代码还没怎么运行但是需要代码功能调用数据
  • ¥15 vue请求不到数据,返回状态200,数据为html
  • ¥15 用白鹭引擎开发棋牌游戏的前端为什么这么难找
  • ¥35 哪位专业人士知道这是什么原件吗?哪里可以买到?
  • ¥15 关于#c##的问题:treenode反序列化后获取不到上一节点和下一节点,Fullpath和Handle报错
  • ¥15 一部手机能否同时用不同的app进入不同的直播间?