Working version

This commit is contained in:
schmelczerandras 2020-09-15 13:37:57 +02:00
parent 77bde04db3
commit b2c2cfcb41
63 changed files with 203 additions and 8301 deletions

1
.gitattributes vendored
View file

@ -1 +0,0 @@
*.psd filter=lfs diff=lfs merge=lfs -text

2
.gitignore vendored
View file

@ -1,3 +1,3 @@
dist
node_modules
package-lock.json
lib

5
.npmignore Normal file
View file

@ -0,0 +1,5 @@
.*
**/tsconfig.json
**/webpack.config.js
node_modules
src

File diff suppressed because it is too large Load diff

0
build/src/game.d.ts vendored
View file

View file

@ -1,7 +0,0 @@
import { IDrawable } from './i-drawable';
export interface IDrawableDescriptor {
uniformName: string;
countMacroName: string;
shaderCombinationSteps: Array<number>;
readonly empty: IDrawable;
}

View file

@ -1,5 +0,0 @@
import { vec2, mat2d } from 'gl-matrix';
export interface IDrawable {
distance(target: vec2): number;
serializeToUniforms(uniforms: any, scale: number, transform: mat2d): void;
}

View file

@ -1,14 +0,0 @@
import { mat2d, vec2, vec3 } from 'gl-matrix';
import { IDrawableDescriptor } from '../i-drawable-descriptor';
import { ILight } from './i-light';
export declare class CircleLight implements ILight {
center: vec2;
lightDrop: number;
color: vec3;
lightness: number;
static descriptor: IDrawableDescriptor;
constructor(center: vec2, lightDrop: number, color: vec3, lightness: number);
distance(_: vec2): number;
serializeToUniforms(uniforms: any, scale: number, transform: mat2d): void;
get value(): vec3;
}

View file

@ -1,15 +0,0 @@
import { mat2d, vec2, vec3 } from 'gl-matrix';
import { IDrawableDescriptor } from '../i-drawable-descriptor';
import { ILight } from './i-light';
export declare class Flashlight implements ILight {
center: vec2;
direction: vec2;
lightDrop: number;
color: vec3;
lightness: number;
static descriptor: IDrawableDescriptor;
constructor(center: vec2, direction: vec2, lightDrop: number, color: vec3, lightness: number);
distance(_: vec2): number;
serializeToUniforms(uniforms: any, scale: number, transform: mat2d): void;
get value(): vec3;
}

View file

@ -1,2 +0,0 @@
import { IDrawable } from '../i-drawable';
export declare type ILight = IDrawable;

View file

@ -1 +0,0 @@
export declare const checkProgram: (gl: WebGL2RenderingContext, program: WebGLProgram) => void;

View file

@ -1 +0,0 @@
export declare const checkShader: (gl: WebGL2RenderingContext, shader: WebGLShader) => void;

View file

@ -1,3 +0,0 @@
export declare const createProgram: (gl: WebGL2RenderingContext, vertexShaderSource: string, fragmentShaderSource: string, substitutions: {
[name: string]: string;
}) => Promise<WebGLProgram>;

View file

@ -1,3 +0,0 @@
export declare const createShader: (gl: WebGL2RenderingContext, type: GLenum, source: string, substitutions: {
[name: string]: string;
}) => WebGLShader;

View file

@ -1,5 +0,0 @@
import { FrameBuffer } from './frame-buffer';
export declare class DefaultFrameBuffer extends FrameBuffer {
constructor(gl: WebGL2RenderingContext);
setSize(): boolean;
}

View file

@ -1,12 +0,0 @@
import { vec2 } from 'gl-matrix';
export declare abstract class FrameBuffer {
protected gl: WebGL2RenderingContext;
renderScale: number;
enableHighDpiRendering: boolean;
protected size: vec2;
protected frameBuffer: WebGLFramebuffer | null;
constructor(gl: WebGL2RenderingContext);
bindAndClear(colorInput?: WebGLTexture): void;
setSize(): boolean;
getSize(): vec2;
}

View file

@ -1,10 +0,0 @@
import { FrameBuffer } from './frame-buffer';
export declare class IntermediateFrameBuffer extends FrameBuffer {
private frameTexture;
private floatLinearEnabled;
constructor(gl: WebGL2RenderingContext);
get colorTexture(): WebGLTexture;
setSize(): boolean;
private configureTexture;
private configureFrameBuffer;
}

View file

@ -1,2 +0,0 @@
export declare const tryEnableExtension: (gl: WebGL2RenderingContext, name: string) => any | null;
export declare const enableExtension: (gl: WebGL2RenderingContext, name: string) => any;

View file

@ -1 +0,0 @@
export declare const getWebGl2Context: (canvas: HTMLCanvasElement) => WebGL2RenderingContext;

View file

@ -1 +0,0 @@
export declare const loadUniform: (gl: WebGL2RenderingContext, value: any, type: GLenum, location: WebGLUniformLocation) => any;

View file

@ -1,11 +0,0 @@
export declare class WebGlStopwatch {
private gl;
private timerExtension;
private timerQuery?;
private isReady;
private resultsInNanoSeconds?;
constructor(gl: WebGL2RenderingContext);
start(): void;
stop(): void;
get resultsInMilliSeconds(): number;
}

View file

@ -1,11 +0,0 @@
import Program from './program';
export declare class FragmentShaderOnlyProgram extends Program {
private vao?;
constructor(gl: WebGL2RenderingContext, sources: [string, string], substitutions: {
[name: string]: string;
});
initialize(): Promise<void>;
bind(): void;
draw(): void;
private prepareScreenQuad;
}

View file

@ -1,10 +0,0 @@
import { vec2 } from 'gl-matrix';
export interface IProgram {
initialize(): Promise<void>;
setDrawingRectangleUV(bottomLeft: vec2, size: vec2): void;
bindAndSetUniforms(values: {
[name: string]: any;
}): void;
draw(): void;
delete(): void;
}

View file

@ -1,25 +0,0 @@
import { vec2 } from 'gl-matrix';
import { IProgram } from './i-program';
export default abstract class Program implements IProgram {
protected gl: WebGL2RenderingContext;
protected program?: WebGLProgram;
private programPromise;
private modelTransform;
private readonly ndcToUv;
private uniforms;
constructor(gl: WebGL2RenderingContext, [vertexShaderSource, fragmentShaderSource]: [string, string], substitutions: {
[name: string]: string;
});
initialize(): Promise<void>;
bindAndSetUniforms(values: {
[name: string]: any;
}): void;
setDrawingRectangleUV(bottomLeft: vec2, size: vec2): void;
setUniforms(values: {
[name: string]: any;
}): void;
delete(): void;
abstract draw(): void;
protected bind(): void;
private queryUniforms;
}

View file

@ -1,20 +0,0 @@
import { vec2 } from 'gl-matrix';
import { IDrawableDescriptor } from '../../drawables/i-drawable-descriptor';
import { IProgram } from './i-program';
export declare class UniformArrayAutoScalingProgram implements IProgram {
private gl;
private descriptors;
private programs;
private current?;
private drawingRectangleBottomLeft;
private drawingRectangleSize;
constructor(gl: WebGL2RenderingContext, shaderSources: [string, string], descriptors: Array<IDrawableDescriptor>);
initialize(): Promise<void>;
bindAndSetUniforms(uniforms: {
[name: string]: any;
}): void;
setDrawingRectangleUV(bottomLeft: vec2, size: vec2): void;
draw(): void;
delete(): void;
private createProgram;
}

View file

@ -1,14 +0,0 @@
import { vec2 } from 'gl-matrix';
import { IDrawable } from './drawables/i-drawable';
import { ILight } from './drawables/lights/i-light';
export interface IRenderer {
initialize(): Promise<void>;
startFrame(deltaTime: DOMHighResTimeStamp): void;
finishFrame(): void;
drawShape(drawable: IDrawable): void;
drawLight(light: ILight): void;
drawInfoText(text: string): void;
readonly canvasSize: vec2;
setViewArea(topLeft: vec2, size: vec2): void;
setCursorPosition(position: vec2): void;
}

View file

@ -1,9 +0,0 @@
import { Autoscaler as AutoScaler } from '../../helper/autoscaler';
export declare class FpsAutoscaler extends AutoScaler {
private timeSinceLastAdjusment;
private exponentialDecayedDeltaTime;
constructor(setters: {
[key: string]: (value: number | boolean) => void;
});
autoscale(lastDeltaTime: DOMHighResTimeStamp): void;
}

View file

@ -1,12 +0,0 @@
import { IDrawable } from '../drawables/i-drawable';
import { IDrawableDescriptor } from '../drawables/i-drawable-descriptor';
import { FrameBuffer } from '../graphics-library/frame-buffer/frame-buffer';
export declare class RenderingPass {
private frame;
private drawables;
private program;
constructor(gl: WebGL2RenderingContext, shaderSources: [string, string], drawableDescriptors: Array<IDrawableDescriptor>, frame: FrameBuffer);
initialize(): Promise<void>;
addDrawable(drawable: IDrawable): void;
render(commonUniforms: any, inputTexture?: WebGLTexture): void;
}

View file

@ -1,18 +0,0 @@
import { vec2 } from 'gl-matrix';
export declare class UniformsProvider {
private gl;
private scaleWorldLengthToNDC;
private transformWorldToNDC;
private viewAreaBottomLeft;
private worldAreaInView;
private squareToAspectRatio;
private uvToWorld;
private cursorPosition;
softShadowsEnabled?: boolean;
constructor(gl: WebGL2RenderingContext);
getUniforms(uniforms: any): any;
private getScreenToWorldTransform;
uvToWorldCoordinate(screenUvPosition: vec2): vec2;
setViewArea(topLeft: vec2, size: vec2): void;
setCursorPosition(position: vec2): void;
}

View file

@ -1,27 +0,0 @@
import { vec2 } from 'gl-matrix';
import { IDrawable } from '../drawables/i-drawable';
import { ILight } from '../drawables/lights/i-light';
import { IRenderer } from '../i-renderer';
export declare class WebGl2Renderer implements IRenderer {
private canvas;
private overlay;
private gl;
private stopwatch?;
private uniformsProvider;
private distanceFieldFrameBuffer;
private lightingFrameBuffer;
private distancePass;
private lightingPass;
private autoscaler;
private initializePromise;
constructor(canvas: HTMLCanvasElement, overlay: HTMLElement);
initialize(): Promise<void>;
drawShape(shape: IDrawable): void;
drawLight(light: ILight): void;
startFrame(deltaTime: DOMHighResTimeStamp): void;
finishFrame(): void;
setViewArea(topLeft: vec2, size: vec2): void;
setCursorPosition(position: vec2): void;
get canvasSize(): vec2;
drawInfoText(text: string): void;
}

View file

@ -1,27 +0,0 @@
export declare const settings: {
enableHighDpiRendering: boolean;
qualityScaling: {
targetDeltaTimeInMilliseconds: number;
deltaTimeError: number;
deltaTimeResponsiveness: number;
adjusmentRateInMilliseconds: number;
qualityTargets: {
distanceRenderScale: number;
finalRenderScale: number;
softShadowsEnabled: boolean;
}[];
startingTargetIndex: number;
scalingOptions: {
additiveIncrease: number;
multiplicativeDecrease: number;
};
};
tileMultiplier: number;
shaderMacros: {};
shaderCombinations: {
lineSteps: number[];
blobSteps: number[];
circleLightSteps: number[];
flashlightSteps: number[];
};
};

View file

@ -1,11 +0,0 @@
declare global {
interface Array<T> {
x: number;
y: number;
}
interface Float32Array {
x: number;
y: number;
}
}
export declare const applyArrayPlugins: () => void;

View file

@ -1,17 +0,0 @@
export declare class Autoscaler {
private setters;
private targets;
private scalingOptions;
private index;
constructor(setters: {
[key: string]: (value: number | boolean) => void;
}, targets: Array<{
[key: string]: number | boolean;
}>, startingIndex: number, scalingOptions: {
additiveIncrease: number;
multiplicativeDecrease: number;
});
increase(): void;
decrease(): void;
private applyScaling;
}

View file

@ -1,2 +0,0 @@
export declare const clamp: (value: number, min: number, max: number) => number;
export declare const clamp01: (value: number) => number;

View file

@ -1 +0,0 @@
export declare const exponentialDecay: (accumulator: number, nextValue: number, biasOfNextValue: number) => number;

View file

@ -1 +0,0 @@
export declare const getCombinations: (values: Array<Array<number>>) => Array<Array<number>>;

View file

@ -1 +0,0 @@
export declare function last<T>(a: Array<T>): T | null;

View file

@ -1 +0,0 @@
export declare const mix: (from: number, to: number, q: number) => number;

View file

@ -1,5 +0,0 @@
export declare abstract class Random {
private static _seed;
static set seed(value: number);
static getRandom(): number;
}

View file

@ -1,2 +0,0 @@
import { vec2 } from 'gl-matrix';
export declare const rotate90Deg: (vec: vec2) => vec2;

View file

@ -1 +0,0 @@
export declare function timeIt(interval?: number): (target: any, propertyKey: string, descriptor: PropertyDescriptor) => PropertyDescriptor;

View file

@ -1 +0,0 @@
export declare const toPercent: (value: number) => string;

View file

@ -1 +0,0 @@
export declare const waitWhileFalse: (body: () => boolean) => Promise<void>;

View file

@ -1 +0,0 @@
export declare const wait: (ms: number) => Promise<void>;

1
build/src/main.d.ts vendored
View file

@ -1 +0,0 @@
export {};

View file

@ -1,50 +1,41 @@
{
"name": "sdf-2d-gl",
"name": "sdf-2d",
"version": "0.0.0",
"scripts": {
"start": "webpack-dev-server --mode development",
"lint": "npx eslint --fix \"src/**/*.ts\" && npx prettier --write \"src/**/*.ts\"",
"build": "webpack && find dist -type f -not -name '*.html' | xargs rm"
"clean": "shx rm -rf _bundles lib lib-esm",
"build": "npm run clean && tsc && tsc -m es6 --outDir lib-esm && webpack"
},
"keywords": [],
"author": "András Schmelczer",
"postcss": {
"plugins": {
"autoprefixer": {}
}
},
"browserslist": [
"defaults"
],
"sideEffects": [
"*.scss"
],
"main": "./dist/sdf-2d-gl.js",
"types": "./dist/sdf-2d-gl.d.ts",
"main": "lib/sdf-2d.js",
"types": "lib/src/main.d.ts",
"files": [
"dist"
"lib"
],
"optimization": {
"usedExports": true
},
"devDependencies": {
"@types/gl-matrix": "^2.4.5",
"@typescript-eslint/eslint-plugin": "^3.9.1",
"@typescript-eslint/parser": "^3.9.1",
"eslint": "^7.2.0",
"@typescript-eslint/eslint-plugin": "^3.10.1",
"@typescript-eslint/parser": "^3.10.1",
"awesome-typescript-loader": "^5.2.1",
"eslint": "^7.9.0",
"eslint-config-prettier": "^6.11.0",
"eslint-plugin-import": "^2.21.2",
"eslint-plugin-import": "^2.22.0",
"eslint-plugin-prettier": "^3.1.4",
"eslint-plugin-unused-imports": "^0.1.3",
"gl-matrix": "^3.3.0",
"prettier": "^2.0.5",
"prettier": "^2.1.1",
"raw-loader": "^4.0.1",
"resolve-url-loader": "^3.1.1",
"terser-webpack-plugin": "^2.3.5",
"ts-loader": "^8.0.1",
"typescript": "^3.8.3",
"webpack": "^4.43.0",
"webpack-cli": "^3.3.12",
"webpack-dev-server": "^3.10.3"
"shx": "^0.3.2",
"terser-webpack-plugin": "^2.3.8",
"ts-loader": "^8.0.3",
"typescript": "^3.9.7",
"webpack": "^4.44.1",
"webpack-cli": "^3.3.11"
}
}

View file

@ -0,0 +1,8 @@
import { Drawable } from './drawable';
export interface DrawableDescriptor {
uniformName: string;
countMacroName: string;
shaderCombinationSteps: Array<number>;
readonly empty: Drawable;
}

15
src/drawables/drawable.ts Normal file
View file

@ -0,0 +1,15 @@
import { mat2d, vec2 } from 'gl-matrix';
import { DrawableDescriptor } from './drawable-descriptor';
export abstract class Drawable {
static get descriptor(): DrawableDescriptor {
throw new Error('This getter should be overriden');
}
public abstract distance(target: vec2): number;
public abstract serializeToUniforms(
uniforms: any,
scale: number,
transform: mat2d
): void;
}

View file

@ -1,22 +1,26 @@
import { mat2d, vec2, vec3 } from 'gl-matrix';
import { settings } from '../../settings';
import { IDrawableDescriptor } from '../i-drawable-descriptor';
import { ILight } from './i-light';
import { settings } from '../../graphics/settings';
import { Drawable } from '../drawable';
import { DrawableDescriptor } from '../drawable-descriptor';
export class CircleLight implements ILight {
public static descriptor: IDrawableDescriptor = {
export class CircleLight extends Drawable {
public static get descriptor(): DrawableDescriptor {
return {
uniformName: 'circleLights',
countMacroName: 'circleLightCount',
shaderCombinationSteps: settings.shaderCombinations.circleLightSteps,
empty: new CircleLight(vec2.fromValues(0, 0), 0, vec3.fromValues(0, 0, 0), 0),
};
}
constructor(
public center: vec2,
public lightDrop: number,
public color: vec3,
public lightness: number
) {}
) {
super();
}
public distance(_: vec2): number {
return 0;

View file

@ -1,10 +1,11 @@
import { mat2d, vec2, vec3 } from 'gl-matrix';
import { settings } from '../../settings';
import { IDrawableDescriptor } from '../i-drawable-descriptor';
import { ILight } from './i-light';
import { settings } from '../../graphics/settings';
import { Drawable } from '../drawable';
import { DrawableDescriptor } from '../drawable-descriptor';
export class Flashlight implements ILight {
public static descriptor: IDrawableDescriptor = {
export class Flashlight extends Drawable {
public static get descriptor(): DrawableDescriptor {
return {
uniformName: 'flashlights',
countMacroName: 'flashlightCount',
shaderCombinationSteps: settings.shaderCombinations.flashlightSteps,
@ -16,6 +17,7 @@ export class Flashlight implements ILight {
0
),
};
}
public constructor(
public center: vec2,
@ -23,7 +25,9 @@ export class Flashlight implements ILight {
public lightDrop: number,
public color: vec3,
public lightness: number
) {}
) {
super();
}
public distance(_: vec2): number {
return 0;

View file

@ -0,0 +1,36 @@
import { mat2d, vec2 } from 'gl-matrix';
import { settings } from '../../graphics/settings';
import { Drawable } from '../drawable';
import { DrawableDescriptor } from '../drawable-descriptor';
export class Circle extends Drawable {
public static get descriptor(): DrawableDescriptor {
return {
uniformName: 'circles',
countMacroName: 'circleCount',
shaderCombinationSteps: settings.shaderCombinations.circleSteps,
empty: new Circle(vec2.fromValues(0, 0), 0),
};
}
constructor(public center: vec2, public radius: number) {
super();
}
public distance(position: vec2): number {
return 0;
}
public serializeToUniforms(uniforms: any, scale: number, transform: mat2d): void {
const { uniformName } = Circle.descriptor;
if (!Object.prototype.hasOwnProperty.call(uniforms, uniformName)) {
uniforms[uniformName] = [];
}
uniforms[uniformName].push({
center: vec2.transformMat2d(vec2.create(), this.center, transform),
radius: this.radius * scale,
});
}
}

View file

@ -1,8 +0,0 @@
import { IDrawable } from './i-drawable';
export interface IDrawableDescriptor {
uniformName: string;
countMacroName: string;
shaderCombinationSteps: Array<number>;
readonly empty: IDrawable;
}

View file

@ -1,6 +0,0 @@
import { vec2, mat2d } from 'gl-matrix';
export interface IDrawable {
distance(target: vec2): number;
serializeToUniforms(uniforms: any, scale: number, transform: mat2d): void;
}

View file

@ -1,3 +0,0 @@
import { IDrawable } from '../i-drawable';
export type ILight = IDrawable;

View file

@ -1,7 +1,7 @@
import { mat2d, vec2 } from 'gl-matrix';
import { DrawableDescriptor } from '../../../drawables/drawable-descriptor';
import { getCombinations } from '../../../helper/get-combinations';
import { last } from '../../../helper/last';
import { IDrawableDescriptor } from '../../drawables/i-drawable-descriptor';
import { FragmentShaderOnlyProgram } from './fragment-shader-only-program';
import { IProgram } from './i-program';
@ -18,7 +18,7 @@ export class UniformArrayAutoScalingProgram implements IProgram {
constructor(
private gl: WebGL2RenderingContext,
shaderSources: [string, string],
private descriptors: Array<IDrawableDescriptor>
private descriptors: Array<DrawableDescriptor>
) {
const names = descriptors.map((o) => o.countMacroName);
for (const combination of getCombinations(

View file

@ -1,6 +1,5 @@
import { vec2 } from 'gl-matrix';
import { IDrawable } from './drawables/i-drawable';
import { ILight } from './drawables/lights/i-light';
import { Drawable } from '../drawables/drawable';
export interface IRenderer {
initialize(): Promise<void>;
@ -8,9 +7,8 @@ export interface IRenderer {
startFrame(deltaTime: DOMHighResTimeStamp): void;
finishFrame(): void;
drawShape(drawable: IDrawable): void;
drawLight(light: ILight): void;
drawInfoText(text: string): void;
drawShape(drawable: Drawable): void;
drawLight(light: Drawable): void;
readonly canvasSize: vec2;
setViewArea(topLeft: vec2, size: vec2): void;

View file

@ -1,18 +1,18 @@
import { vec2 } from 'gl-matrix';
import { IDrawable } from '../drawables/i-drawable';
import { IDrawableDescriptor } from '../drawables/i-drawable-descriptor';
import { Drawable } from '../../drawables/drawable';
import { DrawableDescriptor } from '../../drawables/drawable-descriptor';
import { FrameBuffer } from '../graphics-library/frame-buffer/frame-buffer';
import { UniformArrayAutoScalingProgram } from '../graphics-library/program/uniform-array-autoscaling-program';
import { settings } from '../settings';
export class RenderingPass {
private drawables: Array<IDrawable> = [];
private drawables: Array<Drawable> = [];
private program: UniformArrayAutoScalingProgram;
constructor(
gl: WebGL2RenderingContext,
shaderSources: [string, string],
drawableDescriptors: Array<IDrawableDescriptor>,
drawableDescriptors: Array<DrawableDescriptor>,
private frame: FrameBuffer
) {
this.program = new UniformArrayAutoScalingProgram(
@ -26,7 +26,7 @@ export class RenderingPass {
await this.program.initialize();
}
public addDrawable(drawable: IDrawable) {
public addDrawable(drawable: Drawable) {
this.drawables.push(drawable);
}

View file

@ -1,8 +1,8 @@
import { vec2 } from 'gl-matrix';
import { IDrawable } from '../drawables/i-drawable';
import { CircleLight } from '../drawables/lights/circle-light';
import { Flashlight } from '../drawables/lights/flashlight';
import { ILight } from '../drawables/lights/i-light';
import { Drawable } from '../../drawables/drawable';
import { CircleLight } from '../../drawables/lights/circle-light';
import { Flashlight } from '../../drawables/lights/flashlight';
import { Circle } from '../../drawables/shapes/circle';
import { DefaultFrameBuffer } from '../graphics-library/frame-buffer/default-frame-buffer';
import { IntermediateFrameBuffer } from '../graphics-library/frame-buffer/intermediate-frame-buffer';
import { getWebGl2Context } from '../graphics-library/helper/get-webgl2-context';
@ -28,7 +28,7 @@ export class WebGl2Renderer implements IRenderer {
private initializePromise: Promise<[void, void]>;
constructor(private canvas: HTMLCanvasElement, private overlay: HTMLElement) {
constructor(private canvas: HTMLCanvasElement) {
this.gl = getWebGl2Context(canvas);
this.distanceFieldFrameBuffer = new IntermediateFrameBuffer(this.gl);
@ -37,7 +37,7 @@ export class WebGl2Renderer implements IRenderer {
this.distancePass = new RenderingPass(
this.gl,
[distanceVertexShader, distanceFragmentShader],
[],
[Circle.descriptor],
this.distanceFieldFrameBuffer
);
@ -74,11 +74,11 @@ export class WebGl2Renderer implements IRenderer {
await this.initializePromise;
}
public drawShape(shape: IDrawable) {
public drawShape(shape: Drawable) {
this.distancePass.addDrawable(shape);
}
public drawLight(light: ILight) {
public drawLight(light: Drawable) {
this.lightingPass.addDrawable(light);
}
@ -117,10 +117,4 @@ export class WebGl2Renderer implements IRenderer {
public get canvasSize(): vec2 {
return vec2.fromValues(this.canvas.clientWidth, this.canvas.clientHeight);
}
public drawInfoText(text: string) {
if (this.overlay.innerText != text) {
this.overlay.innerText = text;
}
}
}

View file

@ -51,9 +51,8 @@ export const settings = {
tileMultiplier: 8,
shaderMacros: {},
shaderCombinations: {
lineSteps: [0, 1, 2, 4, 8, 16, 128],
blobSteps: [0, 1, 2, 8],
circleLightSteps: [0, 1],
circleSteps: [0, 1, 16],
flashlightSteps: [0, 1],
},
};

View file

@ -2,8 +2,9 @@
precision lowp float;
#define LINE_COUNT {lineCount}
#define BLOB_COUNT {blobCount}
// #define LINE_COUNT {lineCount}
// #define BLOB_COUNT {blobCount}
#define CIRCLE_COUNT {circleCount}
#define SURFACE_OFFSET 0.001
@ -20,7 +21,7 @@ float smoothMin(float a, float b)
b = pow(b, k);
return pow((a * b) / (a + b), 1.0 / k);
}
/*
#if LINE_COUNT > 0
uniform struct {
vec2 from;
@ -53,7 +54,22 @@ float smoothMin(float a, float b)
minDistance = -myMinDistance;
}
#endif
*/
#if CIRCLE_COUNT > 0
uniform struct {
vec2 center;
float radius;
}[CIRCLE_COUNT] circles;
void circleMinDistance(inout float minDistance, inout float color) {
for (int i = 0; i < CIRCLE_COUNT; i++) {
float dist = distance(circles[i].center, position) - circles[i].radius;
minDistance = min(minDistance, dist);
}
}
#endif
/*
#if BLOB_COUNT > 0
uniform struct {
vec2 headCenter;
@ -88,22 +104,26 @@ float smoothMin(float a, float b)
}
}
#endif
*/
out vec2 fragmentColor;
void main() {
float minDistance = -maxMinDistance;
float color = 0.0;
float minDistance = 10.0; //-maxMinDistance;
float color = 1.0;
#if LINE_COUNT > 0
/*#if LINE_COUNT > 0
lineMinDistance(minDistance, color);
#endif
#if BLOB_COUNT > 0
blobMinDistance(minDistance, color);
#endif*/
#if CIRCLE_COUNT > 0
circleMinDistance(minDistance, color);
#endif
// minDistance / 2.0: NDC to UV scale
fragmentColor = vec2((minDistance - SURFACE_OFFSET) / 2.0, color);
fragmentColor = vec2(minDistance / 2.0, color);
}

View file

@ -1,18 +1,14 @@
import { glMatrix } from 'gl-matrix';
import { IRenderer } from './graphics/i-renderer';
import { WebGl2Renderer } from './graphics/rendering/webgl2-renderer';
import { applyArrayPlugins } from './helper/array';
import { Random } from './helper/random';
glMatrix.setMatrixArrayType(Array);
applyArrayPlugins();
export { Drawable } from './drawables/drawable';
export { CircleLight } from './drawables/lights/circle-light';
export { Circle } from './drawables/shapes/circle';
const main = async () => {
try {
Random.seed = 42;
//await new Game().start();
} catch (e) {
console.error(e);
alert(e);
}
export const compile = (canvas: HTMLCanvasElement): IRenderer => {
glMatrix.setMatrixArrayType(Array);
applyArrayPlugins();
return new WebGl2Renderer(canvas);
};
main();

View file

@ -1,6 +1,6 @@
{
"compilerOptions": {
"outDir": "build",
"outDir": "lib",
"sourceMap": true,
"noImplicitAny": false,
"target": "es5",
@ -12,6 +12,6 @@
"moduleResolution": "Node",
"module": "commonjs",
"declaration": true,
"lib": ["es2015", "dom"]
"lib": ["es6", "dom", "es2017"]
}
}

View file

@ -1,11 +1,34 @@
const path = require('path');
var path = require('path');
const TerserJSPlugin = require('terser-webpack-plugin');
var PATHS = {
entryPoint: path.resolve(__dirname, 'src/main.ts'),
bundles: path.resolve(__dirname, 'lib'),
};
const isProduction = process.env.NODE_ENV == 'production';
const isDevelopment = !isProduction;
module.exports = {
devtool: 'inline-source-map',
entry: {
'sdf-2d': [PATHS.entryPoint],
'sdf-2d.min': [PATHS.entryPoint],
},
output: {
path: PATHS.bundles,
filename: '[name].js',
library: 'SDF2D',
libraryTarget: 'umd',
umdNamedDefine: true,
},
devtool: 'source-map',
watchOptions: {
aggregateTimeout: 600,
ignored: /node_modules/,
},
resolve: {
extensions: ['.ts', '.js'],
},
optimization: {
minimize: true,
minimizer: [
@ -31,10 +54,6 @@ module.exports = {
}),
],
},
plugins: [],
entry: {
main: './src/main.ts',
},
module: {
rules: [
{
@ -43,31 +62,13 @@ module.exports = {
loader: 'raw-loader',
},
},
{
test: /\.(woff2?|ttf|eot|svg)(?:[?#].+)?$/,
use: {
loader: 'file-loader',
options: {
name: '[name].[ext]',
outputPath: 'static/fonts/',
},
},
include: /fonts/,
},
{
test: /\.ts$/,
use: {
loader: 'ts-loader',
loader: 'awesome-typescript-loader',
},
exclude: /node_modules/,
},
],
},
resolve: {
extensions: ['.ts', '.js', '.glsl'],
},
output: {
filename: '[name]-bundle.js',
path: path.resolve(__dirname, 'build'),
},
};