2021SC@SDUSC ClayGL Camera类分析(七)
接上文
删除事件侦听器
参数:
名称 | 类型 | 属性 |
---|---|---|
action | function | |
context | Object | 可选 |
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;
},
注册事件处理程序
参数:
名称 | 类型 | 属性 |
---|---|---|
action | function | |
name | string | |
context | Object | 可选 |
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;
},
注册事件,事件只会触发一次,然后删除
参数:
名称 | 类型 | 属性 |
---|---|---|
action | function | |
name | string | |
context | Object | 可选 |
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);
},
按路径查询后代节点
参数:
名称 | 类型 |
---|---|
path | string |
返回值:
类型 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;
},
删除给定的子场景节点
参数:
名称 | 类型 |
---|---|
node | clay.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);
}
},
删除所有孩子项
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 = [];
},
按角度度沿轴旋转节点,轴穿过点
参数:
名称 | 类型 | 说明 |
---|---|---|
point | clay.Vector3 | Center point |
axis | clay.Vector3 | Center axis |
angle | number | Rotation 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;
};
})(),