JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 17
  • Score
    100M100P100Q50777F
  • License ISC

Generates WebGL-compatible Structs and StructBuffers from a template file.

Package Exports

  • gulp-structify
  • gulp-structify/buf
  • gulp-structify/struct
  • gulp-structify/template

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (gulp-structify) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

gulp-structify

Generates WebGL-compatible structs and struct buffers from a template file.

NPM

Install

npm install gulp-structify --save-dev

Create a template file:

Suppose we want to create a Vec2 struct backed by a Float32Array. We start with a minimal template file called Vec2.template.ts:

import Template from "gulp-structify/template";

/**
 * A two-dimensional vector with (x,y) components.
 */
export class Vec2 extends Template<Float32Array> {
    /**
     * The X component of this Vec2.
     */
    x: number;
    /**
     * The Y component of this Vec2.
     */
    y: number;
}

Note: the template should not include a constructor or any static methods.

Add methods to the template

Now suppose we want to add a dot method to our Vec2 struct. We do this by adding the method to our template:

export class Vec2 extends Template<Float32Array> {
    // ... 

    /**
     * Computes the dot product of this Vec2 with the other Vec2.
     */
    dot(other: Vec2): number {
        return this.x * other.x + this.y * other.y;
    }
}

Note: gulp-structify automatically generates the following methods:

  • set(other: this)
  • add(other: this)
  • subtract(other: this)
  • mulScalar(k: number)
  • divScalar(k: number)
  • equals(other: this)
  • equalsScalar(k: number)
  • epsilonEquals(other: this, e: number)
  • epsilonEqualsScalar(k: number, e: number)
  • toString()

Create gulp task

var gulp = require("gulp");
var rename = require("gulp-rename");
var structify = require("gulp-structify");

// Directory where template file is located
var directory = "./"; 

gulp.task("structify", function () {
    // Search for files ending in .template.ts
    return gulp.src(directory + "*.template.ts")
        // Generate struct file
        .pipe(structify())
        // Remove ".template" from filename
        .pipe(rename(function(p) {
            let base = p.basename;
            let length = base.length - ".template".length;
            p.basename = base.substr(0, length);
        })) 
        // Output to same directory to preserve imports
        .pipe(gulp.dest(directory));
});

Run gulp task

gulp structify

Examples

Template Output Usage
point.template.ts point.ts
vec2.template.ts vec2.ts vec2-usage.ts
color.template.ts color.ts