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;

	}

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