pathfinder/demo/client/src/index.ts

473 lines
18 KiB
TypeScript
Raw Normal View History

2017-08-08 14:23:30 -04:00
// pathfinder/demo/src/index.ts
2017-08-10 21:38:54 -04:00
const base64js = require('base64-js');
2017-08-08 14:23:30 -04:00
const opentype = require('opentype.js');
const TEXT: string = "G";
2017-08-10 21:38:54 -04:00
const FONT_SIZE: number = 16.0;
const PARTITION_FONT_ENDPOINT_URL: string = "/partition-font";
2017-08-13 16:39:51 -04:00
const COMMON_SHADER_URL: string = '/glsl/gles2/common.inc.glsl';
const UINT32_SIZE: number = 4;
const B_POSITION_SIZE: number = 8;
const B_VERTEX_QUAD_SIZE: number = 8;
const B_VERTEX_QUAD_PATH_ID_OFFSET: number = 0;
const B_VERTEX_QUAD_TEX_COORD_OFFSET: number = 4;
const B_VERTEX_QUAD_SIGN_OFFSET: number = 6;
const IDENTITY: Matrix4D = [
1.0, 0.0, 0.0, 0.0,
0.0, 1.0, 0.0, 0.0,
0.0, 0.0, 1.0, 0.0,
0.0, 0.0, 0.0, 1.0,
];
const SHADER_URLS: ShaderMap<ShaderProgramURLs> = {
2017-08-12 00:26:25 -04:00
directCurve: {
vertex: "/glsl/gles2/direct-curve.vs.glsl",
fragment: "/glsl/gles2/direct-curve.fs.glsl",
},
directInterior: {
vertex: "/glsl/gles2/direct-interior.vs.glsl",
fragment: "/glsl/gles2/direct-interior.fs.glsl",
},
};
interface UnlinkedShaderProgram {
vertex: WebGLShader;
fragment: WebGLShader;
}
type Matrix4D = number[];
interface ShaderProgramSource {
vertex: string;
fragment: string;
}
interface ShaderProgramURLs {
vertex: string;
fragment: string;
2017-08-12 00:26:25 -04:00
}
interface ShaderMap<T> {
directCurve: T;
directInterior: T;
2017-08-12 00:26:25 -04:00
}
interface UniformMap {
[uniformName: string]: WebGLUniformLocation;
}
interface AttributeMap {
[attributeName: string]: number;
2017-08-12 00:26:25 -04:00
}
type ShaderType = number;
type ShaderTypeName = 'vertex' | 'fragment';
function expect<T>(value: T | null, message: string): T {
if (value == null)
2017-08-13 16:39:51 -04:00
throw new PathfinderError(message);
2017-08-12 00:26:25 -04:00
return value;
}
function unwrap<T>(value: T | null): T {
return expect(value, "Unexpected null!");
}
2017-08-13 16:39:51 -04:00
class PathfinderError extends Error {
constructor(message?: string | undefined) {
super(message);
}
}
interface Meshes<T> {
readonly bQuads: T;
readonly bVertexPositions: T;
readonly bVertexInfo: T;
readonly coverInteriorIndices: T;
readonly coverCurveIndices: T;
readonly edgeUpperLineIndices: T;
readonly edgeUpperCurveIndices: T;
readonly edgeLowerLineIndices: T;
readonly edgeLowerCurveIndices: T;
}
type BufferType = number;
const BUFFER_TYPES: Meshes<BufferType> = {
bQuads: WebGLRenderingContext.ARRAY_BUFFER,
bVertexPositions: WebGLRenderingContext.ARRAY_BUFFER,
bVertexInfo: WebGLRenderingContext.ARRAY_BUFFER,
coverInteriorIndices: WebGLRenderingContext.ELEMENT_ARRAY_BUFFER,
coverCurveIndices: WebGLRenderingContext.ELEMENT_ARRAY_BUFFER,
edgeUpperLineIndices: WebGLRenderingContext.ELEMENT_ARRAY_BUFFER,
edgeUpperCurveIndices: WebGLRenderingContext.ELEMENT_ARRAY_BUFFER,
edgeLowerLineIndices: WebGLRenderingContext.ELEMENT_ARRAY_BUFFER,
edgeLowerCurveIndices: WebGLRenderingContext.ELEMENT_ARRAY_BUFFER,
};
class PathfinderMeshData implements Meshes<ArrayBuffer> {
2017-08-10 21:38:54 -04:00
constructor(encodedResponse: string) {
const response = JSON.parse(encodedResponse);
if (!('Ok' in response))
2017-08-13 16:39:51 -04:00
throw new PathfinderError("Failed to partition the font!");
2017-08-10 21:38:54 -04:00
const meshes = response.Ok;
for (const bufferName of Object.keys(BUFFER_TYPES) as Array<keyof PathfinderMeshData>)
this[bufferName] = base64js.toByteArray(meshes[bufferName]).buffer;
2017-08-08 14:23:30 -04:00
}
readonly bQuads: ArrayBuffer;
readonly bVertexPositions: ArrayBuffer;
readonly bVertexInfo: ArrayBuffer;
readonly coverInteriorIndices: ArrayBuffer;
readonly coverCurveIndices: ArrayBuffer;
readonly edgeUpperLineIndices: ArrayBuffer;
readonly edgeUpperCurveIndices: ArrayBuffer;
readonly edgeLowerLineIndices: ArrayBuffer;
readonly edgeLowerCurveIndices: ArrayBuffer;
}
class PathfinderMeshBuffers implements Meshes<WebGLBuffer> {
constructor(gl: WebGLRenderingContext, meshData: PathfinderMeshData) {
for (const bufferName of Object.keys(BUFFER_TYPES) as Array<keyof PathfinderMeshBuffers>) {
const bufferType = BUFFER_TYPES[bufferName];
const buffer = expect(gl.createBuffer(), "Failed to create buffer!");
gl.bindBuffer(bufferType, buffer);
gl.bufferData(bufferType, meshData[bufferName], gl.STATIC_DRAW);
console.log(`${bufferName} has size ${meshData[bufferName].byteLength}`);
if (bufferName == 'coverInteriorIndices') {
const typedArray = new Uint32Array(meshData[bufferName]);
let array = [];
for (let i = 0; i < typedArray.length; i++)
array[i] = typedArray[i];
console.log(array.toString());
}
this[bufferName] = buffer;
}
}
readonly bQuads: WebGLBuffer;
readonly bVertexPositions: WebGLBuffer;
readonly bVertexInfo: WebGLBuffer;
readonly coverInteriorIndices: WebGLBuffer;
readonly coverCurveIndices: WebGLBuffer;
readonly edgeUpperLineIndices: WebGLBuffer;
readonly edgeUpperCurveIndices: WebGLBuffer;
readonly edgeLowerLineIndices: WebGLBuffer;
readonly edgeLowerCurveIndices: WebGLBuffer;
2017-08-10 21:38:54 -04:00
}
class AppController {
constructor() {}
2017-08-08 14:23:30 -04:00
start() {
this.view = new PathfinderView(document.getElementById('pf-canvas') as HTMLCanvasElement);
this.loadFontButton = document.getElementById('pf-load-font-button') as HTMLInputElement;
this.loadFontButton.addEventListener('change', () => this.loadFont(), false);
}
loadFont() {
2017-08-12 00:26:25 -04:00
const file = expect(this.loadFontButton.files, "No file selected!")[0];
2017-08-10 21:38:54 -04:00
const reader = new FileReader;
reader.addEventListener('loadend', () => {
this.fontData = reader.result;
this.fontLoaded();
}, false);
reader.readAsArrayBuffer(file);
}
fontLoaded() {
this.font = opentype.parse(this.fontData);
2017-08-12 00:26:25 -04:00
if (!this.font.supported)
2017-08-13 16:39:51 -04:00
throw new PathfinderError("The font type is unsupported.");
2017-08-10 21:38:54 -04:00
2017-08-12 00:26:25 -04:00
const glyphIDs = this.font.stringToGlyphs(TEXT).map((glyph: any) => glyph.index);
2017-08-10 21:38:54 -04:00
const request = {
otf: base64js.fromByteArray(new Uint8Array(this.fontData)),
fontIndex: 0,
glyphIDs: glyphIDs,
pointSize: FONT_SIZE,
};
2017-08-12 13:08:35 -04:00
window.fetch(PARTITION_FONT_ENDPOINT_URL, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(request),
}).then((response) => {
response.text().then((encodedMeshes) => {
this.meshes = new PathfinderMeshData(encodedMeshes);
2017-08-12 13:08:35 -04:00
this.meshesReceived();
});
});
2017-08-08 14:23:30 -04:00
}
2017-08-10 21:38:54 -04:00
meshesReceived() {
this.view.attachMeshes(this.meshes);
2017-08-08 14:23:30 -04:00
}
view: PathfinderView;
loadFontButton: HTMLInputElement;
2017-08-10 21:38:54 -04:00
fontData: ArrayBuffer;
font: any;
meshes: PathfinderMeshData;
2017-08-08 14:23:30 -04:00
}
class PathfinderView {
constructor(canvas: HTMLCanvasElement) {
this.canvas = canvas;
2017-08-12 00:26:25 -04:00
this.initContext();
2017-08-13 16:39:51 -04:00
this.shaderProgramsPromise = this.loadShaders().then(shaders => this.linkShaders(shaders));
2017-08-12 00:26:25 -04:00
window.addEventListener('resize', () => this.resizeToFit(), false);
this.resizeToFit();
}
initContext() {
this.gl = expect(this.canvas.getContext('webgl', { antialias: false, depth: true }),
"Failed to initialize WebGL! Check that your browser supports it.");
this.gl.getExtension('OES_element_index_uint');
2017-08-12 00:26:25 -04:00
}
loadShaders(): Promise<ShaderMap<UnlinkedShaderProgram>> {
let shaders: Partial<ShaderMap<Partial<UnlinkedShaderProgram>>> = {};
2017-08-13 16:39:51 -04:00
return window.fetch(COMMON_SHADER_URL)
.then((response) => response.text())
.then((commonSource) => {
const shaderKeys = Object.keys(SHADER_URLS) as Array<keyof ShaderMap<string>>;
2017-08-13 16:39:51 -04:00
let promises = [];
for (const shaderKey of shaderKeys) {
for (const typeName of ['vertex', 'fragment'] as Array<ShaderTypeName>) {
const type = {
vertex: this.gl.VERTEX_SHADER,
fragment: this.gl.FRAGMENT_SHADER,
}[typeName];
const url = SHADER_URLS[shaderKey][typeName];
promises.push(window.fetch(url)
.then(response => response.text())
.then(source => {
2017-08-12 13:08:35 -04:00
const shader = this.gl.createShader(type);
if (shader == null)
2017-08-13 16:39:51 -04:00
throw new PathfinderError("Failed to create shader!");
this.gl.shaderSource(shader, commonSource + "\n#line 1\n" + source);
2017-08-12 13:08:35 -04:00
this.gl.compileShader(shader);
2017-08-13 16:39:51 -04:00
if (this.gl.getShaderParameter(shader, this.gl.COMPILE_STATUS) == 0) {
const infoLog = this.gl.getShaderInfoLog(shader);
throw new PathfinderError(`Failed to compile ${typeName} shader ` +
`"${shaderKey}":\n${infoLog}`);
}
if (shaders[shaderKey] == null)
2017-08-12 13:08:35 -04:00
shaders[shaderKey] = {};
shaders[shaderKey]![typeName] = shader;
2017-08-13 16:39:51 -04:00
}));
}
2017-08-12 00:26:25 -04:00
}
2017-08-12 13:08:35 -04:00
2017-08-13 16:39:51 -04:00
return Promise.all(promises);
}).then(() => shaders as ShaderMap<UnlinkedShaderProgram>);
2017-08-12 00:26:25 -04:00
}
linkShaders(shaders: ShaderMap<UnlinkedShaderProgram>):
Promise<ShaderMap<PathfinderShaderProgram>> {
2017-08-13 16:39:51 -04:00
return new Promise((resolve, reject) => {
let shaderProgramMap: Partial<ShaderMap<PathfinderShaderProgram>> = {};
for (const shaderName of Object.keys(shaders) as
Array<keyof ShaderMap<UnlinkedShaderProgram>>) {
shaderProgramMap[shaderName] = new PathfinderShaderProgram(this.gl,
shaderName,
shaders[shaderName]);
2017-08-13 16:39:51 -04:00
}
resolve(shaderProgramMap as ShaderMap<PathfinderShaderProgram>);
2017-08-13 16:39:51 -04:00
});
2017-08-12 00:26:25 -04:00
}
attachMeshes(meshes: PathfinderMeshData) {
this.meshes = new PathfinderMeshBuffers(this.gl, meshes);
this.setDirty();
}
setDirty() {
if (this.dirty)
return;
this.dirty = true;
window.requestAnimationFrame(() => this.redraw());
}
2017-08-12 00:26:25 -04:00
resizeToFit() {
2017-08-14 22:08:18 -04:00
const width = window.innerWidth;
const height = window.scrollY + window.innerHeight -
this.canvas.getBoundingClientRect().top;
const devicePixelRatio = window.devicePixelRatio;
this.canvas.style.width = width + 'px';
this.canvas.style.height = height + 'px';
this.canvas.width = width * devicePixelRatio;
this.canvas.height = height * devicePixelRatio;
this.setDirty();
}
redraw() {
this.shaderProgramsPromise.then((shaderPrograms: ShaderMap<PathfinderShaderProgram>) => {
if (this.meshes == null) {
this.dirty = false;
return;
}
// Clear.
this.gl.clearColor(1.0, 1.0, 1.0, 1.0);
this.gl.clearDepth(0.0);
this.gl.clear(this.gl.COLOR_BUFFER_BIT | this.gl.DEPTH_BUFFER_BIT);
// Set up the depth buffer.
this.gl.depthFunc(this.gl.GREATER);
2017-08-14 22:08:18 -04:00
this.gl.depthMask(true);
this.gl.enable(this.gl.DEPTH_TEST);
// Set up the implicit cover interior VAO.
const directInteriorProgram = shaderPrograms.directInterior;
this.gl.useProgram(directInteriorProgram.program);
this.gl.bindBuffer(this.gl.ARRAY_BUFFER, this.meshes.bVertexPositions);
this.gl.vertexAttribPointer(directInteriorProgram.attributes.aPosition,
2,
this.gl.FLOAT,
false,
0,
0);
this.gl.bindBuffer(this.gl.ARRAY_BUFFER, this.meshes.bVertexInfo);
this.gl.vertexAttribPointer(directInteriorProgram.attributes.aPathDepth,
1,
this.gl.UNSIGNED_SHORT, // FIXME(pcwalton)
true,
B_VERTEX_QUAD_SIZE,
B_VERTEX_QUAD_PATH_ID_OFFSET);
this.gl.enableVertexAttribArray(directInteriorProgram.attributes.aPosition);
this.gl.enableVertexAttribArray(directInteriorProgram.attributes.aPathDepth);
this.gl.bindBuffer(this.gl.ELEMENT_ARRAY_BUFFER, this.meshes.coverInteriorIndices);
// Draw direct interior parts.
this.gl.uniformMatrix4fv(directInteriorProgram.uniforms.uTransform, false, IDENTITY);
this.gl.uniform2i(directInteriorProgram.uniforms.uFramebufferSize,
this.canvas.width,
this.canvas.height);
2017-08-14 22:08:18 -04:00
let indexCount = this.gl.getBufferParameter(this.gl.ELEMENT_ARRAY_BUFFER,
this.gl.BUFFER_SIZE) / UINT32_SIZE;
this.gl.drawElements(this.gl.TRIANGLES, indexCount, this.gl.UNSIGNED_INT, 0);
// Disable depth writing.
this.gl.depthMask(false);
// Set up the implicit cover curve VAO.
const directCurveProgram = shaderPrograms.directCurve;
this.gl.useProgram(directCurveProgram.program);
this.gl.bindBuffer(this.gl.ARRAY_BUFFER, this.meshes.bVertexPositions);
this.gl.vertexAttribPointer(directCurveProgram.attributes.aPosition,
2,
this.gl.FLOAT,
false,
0,
0);
this.gl.bindBuffer(this.gl.ARRAY_BUFFER, this.meshes.bVertexInfo);
this.gl.vertexAttribPointer(directCurveProgram.attributes.aTexCoord,
2,
this.gl.UNSIGNED_BYTE,
false,
B_VERTEX_QUAD_SIZE,
B_VERTEX_QUAD_TEX_COORD_OFFSET);
this.gl.vertexAttribPointer(directCurveProgram.attributes.aPathDepth,
1,
this.gl.UNSIGNED_SHORT, // FIXME(pcwalton)
true,
B_VERTEX_QUAD_SIZE,
B_VERTEX_QUAD_PATH_ID_OFFSET);
this.gl.vertexAttribPointer(directCurveProgram.attributes.aSign,
1,
this.gl.BYTE,
false,
B_VERTEX_QUAD_SIZE,
B_VERTEX_QUAD_SIGN_OFFSET);
this.gl.enableVertexAttribArray(directCurveProgram.attributes.aPosition);
this.gl.enableVertexAttribArray(directCurveProgram.attributes.aTexCoord);
this.gl.enableVertexAttribArray(directCurveProgram.attributes.aPathDepth);
this.gl.enableVertexAttribArray(directCurveProgram.attributes.aSign);
this.gl.bindBuffer(this.gl.ELEMENT_ARRAY_BUFFER, this.meshes.coverCurveIndices);
// Draw direct curve parts.
this.gl.uniformMatrix4fv(directCurveProgram.uniforms.uTransform, false, IDENTITY);
this.gl.uniform2i(directCurveProgram.uniforms.uFramebufferSize,
this.canvas.width,
this.canvas.height);
indexCount = this.gl.getBufferParameter(this.gl.ELEMENT_ARRAY_BUFFER,
this.gl.BUFFER_SIZE) / UINT32_SIZE;
this.gl.drawElements(this.gl.TRIANGLES, indexCount, this.gl.UNSIGNED_INT, 0);
// Clear dirty bit and finish.
this.dirty = false;
});
2017-08-08 14:23:30 -04:00
}
canvas: HTMLCanvasElement;
2017-08-12 00:26:25 -04:00
gl: WebGLRenderingContext;
shaderProgramsPromise: Promise<ShaderMap<PathfinderShaderProgram>>;
meshes: PathfinderMeshBuffers;
dirty: boolean;
2017-08-12 00:26:25 -04:00
}
class PathfinderShaderProgram {
constructor(gl: WebGLRenderingContext,
programName: string,
unlinkedShaderProgram: UnlinkedShaderProgram) {
this.program = expect(gl.createProgram(), "Failed to create shader program!");
for (const compiledShader of Object.values(unlinkedShaderProgram))
gl.attachShader(this.program, compiledShader);
gl.linkProgram(this.program);
if (gl.getProgramParameter(this.program, gl.LINK_STATUS) == 0) {
const infoLog = gl.getProgramInfoLog(this.program);
throw new PathfinderError(`Failed to link program "${programName}":\n${infoLog}`);
}
const uniformCount = gl.getProgramParameter(this.program, gl.ACTIVE_UNIFORMS);
const attributeCount = gl.getProgramParameter(this.program, gl.ACTIVE_ATTRIBUTES);
let uniforms: UniformMap = {};
let attributes: AttributeMap = {};
for (let uniformIndex = 0; uniformIndex < uniformCount; uniformIndex++) {
const uniformName = unwrap(gl.getActiveUniform(this.program, uniformIndex)).name;
uniforms[uniformName] = expect(gl.getUniformLocation(this.program, uniformName),
`Didn't find uniform "${uniformName}"!`);
}
for (let attributeIndex = 0; attributeIndex < attributeCount; attributeIndex++) {
const attributeName = unwrap(gl.getActiveAttrib(this.program, attributeIndex)).name;
attributes[attributeName] = attributeIndex;
}
this.uniforms = uniforms;
this.attributes = attributes;
2017-08-12 00:26:25 -04:00
}
readonly uniforms: UniformMap;
readonly attributes: AttributeMap;
readonly program: WebGLProgram;
2017-08-08 14:23:30 -04:00
}
function main() {
const controller = new AppController;
window.addEventListener('load', () => controller.start(), false);
}
main();