如上图所示,使用的Laya版本(2.6.0)
新建SkyBox文件夹下,添加SkyBox.lmat,SkyBox.ltc,以及六张方位图(注意:所有文件在同一目录下,且资源分辨率一样)
SkyBox.lmat
{
"version":"LAYAMATERIAL:02",
"props":{
"type":"Laya.SkyBoxMaterial",
"exposure":1,
"rotation":0,
"vectors":[
{
"name":"tintColor",
"value":[
0.5,
0.5,
0.5,
0.5
]
}
],
"textures":[
{
"name":"textureCube",
"path":"SkyBox.ltc"
}
]
}
}
SkyBox.ltc
{
"front": "Sunny.front.jpg",
"back": "Sunny.back.jpg",
"left": "Sunny.left.jpg",
"right": "Sunny.right.jpg",
"up": "Sunny.up.jpg",
"down": "Sunny.down.jpg",
"size":512
}
一开始按照官方添加:报错一直是黑的
就报出错误: Uncaught TypeError: Cannot read property 'charAt' of undefined at Function.formatRelativePath (laya.d3.js:30779) at Loader._onTextureCubeLtcLoaded (laya.d3.js:31073) at EventHandler.runWith (laya.core.js:999) at Loader.event (laya.core.js:1038) at Loader.complete (laya.core.js:18675) at Loader.onLoaded (laya.core.js:18631) at EventHandler.runWith (laya.core.js:997) at HttpRequest.event (laya.core.js:1038) at HttpRequest.complete (laya.core.js:18283) at HttpRequest._onLoad (laya.core.js:18254)
调试发现:源码: var urls = [Laya3D.formatRelativePath(ltcBasePath, ltcData.front),
Laya3D.formatRelativePath(ltcBasePath, ltcData.back),
Laya3D.formatRelativePath(ltcBasePath, ltcData.left),
Laya3D.formatRelativePath(ltcBasePath, ltcData.right),
Laya3D.formatRelativePath(ltcBasePath, ltcData.up),
Laya3D.formatRelativePath(ltcBasePath, ltcData.down)];
给出的名字并不是“px”,“nx”之类的,修改成“front”,“back”这些就解决了,
添加天空盒:
//天空盒
Laya.BaseMaterial.load("SkyBox/SkyBox.lmat", Laya.Handler.create(this, this.onLoadSkyBox));
onLoadSkyBox(mat: Laya.SkyBoxMaterial)
{
//如果没有在.lmat里设置着色,可在此设置,否则场景会黑漆漆的
//mat.tintColor = new Laya.Vector4(0.5, 0.5, 0.5, 0.5)
//获取场景的天空渲染器
var skyRenderer: Laya.SkyRenderer = this.actionScene.skyRenderer;
//附上天空盒
skyRenderer.mesh = Laya.SkyBox.instance;
//设置材质
skyRenderer.material = mat;
// //模拟动态
// Laya.timer.frameLoop(1, this, () => {
// //曝光度
// this.scene.skyRenderer.material.exposure = Math.sin(exposureNumber += 0.01) + 1;
// //材质旋转
// this.scene.skyRenderer.material.rotation += 0.1;
// });
}
效果: