2021SC@SDUSC ClayGL Camera类分析(八)

2021SC@SDUSC ClayGL Camera类分析(八)
接上文

setLocalTransform(matrix)

设置本地变换并分解为 SRT

/**
     * Set the local transform and decompose to SRT
     * @param {clay.Matrix4} matrix
     */
    setLocalTransform: function (matrix) {
        mat4.copy(this.localTransform.array, matrix.array);
        this.decomposeLocalTransform();
    },

参数:

名称类型
matrixclay.Matrix4

setName(name)

设置场景节点的名称

    setName: function (name) {
        var scene = this._scene;
        if (scene) {
            var nodeRepository = scene._nodeRepository;
            delete nodeRepository[this.name];
            nodeRepository[name] = this;
        }
        this.name = name;
    },

参数:

名称类型
namestring

setProjectionMatrix(projectionMatrix)

设置相机投影矩阵

/**
     * Set camera projection matrix
     * @param {clay.Matrix4} projectionMatrix
     */
    setProjectionMatrix: function (projectionMatrix) {
        Matrix4.copy(this.projectionMatrix, projectionMatrix);
        Matrix4.invert(this.invProjectionMatrix, projectionMatrix);
        this.decomposeProjectionMatrix();
    },

参数:

名称类型
projectionMatrixclay.Matrix4

setViewMatrix()

设置相机视图矩阵

    setViewMatrix: function (viewMatrix) {
        Matrix4.copy(this.viewMatrix, viewMatrix);
        Matrix4.invert(this.worldTransform, viewMatrix);
        this.decomposeWorldTransform();
    },

setWorldTransform(matrix)

设置世界变换并分解为 SRT


    setWorldTransform: function (matrix) {
        mat4.copy(this.worldTransform.array, matrix.array);
        this.decomposeWorldTransform();
    },

参数:

名称类型
matrixclay.Matrix4

success(action, contextopt)

once函数的别名(‘success’)

/**
     * Alias of on('success')
     * @param  {Function} action
     * @param  {Object} [context]
     * @chainable
     */
    success: function(action, context) {
        return this.once('success', action, context);
    },

参数:

名称类型属性
actionfunction
contextObject可选

traverse(callback, contextopt)

深度首先遍历其所有后代场景节点。

在遍历期间,不要在回调中添加、删除操作。

    traverse: function (callback, context) {
        callback.call(context, this);
        var _children = this._children;
        for(var i = 0, len = _children.length; i < len; i++) {
            _children[i].traverse(callback, context);
        }
    },

参数:

名称类型属性
callbackfunction
contextNode可选

trigger(name)

触发事件

    trigger: function(name) {
        if (!this.hasOwnProperty('__handlers__')) {
            return;
        }
        if (!this.__handlers__.hasOwnProperty(name)) {
            return;
        }
        var hdls = this.__handlers__[name];
        var l = hdls.length, i = -1, args = arguments;
        // Optimize advise from backbone
        switch (args.length) {
            case 1:
                while (++i < l) {
                    hdls[i].action.call(hdls[i].context);
                }
                return;
            case 2:
                while (++i < l) {
                    hdls[i].action.call(hdls[i].context, args[1]);
                }
                return;
            case 3:
                while (++i < l) {
                    hdls[i].action.call(hdls[i].context, args[1], args[2]);
                }
                return;
            case 4:
                while (++i < l) {
                    hdls[i].action.call(hdls[i].context, args[1], args[2], args[3]);
                }
                return;
            case 5:
                while (++i < l) {
                    hdls[i].action.call(hdls[i].context, args[1], args[2], args[3], args[4]);
                }
                return;
            default:
                while (++i < l) {
                    hdls[i].action.apply(hdls[i].context, Array.prototype.slice.call(args, 1));
                }
                return;
        }
    },

参数:

名称类型
namestring

update(forceUpdateWorld)

以递归方式更新本地变换和世界变换


    update: function (forceUpdateWorld) {
        if (this.autoUpdateLocalTransform) {
            this.updateLocalTransform();
        }
        else {
            // Transform is manually setted
            forceUpdateWorld = true;
        }
        if (forceUpdateWorld || this._needsUpdateWorldTransform) {
            this._updateWorldTransformTopDown();
            forceUpdateWorld = true;
            this._needsUpdateWorldTransform = false;
        }
        var children = this._children;
        for(var i = 0, len = children.length; i < len; i++) {
            children[i].update(forceUpdateWorld);
        }
    },

参数:

名称类型
forceUpdateWorldboolean

updateLocalTransform()

从 SRT 更新本地变换 请注意,如果位置、旋转、比例_dirty标记全部为 false,则不会更新本地变换

    updateLocalTransform: function () {
        var position = this.position;
        var rotation = this.rotation;
        var scale = this.scale;
        if (this.transformNeedsUpdate()) {
            var m = this.localTransform.array;
            // Transform order, scale->rotation->position
            mat4.fromRotationTranslation(m, rotation.array, position.array);
            mat4.scale(m, m, scale.array);
            rotation._dirty = false;
            scale._dirty = false;
            position._dirty = false;
            this._needsUpdateWorldTransform = true;
        }
    },

updateProjectionMatrix()

更新投影矩阵,在更新后调用

    updateProjectionMatrix: function () {},

updateWorldTransform()

在整个场景更新之前更新世界变换。


    updateWorldTransform: function () {
        // Find the root node which transform needs update;
        var rootNodeIsDirty = this;
        while (rootNodeIsDirty && rootNodeIsDirty.getParent()
            && rootNodeIsDirty.getParent().transformNeedsUpdate()
        ) {
            rootNodeIsDirty = rootNodeIsDirty.getParent();
        }
        rootNodeIsDirty.update();
    },
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值