BabylonJS - 动态纹理

BabylonJS的动态纹理创建了一个画布,您可以轻松地在纹理上书写文字.它还允许您使用canvas并使用html5 canvas提供的所有功能与动态纹理一起使用.

我们将处理一个示例,该示例将显示如何在其上写入文本纹理并且还会在我们创建的网格上绘制一个贝塞尔曲线.

语法

以下是创建动态纹理和减号的语法;

var myDynamicTexture = new BABYLON.DynamicTexture(name,option,scene);

参数

以下是创建动态纹理所需的参数 :

  • name : 动态纹理的名称

  • 选项 : 将具有动态纹理的宽度和高度

  • 场景 : 场景创建

语法

以下是在纹理上写文字的语法 :

myDynamicTexture.drawText(text, x, y, font, color, canvas color, invertY, update);

参数

以下是在纹理上写文字所需的参数 :

  • text : 要写的文字;

  • x : 距离左手边缘的距离;

  • Y : 距离顶边或底边的距离,取决于invertY;

  • font : 字体定义,格式为font-style,font-size,font_name;

  • invertY : 默认情况下为true,在这种情况下,y是距离顶部的距离,当为false时,y是距离底部的距离,字母反转;

  • 更新 : 默认情况下为true,动态纹理将立即更新.

演示

<!doctype html>
<html>
   <head>
      <meta charset = "utf-8">
      <title>MDN Games: Babylon.js demo - shapes</title>
      <script src = "https://end3r.github.io/MDN-Games-3D/Babylon.js/js/babylon.js"></script>    
      <style>
         html,body,canvas { margin: 0; padding: 0; width: 100%; height: 100%; font-size: 0; }
      </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);

            var camera = new BABYLON.ArcRotateCamera("Camera", -Math.PI/2, Math.PI / 3, 25, BABYLON.Vector3.Zero(), scene);
            camera.attachControl(canvas, true);

            var light = new BABYLON.HemisphericLight("light1", new BABYLON.Vector3(0, 1, 0), scene);
            light.intensity = 0.7;	

            var box = BABYLON.Mesh.CreateBox("box", 3.0, scene);
            box.position = new BABYLON.Vector3(0, 0, -5); 

            //Create dynamic texture		
            var textureGround = new BABYLON.DynamicTexture("dynamic texture", {width:512, height:256}, scene);   
            var textureContext = textureGround.getContext();

            var materialGround = new BABYLON.StandardMaterial("Mat", scene);    				
            materialGround.diffuseTexture = textureGround;
            box.material = materialGround;

            //Add text to dynamic texture
            var font = "bold 60px Arial";
            textureGround.drawText("Box", 200, 150, font, "green", "white", true, true);
            return scene;
         };
         var scene = createScene();
         engine.runRenderLoop(function() {
            scene.render();
         });
      </script>
   </body>
</html>

输出

动态纹理

动态纹理还允许在动态纹理上使用html5画布方法和属性,如下所示;

语法

var ctx = myDynamicTexture.getContext();

演示

<!doctype html>
<html>
   <head>
      <meta charset = "utf-8">
      <title> Babylon.JS : Demo2</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);

            var camera = new BABYLON.ArcRotateCamera("Camera", -Math.PI/2, Math.PI / 3, 25, BABYLON.Vector3.Zero(), scene);
            camera.attachControl(canvas, true);

            var light = new BABYLON.HemisphericLight("light1", new BABYLON.Vector3(0, 1, 0), scene);
            light.intensity = 0.7;		

            var ground = BABYLON.MeshBuilder.CreateGround("ground1", {width: 20, height: 10, subdivisions: 25}, scene);

            //Create dynamic texture
            var textureGround = new BABYLON.DynamicTexture("dynamic texture", 512, scene);   
            var textureContext = textureGround.getContext();

            var materialGround = new BABYLON.StandardMaterial("Mat", scene);    				
            materialGround.diffuseTexture = textureGround;
            ground.material = materialGround;

            //Draw on canvas
            textureContext.beginPath();
            textureContext.moveTo(75,40);
            textureContext.bezierCurveTo(75,37,70,25,50,25);
            textureContext.bezierCurveTo(20,25,20,62.5,20,62.5);
            textureContext.bezierCurveTo(20,80,40,102,75,120);
            textureContext.bezierCurveTo(110,102,130,80,130,62.5);
            textureContext.bezierCurveTo(130,62.5,130,25,100,25);
            textureContext.bezierCurveTo(85,25,75,37,75,40);
            textureContext.fillStyle = "red";
            textureContext.fill();
            textureGround.update();
            
            return scene;
         };
         var scene = createScene();
            engine.runRenderLoop(function() {
            scene.render();
         });
      </script>
   </body>
</html>

输出

动态纹理1

解释

我们创建了地面网格并为其添加了动态纹理.

//地面网格
 var ground = BABYLON.MeshBuilder.CreateGround("ground1",{width:20,height:10,subdivisions:25},scene); 
//创建动态纹理
 var textureGround = new BABYLON.DynamicTexture("dynamic texture",512,scene); 
//使用标准材料将动态纹理添加到地面
 var materialGround = new BABYLON.StandardMaterial("Mat",scene); 
 materialGround.diffuseTexture = textureGround; 
 ground.material = materialGround;

要在动态纹理上使用画布,我们需要先调用canvas方法和减号;

var textureContext = textureGround.getContext()

在画布上,我们将添加bezierCurve如下 :

 textureContext.beginPath(); 
 textureContext.moveTo(75,40); 
 textureContext.bezierCurveTo(75,37,70,25,50,25); 
 textureContext.bezierCurveTo(20,25,20,62.5,20,62.5); 
 textureContext.bezierCurveTo(20,80,40,102,75,120); 
 textureContext.bezierCurveTo(110,102,130,80,130,62.5); 
 textureContext.bezierCurveTo(130,62.5,130,25,100,25); 
 textureContext.bezierCurveTo(85,25,75,37,75,40); 
 textureContext.fillStyle ="red"; 
 textureContext.fill(); 
 textureGround.update();