mirror of
https://github.com/motajs/template.git
synced 2026-11-21 04:04:30 +08:00
init: 2.B
This commit is contained in:
commit
6bf32174d8
610 files changed
+151444
No files matched your search
@@ -0,0 +1,151 @@
|
||||
import { RenderItem } from '@motajs/render-core';
|
||||
import { IWeather, IWeatherController, IWeatherInstance } from './types';
|
||||
import { logger } from '@motajs/common';
|
||||
import { isNil } from 'lodash-es';
|
||||
import { Ticker } from 'mutate-animate';
|
||||
|
||||
type WeatherConstructor = new () => IWeather;
|
||||
|
||||
export class WeatherController implements IWeatherController {
|
||||
/** 暴露到全局的控制器 */
|
||||
static extern: Map<string, IWeatherController> = new Map();
|
||||
/** 注册的天气 */
|
||||
static weathers: Map<string, WeatherConstructor> = new Map();
|
||||
|
||||
private static ticker: Ticker = new Ticker();
|
||||
|
||||
/** 暴露至全局的 id */
|
||||
private externId?: string;
|
||||
/** 天气元素纵深 */
|
||||
private zIndex: number = 100;
|
||||
|
||||
readonly active: Set<IWeatherInstance> = new Set();
|
||||
|
||||
container: RenderItem | null = null;
|
||||
|
||||
constructor() {
|
||||
WeatherController.ticker.add(this.tick);
|
||||
}
|
||||
|
||||
private tick = (time: number) => {
|
||||
this.active.forEach(v => v.weather.tick(time));
|
||||
};
|
||||
|
||||
/**
|
||||
* 设置天气元素纵深,第一个天气会被设置为 `zIndex`,之后依次是 `zIndex+1` `zIndex+2` ...
|
||||
* @param zIndex 第一个天气的纵深
|
||||
*/
|
||||
setZIndex(zIndex: number) {
|
||||
this.zIndex = zIndex;
|
||||
let n = zIndex;
|
||||
this.active.forEach(v => {
|
||||
v.setZIndex(n);
|
||||
n++;
|
||||
});
|
||||
}
|
||||
|
||||
bind(container: RenderItem): void {
|
||||
if (this.container) {
|
||||
logger.warn(65);
|
||||
return;
|
||||
}
|
||||
this.container = container;
|
||||
}
|
||||
|
||||
private getWeatherObject<R extends RenderItem, T extends IWeather<R>>(
|
||||
weather: string | T
|
||||
): T | null {
|
||||
if (typeof weather === 'string') {
|
||||
const Weather = WeatherController.weathers.get(weather);
|
||||
if (!Weather) {
|
||||
logger.warn(25, weather);
|
||||
return null;
|
||||
}
|
||||
return new Weather() as T;
|
||||
} else {
|
||||
return weather;
|
||||
}
|
||||
}
|
||||
|
||||
activate(weather: string, level?: number): IWeatherInstance | null;
|
||||
activate<R extends RenderItem, T extends IWeather<R>>(
|
||||
weather: T,
|
||||
level?: number
|
||||
): IWeatherInstance<R, T> | null;
|
||||
activate<R extends RenderItem, T extends IWeather<R>>(
|
||||
weather: string | T,
|
||||
level: number = 5
|
||||
): IWeatherInstance<R, T> | null {
|
||||
const obj = this.getWeatherObject<R, T>(weather);
|
||||
if (!obj || !this.container) return null;
|
||||
const element = obj.create(level);
|
||||
element.size(this.container.width, this.container.height);
|
||||
const instance = new WeatherInstance(obj, element);
|
||||
instance.setZIndex(this.zIndex + this.active.size);
|
||||
this.active.add(instance);
|
||||
this.container.appendChild(element);
|
||||
return instance;
|
||||
}
|
||||
|
||||
deactivate(instance: IWeatherInstance): void {
|
||||
this.container?.removeChild(instance.element);
|
||||
instance.weather.destroy();
|
||||
this.active.delete(instance);
|
||||
}
|
||||
|
||||
clearWeather(): void {
|
||||
this.active.forEach(v => {
|
||||
v.weather.destroy();
|
||||
});
|
||||
this.active.clear();
|
||||
}
|
||||
|
||||
/**
|
||||
* 将此控制器暴露至全局,允许使用 {@link WeatherController.get} 获取到实例
|
||||
* @param id 暴露给全局的 id
|
||||
*/
|
||||
extern(id: string) {
|
||||
WeatherController.extern.set(id, this);
|
||||
this.externId = id;
|
||||
}
|
||||
|
||||
destroy() {
|
||||
this.clearWeather();
|
||||
WeatherController.ticker.remove(this.tick);
|
||||
if (!isNil(this.externId)) {
|
||||
WeatherController.extern.delete(this.externId);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取暴露至全局的控制器
|
||||
* @param id 控制器暴露至全局的 id
|
||||
*/
|
||||
static get(id: string): IWeatherController | null {
|
||||
return this.extern.get(id) ?? null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 注册一个天气
|
||||
* @param id 天气的名称
|
||||
* @param weather 天气的构造器
|
||||
*/
|
||||
static register(id: string, weather: WeatherConstructor) {
|
||||
this.weathers.set(id, weather);
|
||||
}
|
||||
}
|
||||
|
||||
export class WeatherInstance<
|
||||
R extends RenderItem = RenderItem,
|
||||
T extends IWeather<R> = IWeather<R>
|
||||
> implements IWeatherInstance<R, T>
|
||||
{
|
||||
constructor(
|
||||
readonly weather: T,
|
||||
readonly element: R
|
||||
) {}
|
||||
|
||||
setZIndex(zIndex: number): void {
|
||||
this.element.setZIndex(zIndex);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
import { WeatherController } from './controller';
|
||||
import {
|
||||
CloudWeather,
|
||||
FogWeather,
|
||||
RainWeather,
|
||||
SnowWeather,
|
||||
SunWeather
|
||||
} from './presets';
|
||||
|
||||
export function createWeather() {
|
||||
WeatherController.register('cloud', CloudWeather);
|
||||
WeatherController.register('rain', RainWeather);
|
||||
WeatherController.register('snow', SnowWeather);
|
||||
WeatherController.register('sun', SunWeather);
|
||||
WeatherController.register('fog', FogWeather);
|
||||
}
|
||||
|
||||
export * from './presets';
|
||||
export * from './controller';
|
||||
export * from './types';
|
||||
export * from './weather';
|
||||
@@ -0,0 +1,10 @@
|
||||
import { CloudLike } from './cloudLike';
|
||||
import { SizedCanvasImageSource } from '@motajs/render-core';
|
||||
|
||||
export class CloudWeather extends CloudLike {
|
||||
getImage(): SizedCanvasImageSource | null {
|
||||
return core.material.images.images['cloud.png'] ?? null;
|
||||
}
|
||||
|
||||
onDestroy(): void {}
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
import { MotaOffscreenCanvas2D, Sprite } from '@motajs/render-core';
|
||||
import { Weather } from '../weather';
|
||||
import { SizedCanvasImageSource } from '@motajs/render-core';
|
||||
|
||||
export abstract class CloudLike extends Weather<Sprite> {
|
||||
/** 不透明度 */
|
||||
private alpha: number = 0;
|
||||
/** 水平速度 */
|
||||
private vx: number = 0;
|
||||
/** 竖直速度 */
|
||||
private vy: number = 0;
|
||||
/** 水平位置 */
|
||||
private cx: number = 0;
|
||||
/** 竖直位置 */
|
||||
private cy: number = 0;
|
||||
/** 移动的最大速度 */
|
||||
private maxSpeed: number = 1;
|
||||
/** 上一次执行速度变换的时刻 */
|
||||
private lastTick = 0;
|
||||
/** 绘制天气使用的图片 */
|
||||
private image: SizedCanvasImageSource | null = null;
|
||||
|
||||
/**
|
||||
* 获取类多云天气所使用的图片
|
||||
*/
|
||||
abstract getImage(): SizedCanvasImageSource | null;
|
||||
|
||||
private drawImage(canvas: MotaOffscreenCanvas2D) {
|
||||
const ctx = canvas.ctx;
|
||||
if (!this.image) return;
|
||||
ctx.globalAlpha = this.alpha;
|
||||
const { width, height } = this.image;
|
||||
for (let x = -1; x < 2; x++) {
|
||||
for (let y = -1; y < 2; y++) {
|
||||
const dx = x * width + this.cx;
|
||||
const dy = y * height + this.cy;
|
||||
if (dx > canvas.width || dy > canvas.height) continue;
|
||||
if (dx + width < 0 || dy + height < 0) continue;
|
||||
ctx.drawImage(this.image, dx, dy, width, height);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
tick(time: number): void {
|
||||
if (!this.element || !this.image) return;
|
||||
this.element.update();
|
||||
const dt = time - this.lastTick;
|
||||
this.lastTick = time;
|
||||
if (dt > 100) return;
|
||||
const dvx = (Math.random() - 0.5) * this.level * 10;
|
||||
const dvy = (Math.random() - 0.5) * this.level * 10;
|
||||
const addx = (dvx * dt) / 1000;
|
||||
const addy = (dvy * dt) / 1000;
|
||||
if (Math.sign(addx) === Math.sign(this.vx)) {
|
||||
const ratio = Math.sqrt(
|
||||
(this.maxSpeed - Math.abs(this.vx)) / this.maxSpeed
|
||||
);
|
||||
const value = Math.abs(addx) * ratio;
|
||||
this.vx += value * Math.sign(addx);
|
||||
} else {
|
||||
this.vx += addx;
|
||||
}
|
||||
if (Math.sign(addy) === Math.sign(this.vy)) {
|
||||
const ratio = Math.sqrt(
|
||||
(this.maxSpeed - Math.abs(this.vy)) / this.maxSpeed
|
||||
);
|
||||
const value = Math.abs(addy) * ratio;
|
||||
this.vy += value * Math.sign(addy);
|
||||
} else {
|
||||
this.vy += addy;
|
||||
}
|
||||
this.cx += (this.vx * dt) / 1000;
|
||||
this.cy += (this.vy * dt) / 1000;
|
||||
this.cx %= this.image.width;
|
||||
this.cy %= this.image.height;
|
||||
}
|
||||
|
||||
createElement(level: number): Sprite {
|
||||
const element = new Sprite('static', true);
|
||||
element.setRenderFn(canvas => this.drawImage(canvas));
|
||||
this.maxSpeed = Math.sqrt(level) * 100;
|
||||
this.vx = ((Math.random() - 0.5) * this.maxSpeed) / 2;
|
||||
this.vy = ((Math.random() - 0.5) * this.maxSpeed) / 2;
|
||||
this.alpha = Math.sqrt(level) / 10;
|
||||
this.image = this.getImage();
|
||||
return element;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
import { MotaOffscreenCanvas2D } from '@motajs/render-core';
|
||||
import { CloudLike } from './cloudLike';
|
||||
import { SizedCanvasImageSource } from '@motajs/render-core';
|
||||
|
||||
export class FogWeather extends CloudLike {
|
||||
/** 雾天气的图像比较小,因此将四个进行合并 */
|
||||
private static mergedFog: MotaOffscreenCanvas2D | null = null;
|
||||
|
||||
getImage(): SizedCanvasImageSource | null {
|
||||
if (FogWeather.mergedFog) {
|
||||
return FogWeather.mergedFog.canvas;
|
||||
} else {
|
||||
return FogWeather.mergeFog();
|
||||
}
|
||||
}
|
||||
|
||||
onDestroy(): void {}
|
||||
|
||||
/**
|
||||
* 将雾天气的图片 2x2 合并
|
||||
*/
|
||||
static mergeFog() {
|
||||
const image = core.material.images.images['fog.png'];
|
||||
if (!image) return null;
|
||||
const { width, height } = image;
|
||||
this.mergedFog = new MotaOffscreenCanvas2D();
|
||||
this.mergedFog.size(width * 2, height * 2);
|
||||
const ctx = this.mergedFog.ctx;
|
||||
ctx.drawImage(image, 0, 0, width, width);
|
||||
ctx.drawImage(image, width, 0, width, width);
|
||||
ctx.drawImage(image, 0, height, width, width);
|
||||
ctx.drawImage(image, width, height, width, width);
|
||||
return this.mergedFog.canvas;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
export * from './cloud';
|
||||
export * from './cloudLike';
|
||||
export * from './fog';
|
||||
export * from './rain';
|
||||
export * from './snow';
|
||||
export * from './sun';
|
||||
@@ -0,0 +1,221 @@
|
||||
import {
|
||||
ShaderProgram,
|
||||
IShaderUniform,
|
||||
UniformType,
|
||||
MotaOffscreenCanvas2D,
|
||||
GL2
|
||||
} from '@motajs/render';
|
||||
import { Weather } from '../weather';
|
||||
|
||||
const rainVs = /* glsl */ `
|
||||
in vec2 a_rainVertex;
|
||||
in vec2 a_offset; // 雨滴的中心位置
|
||||
in vec4 a_data; // x: 雨滴宽度; y: 雨滴长度; z: 雨滴旋转角度,0表示向下,逆时针为正;
|
||||
// w: 属于哪一种雨,需要两种雨反复循环,一种无法实现循环,0表示第一种,1表示第二种
|
||||
|
||||
uniform float u_progress; // 下雨进度,从最上落到最下是0.5个进度,以保证不会出现奇怪的问题
|
||||
|
||||
out vec2 v_center;
|
||||
out vec2 v_data; // 雨滴宽度与高度
|
||||
out vec2 v_pos;
|
||||
|
||||
mat2 createScaleMatrix(float x, float y) {
|
||||
return mat2(
|
||||
x, 0,
|
||||
0, y
|
||||
);
|
||||
}
|
||||
|
||||
vec2 getOffsetByProgress(vec2 offset) {
|
||||
if (a_data.w == 0.0) {
|
||||
if (u_progress < 0.5) {
|
||||
return offset * u_progress * 2.0;
|
||||
} else {
|
||||
return offset * (u_progress - 1.0) * 2.0;
|
||||
}
|
||||
} else {
|
||||
return offset * u_progress * 2.0;
|
||||
}
|
||||
}
|
||||
|
||||
void main() {
|
||||
float cosTheta = cos(a_data.z);
|
||||
float sinTheta = sin(a_data.z);
|
||||
mat2 rotate = mat2(
|
||||
cosTheta, -sinTheta,
|
||||
sinTheta, cosTheta
|
||||
);
|
||||
vec2 offset = getOffsetByProgress(vec2(-sinTheta * 2.0, -cosTheta * 2.0));
|
||||
mat2 scale = createScaleMatrix(a_data.x, a_data.y);
|
||||
vec2 off = a_offset + offset;
|
||||
vec2 pos = rotate * scale * a_rainVertex + off;
|
||||
v_center = off;
|
||||
v_pos = pos;
|
||||
gl_Position = vec4(pos, 0.0, 1.0);
|
||||
}
|
||||
`;
|
||||
|
||||
const rainFs = /* glsl */ `
|
||||
in vec2 v_center;
|
||||
in vec2 v_data; // 雨滴的宽度与长度
|
||||
in vec2 v_pos;
|
||||
|
||||
uniform vec4 u_color; // 雨滴的颜色
|
||||
|
||||
out vec4 outColor;
|
||||
|
||||
float random(vec2 uv) {
|
||||
return fract(sin(dot(uv, vec2(12.9898, 78.233))) * 43758.5453);
|
||||
}
|
||||
|
||||
void main() {
|
||||
float ran = random(v_pos);
|
||||
vec2 pos = vec2(v_pos.x + ran * 0.01, v_pos.y);
|
||||
vec2 texPos = (pos + 1.0) / 2.0;
|
||||
texPos.y = 1.0 - texPos.y;
|
||||
vec4 tex = texture(u_sampler, texPos);
|
||||
outColor = mix(u_color, tex, 0.75);
|
||||
}
|
||||
`;
|
||||
|
||||
/** 雨滴顶点坐标 */
|
||||
const vertex = new Float32Array([-1, -1, 1, -1, -1, 1, 1, 1]);
|
||||
|
||||
interface RainCreateData {
|
||||
/** 进度变量 */
|
||||
readonly uProgress: IShaderUniform<UniformType.Uniform1f> | null;
|
||||
/** 下雨着色器程序 */
|
||||
readonly program: ShaderProgram;
|
||||
}
|
||||
|
||||
class RainShader extends GL2 {
|
||||
/** 下雨程序 */
|
||||
private rainProgram: ShaderProgram | null = null;
|
||||
/** 背景程序 */
|
||||
private backProgram: ShaderProgram | null = null;
|
||||
|
||||
create(level: number): RainCreateData {
|
||||
const gl = this.gl;
|
||||
const program = this.createProgram(ShaderProgram);
|
||||
program.fs(rainFs);
|
||||
program.vs(rainVs);
|
||||
program.requestCompile();
|
||||
this.useProgram(program);
|
||||
const pos = program.defineAttribArray('a_rainVertex');
|
||||
program.defineAttribArray('a_offset');
|
||||
program.defineAttribArray('a_data');
|
||||
program.defineUniform('u_color', this.UNIFORM_4f);
|
||||
const uProgress = program.defineUniform('u_progress', this.UNIFORM_1f);
|
||||
program.mode(this.DRAW_ARRAYS_INSTANCED);
|
||||
|
||||
if (pos) {
|
||||
pos.buffer(vertex, gl.STATIC_DRAW);
|
||||
pos.pointer(2, gl.FLOAT, false, 0, 0);
|
||||
pos.enable();
|
||||
}
|
||||
|
||||
program.paramArraysInstanced(gl.TRIANGLE_STRIP, 0, 4, 100 * level);
|
||||
|
||||
this.generateRainPath(level, program);
|
||||
this.rainProgram = program;
|
||||
this.backProgram = this.createProgram(ShaderProgram);
|
||||
this.backProgram.requestCompile();
|
||||
|
||||
return { uProgress, program };
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成雨滴
|
||||
* @param num 雨滴数量
|
||||
*/
|
||||
generateRainPath(level: number, program: ShaderProgram) {
|
||||
const num = level * 100;
|
||||
const angle = (((Math.random() - 0.5) * Math.PI) / 30) * level;
|
||||
const deviation = (Math.PI / 180) * (12 - level);
|
||||
|
||||
const aOffset = program.getAttribArray('a_offset');
|
||||
const aData = program.getAttribArray('a_data');
|
||||
const color = program.getUniform<UniformType.Uniform4f>('u_color');
|
||||
const gl = this.gl;
|
||||
if (!aOffset || !aData || !color) return;
|
||||
|
||||
const tan = Math.tan(angle);
|
||||
|
||||
const offset = new Float32Array(num * 2);
|
||||
const data = new Float32Array(num * 4);
|
||||
const half = num / 2;
|
||||
for (let i = 0; i < half; i++) {
|
||||
const ox = Math.random() * 3 - 1.5;
|
||||
const oy = Math.random() * 2 - 1;
|
||||
const rad = angle + (Math.random() - 0.5) * Math.PI * deviation;
|
||||
const length = Math.random() * 0.05 + 0.03;
|
||||
const width = Math.random() * 0.002 + 0.002;
|
||||
offset.set([ox, oy], i * 2);
|
||||
data.set([width, length, rad, 0], i * 4);
|
||||
}
|
||||
for (let i = half; i < num; i++) {
|
||||
const ox = Math.random() * 3 - 1.5 + tan * 2;
|
||||
const oy = Math.random() * 2 + 1;
|
||||
const rad = angle + (Math.random() - 0.5) * Math.PI * deviation;
|
||||
const length = Math.random() * 0.05 + 0.03;
|
||||
const width = Math.random() * 0.002 + 0.002;
|
||||
offset.set([ox, oy], i * 2);
|
||||
data.set([width, length, rad, 1], i * 4);
|
||||
}
|
||||
|
||||
aOffset.buffer(offset, gl.STATIC_DRAW);
|
||||
aData.buffer(data, gl.STATIC_DRAW);
|
||||
aOffset.pointer(2, gl.FLOAT, false, 0, 0);
|
||||
aOffset.divisor(1);
|
||||
aOffset.enable();
|
||||
aData.pointer(4, gl.FLOAT, false, 0, 0);
|
||||
aData.divisor(1);
|
||||
aData.enable();
|
||||
|
||||
color.set(1, 1, 1, 1);
|
||||
}
|
||||
|
||||
protected drawScene(
|
||||
canvas: MotaOffscreenCanvas2D,
|
||||
gl: WebGL2RenderingContext
|
||||
): void {
|
||||
const program1 = this.backProgram;
|
||||
const program2 = this.rainProgram;
|
||||
if (!program1 || !program2) return;
|
||||
this.useProgram(program1);
|
||||
program1.texTexture('u_sampler', canvas.canvas);
|
||||
this.draw(gl, program1);
|
||||
this.useProgram(program2);
|
||||
program2.texTexture('u_sampler', canvas.canvas);
|
||||
this.draw(gl, program2);
|
||||
}
|
||||
}
|
||||
|
||||
export class RainWeather extends Weather<GL2> {
|
||||
/** 下雨流程的 uniform 变量 */
|
||||
private progress: IShaderUniform<UniformType.Uniform1f> | null = null;
|
||||
/** 下雨着色器程序 */
|
||||
private program: ShaderProgram | null = null;
|
||||
/** 一个周期的时长 */
|
||||
private duration: number = 1;
|
||||
|
||||
createElement(level: number): GL2 {
|
||||
const shader = new RainShader();
|
||||
shader.setHD(true);
|
||||
const { uProgress, program } = shader.create(level);
|
||||
this.progress = uProgress;
|
||||
this.program = program;
|
||||
this.duration = 5000 - 300 * this.level;
|
||||
return shader;
|
||||
}
|
||||
|
||||
tick(timestamp: number): void {
|
||||
if (!this.element || !this.program) return;
|
||||
this.element.update();
|
||||
const time = this.duration;
|
||||
const progress = (timestamp % time) / time;
|
||||
this.progress?.set(progress);
|
||||
}
|
||||
|
||||
onDestroy(): void {}
|
||||
}
|
||||
@@ -0,0 +1,230 @@
|
||||
import {
|
||||
ShaderProgram,
|
||||
IShaderUniform,
|
||||
UniformType,
|
||||
MotaOffscreenCanvas2D,
|
||||
GL2
|
||||
} from '@motajs/render';
|
||||
import { Weather } from '../weather';
|
||||
|
||||
const snowVs = /* glsl */ `
|
||||
in vec2 a_snowVertex;
|
||||
in vec2 a_offset; // 雪花的中心位置
|
||||
in vec4 a_data; // x: 雪花直径; y: 留空; z: 雨滴旋转角度,0表示向下,逆时针为正;
|
||||
// w: 属于哪一种雪,需要两种雪反复循环,一种无法实现循环,0表示第一种,1表示第二种
|
||||
|
||||
uniform float u_progress; // 下雪进度,从最上落到最下是0.5个进度,以保证不会出现奇怪的问题
|
||||
|
||||
out vec2 v_center;
|
||||
out vec2 v_data; // 雪花宽度与高度
|
||||
out vec2 v_pos;
|
||||
|
||||
mat2 createScaleMatrix(float x, float y) {
|
||||
return mat2(
|
||||
x, 0,
|
||||
0, y
|
||||
);
|
||||
}
|
||||
|
||||
vec2 getOffsetByProgress(vec2 offset) {
|
||||
if (a_data.w == 0.0) {
|
||||
if (u_progress < 0.5) {
|
||||
return offset * u_progress * 2.0;
|
||||
} else {
|
||||
return offset * (u_progress - 1.0) * 2.0;
|
||||
}
|
||||
} else {
|
||||
return offset * u_progress * 2.0;
|
||||
}
|
||||
}
|
||||
|
||||
void main() {
|
||||
float cosTheta = cos(a_data.z);
|
||||
float sinTheta = sin(a_data.z);
|
||||
mat2 rotate = mat2(
|
||||
cosTheta, -sinTheta,
|
||||
sinTheta, cosTheta
|
||||
);
|
||||
vec2 offset = getOffsetByProgress(vec2(-sinTheta * 2.0, -cosTheta * 2.0));
|
||||
mat2 scale = createScaleMatrix(a_data.x, a_data.x);
|
||||
vec2 off = a_offset + offset;
|
||||
vec2 pos = rotate * scale * a_snowVertex + off;
|
||||
v_center = off;
|
||||
v_pos = pos;
|
||||
gl_Position = vec4(pos, 0.0, 1.0);
|
||||
}
|
||||
`;
|
||||
|
||||
const snowFs = /* glsl */ `
|
||||
in vec2 v_center;
|
||||
in vec2 v_data; // 雪花的宽度与长度
|
||||
in vec2 v_pos;
|
||||
|
||||
uniform vec4 u_color; // 雪花的颜色
|
||||
|
||||
out vec4 outColor;
|
||||
|
||||
void main() {
|
||||
vec2 pos = vec2(v_pos.x, v_pos.y);
|
||||
vec2 texPos = (pos + 1.0) / 2.0;
|
||||
texPos.y = 1.0 - texPos.y;
|
||||
vec4 tex = texture(u_sampler, texPos);
|
||||
outColor = mix(u_color, tex, 0.2);
|
||||
}
|
||||
`;
|
||||
|
||||
const CIRCLE_POINTS = 8;
|
||||
|
||||
function generateCircle(points: number) {
|
||||
const array: number[] = [0, 0];
|
||||
for (let i = 0; i < points; i++) {
|
||||
const rad = Math.PI * 2 - (Math.PI * 2 * i) / points;
|
||||
array.push(Math.cos(rad), Math.sin(rad));
|
||||
}
|
||||
array.push(Math.cos(0), Math.sin(0));
|
||||
return array;
|
||||
}
|
||||
|
||||
/** 雪花顶点坐标 */
|
||||
const vertex = new Float32Array(generateCircle(CIRCLE_POINTS));
|
||||
|
||||
interface SnowCreateData {
|
||||
/** 进度变量 */
|
||||
readonly uProgress: IShaderUniform<UniformType.Uniform1f> | null;
|
||||
/** 下雪着色器程序 */
|
||||
readonly program: ShaderProgram;
|
||||
}
|
||||
|
||||
class SnowShader extends GL2 {
|
||||
/** 下雪程序 */
|
||||
private snowProgram: ShaderProgram | null = null;
|
||||
/** 背景程序 */
|
||||
private backProgram: ShaderProgram | null = null;
|
||||
|
||||
create(level: number): SnowCreateData {
|
||||
const gl = this.gl;
|
||||
const program = this.createProgram(ShaderProgram);
|
||||
program.fs(snowFs);
|
||||
program.vs(snowVs);
|
||||
program.requestCompile();
|
||||
this.useProgram(program);
|
||||
const pos = program.defineAttribArray('a_snowVertex');
|
||||
program.defineAttribArray('a_offset');
|
||||
program.defineAttribArray('a_data');
|
||||
program.defineUniform('u_color', this.UNIFORM_4f);
|
||||
const uProgress = program.defineUniform('u_progress', this.UNIFORM_1f);
|
||||
program.mode(this.DRAW_ARRAYS_INSTANCED);
|
||||
|
||||
if (pos) {
|
||||
pos.buffer(vertex, gl.STATIC_DRAW);
|
||||
pos.pointer(2, gl.FLOAT, false, 0, 0);
|
||||
pos.enable();
|
||||
}
|
||||
|
||||
program.paramArraysInstanced(
|
||||
gl.TRIANGLE_FAN,
|
||||
0,
|
||||
CIRCLE_POINTS * 2 + 4,
|
||||
200 * level
|
||||
);
|
||||
|
||||
this.generateSnowPath(level, program);
|
||||
this.snowProgram = program;
|
||||
this.backProgram = this.createProgram(ShaderProgram);
|
||||
this.backProgram.requestCompile();
|
||||
|
||||
return { uProgress, program };
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成雪花
|
||||
* @param level 天气等级
|
||||
*/
|
||||
generateSnowPath(level: number, program: ShaderProgram) {
|
||||
const num = level * 200;
|
||||
const angle = (((Math.random() - 0.5) * Math.PI) / 30) * level;
|
||||
const deviation = (Math.PI / 180) * (12 - level);
|
||||
|
||||
const aOffset = program.getAttribArray('a_offset');
|
||||
const aData = program.getAttribArray('a_data');
|
||||
const color = program.getUniform<UniformType.Uniform4f>('u_color');
|
||||
const gl = this.gl;
|
||||
if (!aOffset || !aData || !color) return;
|
||||
|
||||
const tan = Math.tan(angle);
|
||||
|
||||
const offset = new Float32Array(num * 2);
|
||||
const data = new Float32Array(num * 4);
|
||||
const half = num / 2;
|
||||
for (let i = 0; i < half; i++) {
|
||||
const ox = Math.random() * 3 - 1.5;
|
||||
const oy = Math.random() * 2 - 1;
|
||||
const rad = angle + (Math.random() - 0.5) * Math.PI * deviation;
|
||||
const diameter = Math.random() * 0.002 + 0.005;
|
||||
offset.set([ox, oy], i * 2);
|
||||
data.set([diameter, 0, rad, 0], i * 4);
|
||||
}
|
||||
for (let i = half; i < num; i++) {
|
||||
const ox = Math.random() * 3 - 1.5 + tan * 2;
|
||||
const oy = Math.random() * 2 + 1;
|
||||
const rad = angle + (Math.random() - 0.5) * Math.PI * deviation;
|
||||
const diameter = Math.random() * 0.002 + 0.005;
|
||||
offset.set([ox, oy], i * 2);
|
||||
data.set([diameter, 0, rad, 1], i * 4);
|
||||
}
|
||||
|
||||
aOffset.buffer(offset, gl.STATIC_DRAW);
|
||||
aData.buffer(data, gl.STATIC_DRAW);
|
||||
aOffset.pointer(2, gl.FLOAT, false, 0, 0);
|
||||
aOffset.divisor(1);
|
||||
aOffset.enable();
|
||||
aData.pointer(4, gl.FLOAT, false, 0, 0);
|
||||
aData.divisor(1);
|
||||
aData.enable();
|
||||
|
||||
color.set(1, 1, 1, 0.8);
|
||||
}
|
||||
|
||||
protected drawScene(
|
||||
canvas: MotaOffscreenCanvas2D,
|
||||
gl: WebGL2RenderingContext
|
||||
): void {
|
||||
const program1 = this.backProgram;
|
||||
const program2 = this.snowProgram;
|
||||
if (!program1 || !program2) return;
|
||||
this.useProgram(program1);
|
||||
program1.texTexture('u_sampler', canvas.canvas);
|
||||
this.draw(gl, program1);
|
||||
this.useProgram(program2);
|
||||
this.draw(gl, program2);
|
||||
}
|
||||
}
|
||||
|
||||
export class SnowWeather extends Weather<GL2> {
|
||||
/** 下雪流程的 uniform 变量 */
|
||||
private progress: IShaderUniform<UniformType.Uniform1f> | null = null;
|
||||
/** 下雪着色器程序 */
|
||||
private program: ShaderProgram | null = null;
|
||||
/** 一个周期的时长 */
|
||||
private duration: number = 1;
|
||||
|
||||
createElement(level: number): GL2 {
|
||||
const shader = new SnowShader();
|
||||
shader.setHD(true);
|
||||
const { uProgress, program } = shader.create(level);
|
||||
this.progress = uProgress;
|
||||
this.program = program;
|
||||
this.duration = 15000 - 800 * this.level;
|
||||
return shader;
|
||||
}
|
||||
|
||||
tick(timestamp: number): void {
|
||||
if (!this.element || !this.program) return;
|
||||
this.element.update();
|
||||
const time = this.duration;
|
||||
const progress = (timestamp % time) / time;
|
||||
this.progress?.set(progress);
|
||||
}
|
||||
|
||||
onDestroy(): void {}
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
import { MotaOffscreenCanvas2D, Sprite } from '@motajs/render-core';
|
||||
import { Weather } from '../weather';
|
||||
import { clamp } from 'lodash-es';
|
||||
|
||||
export class SunWeather extends Weather<Sprite> {
|
||||
/** 阳光图片 */
|
||||
private image: HTMLImageElement | null = null;
|
||||
/** 阳光图片的不透明度 */
|
||||
private alpha: number = 0;
|
||||
/** 阳光的最大不透明度 */
|
||||
private maxAlpha: number = 0;
|
||||
/** 阳光的最小不透明度 */
|
||||
private minAlpha: number = 0;
|
||||
/** 不透明度变化率 */
|
||||
private va: number = 0;
|
||||
/** 上一帧的时刻 */
|
||||
private lastTick: number = 0;
|
||||
|
||||
drawSun(canvas: MotaOffscreenCanvas2D) {
|
||||
if (!this.image) return;
|
||||
const ctx = canvas.ctx;
|
||||
ctx.globalAlpha = this.alpha;
|
||||
ctx.drawImage(this.image, 0, 0, canvas.width, canvas.height);
|
||||
}
|
||||
|
||||
tick(timestamp: number): void {
|
||||
this.element?.update();
|
||||
const dt = timestamp - this.lastTick;
|
||||
this.lastTick = timestamp;
|
||||
if (dt > 100) return;
|
||||
const aa = (Math.random() - 0.5) * this.level;
|
||||
this.va += (aa * dt) / 1000;
|
||||
this.va = clamp(this.va, 0.1);
|
||||
this.alpha += (this.va * dt) / 1000;
|
||||
if (this.alpha < this.minAlpha) {
|
||||
this.va = Math.abs(this.va);
|
||||
this.alpha = this.minAlpha;
|
||||
} else if (this.alpha > this.maxAlpha) {
|
||||
this.va = -Math.abs(this.va);
|
||||
this.alpha = this.maxAlpha;
|
||||
}
|
||||
}
|
||||
|
||||
createElement(level: number): Sprite {
|
||||
const element = new Sprite('static', true);
|
||||
element.setRenderFn(canvas => this.drawSun(canvas));
|
||||
this.maxAlpha = level / 10;
|
||||
this.minAlpha = level / 20;
|
||||
this.alpha = (this.maxAlpha + this.minAlpha) / 2;
|
||||
this.image = core.material.images.images['sun.png'];
|
||||
return element;
|
||||
}
|
||||
|
||||
onDestroy(): void {}
|
||||
}
|
||||
@@ -0,0 +1,87 @@
|
||||
import { RenderItem } from '@motajs/render-core';
|
||||
|
||||
export interface IWeather<T extends RenderItem = RenderItem> {
|
||||
/** 天气的等级,-1 表示未创建 */
|
||||
readonly level: number;
|
||||
|
||||
/**
|
||||
* 创建天气
|
||||
* @param level 天气等级
|
||||
* @returns 天气的渲染元素
|
||||
*/
|
||||
create(level: number): T;
|
||||
|
||||
/**
|
||||
* 摧毁这个天气
|
||||
*/
|
||||
destroy(): void;
|
||||
|
||||
/**
|
||||
* 每帧执行一次的函数
|
||||
* @param timestamp 当前时间戳
|
||||
*/
|
||||
tick(timestamp: number): void;
|
||||
}
|
||||
|
||||
export interface IWeatherInstance<
|
||||
R extends RenderItem = RenderItem,
|
||||
T extends IWeather<R> = IWeather<R>
|
||||
> {
|
||||
/** 天气对象 */
|
||||
readonly weather: T;
|
||||
/** 天气的渲染元素 */
|
||||
readonly element: R;
|
||||
|
||||
/**
|
||||
* 设置这个天气的纵深
|
||||
* @param zIndex 纵深
|
||||
*/
|
||||
setZIndex(zIndex: number): void;
|
||||
}
|
||||
|
||||
export interface IWeatherController {
|
||||
/** 天气控制器绑定到的渲染元素 */
|
||||
readonly container: RenderItem | null;
|
||||
/** 所有已激活的天气 */
|
||||
readonly active: Set<IWeatherInstance>;
|
||||
|
||||
/**
|
||||
* 绑定控制器到渲染元素
|
||||
* @param container 绑定的渲染元素,需要能够添加子元素,一般绑定到 `container`
|
||||
*/
|
||||
bind(container: RenderItem): void;
|
||||
|
||||
/**
|
||||
* 使用天气名称添加天气
|
||||
* @param weather 天气的名称
|
||||
* @param level 天气的等级
|
||||
* @returns 天气实例
|
||||
*/
|
||||
activate(weather: string, level?: number): IWeatherInstance | null;
|
||||
/**
|
||||
* 使用天气对象添加天气
|
||||
* @param weather 天气对象
|
||||
* @param level 天气的等级
|
||||
* @returns 天气实例
|
||||
*/
|
||||
activate<R extends RenderItem, T extends IWeather<R>>(
|
||||
weather: T,
|
||||
level?: number
|
||||
): IWeatherInstance<R, T> | null;
|
||||
|
||||
/**
|
||||
* 取消天气
|
||||
* @param instance 天气实例
|
||||
*/
|
||||
deactivate(instance: IWeatherInstance): void;
|
||||
|
||||
/**
|
||||
* 清空天气
|
||||
*/
|
||||
clearWeather(): void;
|
||||
|
||||
/**
|
||||
* 摧毁这个控制器
|
||||
*/
|
||||
destroy(): void;
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
import { RenderItem } from '@motajs/render-core';
|
||||
import { IWeather } from './types';
|
||||
|
||||
export abstract class Weather<T extends RenderItem> implements IWeather<T> {
|
||||
level: number = -1;
|
||||
|
||||
protected element: T | null = null;
|
||||
|
||||
create(level: number): T {
|
||||
this.level = level;
|
||||
const element = this.createElement(level);
|
||||
this.element = element;
|
||||
return element;
|
||||
}
|
||||
|
||||
destroy(): void {
|
||||
this.element?.destroy();
|
||||
this.onDestroy();
|
||||
}
|
||||
|
||||
abstract tick(timestamp: number): void;
|
||||
|
||||
/**
|
||||
* 创建天气的渲染元素,并进行初始化
|
||||
* @param level 天气的等级
|
||||
*/
|
||||
abstract createElement(level: number): T;
|
||||
|
||||
/**
|
||||
* 当天气被摧毁时调用,进行清理工作,会自动摧毁渲染元素,不需要手动处理
|
||||
*/
|
||||
abstract onDestroy(): void;
|
||||
}
|
||||
Reference in new issue
Block a user