three.js源码翻译-Light.js

three.js源码翻译-Light.js

说明

Light.js是three中所有光源的基类,即所有光源继承自该方法类,同时Light方法同样的继承自Object3D。在three中基础光源有四类:环境光、方向光、pointLight/spotLight、半球光(模拟室外光源)。目前拓展的光源有区域光(区域光目前只对pbr流程的材质有效果,同时需要相应的shader)。在这些光源中可以创建阴影的光源有方向光和pointLight/spotLight,其他的光源无法创建阴影。

源码位置及翻译

源码位置

src/light/Light.js

源码翻译

/**
 *	three 灯光的基类,该基类也是继承自object3d对象
 *	该对象接受两个参数分别为灯光的强度和灯光的颜色
 * @param {Color} color
 * @param {Number} intensity
 */
function Light( color, intensity ) {

	Object3D.call( this );
	//设置类型
	this.type = 'Light';

	this.color = new Color( color );
	this.intensity = intensity !== undefined ? intensity : 1;
	//接受阴影设置为undefined,灯光不接受阴影,只能产生阴影
	this.receiveShadow = undefined;

}
//灯光的方法
Light.prototype = Object.assign( Object.create( Object3D.prototype ), {

	constructor: Light,
	//是否为灯光类
	isLight: true,
	//复制方法,接受一个灯光对象,
	copy: function ( source ) {

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

		this.color.copy( source.color );
		this.intensity = source.intensity;

		return this;

	},
	//变成json的方法,接受的也是一个灯光对象
	toJSON: function ( meta ) {

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

		data.object.color = this.color.getHex();
		data.object.intensity = this.intensity;

		if ( this.groundColor !== undefined ) data.object.groundColor = this.groundColor.getHex();

		if ( this.distance !== undefined ) data.object.distance = this.distance;
		if ( this.angle !== undefined ) data.object.angle = this.angle;
		if ( this.decay !== undefined ) data.object.decay = this.decay;
		if ( this.penumbra !== undefined ) data.object.penumbra = this.penumbra;

		if ( this.shadow !== undefined ) data.object.shadow = this.shadow.toJSON();

		return data;

	}

} );
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章