’ #ffffff '十六进制转 BABYLON.Color3
BABYLON.Color3颜色使用Web上的十六进制颜色,我们在BABYLON中一般都用BABYLON.Color3表示颜色,它有三个参数示例如下:
BABYLON.Color3(1,0,1); 表示rgb(255, 0,255),其中它的参数表示百分比。在Web中我们也常使用 #FF00FF来表示它。下面代码示例是使用十六进制的#FFFFFF这样的字符串。
function HexToRGB(hexColor){
if (typeof (hexColor) == 'undefined' || hexColor.length < 6) {
return new BABYLON.Color3(1, 0, 1);
}
let hex = (hexColor.charAt(0) == "#") ? hexColor.substring(1, 7) : hexColor;
var r = parseInt(hex.substring(0, 2), 16) / 255;
var g = parseInt(hex.substring(2, 4), 16) / 255;
var b = parseInt(hex.substring(4, 6), 16) / 255;
return new BABYLON.Color3(r, g, b);
}
var color = HexToRGB('#ff00ff');//例
’ rgb(0,0,0) ’ 转 BABYLON.Color3
function RGBnum(color){
let numSrt = (color.substring(4, color.length - 1)).split(","); // 颜色转换
let newColor = new BABYLON.Color3(
Number.parseInt(numSrt[0]) / 255 -0.2,
Number.parseInt(numSrt[1]) / 255 -0.2,
Number.parseInt(numSrt[2]) / 255 -0.2
);
}
设置颜色
this.models.material.albedoColor = new BABYLON.Color3(newColor.r,newColor.g,newColor.b);
四种光
1:自发光
2:环境光
3:散射光
4:镜面光
光的优先级的表现
自发光覆盖其它任何选项。
环境光覆盖除自发光以外的任何选项。
散射光、镜面光可以同时存在。
其实所有光都是叠加表现的,这里说的“覆盖”指的都是白光的情况。
光的设置
自发光只需设置材料的自发光选项。
环境光需要开启场景的环境光选项,同时设置材料的环境光选项,缺一不可。
散射光需要设置光源,同时设置材料的散射选项,二者共同决定物体颜色。
镜面光需要设置光源,同时设置材料的镜面选项,二者共同决定镜面颜色。
设置相关选项的代码
//自发光
this.models.material.emissiveColor = newColor
//环境光
scene.ambientColor = new BABYLON.Color3(1, 1, 1);
material.ambientColor = new BABYLON.Color3(1, 1, 1);
//散射光
light.diffuse = new BABYLON.Color3(1, 1, 1);
material.diffuseColor = new BABYLON.Color3(1, 1, 1);
//镜面光
light.specular = new BABYLON.Color3(1, 1, 1);
material.specularColor = new BABYLON.Color3(0, 0, 0);