Babylon.js环境、材质与动画:打造交互式3D体验

使用HDR环境

创建环境贴图

将六个面(上、下、前、后、左、右)的图像组合成一个立方体贴图。

六个面的贴图命名为(_px,_py,_pz,_nx,_ny,_nz)

var skybox = BABYLON.Mesh.CreateBox("skyBox", 100.0, scene);  
var skyboxMaterial = new BABYLON.StandardMaterial("skyBox", scene);  
skyboxMaterial.backFaceCulling = false;  
skyboxMaterial.disableLighting = true;  
skybox.material = skyboxMaterial;  
skybox.infiniteDistance = true;  
skyboxMaterial.disableLighting = true;  
skyboxMaterial.reflectionTexture = new BABYLON.CubeTexture("文件路径", scene);  
skyboxMaterial.reflectionTexture.coordinatesMode = BABYLON.Texture.SKYBOX_MODE;  
skybox.renderingGroupId = 0;  

 加载环境贴图

加载.env贴图

scene.environmentTexture = new BABYLON.CubeTexture("textures/country.env", scene);  
scene.createDefaultSkybox(scene.environmentTexture, false, 1000, 0, false); 

 

 加载.hdr贴图 

scene.environmentTexture = new BABYLON.HDRCubeTexture("/textures/industrial.hdr", scene, 512);    
scene.createDefaultSkybox(scene.environmentTexture, false, 1000, 0, false)  

材质

使用.hdr材质 

var hdrTexture = new BABYLON.HDRCubeTexture("textures/country.hdr", scene, 512);   
// Our built-in 'sphere' shape. Params: name, subdivs, size, scene  
var sphere = BABYLON.Mesh.CreateSphere("sphere1", 32, 4, scene);  
var sphereMtl = new BABYLON.PBRMaterial("sphereMtl", scene);  
sphereMtl.reflectionTexture = hdrTexture;  
sphere.material= sphereMtl;  

颜色材质

var sphere = BABYLON.MeshBuilder.CreateSphere("sphere", {diameter: 2, segments: 32}, scene);  
material = new BABYLON.StandardMaterial("bab5", scene);  
material.emissiveColor = new BABYLON.Color3(0, 0.4, 0);    
sphere.material = material;  

 

玻璃材质

// 创建球体  
var sphere = BABYLON.MeshBuilder.CreateSphere("sphere", { diameter: 2 }, scene);  

// 创建PBR材质并设置属性  
var material = new BABYLON.PBRMaterial("material", scene);  
material.reflectionTexture = new BABYLON.CubeTexture("textures/environment.dds", scene); // 设置环境贴图  
material.refractionTexture = new BABYLON.CubeTexture("textures/environment.dds", scene); // 设置折射贴图  
material.indexOfRefraction = 0.3; // 设置折射率  
material.alpha = 0.4; // 设置透明度  
material.albedoColor = new BABYLON.Color3(0, 0, 1); // 设置蓝色  
material.metallic = 0; // 设置金属度为0,使其呈现非金属外观  
material.roughness = 0.2; // 设置粗糙度  

// 将材质应用于球体  
sphere.material = material; 

 

 路径动画

// 创建路径  
var path = [  
    new BABYLON.Vector3(0, 0, 0),  
    new BABYLON.Vector3(3, 0, 3),  
    new BABYLON.Vector3(-3, 0, 6),  
    new BABYLON.Vector3(0, 0, 9)  
];  
  
// 创建路径动画  
var animationPath = new BABYLON.Path3D(path);  
var animation = new BABYLON.Animation("pathAnimation", "position", 30,  BABYLON.Animation.ANIMATIONTYPE_VECTOR3, BABYLON.Animation.ANIMATIONLOOPMODE_CYCLE);  
  
// 设置路径动画的关键帧  
var keys = [];  
for (var i = 0; i < path.length; i++) {  
    keys.push({  
        frame: i * 30,  
        value: path[i]  
    });  
}  
animation.setKeys(keys);  
// 创建动画目标对象  
var sphere = BABYLON.MeshBuilder.CreateSphere("sphere", { size: 1 }, scene);    
material = new BABYLON.StandardMaterial("bab5", scene);    
material.emissiveColor = new BABYLON.Color3(0, 0.4, 0);      
sphere.material = material;   
  
// 设置起始位置  
sphere.position = path[0];   
  
// 应用动画  
sphere.animations.push(animation);  
scene.beginAnimation(sphere, 0, path.length * 30, true); 

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值