2022-06-02 01:29:03 +04:00
|
|
|
import { Component, ElementRef, ViewChild, HostListener, Input, Output, EventEmitter, OnInit,
|
|
|
|
OnDestroy, OnChanges, ChangeDetectionStrategy, NgZone, AfterViewInit } from '@angular/core';
|
2022-05-30 17:29:30 +00:00
|
|
|
import { StateService } from 'src/app/services/state.service';
|
2022-06-05 23:40:36 +04:00
|
|
|
import { MempoolBlockDelta, TransactionStripped } from 'src/app/interfaces/websocket.interface';
|
|
|
|
import { Subscription, BehaviorSubject, merge, of } from 'rxjs';
|
|
|
|
import { switchMap, filter } from 'rxjs/operators';
|
2022-05-30 17:29:30 +00:00
|
|
|
import { WebsocketService } from 'src/app/services/websocket.service';
|
2022-05-30 21:53:39 +00:00
|
|
|
import { FastVertexArray } from './fast-vertex-array';
|
|
|
|
import BlockScene from './block-scene';
|
|
|
|
import TxSprite from './tx-sprite';
|
|
|
|
import TxView from './tx-view';
|
2022-05-30 17:29:30 +00:00
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'app-mempool-block-overview',
|
|
|
|
templateUrl: './mempool-block-overview.component.html',
|
2022-05-30 21:53:39 +00:00
|
|
|
styleUrls: ['./mempool-block-overview.component.scss'],
|
2022-05-30 17:29:30 +00:00
|
|
|
changeDetection: ChangeDetectionStrategy.OnPush,
|
|
|
|
})
|
2022-06-02 01:29:03 +04:00
|
|
|
export class MempoolBlockOverviewComponent implements OnInit, OnDestroy, OnChanges, AfterViewInit {
|
2022-05-30 17:29:30 +00:00
|
|
|
@Input() index: number;
|
2022-06-02 01:29:03 +04:00
|
|
|
@Output() txPreviewEvent = new EventEmitter<TransactionStripped | void>();
|
2022-05-30 17:29:30 +00:00
|
|
|
|
2022-05-30 21:53:39 +00:00
|
|
|
@ViewChild('blockCanvas')
|
|
|
|
canvas: ElementRef<HTMLCanvasElement>;
|
|
|
|
|
|
|
|
gl: WebGLRenderingContext;
|
|
|
|
animationFrameRequest: number;
|
|
|
|
displayWidth: number;
|
|
|
|
displayHeight: number;
|
2022-06-12 18:15:30 +00:00
|
|
|
cssWidth: number;
|
|
|
|
cssHeight: number;
|
2022-05-30 21:53:39 +00:00
|
|
|
shaderProgram: WebGLProgram;
|
|
|
|
vertexArray: FastVertexArray;
|
|
|
|
running: boolean;
|
|
|
|
scene: BlockScene;
|
2022-05-31 14:16:35 +00:00
|
|
|
hoverTx: TxView | void;
|
|
|
|
selectedTx: TxView | void;
|
2022-05-30 21:53:39 +00:00
|
|
|
lastBlockHeight: number;
|
|
|
|
blockIndex: number;
|
2022-05-31 22:25:09 +00:00
|
|
|
isLoading$ = new BehaviorSubject<boolean>(true);
|
2022-05-30 21:53:39 +00:00
|
|
|
|
2022-05-31 21:36:42 +00:00
|
|
|
blockSub: Subscription;
|
|
|
|
deltaSub: Subscription;
|
2022-05-30 17:29:30 +00:00
|
|
|
|
|
|
|
constructor(
|
|
|
|
public stateService: StateService,
|
|
|
|
private websocketService: WebsocketService,
|
2022-06-02 01:29:03 +04:00
|
|
|
readonly ngZone: NgZone,
|
2022-05-30 21:53:39 +00:00
|
|
|
) {
|
2022-06-02 01:29:03 +04:00
|
|
|
this.vertexArray = new FastVertexArray(512, TxSprite.dataSize);
|
2022-05-30 21:53:39 +00:00
|
|
|
}
|
2022-05-30 17:29:30 +00:00
|
|
|
|
|
|
|
ngOnInit(): void {
|
2022-06-05 23:40:36 +04:00
|
|
|
this.blockSub = merge(
|
|
|
|
of(true),
|
|
|
|
this.stateService.connectionState$.pipe(filter((state) => state === 2))
|
|
|
|
)
|
|
|
|
.pipe(switchMap(() => this.stateService.mempoolBlockTransactions$))
|
|
|
|
.subscribe((transactionsStripped) => {
|
|
|
|
this.replaceBlock(transactionsStripped);
|
|
|
|
});
|
2022-05-31 21:36:42 +00:00
|
|
|
this.deltaSub = this.stateService.mempoolBlockDelta$.subscribe((delta) => {
|
2022-06-02 01:29:03 +04:00
|
|
|
this.updateBlock(delta);
|
|
|
|
});
|
2022-05-30 17:29:30 +00:00
|
|
|
}
|
|
|
|
|
2022-05-30 21:53:39 +00:00
|
|
|
ngAfterViewInit(): void {
|
2022-06-02 01:29:03 +04:00
|
|
|
this.canvas.nativeElement.addEventListener('webglcontextlost', this.handleContextLost, false);
|
|
|
|
this.canvas.nativeElement.addEventListener('webglcontextrestored', this.handleContextRestored, false);
|
|
|
|
this.gl = this.canvas.nativeElement.getContext('webgl');
|
|
|
|
this.initCanvas();
|
2022-05-30 21:53:39 +00:00
|
|
|
|
2022-06-02 01:29:03 +04:00
|
|
|
this.resizeCanvas();
|
2022-05-30 21:53:39 +00:00
|
|
|
}
|
|
|
|
|
2022-05-30 17:29:30 +00:00
|
|
|
ngOnChanges(changes): void {
|
|
|
|
if (changes.index) {
|
2022-06-02 01:29:03 +04:00
|
|
|
this.clearBlock(changes.index.currentValue > changes.index.previousValue ? 'right' : 'left');
|
|
|
|
this.isLoading$.next(true);
|
2022-05-30 17:29:30 +00:00
|
|
|
this.websocketService.startTrackMempoolBlock(changes.index.currentValue);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ngOnDestroy(): void {
|
2022-05-31 21:36:42 +00:00
|
|
|
this.blockSub.unsubscribe();
|
|
|
|
this.deltaSub.unsubscribe();
|
2022-05-30 17:29:30 +00:00
|
|
|
this.websocketService.stopTrackMempoolBlock();
|
|
|
|
}
|
|
|
|
|
2022-05-31 22:25:09 +00:00
|
|
|
clearBlock(direction): void {
|
|
|
|
if (this.scene) {
|
2022-06-02 01:29:03 +04:00
|
|
|
this.scene.exit(direction);
|
2022-05-31 22:25:09 +00:00
|
|
|
}
|
2022-06-02 01:29:03 +04:00
|
|
|
this.hoverTx = null;
|
|
|
|
this.selectedTx = null;
|
|
|
|
this.txPreviewEvent.emit(null);
|
2022-05-31 22:25:09 +00:00
|
|
|
}
|
|
|
|
|
2022-06-02 01:29:03 +04:00
|
|
|
replaceBlock(transactionsStripped: TransactionStripped[]): void {
|
2022-05-31 21:36:42 +00:00
|
|
|
if (!this.scene) {
|
2022-06-02 01:29:03 +04:00
|
|
|
this.scene = new BlockScene({ width: this.displayWidth, height: this.displayHeight, resolution: 75,
|
|
|
|
blockLimit: this.stateService.blockVSize, vertexArray: this.vertexArray });
|
2022-05-31 21:36:42 +00:00
|
|
|
}
|
2022-06-02 01:29:03 +04:00
|
|
|
const blockMined = (this.stateService.latestBlockHeight > this.lastBlockHeight);
|
|
|
|
if (this.blockIndex !== this.index) {
|
|
|
|
const direction = (this.blockIndex == null || this.index < this.blockIndex) ? 'left' : 'right';
|
|
|
|
this.scene.enter(transactionsStripped, direction);
|
2022-05-31 21:36:42 +00:00
|
|
|
} else {
|
2022-06-02 01:29:03 +04:00
|
|
|
this.scene.replace(transactionsStripped, blockMined ? 'right' : 'left');
|
2022-05-30 21:53:39 +00:00
|
|
|
}
|
2022-05-31 21:36:42 +00:00
|
|
|
|
2022-06-02 01:29:03 +04:00
|
|
|
this.lastBlockHeight = this.stateService.latestBlockHeight;
|
|
|
|
this.blockIndex = this.index;
|
|
|
|
this.isLoading$.next(false);
|
2022-05-30 21:53:39 +00:00
|
|
|
}
|
|
|
|
|
2022-05-31 21:36:42 +00:00
|
|
|
updateBlock(delta: MempoolBlockDelta): void {
|
2022-05-30 21:53:39 +00:00
|
|
|
if (!this.scene) {
|
2022-06-02 01:29:03 +04:00
|
|
|
this.scene = new BlockScene({ width: this.displayWidth, height: this.displayHeight, resolution: 75,
|
|
|
|
blockLimit: this.stateService.blockVSize, vertexArray: this.vertexArray });
|
2022-05-30 21:53:39 +00:00
|
|
|
}
|
2022-06-02 01:29:03 +04:00
|
|
|
const blockMined = (this.stateService.latestBlockHeight > this.lastBlockHeight);
|
|
|
|
|
|
|
|
if (this.blockIndex !== this.index) {
|
|
|
|
const direction = (this.blockIndex == null || this.index < this.blockIndex) ? 'left' : 'right';
|
|
|
|
this.scene.exit(direction);
|
|
|
|
this.scene = new BlockScene({ width: this.displayWidth, height: this.displayHeight, resolution: 75,
|
|
|
|
blockLimit: this.stateService.blockVSize, vertexArray: this.vertexArray });
|
|
|
|
this.scene.enter(delta.added, direction);
|
2022-05-30 21:53:39 +00:00
|
|
|
} else {
|
2022-06-02 01:29:03 +04:00
|
|
|
this.scene.update(delta.added, delta.removed, blockMined ? 'right' : 'left', blockMined);
|
2022-05-30 21:53:39 +00:00
|
|
|
}
|
|
|
|
|
2022-06-02 01:29:03 +04:00
|
|
|
this.lastBlockHeight = this.stateService.latestBlockHeight;
|
|
|
|
this.blockIndex = this.index;
|
|
|
|
this.isLoading$.next(false);
|
2022-05-30 17:29:30 +00:00
|
|
|
}
|
2022-05-30 21:53:39 +00:00
|
|
|
|
2022-06-02 01:29:03 +04:00
|
|
|
initCanvas(): void {
|
|
|
|
this.gl.clearColor(0.0, 0.0, 0.0, 0.0);
|
|
|
|
this.gl.clear(this.gl.COLOR_BUFFER_BIT);
|
2022-05-30 21:53:39 +00:00
|
|
|
|
|
|
|
const shaderSet = [
|
|
|
|
{
|
|
|
|
type: this.gl.VERTEX_SHADER,
|
|
|
|
src: vertShaderSrc
|
|
|
|
},
|
|
|
|
{
|
|
|
|
type: this.gl.FRAGMENT_SHADER,
|
|
|
|
src: fragShaderSrc
|
|
|
|
}
|
2022-06-02 01:29:03 +04:00
|
|
|
];
|
2022-05-30 21:53:39 +00:00
|
|
|
|
2022-06-02 01:29:03 +04:00
|
|
|
this.shaderProgram = this.buildShaderProgram(shaderSet);
|
2022-05-30 21:53:39 +00:00
|
|
|
|
2022-06-02 01:29:03 +04:00
|
|
|
this.gl.useProgram(this.shaderProgram);
|
2022-05-30 21:53:39 +00:00
|
|
|
|
|
|
|
// Set up alpha blending
|
|
|
|
this.gl.enable(this.gl.BLEND);
|
|
|
|
this.gl.blendFunc(this.gl.ONE, this.gl.ONE_MINUS_SRC_ALPHA);
|
|
|
|
|
2022-06-02 01:29:03 +04:00
|
|
|
const glBuffer = this.gl.createBuffer();
|
|
|
|
this.gl.bindBuffer(this.gl.ARRAY_BUFFER, glBuffer);
|
2022-05-30 21:53:39 +00:00
|
|
|
|
|
|
|
/* SET UP SHADER ATTRIBUTES */
|
|
|
|
Object.keys(attribs).forEach((key, i) => {
|
2022-06-02 01:29:03 +04:00
|
|
|
attribs[key].pointer = this.gl.getAttribLocation(this.shaderProgram, key);
|
2022-05-30 21:53:39 +00:00
|
|
|
this.gl.enableVertexAttribArray(attribs[key].pointer);
|
2022-06-02 01:29:03 +04:00
|
|
|
});
|
2022-05-30 21:53:39 +00:00
|
|
|
|
2022-06-02 01:29:03 +04:00
|
|
|
this.start();
|
2022-05-30 21:53:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
handleContextLost(event): void {
|
2022-06-02 01:29:03 +04:00
|
|
|
event.preventDefault();
|
|
|
|
cancelAnimationFrame(this.animationFrameRequest);
|
|
|
|
this.animationFrameRequest = null;
|
|
|
|
this.running = false;
|
2022-05-30 21:53:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
handleContextRestored(event): void {
|
2022-06-02 01:29:03 +04:00
|
|
|
this.initCanvas();
|
2022-05-30 21:53:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@HostListener('window:resize', ['$event'])
|
|
|
|
resizeCanvas(): void {
|
2022-06-12 18:15:30 +00:00
|
|
|
this.cssWidth = this.canvas.nativeElement.parentElement.clientWidth;
|
|
|
|
this.cssHeight = this.canvas.nativeElement.parentElement.clientHeight;
|
|
|
|
this.displayWidth = window.devicePixelRatio * this.cssWidth;
|
|
|
|
this.displayHeight = window.devicePixelRatio * this.cssHeight;
|
2022-06-02 01:29:03 +04:00
|
|
|
this.canvas.nativeElement.width = this.displayWidth;
|
|
|
|
this.canvas.nativeElement.height = this.displayHeight;
|
|
|
|
if (this.gl) {
|
2022-06-12 18:15:30 +00:00
|
|
|
this.gl.viewport(0, 0, this.displayWidth, this.displayHeight);
|
2022-06-02 01:29:03 +04:00
|
|
|
}
|
|
|
|
if (this.scene) {
|
|
|
|
this.scene.resize({ width: this.displayWidth, height: this.displayHeight });
|
|
|
|
}
|
2022-05-30 21:53:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
compileShader(src, type): WebGLShader {
|
2022-06-02 01:29:03 +04:00
|
|
|
const shader = this.gl.createShader(type);
|
2022-05-30 21:53:39 +00:00
|
|
|
|
2022-06-02 01:29:03 +04:00
|
|
|
this.gl.shaderSource(shader, src);
|
|
|
|
this.gl.compileShader(shader);
|
2022-05-30 21:53:39 +00:00
|
|
|
|
|
|
|
if (!this.gl.getShaderParameter(shader, this.gl.COMPILE_STATUS)) {
|
2022-06-02 01:29:03 +04:00
|
|
|
console.log(`Error compiling ${type === this.gl.VERTEX_SHADER ? 'vertex' : 'fragment'} shader:`);
|
|
|
|
console.log(this.gl.getShaderInfoLog(shader));
|
2022-05-30 21:53:39 +00:00
|
|
|
}
|
2022-06-02 01:29:03 +04:00
|
|
|
return shader;
|
2022-05-30 21:53:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
buildShaderProgram(shaderInfo): WebGLProgram {
|
2022-06-02 01:29:03 +04:00
|
|
|
const program = this.gl.createProgram();
|
2022-05-30 21:53:39 +00:00
|
|
|
|
|
|
|
shaderInfo.forEach((desc) => {
|
2022-06-02 01:29:03 +04:00
|
|
|
const shader = this.compileShader(desc.src, desc.type);
|
2022-05-30 21:53:39 +00:00
|
|
|
if (shader) {
|
2022-06-02 01:29:03 +04:00
|
|
|
this.gl.attachShader(program, shader);
|
2022-05-30 21:53:39 +00:00
|
|
|
}
|
2022-06-02 01:29:03 +04:00
|
|
|
});
|
2022-05-30 21:53:39 +00:00
|
|
|
|
2022-06-02 01:29:03 +04:00
|
|
|
this.gl.linkProgram(program);
|
2022-05-30 21:53:39 +00:00
|
|
|
|
|
|
|
if (!this.gl.getProgramParameter(program, this.gl.LINK_STATUS)) {
|
2022-06-02 01:29:03 +04:00
|
|
|
console.log('Error linking shader program:');
|
|
|
|
console.log(this.gl.getProgramInfoLog(program));
|
2022-05-30 21:53:39 +00:00
|
|
|
}
|
|
|
|
|
2022-06-02 01:29:03 +04:00
|
|
|
return program;
|
2022-05-30 21:53:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
start(): void {
|
2022-06-02 01:29:03 +04:00
|
|
|
this.running = true;
|
|
|
|
this.ngZone.runOutsideAngular(() => this.run());
|
2022-05-30 21:53:39 +00:00
|
|
|
}
|
|
|
|
|
2022-06-02 01:29:03 +04:00
|
|
|
run(now?: DOMHighResTimeStamp): void {
|
2022-05-30 21:53:39 +00:00
|
|
|
if (!now) {
|
2022-06-02 01:29:03 +04:00
|
|
|
now = performance.now();
|
2022-05-30 21:53:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* SET UP SHADER UNIFORMS */
|
|
|
|
// screen dimensions
|
2022-06-02 01:29:03 +04:00
|
|
|
this.gl.uniform2f(this.gl.getUniformLocation(this.shaderProgram, 'screenSize'), this.displayWidth, this.displayHeight);
|
2022-05-30 21:53:39 +00:00
|
|
|
// frame timestamp
|
2022-06-02 01:29:03 +04:00
|
|
|
this.gl.uniform1f(this.gl.getUniformLocation(this.shaderProgram, 'now'), now);
|
2022-05-30 21:53:39 +00:00
|
|
|
|
|
|
|
/* SET UP SHADER ATTRIBUTES */
|
|
|
|
Object.keys(attribs).forEach((key, i) => {
|
|
|
|
this.gl.vertexAttribPointer(attribs[key].pointer,
|
|
|
|
attribs[key].count, // number of primitives in this attribute
|
|
|
|
this.gl[attribs[key].type], // type of primitive in this attribute (e.g. gl.FLOAT)
|
|
|
|
false, // never normalised
|
|
|
|
stride, // distance between values of the same attribute
|
|
|
|
attribs[key].offset); // offset of the first value
|
2022-06-02 01:29:03 +04:00
|
|
|
});
|
2022-05-30 21:53:39 +00:00
|
|
|
|
2022-06-02 01:29:03 +04:00
|
|
|
const pointArray = this.vertexArray.getVertexData();
|
2022-05-30 21:53:39 +00:00
|
|
|
|
|
|
|
if (pointArray.length) {
|
2022-06-02 01:29:03 +04:00
|
|
|
this.gl.bufferData(this.gl.ARRAY_BUFFER, pointArray, this.gl.DYNAMIC_DRAW);
|
|
|
|
this.gl.drawArrays(this.gl.TRIANGLES, 0, pointArray.length / TxSprite.vertexSize);
|
2022-05-30 21:53:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* LOOP */
|
|
|
|
if (this.running) {
|
|
|
|
if (this.animationFrameRequest) {
|
2022-06-02 01:29:03 +04:00
|
|
|
cancelAnimationFrame(this.animationFrameRequest);
|
|
|
|
this.animationFrameRequest = null;
|
2022-05-30 21:53:39 +00:00
|
|
|
}
|
|
|
|
this.animationFrameRequest = requestAnimationFrame(() => this.run());
|
|
|
|
}
|
|
|
|
}
|
2022-05-31 14:16:35 +00:00
|
|
|
|
|
|
|
@HostListener('click', ['$event'])
|
|
|
|
onClick(event) {
|
2022-06-02 01:29:03 +04:00
|
|
|
this.setPreviewTx(event.offsetX, event.offsetY, true);
|
2022-05-31 14:16:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@HostListener('pointermove', ['$event'])
|
|
|
|
onPointerMove(event) {
|
2022-06-02 01:29:03 +04:00
|
|
|
this.setPreviewTx(event.offsetX, event.offsetY, false);
|
2022-05-31 14:16:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@HostListener('pointerleave', ['$event'])
|
|
|
|
onPointerLeave(event) {
|
2022-06-02 01:29:03 +04:00
|
|
|
this.setPreviewTx(-1, -1, false);
|
2022-05-31 14:16:35 +00:00
|
|
|
}
|
|
|
|
|
2022-06-12 18:15:30 +00:00
|
|
|
setPreviewTx(cssX: number, cssY: number, clicked: boolean = false) {
|
|
|
|
const x = cssX * window.devicePixelRatio;
|
|
|
|
const y = cssY * window.devicePixelRatio;
|
2022-05-31 14:16:35 +00:00
|
|
|
if (this.scene && (!this.selectedTx || clicked)) {
|
2022-06-02 01:29:03 +04:00
|
|
|
const selected = this.scene.getTxAt({ x, y });
|
|
|
|
const currentPreview = this.selectedTx || this.hoverTx;
|
2022-05-31 14:16:35 +00:00
|
|
|
|
|
|
|
if (selected !== currentPreview) {
|
2022-06-02 01:29:03 +04:00
|
|
|
if (currentPreview) {
|
|
|
|
currentPreview.setHover(false);
|
|
|
|
}
|
2022-05-31 14:16:35 +00:00
|
|
|
if (selected) {
|
2022-06-02 01:29:03 +04:00
|
|
|
selected.setHover(true);
|
2022-05-31 14:16:35 +00:00
|
|
|
this.txPreviewEvent.emit({
|
|
|
|
txid: selected.txid,
|
|
|
|
fee: selected.fee,
|
|
|
|
vsize: selected.vsize,
|
|
|
|
value: selected.value
|
2022-06-02 01:29:03 +04:00
|
|
|
});
|
|
|
|
if (clicked) {
|
|
|
|
this.selectedTx = selected;
|
|
|
|
} else {
|
|
|
|
this.hoverTx = selected;
|
|
|
|
}
|
2022-05-31 14:16:35 +00:00
|
|
|
} else {
|
|
|
|
if (clicked) {
|
2022-06-02 01:29:03 +04:00
|
|
|
this.selectedTx = null;
|
2022-05-31 14:16:35 +00:00
|
|
|
}
|
2022-06-02 01:29:03 +04:00
|
|
|
this.hoverTx = null;
|
|
|
|
this.txPreviewEvent.emit(null);
|
2022-05-31 14:16:35 +00:00
|
|
|
}
|
|
|
|
} else if (clicked) {
|
|
|
|
if (selected === this.selectedTx) {
|
2022-06-02 01:29:03 +04:00
|
|
|
this.hoverTx = this.selectedTx;
|
|
|
|
this.selectedTx = null;
|
2022-05-31 14:16:35 +00:00
|
|
|
} else {
|
2022-06-02 01:29:03 +04:00
|
|
|
this.selectedTx = selected;
|
2022-05-31 14:16:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-05-30 21:53:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// WebGL shader attributes
|
|
|
|
const attribs = {
|
|
|
|
offset: { type: 'FLOAT', count: 2, pointer: null, offset: 0 },
|
|
|
|
posX: { type: 'FLOAT', count: 4, pointer: null, offset: 0 },
|
|
|
|
posY: { type: 'FLOAT', count: 4, pointer: null, offset: 0 },
|
|
|
|
posR: { type: 'FLOAT', count: 4, pointer: null, offset: 0 },
|
|
|
|
colR: { type: 'FLOAT', count: 4, pointer: null, offset: 0 },
|
|
|
|
colG: { type: 'FLOAT', count: 4, pointer: null, offset: 0 },
|
|
|
|
colB: { type: 'FLOAT', count: 4, pointer: null, offset: 0 },
|
|
|
|
colA: { type: 'FLOAT', count: 4, pointer: null, offset: 0 }
|
2022-06-02 01:29:03 +04:00
|
|
|
};
|
2022-05-30 21:53:39 +00:00
|
|
|
// Calculate the number of bytes per vertex based on specified attributes
|
|
|
|
const stride = Object.values(attribs).reduce((total, attrib) => {
|
2022-06-02 01:29:03 +04:00
|
|
|
return total + (attrib.count * 4);
|
|
|
|
}, 0);
|
2022-05-30 21:53:39 +00:00
|
|
|
// Calculate vertex attribute offsets
|
|
|
|
for (let i = 0, offset = 0; i < Object.keys(attribs).length; i++) {
|
2022-06-02 01:29:03 +04:00
|
|
|
const attrib = Object.values(attribs)[i];
|
|
|
|
attrib.offset = offset;
|
|
|
|
offset += (attrib.count * 4);
|
2022-05-30 21:53:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const vertShaderSrc = `
|
|
|
|
varying lowp vec4 vColor;
|
|
|
|
|
|
|
|
// each attribute contains [x: startValue, y: endValue, z: startTime, w: rate]
|
|
|
|
// shader interpolates between start and end values at the given rate, from the given time
|
|
|
|
|
|
|
|
attribute vec2 offset;
|
|
|
|
attribute vec4 posX;
|
|
|
|
attribute vec4 posY;
|
|
|
|
attribute vec4 posR;
|
|
|
|
attribute vec4 colR;
|
|
|
|
attribute vec4 colG;
|
|
|
|
attribute vec4 colB;
|
|
|
|
attribute vec4 colA;
|
|
|
|
|
|
|
|
uniform vec2 screenSize;
|
|
|
|
uniform float now;
|
|
|
|
|
|
|
|
float smootherstep(float x) {
|
|
|
|
x = clamp(x, 0.0, 1.0);
|
|
|
|
float ix = 1.0 - x;
|
|
|
|
x = x * x;
|
|
|
|
return x / (x + ix * ix);
|
|
|
|
}
|
|
|
|
|
|
|
|
float interpolateAttribute(vec4 attr) {
|
|
|
|
float d = (now - attr.z) * attr.w;
|
|
|
|
float delta = smootherstep(d);
|
|
|
|
return mix(attr.x, attr.y, delta);
|
|
|
|
}
|
|
|
|
|
|
|
|
void main() {
|
|
|
|
vec4 screenTransform = vec4(2.0 / screenSize.x, 2.0 / screenSize.y, -1.0, -1.0);
|
|
|
|
// vec4 screenTransform = vec4(1.0 / screenSize.x, 1.0 / screenSize.y, -0.5, -0.5);
|
|
|
|
|
|
|
|
float radius = interpolateAttribute(posR);
|
|
|
|
vec2 position = vec2(interpolateAttribute(posX), interpolateAttribute(posY)) + (radius * offset);
|
|
|
|
|
|
|
|
gl_Position = vec4(position * screenTransform.xy + screenTransform.zw, 1.0, 1.0);
|
|
|
|
|
|
|
|
float red = interpolateAttribute(colR);
|
|
|
|
float green = interpolateAttribute(colG);
|
|
|
|
float blue = interpolateAttribute(colB);
|
|
|
|
float alpha = interpolateAttribute(colA);
|
|
|
|
|
|
|
|
vColor = vec4(red, green, blue, alpha);
|
|
|
|
}
|
2022-06-02 01:29:03 +04:00
|
|
|
`;
|
2022-05-30 21:53:39 +00:00
|
|
|
|
|
|
|
const fragShaderSrc = `
|
|
|
|
varying lowp vec4 vColor;
|
|
|
|
|
|
|
|
void main() {
|
|
|
|
gl_FragColor = vColor;
|
|
|
|
// premultiply alpha
|
|
|
|
gl_FragColor.rgb *= gl_FragColor.a;
|
2022-05-30 17:29:30 +00:00
|
|
|
}
|
2022-06-02 01:29:03 +04:00
|
|
|
`;
|