
- BabylonJS 教程
- BabylonJS - 首頁
- BabylonJS - 簡介
- BabylonJS - 環境設定
- BabylonJS - 概述
- BabylonJS - 基本元素
- BabylonJS - 材質
- BabylonJS - 動畫
- BabylonJS - 相機
- BabylonJS - 光照
- BabylonJS - 引數化形狀
- BabylonJS - 網格
- 向量位置和旋轉
- BabylonJS - 貼花
- BabylonJS - Curve3
- BabylonJS - 動態紋理
- BabylonJS -視差貼圖
- BabylonJS - 鏡頭光暈
- BabylonJS - 建立螢幕截圖
- BabylonJS - 反射探針
- 標準渲染管線
- BabylonJS - 著色器材質
- BabylonJS - 骨骼和骨架
- BabylonJS - 物理引擎
- BabylonJS - 播放聲音和音樂
- BabylonJS 有用資源
- BabylonJS - 快速指南
- BabylonJS - 有用資源
- BabylonJS - 討論
BabylonJS - 著色器材質
著色器材質將材質作為輸出。您可以將此材質應用於任何網格。它基本上將資料從您的場景傳遞到頂點和片段著色器。
要獲取著色器材質,呼叫以下類:
var myShaderMaterial = new BABYLON.ShaderMaterial(name, scene, route, options);
引數
考慮以下與著色器材質相關的引數:
名稱 - 一個字串,命名著色器。
場景 - 使用著色器的場景。
路徑 - 透過以下三種方式之一指向著色器程式碼的路徑:
object - { vertex: "custom", fragment: "custom" }, used with BABYLON.Effect.ShadersStore["customVertexShader"] and BABYLON.Effect.ShadersStore["customFragmentShader"]
object - { vertexElement: "vertexShaderCode", fragmentElement: "fragmentShaderCode" }, used with shader code in <script> tags
string - "./COMMON_NAME",
最後提到的語法與index.html資料夾中的外部檔案COMMON_NAME.vertex.fx和COMMON_NAME.fragment.fx一起使用。
選項 - 包含屬性和均勻陣列的物件,包含它們的名稱作為字串。
帶有值的著色器語法如下所示:
var shaderMaterial = new BABYLON.ShaderMaterial("shader", scene, { vertex: "custom", fragment: "custom", }, { attributes: ["position", "normal", "uv"], uniforms: ["world", "worldView", "worldViewProjection", "view", "projection"] });
屬性必須以陣列形式存在。這些包含位置、法線和uv,它們是vector3 3D浮點數向量。
vec2 - 一個二維浮點數向量。
vec3 - 一個三維浮點數向量。
mat4 - 一個具有4列和4行浮點數的矩陣。
gl_Position - 它提供螢幕座標的位置資料。
gl_FragColor - 它提供用於表示螢幕上面的顏色資料。
以上是GLSL語言中的內建變數。
由於頂點位置需要儘可能精確,因此所有浮點數都應設定為具有高精度。這在每個著色器的程式碼開頭使用– precision highp float 完成。precision highp float 確定浮點數使用的精度。
以下演示基於第一種物件方法。
演示
<!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"> //downloaded HDR files from :http://www.hdrlabs.com/sibl/archive.html 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 / 4, Math.PI / 4, 4, BABYLON.Vector3.Zero(), scene); camera.attachControl(canvas, true); var light = new BABYLON.HemisphericLight("light1", new BABYLON.Vector3(0, 1, 0), scene); BABYLON.Effect.ShadersStore["customVertexShader"] = "\r\n" + "precision highp float;\r\n" + "// Attributes\r\n" + "attribute vec3 position;\r\n" + "attribute vec2 uv;\r\n" + "// Uniforms\r\n" + "uniform mat4 worldViewProjection;\r\n" + "// Varying\r\n" + "varying vec2 vUV;\r\n" + "void main(void) { \r\n" + "gl_Position = worldViewProjection * vec4(position, 1.0);\r\n" + "vUV = uv;\r\n"+" } \r\n"; BABYLON.Effect.ShadersStore["customFragmentShader"] = "\r\n"+ "precision highp float;\r\n" + "varying vec2 vUV;\r\n" + "uniform sampler2D textureSampler;\r\n" + "void main(void) { \r\n"+ "gl_FragColor = texture2D(textureSampler, vUV);\r\n"+" } \r\n"; var shaderMaterial = new BABYLON.ShaderMaterial("shader", scene, { vertex: "custom", fragment: "custom", }, { attributes: ["position", "normal", "uv"], uniforms: ["world", "worldView", "worldViewProjection", "view", "projection"] }); var mainTexture = new BABYLON.Texture("images/mat.jpg", scene); shaderMaterial.setTexture("textureSampler", mainTexture); shaderMaterial.backFaceCulling = false; var box = BABYLON.MeshBuilder.CreateBox("box", {}, scene); box.material = shaderMaterial; return scene; }; var scene = createScene(); engine.runRenderLoop(function() { scene.render(); }); </script> </body> </html>
輸出
以上程式碼行將生成以下輸出:

在這個演示中,我們使用了影像mat.jpg。這些影像是儲存在本地images/資料夾中,並以下面的方式作為參考貼上。您可以下載任何您選擇的影像並將其用於演示連結中。
Images/mat.jpg

廣告