BabylonJS——视频纹理



要在场景中显示视频,BabylonJS具有视频纹理功能。videotexture将视频数组作为输入。

对于视频纹理,我们将使用mp4文件。请下载您选择的mp4并将其用于下面的演示中。

语法

video.material.diffuseTexture = new BABYLON.VideoTexture("video",
["mp4 file", "webm file"], scene, true);

演示

<!doctype html>
<html>
   <head>
      <meta charset = "utf-8">
      <title>BabylonJs - Basic Element-Creating Scene</title>
      <script src = "babylon.js"></script>
      <style>
         canvas {width: 100%; height: 100%;}
      </style>
   </head>
   
   <body>
      <canvas id = "renderCanvas"></canvas>
      <script type = "text/javascript">
         var canvas = document.getElementById("renderCanvas");
         var engine = new BABYLON.Engine(canvas, true);
         
         var createScene  = function() {
            var scene = new BABYLON.Scene(engine);

            //Adding an Arc Rotate Camera
            var camera = new BABYLON.ArcRotateCamera("Camera", 0, 0.8, 100, BABYLON.Vector3.Zero(), scene);
            camera.attachControl(canvas, false);

            // This creates a light, aiming 0,1,0 - to the sky (non-mesh)
            var light = new BABYLON.HemisphericLight("light1", new BABYLON.Vector3(0, 1, 0), scene);

            // Default intensity is 1. Let's dim the light a small amount
            light.intensity = 0.7;

            // Our built-in 'sphere' shape. Params: name, subdivs, size, scene
            var ground = BABYLON.Mesh.CreateGround("ground1", 100, 50, 2, scene);

            // Move the sphere upward 1/2 its height
            ground.position.y = 1;

            var mat = new BABYLON.StandardMaterial("mat", scene);

            var videoTexture = new BABYLON.VideoTexture("video", ["sounds/video.mp4"], scene, true, true);

            mat.diffuseTexture = videoTexture;
            ground.material = mat;

            scene.onPointerUp = function () {
               videoTexture.video.play();
            }
            return scene;
         };
         var scene = createScene();
         engine.runRenderLoop(function() {
            scene.render();
         });
      </script>
   </body>
</html>

输出

以上代码行将生成以下输出 -

Video Texture
babylonjs_mesh.htm
广告