Babylonjs-parallax-mapping

提供:Dev Guides
移動先:案内検索

BabylonJS-視差マッピング

視差マッピングは、オフセットマッピングとも呼ばれます。 ジオメトリの表面のレリーフの効果を強調するために、マテリアルのテクスチャにオフセットとして適用される高さマップを使用します。 3Dワールドでは、深さを適用した石の壁はより見た目がよく、エンドユーザーにはリアルに見えます。 急角度のビュー角度では、テクスチャ座標がより変位し、ビューの変化に伴う視差効果により奥行きの錯覚を与えます。

Parallexマッピングは標準マテリアルで使用されます。 これについては、標準の素材の章で学びました。

パラレックスマッピングには3つのプロパティがあります。

  • material.useParallax = true; -これは、視差マッピングを有効にします。 このプロパティを使用するには、最初にマテリアルにバンプテクスチャを割り当てる必要があります。
  • material.useParallaxOcclusion = true; -このプロパティを使用するには、useParallaxをtrueに設定する必要があります。 パララックスオクルージョンを有効にします。
  • material.parallaxScaleBias = 0.1; -深さのスケーリング係数をメッシュに適用します。Parallaxの場合、.05〜.1の値が適切です。 オクルージョンの場合、0.2に到達できます。

Demo

<!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.jpg および a3.png を使用しました。 画像はimages/フォルダーにローカルに保存され、参照用に以下に貼り付けられます。 選択した任意の画像をダウンロードして、デモリンクで使用できます。

Images/a1.png

A1 Wall

Images/a2.png

A2 Wall

Images/pebble.jpg

A1 Wall

images/a3.png

A3 Wall