Three.js光源梳理1——光照基类(Light)
2022-02-08 本文已影响0人
ShawnWeasley
Three.js光源系统基本上与Unity没有太大差别,下图是当前Three.js版本提供的全部光源,本系列梳理一下各种光源类型和使用方法。
![](https://img.haomeiwen.com/i5625720/9b205d122e1a06f5.png)
所有的光源均基于光照基类Light,而Light本身作为其他光源的父类,不能被直接调用。
Light类构造:
color(颜色)
intensity(强度)
Light类属性:
color(颜色)
intensity(强度)
Light类方法:
copy(复制color和intensity)
toJson(将光照信息转为json格式)
Light.js如下,继承于Object3D,仅实现了上述的属性和方法和标记Light.prototype.isLight为true;:
import { Object3D } from '../core/Object3D.js';
import { Color } from '../math/Color.js';
class Light extends Object3D {
constructor( color, intensity = 1 ) {
super();
this.type = 'Light';
this.color = new Color( color );
this.intensity = intensity;
}
dispose() {
// Empty here in base class; some subclasses override.
}
copy( source ) {
super.copy( source );
this.color.copy( source.color );
this.intensity = source.intensity;
return this;
}
toJSON( meta ) {
const data = super.toJSON( 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;
}
}
Light.prototype.isLight = true;
export { Light };
源码的内容很简单,但并不重要,会用就行了,这里把源码放在这里便于大家理解。