你的位置:首页 > 信息动态 > 新闻中心
信息动态
联系我们

2021SC@SDUSC 【软件工程应用与实践】Claygl项目 代码分析(七)

2021/12/28 22:16:43

2021SC@SDUSC

目录

  • 一、Scene类简介
  • 二、代码分析

一、Scene类简介

继续对Scene类进行分析。

二、代码分析

1、decomposeLocalTransform()

decomposeLocalTransform: function (keepScale) {
        var scale = !keepScale ? this.scale: null;
        this.localTransform.decomposeMatrix(scale, this.rotation, this.position);
    },

分析:将局部变换分解为 SRT

2、decomposeWorldTransform()

decomposeWorldTransform: (function () {
        var tmp = mat4.create();
        return function (keepScale) {
            var localTransform = this.localTransform;
            var worldTransform = this.worldTransform;
            // Assume world transform is updated
            if (this._parent) {
                mat4.invert(tmp, this._parent.worldTransform.array);
                mat4.multiply(localTransform.array, tmp, worldTransform.array);
            } else {
                mat4.copy(localTransform.array, worldTransform.array);
            }
            var scale = !keepScale ? this.scale: null;
            localTransform.decomposeMatrix(scale, this.rotation, this.position);
        };
    })(),
    transformNeedsUpdate: function () {
        return this.position._dirty
            || this.rotation._dirty
            || this.scale._dirty;
    },

分析:分解世界变换为SRT

3、dispose()

dispose: function () {
        this.material = null;
        this._opaqueList = [];
        this._transparentList = [];
        this.lights = [];
        this._lightUniforms = {};
        this._lightNumber = {};
        this._nodeRepository = {};
    }

分析:清除所有的场景对象但是gl的资源比如texture, shader不会被清除。 大多数情况下,应该使用 Renderer 中的 disposeScene 方法进行处理。

4、eachChild(callback, context)

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

参数callback的类型为function ,参数context的类型为Node。
分析:遍历所有子节点。不要在迭代期间在回调中执行添加、删除操作。

5、getChildByName(name)

getChildByName: function (name) {
        var children = this._children;
        for (var i = 0; i < children.length; i++) {
            if (children[i].name === name) {
                return children[i];
            }
        }
    },

分析:遍历孩子节点,获取给定名字的第一个孩子

6、getDescendantByName(name)

 getDescendantByName: function (name) {
        var children = this._children;
        for (var i = 0; i < children.length; i++) {
            var child = children[i];
            if (child.name === name) {
                return child;
            } else {
                var res = child.getDescendantByName(name);
                if (res) {
                    return res;
                }
            }
        }
    },

分析:获取第一个具有给定名称的后代

7、getMainCamera()

 getMainCamera: function () {
        return this._cameraList[0];
    },

分析:获取场景的主相机。

8、getPath(rootNode)

getPath: function (rootNode) {
        if (!this._parent) {
            return '/';
        }
        var current = this._parent;
        var path = this.name;
        while (current._parent) {
            path = current.name + '/' + path;
            if (current._parent == rootNode) {
                break;
            }
            current = current._parent;
        }
        if (!current._parent && rootNode) {
            return null;
        }
        return path;
    },

参数rootNode的类型为 clay.Node
分析:获取查询路径,相对于 rootNode(默认为场景)