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

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

南宫松
2023-12-01

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

1.off(action, contextopt)

删除事件侦听器
参数:

名称类型属性
actionfunction
contextObject可选
    off: function(name, action) {
        var handlers = this.__handlers__ || (this.__handlers__={});
        if (!action) {
            handlers[name] = [];
            return;
        }
        if (handlers[name]) {
            var hdls = handlers[name];
            var retains = [];
            for (var i = 0; i < hdls.length; i++) {
                if (action && hdls[i].action !== action) {
                    retains.push(hdls[i]);
                }
            }
            handlers[name] = retains;
        }
        return this;
    },

2.on(name, action, contextopt)

注册事件处理程序
参数:

名称类型属性
actionfunction
namestring
contextObject可选
    on: function(name, action, context) {
        if (!name || !action) {
            return;
        }
        var handlers = this.__handlers__ || (this.__handlers__={});
        if (!handlers[name]) {
            handlers[name] = [];
        }
        else {
            if (this.has(name, action)) {
                return;
            }
        }
        var handler = new Handler(action, context || this);
        handlers[name].push(handler);
        return this;
    },

3.once(name, action, contextopt)

注册事件,事件只会触发一次,然后删除
参数:

名称类型属性
actionfunction
namestring
contextObject可选
    once: function(name, action, context) {
        if (!name || !action) {
            return;
        }
        var self = this;
        function wrapper() {
            self.off(name, wrapper);
            action.apply(this, arguments);
        }
        return this.on(name, wrapper, context);
    },

4.queryNode(path) → {clay.Node}

按路径查询后代节点

参数:

名称类型
pathstring

返回值:
类型 clay.Node
例如:
node.queryNode(‘root/parent/child’);


    queryNode: function (path) {
        if (!path) {
            return;
        }
        // TODO Name have slash ?
        var pathArr = path.split('/');
        var current = this;
        for (var i = 0; i < pathArr.length; i++) {
            var name = pathArr[i];
            // Skip empty
            if (!name) {
                continue;
            }
            var found = false;
            var children = current._children;
            for (var j = 0; j < children.length; j++) {
                var child = children[j];
                if (child.name === name) {
                    current = child;
                    found = true;
                    break;
                }
            }
            // Early return if not found
            if (!found) {
                return;
            }
        }
        return current;
    },

5.remove(node)

删除给定的子场景节点
参数:

名称类型
nodeclay.Node
    remove: function (node) {
        var children = this._children;
        var idx = children.indexOf(node);
        if (idx < 0) {
            return;
        }
        children.splice(idx, 1);
        node._parent = null;
        if (this._scene) {
            node.traverse(this._removeSelfFromScene, this);
        }
    },

6.removeAll()

删除所有孩子项

    removeAll: function () {
        var children = this._children;
        for (var idx = 0; idx < children.length; idx++) {
            children[idx]._parent = null;
            if (this._scene) {
                children[idx].traverse(this._removeSelfFromScene, this);
            }
        }
        this._children = [];
    },

7.rotateAround(point, axis, angle)

按角度度沿轴旋转节点,轴穿过点
参数:

名称类型说明
pointclay.Vector3Center point
axisclay.Vector3Center axis
anglenumberRotation angle
    rotateAround: (function () {
        var v = new Vector3();
        var RTMatrix = new Matrix4();
        // TODO improve performance
        return function (point, axis, angle) {
            v.copy(this.position).subtract(point);
            var localTransform = this.localTransform;
            localTransform.identity();
            // parent node
            localTransform.translate(point);
            localTransform.rotate(angle, axis);
            RTMatrix.fromRotationTranslation(this.rotation, v);
            localTransform.multiply(RTMatrix);
            localTransform.scale(this.scale);
            this.decomposeLocalTransform();
            this._needsUpdateWorldTransform = true;
        };
    })(),

 类似资料: