当前位置: 首页 > 工具软件 > ClayGL > 使用案例 >

2021SC@SDUSC ClayGL Skybox类分析(十二)

翟黎明
2023-12-01

2021SC@SDUSC ClayGL Skybox类分析(十二)

new 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

scene :clay.Scene

天空盒实例对象,初始值为NULL.
类型:clay.Scene

        scene: null,

Methods

attachScene(scene)

将天空盒附加到场景
参数:

名称类型
sceneclay.Scene
 {
    attachScene: function (scene) {
        if (this.scene) {
            this.detachScene();
        }
        scene.skybox = this;
        this.scene = scene;
        scene.on('beforerender', this._beforeRenderScene, this);
    },

detachScene()

从场景中分离出(天空盒)

    detachScene: function () {
        if (this.scene) {
            this.scene.off('beforerender', this._beforeRenderScene);
            this.scene.skybox = null;
        }
        this.scene = null;
    },

dispose(renderer)

处理天空盒
参数:

名称类型
rendererclay.Renderer
    dispose: function (renderer) {
        this.detachScene();
        this.geometry.dispose(renderer);
    },

getEnvironmentMap() → {clay.TextureCube}

获取环境贴图
返回值:类型 clay.TextureCube

/**
     * @return {clay.TextureCube}
     */
    getEnvironmentMap: function () {
        return this.material.get('environmentMap');
    },

setEnvironmentMap(envMap)

设置环境贴图
参数:

名称类型
envMapclay.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);
    },
 类似资料: