Appearance
plugin-effect
特效。
样例
安装
bash
npm install @soonspacejs/plugin-effect -S
# or
yarn add @soonspacejs/plugin-effect -S
使用方法
js
import SoonSpace from 'soonspacejs';
import EffectPlugin from '@soonspacejs/plugin-effect';
const ssp = new SoonSpace({
el: '#view',
options: {},
events: {},
});
const effectPlugin = ssp.registerPlugin(EffectPlugin, 'effectPlugin');
属性
weatherPresetImgs
天气效果预设图片
雨滴图片
js
effectPlugin.weatherPresetImgs.rain;
下雪图片
js
effectPlugin.weatherPresetImgs.snow;
方法
createFlame
创建火焰
定义
ts
type PluginObjectInfo = BaseObject3DInfo;
interface FlameOptions extends PluginObjectInfo {
magnitude?: number;
gain?: number;
imgUrl?: string;
}
function createFlame(options: FlameOptions): PluginObject;
使用
js
effectPlugin.createFlame({
id: 'testFlame',
position: {
x: 0,
y: 50,
z: 0,
},
});
参数
options
- 描述: 配置
- 必填:
- 类型:
FlameOptions
FlameOptions
属性 | 描述 | 类型 | 必填 | 默认值 |
---|---|---|---|---|
magnitude | 火焰量级 | number | 1.3 | |
gain | 火焰增益 | number | 0.5 | |
imgUrl | 火焰图片 | string | 内置图片 |
其他配置参考 BaseObject3DInfo
createSmoke
创建烟雾
定义
ts
interface SmokeOptions extends PluginObjectInfo {
imgUrl?: string;
count?: number;
maxAge?: number;
size?: number;
acceleration?: number;
velocity?: IVector3;
color?: IColor[];
}
function createSmoke(options: SmokeOptions): PluginObject;
用法
js
effectPlugin.createSmoke({
id: 'testSmoke',
name: 'testSmoke',
position: {
x: -300,
y: 0,
z: 300,
},
});
参数
options
- 描述: 配置
- 必填:
- 类型:
SmokeOptions
SmokeOptions
属性 | 描述 | 类型 | 必填 | 默认值 |
---|---|---|---|---|
imgUrl | 烟雾图片 | string | 内置图片 | |
count | 烟雾粒子的数量 | number | 1000 | |
maxAge | 烟雾粒子的显示时间(秒) | number | 4 | |
size | 烟雾粒子的大小 | number | 200 | |
acceleration | 烟雾粒子的加速度 | number | 10 | |
velocity | 烟雾粒子的扩散方向 | IVector3 | { x: 100, y: 100, z: 100 } | |
color | 烟雾粒子的颜色 | IColor[] | [0x333333, 0x111111] |
其他配置参考 BaseObject3DInfo
createSmoke2
创建烟雾
样例
定义
ts
interface Smoke2Options extends PluginObjectInfo {
imgUrl?: string;
radius?: number;
maxLife?: number;
maxSize?: number;
rate?: number;
colors?: IColor[];
blending?: Blending;
}
function createSmoke2(options: Smoke2Options): PluginObject;
用法
js
const smoke = effectPlugin.createSmoke2({
id: 'smoke2',
position: { x: 0, y: -20, z: 0 },
scale: {
x: 5,
y: 5,
z: 5,
},
radius: 2,
maxSize: 80,
maxLife: 30,
rate: 20,
colors: [ 0x000000, 0xffffff ],
});
参数
options
- 描述: 配置
- 必填:
- 类型:
Smoke2Options
Smoke2Options
属性 | 描述 | 类型 | 必填 | 默认值 |
---|---|---|---|---|
imgUrl | 烟雾图片 | string | 内置图片 | |
radius | 烟雾扩散半径 | number | 0.5 | |
maxLife | 烟雾最大存在时间(秒) | number | 3.5 | |
maxSize | 烟雾最大的大小 | number | 3.0 | |
rate | 烟雾的上升速率 | number | 10 | |
colors | 烟雾颜色数组 | IColor[] | [ 0x000000, 0xffffff ] | |
blending | 烟雾材质混合 | THREE.Blending | NormalBlending |
createWater
创建水面
定义
ts
interface WaterOptions extends ShaderMaterialParameters {
/**
* 水面的多边形区域
*/
polygon: IVector3[];
/**
* 水面的高度
* 默认为第一个顶点的 y 值
*/
height?: number;
/**
* 太阳的照射的方向
*/
sunDirection?: Vector3;
/**
* 太阳的颜色
*/
sunColor?: ColorRepresentation;
/**
* 水的颜色
*/
waterColor?: ColorRepresentation;
/**
* 水波的强度
*/
distortionScale?: number;
/**
* 透明度
*/
alpha?: number;
/**
* 速度
*/
speed?: number;
/**
* 水面法线纹理
*/
waterNormals?: string | Texture;
fog?: boolean;
eye?: Vector3;
clipBias?: number;
textureWidth?: number;
textureHeight?: number;
}
interface CreateWaterOptions extends PluginObjectInfo, Omit<WaterOptions, 'userData'> {
/**
* 是否开启水的动画
* 如果开启,则会一直触发场景渲染
* 如果关闭,则不会主动触发场景渲染,但在渲染渲染时仍会有动画效果,只是不会主动触发场景渲染
*/
animation?: boolean;
}
function createWater(options: CreateWaterOptions): PluginObject;
用法
js
effectPlugin.createWater({
id: 'water',
position: { x: 0, y: 0.1, z: 0 },
polygon: [
{ x: 0, y: 50, z: 0 },
{ x: 0, y: 1, z: 1000 },
{ x: 1500, y: 0, z: 1000 },
{ x: 1000, y: 0, z: 0 },
],
waterColor: '#ff6600',
});
其他配置参考 BaseObject3DInfo
createContactShadows
创建接触阴影
样例
定义
ts
interface ContactShadowsOptions extends Omit<PluginObjectInfo, 'scale'> {
opacity?: number;
width?: number;
height?: number;
blur?: number;
far?: number;
resolution?: number;
frames?: number;
scale?: number | [x: number, y: number];
darkness?: number;
}
function createContactShadows(options: ContactShadowsOptions): PluginObject;
用法
js
effectPlugin.createContactShadows({
id: 'shadows',
position: { x: 0, y: -0.1, z: 0 },
opacity: 1,
scale: [100, 100],
blur: 2,
far: 1,
resolution: 1024,
frames: 10,
darkness: 2,
});
参数
options
- 描述: 配置
- 必填:
- 类型:
ContactShadowsOptions
ContactShadowsOptions
属性 | 描述 | 类型 | 必填 | 默认值 |
---|---|---|---|---|
opacity | 不透明度 | number | 1 | |
width | 平面宽度 | number | 1 | |
height | 平面高度 | number | 1 | |
blur | 模糊 | number | 1 | |
far | 向上取样的阴影距离 | number | 10 | |
resolution | 阴影平面的分辨率 | number | 512 | |
frames | 阴影的渲染帧数 | number | Infinity | |
scale | 用于乘以宽高 | number | [x: number, y: number] | 10 | |
darkness | 阴影的黑暗系数 | number | 1 |
其他配置参考 BaseObject3DInfo
openWeather
开启天气效果
样例
定义
ts
interface WeatherOptions {
imgUrl?: string;
color?: IColor;
size?: number;
opacity?: number;
count?: number;
range?: number;
velocityX?: [number, number];
velocityY?: [number, number];
}
function openWeather(options?: WeatherOptions): void;
用法
js
effectPlugin.openWeather({
imgUrl: effectPlugin.weatherPresetImgs.snow,
count: 500,
color: 0xffffff,
size: 1.4,
opacity: 0.8,
velocityX: [-0.01, 0.01],
velocityY: [0.2, 0.3],
});
参数
options
- 描述: 配置
- 必填:
- 类型:
WeatherOptions
WeatherOptions
属性 | 描述 | 类型 | 必填 | 默认值 |
---|---|---|---|---|
imgUrl | 粒子贴图 | string | 内置雨滴图 | |
color | 粒子颜色 | IColor | 0xffffff | |
size | 粒子大小 | number | 1 | |
opacity | 粒子不透明度 | number | 0.9 | |
count | 粒子数量 | number | 500 | |
range | 生成范围 | number | 100 | |
velocityX | 左右浮动的随机范围 | [number, number] | [-0.02, 0.02] | |
velocityY | 下降速度的随机范围 | [number, number] | [0.4, 0.8] |
提示
重复调用 openWeather
方法自动会将之前的天气效果关闭。
关闭天气需要使用 closeWeather
方法
closeWeather
关闭天气效果
定义
ts
function closeWeather(): void;
用法
js
effectPlugin.closeWeather();
createSparkles
创建星星
样例
定义
ts
interface SparklesOptions extends PluginObjectInfo {
count?: number;
speed?: number | Float32Array;
opacity?: number | Float32Array;
color?: IColor | Float32Array;
size?: number | Float32Array;
scalar?: number | [number, number, number] | IVector3;
noise?: number | [number, number, number] | IVector3 | Float32Array;
}
function createSparkles(options: SparklesOptions): PluginObject;
用法
js
effectPlugin.createSparkles({
id: 'testSparkles',
position: {
x: 0,
y: 2,
z: 0,
},
count: 100,
scalar: 8,
size: 2,
speed: 0.8,
opacity: 0.5,
noise: 1,
color: '#ff0',
});
参数
options
- 描述: 配置
- 必填:
- 类型:
SparklesOptions
SparklesOptions
属性 | 描述 | 类型 | 必填 | 默认值 |
---|---|---|---|---|
count | 粒子的数量 | number | 100 | |
speed | 粒子的速度 | number | Float32Array | 1 | |
opacity | 粒子的不透明度 | number | Float32Array | 1 | |
color | 粒子颜色 | IColor | Float32Array | 0xffffff | |
size | 粒子大小 | number | 2 | |
scalar | 粒子扩散范围 | number | [number, number, number] | IVector3 | 8 | |
noise | 粒子运动系数 | number | [number, number, number] | IVector3 | Float32Array | 1 |
其他配置参考 BaseObject3DInfo
createParticleCluster
创建粒子簇
样例
定义
ts
function createParticleCluster(options: ParticleClusterOptions): ParticleCluster;
参数类型:
ts
/**
* 粒子簇特征
*/
export interface ParticleClusterFeature {
/**
* 半径
*
* @defaultValue 10
*/
radius?: number;
/**
* 值
*
* @defaultValue 100
*/
value?: number;
/**
* 实心因子
*
* @defaultValue 0.7
*/
solid?: number;
/**
* 空心因子
*
* @defaultValue 0
*/
hollow?: number;
/**
* 中心点的密度
* @remarks
* 单位空间中有多少个点
*
* @defaultValue 1
*/
density?: number;
/**
* 粒子云的形状
*/
shape?: ParticleClusterShape;
/**
* 密度梯度函数
*/
densityGradient?: GetGradientValue;
valueGradient?: GetGradientValue;
/**
* 映射区间
* @remarks
* x 为最小值,y 为最大值
*
* @defaultValue {x:0,y:100}
*/
clim?: IVector2;
/**
* 生成粒子时使用的距离步长
* @remarks
* 这个也会影响粒子的密度 和 粒子个数;
* 建议所有粒子的步长一样;
*
* @defaultValue 3
*/
step?: number;
}
/**
* 粒子簇特性点
*/
export type ParticleClusterFeaturePoint<IVec extends IVector> = ParticleClusterFeature & IVec;
/**
* 很多点粒子族
*/
export interface CreatePointParticleClusterDataArrOptions<IVec extends IVector> extends ParticleClusterFeature {
points: ParticleClusterFeaturePoint<IVec>[];
}
export interface CreateLineParticleClusterDataArrOptions<IVec extends IVector>
extends CreatePointParticleClusterDataArrOptions<IVec> {
radiusGradient?: GetLineGradientValue;
lineDensityGradient?: GetLineGradientValue;
lineValueGradient?: GetLineGradientValue;
lineStep?: number;
}
/**
* 很多点粒子族
*/
export interface CreatePointParticleClusterDataArrOptions<IVec extends IVector> extends ParticleClusterFeature {
points: ParticleClusterFeaturePoint<IVec>[];
}
/**
* ParticleClusterGeometry 的选项
*/
export type ParticleClusterGeometryOptions = CreatePointParticleClusterDataArrOptions<IVector3> &
CreateLineParticleClusterDataArrOptions<IVector3> &
CreateHeatParticleClusterDataArrOptions<IVector3> &
Omit<ClusterGeometryOptions, 'clusters'> & {
clusterType?: ClusterType;
};
export type ParticleClusterOptions = ParticleClusterGeometryOptions & PointsMaterialParameters;
ParticleCluster
ts
export declare class ParticleCluster extends Points {
readonly isParticleCluster = true;
constructor(options?: ParticleClusterOptions);
get options(): ParticleClusterGeometryOptions;
set options(value: ParticleClusterGeometryOptions);
setOptions(options: ParticleClusterGeometryOptions): void;
addPoint(
point: ParticleClusterFeaturePoint<IVector3>[] | ParticleClusterFeaturePoint<IVector3>,
options?: Omit<ParticleClusterGeometryOptions, 'points'>
): void;
convertPoints(points: ParticleClusterFeaturePoint<IVector3>[]): ParticleClusterFeatureVector<IVector3>[];
}
用法
js
// 创建粒子簇
const particle = effectPlugin.createParticleCluster({
points: [
{ x: 0, y: 0, z: 0, value: 80, radius: 100 },
{ x: 0, y: 50, z: 0, value: 20, radius: 40 },
],
clim: { x: 0, y: 50 },
gradient: [
[0, '#ff0000ff'],
[0.4, '#00ff00aa'],
[0.9, '#0000ff77'],
],
size: 0.01,
opacity: 0.3,
clusterType: ClusterType.Point,
});
// 继承添加点
particle.addPoint([
{ x: 30, y: 0, z: 0, value: 40, radius: 60 },
{ x: 0, y: 20, z: 10, value: 30, radius: 40 },
]);
createPointsWave
创建粒子波浪
样例
定义
ts
interface PointsWaveOptions extends PluginObjectInfo {
xAxisCount?: number;
zAxisCount?: number;
showDistance?: number;
separation?: number;
size?: number;
maxFluctua?: number;
color?: IColor;
opacity?: number;
}
function createPointsWave(options: PointsWaveOptions): PluginObject;
用法
js
const pointsWave = effectPlugin.createPointsWave({
id: 'pointsWave',
position: {
x: 0,
y: -20,
z: 0,
},
xAxisCount: 100,
zAxisCount: 100,
showDistance: 0,
separation: 20,
size: 5,
maxFluctua: 0.1,
color: '#485c7c',
opacity: 1,
});
参数
options
- 描述: 配置
- 必填:
- 类型:
PointsWaveOptions
PointsWaveOptions
属性 | 描述 | 类型 | 必填 | 默认值 |
---|---|---|---|---|
xAxisCount | x轴粒子数量 | number | 100 | |
xAxisCount | y轴粒子数量 | number | 100 | |
showDistance | 开始显示粒子的距离 | number | 0 | |
separation | 分隔距离 | number | 20 | |
size | 大小 | number | 5 | |
maxFluctua | 最大波动系数 | number | 0.1 | |
color | 颜色 | IColor | #485c7c | |
opacity | 不透明度 | number | 1 |
其他配置参考 BaseObject3DInfo
createBuilds
创建建筑
样例
同上
定义
ts
interface BuildsOptions extends PluginObjectInfo {
buildWidth?: number;
buildHeigh?: number;
buildDepth?: number;
count?: number;
gapX?: number;
gapZ?: number;
showDistance?: number;
randShift?: number;
color?: IColor;
}
function createBuilds(options: BuildsOptions): PluginObject;
用法
js
const builds = effectPlugin.createBuilds({
id: 'builds',
position: {
x: 0,
y: -50,
z: 0,
},
buildWidth: 100,
buildDepth: 100,
buildHeigh: 200,
count: 12,
gapX: 2,
gapZ: 2,
showDistance: 1000,
randShift: 0.5,
color: '#485c7c',
});
参数
options
- 描述: 配置
- 必填:
- 类型:
BuildsOptions
BuildsOptions
属性 | 描述 | 类型 | 必填 | 默认值 |
---|---|---|---|---|
buildWidth | 建筑宽度(x) | number | 100 | |
buildDepth | 建筑深度(z) | number | 100 | |
buildHeigh | 建筑高度(y) | number | 200 | |
count | 数量(实际数量为count平方) | number | 12 | |
gapX | x轴间隔(配合randShift) | number | 2 | |
gapZ | z轴间隔(配合randShift) | number | 2 | |
showDistance | 开始显示建筑的距离 | number | 1000 | |
randShift | 随机分布系数 | number | 0.5 | |
color | 颜色 | IColor | #485c7c |
其他配置参考 BaseObject3DInfo
removeEffect
删除效果
定义
ts
function removeEffect(id: PluginObjectInfo['id']): boolean;
用法
js
effectPlugin.removeEffect('test');
弃用警告
请使用 removeObjectById
替代