2021SC@SDUSC ClayGL Skybox类分析(十二)
/**
* @constructor clay.plugin.Skybox
*
* @example
* var skyTex = new clay.TextureCube();
* skyTex.load({
* 'px': 'assets/textures/sky/px.jpg',
* 'nx': 'assets/textures/sky/nx.jpg'
* 'py': 'assets/textures/sky/py.jpg'
* 'ny': 'assets/textures/sky/ny.jpg'
* 'pz': 'assets/textures/sky/pz.jpg'
* 'nz': 'assets/textures/sky/nz.jpg'
* });
* var skybox = new clay.plugin.Skybox({
* scene: scene
* });
* skybox.material.set('environmentMap', skyTex);
*/
var Skybox = Mesh.extend(function () {
var skyboxShader = new Shader({
vertex: Shader.source('clay.skybox.vertex'),
fragment: Shader.source('clay.skybox.fragment')
});
var material = new Material({
shader: skyboxShader,
depthMask: false
});
return {
/**
* @type {clay.Scene}
* @memberOf clay.plugin.Skybox.prototype
*/
scene: null,
geometry: new CubeGeometry(),
material: material,
environmentMap: null,
culling: false,
_dummyCamera: new PerspectiveCamera()
};
}, function () {
var scene = this.scene;
if (scene) {
this.attachScene(scene);
}
if (this.environmentMap) {
this.setEnvironmentMap(this.environmentMap);
}
},
Example
var skyTex = new clay.TextureCube();
skyTex.load({
‘px’: ‘assets/textures/sky/px.jpg’,
‘nx’: ‘assets/textures/sky/nx.jpg’
‘py’: ‘assets/textures/sky/py.jpg’
‘ny’: ‘assets/textures/sky/ny.jpg’
‘pz’: ‘assets/textures/sky/pz.jpg’
‘nz’: ‘assets/textures/sky/nz.jpg’
});
var skybox = new clay.plugin.Skybox({
scene: scene
});
skybox.material.set(‘environmentMap’, skyTex);
Members
天空盒实例对象,初始值为NULL.
类型:clay.Scene
scene: null,
将天空盒附加到场景
参数:
名称 | 类型 |
---|---|
scene | clay.Scene |
{
attachScene: function (scene) {
if (this.scene) {
this.detachScene();
}
scene.skybox = this;
this.scene = scene;
scene.on('beforerender', this._beforeRenderScene, this);
},
从场景中分离出(天空盒)
detachScene: function () {
if (this.scene) {
this.scene.off('beforerender', this._beforeRenderScene);
this.scene.skybox = null;
}
this.scene = null;
},
处理天空盒
参数:
名称 | 类型 |
---|---|
renderer | clay.Renderer |
dispose: function (renderer) {
this.detachScene();
this.geometry.dispose(renderer);
},
获取环境贴图
返回值:类型 clay.TextureCube
/**
* @return {clay.TextureCube}
*/
getEnvironmentMap: function () {
return this.material.get('environmentMap');
},
设置环境贴图
参数:
名称 | 类型 |
---|---|
envMap | clay.TextureCube |
setEnvironmentMap: function (envMap) {
if (envMap.textureType === 'texture2D') {
this.material.define('EQUIRECTANGULAR');
// LINEAR filter can remove the artifacts in pole
envMap.minFilter = Texture.LINEAR;
}
else {
this.material.undefine('EQUIRECTANGULAR');
}
this.material.set('environmentMap', envMap);
},