08 - three.js 笔记 场景对象 Scene 常用方法和属性

Scene 对象是 three.js 的核心对象它继承自 Object3D 对象。
在场景中我们可以放置 对象 灯光。

构造器 : Scene()
父对象 : Object3D
源码版本 : r93

Scene对象的源码:

function Scene() {
    // 继承Object3D对象
    Object3D.call( this );
    // 类型
    this.type = 'Scene';
    // 场景背景
    this.background = null;
    // 雾化效果
    this.fog = null;
    // 全局材质
    this.overrideMaterial = null;
    // 渲染器是否检查每一帧的矩矩阵更新,默认检查
    this.autoUpdate = true; // checked by the renderer

}
Scene.prototype = Object.assign( Object.create( Object3D.prototype ), {

    constructor: Scene,
    // 把给定的对象复制到当前对象中
    copy: function ( source, recursive ) {

        Object3D.prototype.copy.call( this, source, recursive );

        if ( source.background !== null ) this.background = source.background.clone();
        if ( source.fog !== null ) this.fog = source.fog.clone();
        if ( source.overrideMaterial !== null ) this.overrideMaterial = source.overrideMaterial.clone();

        this.autoUpdate = source.autoUpdate;
        this.matrixAutoUpdate = source.matrixAutoUpdate;

        return this;

    },
    // 将对象以JSON格式返回
    toJSON: function ( meta ) {

        var data = Object3D.prototype.toJSON.call( this, meta );

        if ( this.background !== null ) data.object.background = this.background.toJSON( meta );
        if ( this.fog !== null ) data.object.fog = this.fog.toJSON();

        return data;

    }

} );

通过源码可以得知Scene对象的属性和方法.

属性
属性 描述 默认值
type 类型 ‘Scene’
background 用来设置场景渲染时的背景,会率先渲染,可以赋值为Color对象、Texture 画布纹理对象、或者是一个立方体纹理对象 CubeTexture null
fog 在场景中加入物化效果 null
overrideMaterial 覆盖场景中的所有Mesh对象的材质属性 null
autoUpdate 渲染器是否检查每一帧的矩矩阵更新 true
常用方法
方法 描述 作用
copy 继承自Object3D对象copy ( object : Object3D, recursive : Boolean )recursive为true,则object对象的子对象也将会被复制 把给定对象复制到当前对象中
toJSON toJSON: function ( meta )继承Object3D 以json格式返回场景数据
children children : Object3D 返回一个场景中所有对象的列表,包括相机和光源
getObjectByName getObjectByName ( name : String ) : Object3D 创建对象的时候,可以通过name属性为对象指定一个名字,然后可以通过这个方法,根据 name 来查找该对象,并这个对象,Object3D的方法中还可以根据 id 进行查找
add add ( object : Object3D, ... ) : null 向场景中添加对象,返回null
remove remove ( object : Object3D, ... ) : null 删除场景中的对象
traverse traverse ( callback : Function ) : null children 属性返回场景中所有子对象的列表,通过traverse函数,可以在当前对象和它的子对象列表上执行回调

Scene对象的父对象是Object3D对象,若想知道更多的方法和属性,请查看官方文档中的Object3D对象介绍。

猜你喜欢

转载自blog.csdn.net/ithanmang/article/details/80800635