Skip to content

灯光

提示

v2.4.0 之后内部已经不在创建额外的灯光,控制场景的明亮度推荐使用 setToneMapping 控制

样例:

createAmbientLight

创建环境光

定义:

ts
interface BaseLightInfo {
  id: string | number;
  name?: string;
  color?: IColor;
  intensity?: number;
}

interface AmbientLightOptions extends BaseLightInfo {}

function createAmbientLight(options: AmbientLightOptions): THREE.AmbientLight;

用法:

js
ssp.createAmbientLight({
  id: 'ambientLight',
  name: 'ambientLight',
});

参数:

options

  • 描述: 环境光配置项
  • 必填:
  • 类型: AmbientLightOptions
AmbientLightOptions
属性描述类型必填默认值
id唯一IDstring | number
name名称string
color颜色IColor0x9a9a9a
intensity光照强度number10

setAmbientLight

设置环境光

定义:

ts
function setAmbientLight(options: AmbientLightOptions): boolean;

用法:

js
const isUpdated = ssp.setAmbientLight({
  id: 'ambientLight',
  color: 0x8a8a8a,
  intensity: 0.5,
});
if (isUpdated) {
  console.log('环境光配置更新成功');
}

提示

setAmbientLightcreateAmbientLightoptions 完全一致。

setAmbientLight 用于更新场景已存在的光的配置,createAmbientLight 用于创建一个光。

createDirectionalLight

创建平行光

定义:

ts
interface ShadowOptions {
  openShadow?: boolean;
  shadowAutoUpdate?: boolean;
  mapSize?: number;
}

interface DirectionalLightOptions extends BaseLightInfo, ShadowOptions {
  position?: Position;
  target?: Position;
}

function createDirectionalLight(
  options: DirectionalLightOptions
): THREE.DirectionalLight;

用法:

js
ssp.createDirectionalLight({
  id: 'directionalLight',
  name: 'directionalLight',
});

参数:

options

  • 描述: 平行光配置项
  • 必填:
  • 类型: DirectionalLightOptions

DirectionalLightOptions

属性描述类型必填默认值
id唯一IDstring | number
name名称string
color颜色IColor0xffffff
intensity光照强度number10
position光源的位置Position{ x: 0, y: 1000, z: 0 }
target光照向的位置Position{ x: 0, y: -100, z: 0 }
openShadow是否开启阴影booleanfalse
shadowAutoUpdate阴影是否自动更新,如果为 `false` 需要调用 `updateAllShadow` 来更新阴影booleanfalse
mapSize阴影的贴图区域大小。值越大,阴影质量越好。但也会增加性能损耗number4096

提示

开启光源的阴影时,默认是静态的阴影。需要手动调用 updateAllShadow 来更新阴影。

按需生成阴影,这样可以大大提升开启阴影时的场景性能。

setDirectionalLight

设置平行光

定义:

ts
function setDirectionalLight(options: DirectionalLightOptions): boolean;

用法:

js
const isUpdated = ssp.setDirectionalLight({
  id: 'directionalLight',
  color: 0x8a8a8a,
  intensity: 0.5,
});
if (isUpdated) {
  console.log('平行光配置更新成功');
}

createHemisphereLight

创建半球光

定义:

ts
interface HemisphereLightOptions extends BaseLightInfo {
  skyColor?: IColor;
  groundColor?: IColor;
  position?: Position;
}

function createHemisphereLight(
  options: HemisphereLightOptions
): THREE.HemisphereLight;

用法:

js
ssp.createHemisphereLight({
  id: 'hemiLight',
  name: 'hemiLight',
  intensity: 0.1,
});

参数:

options

  • 描述: 半球光配置项
  • 必填:
  • 类型: HemisphereLightOptions
HemisphereLightOptions
属性描述类型必填默认值
id唯一IDstring | number
name名称string
intensity光照强度number10
skyColor天空颜色IColor0xffffff
groundColor地面颜色IColor0xdddddd
position光的朝向位置Position{ x: 0, y: 0, z: 0 }

setHemisphereLight

设置半球光

定义:

ts
function setHemisphereLight(options: HemisphereLightOptions): boolean;

用法:

js
const isUpdated = ssp.setHemisphereLight({
  id: 'hemiLight',
  color: 0x8a8a8a,
  intensity: 0.5,
});
if (isUpdated) {
  console.log('半球光配置更新成功');
}

createSpotLight

创建聚光灯

定义:

ts
interface SpotLightOptions extends BaseLightInfo, ShadowOptions {
  angle?: number;
  position?: Position;
  target?: Position;
}

function createSpotLight(options: SpotLightOptions): THREE.SpotLight;

用法:

js
ssp.createSpotLight({
  id: 'spotLight',
  name: 'spotLight',
});

参数:

options

  • 描述: 聚光灯配置项
  • 必填:
  • 类型: SpotLightOptions
SpotLightOptions
属性描述类型必填默认值
id唯一IDstring | number
name名称string
color颜色IColor0xffffff
intensity光照强度number10
angle光照方向扩散的角度(最大值为180)number45
position光源的位置Position{ x: 0, y: 500, z: 0 }
target光照向的位置Position{ x: 0, y: 0, z: 0 }
openShadow是否开启阴影booleanfalse
shadowAutoUpdate阴影是否自动更新,如果为 `false` 需要调用 `updateAllShadow` 来更新阴影booleanfalse
mapSize阴影的贴图区域大小。值越大,阴影质量越好。但也会增加性能损耗number4096

setSpotLight

设置半球光

定义:

ts
function setSpotLight(options: SpotLightOptions): boolean;

用法:

js
const isUpdated = ssp.setSpotLight({
  id: 'spotLight',
  color: 0x8a8a8a,
  intensity: 0.5,
});
if (isUpdated) {
  console.log('聚光灯配置更新成功');
}

createPointLight

创建点光源

定义:

ts
interface PointLightOptions extends BaseLightInfo, ShadowOptions {
  position?: Position;
  distance?: number;
}

function createPointLight(options: PointLightOptions): THREE.PointLight;

用法:

js
ssp.createPointLight({ id: 'pointLight', name: 'pointLight' });

参数:

options

  • 描述: 点光源配置项
  • 必填:
  • 类型: PointLightOptions
PointLightOptions
属性描述类型必填默认值
id唯一IDstring | number
name名称string
color颜色IColor0xffffff
intensity光照强度number10
position光源的位置Position{ x: 0, y: 500, z: 0 }
distance光照范围number5000
openShadow是否开启阴影booleanfalse
shadowAutoUpdate阴影是否自动更新,如果为 `false` 需要调用 `updateAllShadow` 来更新阴影booleanfalse
mapSize阴影的贴图区域大小。值越大,阴影质量越好。但也会增加性能损耗number4096

setPointLight

设置点光源

定义:

ts
function setPointLight(options: PointLightOptions): boolean;

用法:

js
const isUpdated = ssp.setPointLight({
  id: 'pointLight',
  color: 0x8a8a8a,
  intensity: 0.5,
});
if (isUpdated) {
  console.log('点光源配置更新成功');
}

createRectAreaLight

创建矩形区域光源

样例:

定义:

ts
interface RectAreaLightOptions extends BaseLightInfo {
  position?: Position;
  width?: number;
  height?: number;
}

function createRectAreaLight(
  options: RectAreaLightOptions
): THREE.RectAreaLight;

用法:

js
ssp.createRectAreaLight({
  id: 'rectAreaLight',
  name: 'rectAreaLight',
  intensity: 0.8,
  color: 0xffff00,
  width: 20,
  height: 50,
  position: {
    x: 0,
    y: 0,
    z: -10,
  },
});

参数:

options

  • 描述: 矩形区域光源配置项
  • 必填:
  • 类型: RectAreaLightOptions
RectAreaLightOptions
属性描述类型必填默认值
id唯一IDstring | number
name名称string
color颜色IColor0xffffff
intensity光照强度number10
width矩形区域的宽度number10
height矩形区域的高度number10
position光源的位置Position{ x: 0, y: 0, z: 0 }

setRectAreaLight

设置矩形区域光源

定义:

ts
function setRectAreaLight(options: RectAreaLightOptions): boolean;

用法:

js
const isUpdated = ssp.setRectAreaLight({
  id: 'rectAreaLight',
  color: 0x693333,
  intensity: 2,
  width: 20,
  height: 30,
});
if (isUpdated) {
  console.log('矩形区域光源配置更新成功');
}

getLightById

根据 id 查询 Light 对象

定义:

ts
function getLightById<T extends Light>(id: BaseObject3DInfo['id']): T | null;

用法:

js
const pointLight = ssp.getLightById('pointLight');

removeLightById

根据 id 移除 Light 对象

定义:

ts
function removeLightById(id: BaseObject3DInfo['id']): boolean;

用法:

js
const isRemoved = ssp.removeLightById('pointLight');
if (isRemoved) {
  console.log('灯光移除成功!!!');
}

clearLight

清空 Light 对象

定义:

ts
function clearLight(): void;

用法:

js
ssp.clearLight();

showAllLight

显示所有光

定义:

ts
function showAllLight(): void;

用法:

js
ssp.showAllLight();

hideAllLight

隐藏所有光

定义:

ts
function hideAllLight(): void;

用法:

js
ssp.hideAllLight();

updateAllShadow

更新所有光源的阴影

定义:

ts
function updateAllShadow(): void;

用法:

js
ssp.updateAllShadow();

浙ICP备16043491号