babylonjs: 设置材料颜色 及 自发光,环境光,散射光,镜面光

’ #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);

改变颜色

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值