BabylonJS -视差贴图



视差贴图也称为偏移贴图。它使用高度图,该高度图作为材质纹理的偏移量应用,以增强几何体表面浮雕效果。在 3D 世界中,带有深度应用的石墙将具有更明显的视觉效果,并且对最终用户来说看起来更逼真。在较陡的视角下,纹理坐标会发生更大的位移,由于视差效果,随着视角的变化,会产生深度的错觉。

视差贴图与标准材质一起使用。我们在标准材质章节学习了这一点。

视差贴图有三个属性。

  • material.useParallax = true; − 这将启用视差贴图。要使用此属性,您需要先将凹凸纹理分配给材质。

  • material.useParallaxOcclusion = true; − 要使用此属性,您必须将 useParallax 设置为 true。它启用视差遮挡。

  • material.parallaxScaleBias = 0.1; − 为要分配给网格的深度应用缩放因子。对于视差,0.05 到 0.1 之间的值很好。对于遮挡,您可以达到 0.2。

演示

<!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() {
            // This creates a basic Babylon Scene object (non-mesh)
            var scene = new BABYLON.Scene(engine);

            // This creates and positions a free camera (non-mesh)
            var camera = new BABYLON.ArcRotateCamera("camera1", 0, Math.PI / 2, 100, new BABYLON.Vector3(0, 0, 0), scene);
            camera.attachControl(canvas, false);

            // This targets the camera to scene origin
            camera.setTarget(BABYLON.Vector3.Zero());

            // 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;

            var mesh = BABYLON.Mesh.CreateBox("box01", 25, scene);
            mesh.position = new BABYLON.Vector3(0, 0, 0);

            var brickWallDiffURL = "images/a1.png";
            var brickWallNHURL = "images/a2.png";
            var stoneDiffURL = "images/pebble.jpg";
            var stoneNHURL = "images/a3.png";

            var stoneDiffuseTexture = new BABYLON.Texture(stoneDiffURL, scene);
            
            var stoneNormalsHeightTexture = new BABYLON.Texture(stoneNHURL, scene);
            
            var wallDiffuseTexture = new BABYLON.Texture(brickWallDiffURL, scene);
            
            var wallNormalsHeightTexture = new BABYLON.Texture(brickWallNHURL, scene);
            
            var normalsHeightTexture = stoneNormalsHeightTexture;

            var material = new BABYLON.StandardMaterial("mtl01", scene);
            material.diffuseTexture = stoneDiffuseTexture;
            material.bumpTexture = stoneNormalsHeightTexture;
            
            material.useParallax = true;
            material.useParallaxOcclusion = true;
            material.parallaxScaleBias = 0.1;
            material.specularPower = 1000.0;
            material.specularColor = new BABYLON.Color3(0.5, 0.5, 0.5);
            mesh.material = material;	
            return scene;		
         };
         var scene = createScene();
         engine.runRenderLoop(function() {
            scene.render();
         });
      </script>
   </body>
</html>

输出

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

Parallex Mapping

在此演示中,我们使用了图像a1.png、a2.png、pebble.jpga3.png。这些图像存储在本地 images/ 文件夹中,并在下面粘贴以供参考。您可以下载任何您选择的图像并在演示链接中使用。

Images/a1.png

A1 Wall

Images/a2.png

A2 Wall

Images/pebble.jpg

A1 Wall

images/a3.png

A3 Wall
广告