ts-game-engine
Version:
Simple WebGL game/render engine written in TypeScript
105 lines (104 loc) • 4.63 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const Mesh_1 = require("./Mesh");
const gl_matrix_1 = require("gl-matrix");
const Constants_1 = require("../Constants");
class MDLMesh extends Mesh_1.Mesh {
constructor(scene, mdlFileURL, onFinishCallback) {
super(scene);
this.MAGIC = 0x4C444D20; // " MDL"
this.FORMAT_VERSION = 1;
this.FinishLoading = (evt) => {
const request = evt.target;
if (request.readyState !== request.DONE)
return;
if (request.status !== 200)
return;
const blob = request.response;
const reader = new FileReader();
reader.readAsArrayBuffer(blob);
reader.addEventListener("loadend", this.FinishReading);
};
this.FinishReading = (evt) => {
var _a, _b;
const reader = evt.target;
if (reader.readyState !== reader.DONE) {
throw new Error(`Loading MDL Error: ${evt}`);
}
const buffer = reader.result;
const view = new DataView(buffer);
// Start reading the file data ----------------------------------------------------------------------------------------
let o = 0;
let magic = view.getUint32(o, true);
o += 4;
if (magic !== this.MAGIC) {
throw new Error("Invalid MDL file.");
}
let version = view.getUint32(o, true);
if (version !== this.FORMAT_VERSION) {
throw new Error("Invalid MDL file version.");
}
o = 0x10;
const boundsMin = gl_matrix_1.vec3.create();
boundsMin[0] = view.getFloat32(o, true);
o += 4;
boundsMin[1] = view.getFloat32(o, true);
o += 4;
boundsMin[2] = view.getFloat32(o, true);
o += 4;
const boundsMax = gl_matrix_1.vec3.create();
boundsMax[0] = view.getFloat32(o, true);
o += 4;
boundsMax[1] = view.getFloat32(o, true);
o += 4;
boundsMax[2] = view.getFloat32(o, true);
o += 4;
const center = gl_matrix_1.vec3.create();
center[0] = view.getFloat32(o, true);
o += 4;
center[1] = view.getFloat32(o, true);
o += 4;
center[2] = view.getFloat32(o, true);
o += 4;
const radius = view.getFloat32(o, true);
o += 4;
const vertexCount = view.getInt32(o, true);
o += 4;
const indexCount = view.getInt32(o, true);
o += 4;
const vertexFormat = view.getUint32(o, true);
o += 4;
const indexFormat = view.getUint8(o);
o += 4;
if (indexFormat !== 0 /* UInt16 */) {
throw new Error("16bit indices in MDL are currently not supported.");
}
let vertexDataSize = 0;
if ((vertexFormat & 1 /* Position */) !== 0)
vertexDataSize += Mesh_1.VERTEX_POSITION_SIZE;
if ((vertexFormat & 2 /* Color */) !== 0)
vertexDataSize += Mesh_1.VERTEX_COLOR_SIZE;
if ((vertexFormat & 4 /* Normal */) !== 0)
vertexDataSize += Mesh_1.VERTEX_NORMAL_SIZE;
if ((vertexFormat & 8 /* UV0 */) !== 0)
vertexDataSize += Mesh_1.VERTEX_UV_SIZE;
if ((vertexFormat & 16 /* UV1 */) !== 0)
vertexDataSize += Mesh_1.VERTEX_UV_SIZE;
const vertexData = new Float32Array(buffer, o, vertexDataSize * vertexCount);
this.SetVertexData(vertexFormat, 4 /* Triangles */, vertexCount, vertexData, false);
o += vertexDataSize * vertexCount * Constants_1.FLOAT_SIZE;
const indexData = new Uint16Array(buffer, o, indexCount);
this.SetIndexData(indexData);
this.SetBounds(boundsMin, boundsMax); // { center: center, radius: radius }
this.isLoaded = true;
(_b = (_a = this).onFinishCallback) === null || _b === void 0 ? void 0 : _b.call(_a);
};
this.onFinishCallback = onFinishCallback;
let request = new XMLHttpRequest();
request.addEventListener("load", this.FinishLoading);
request.open("GET", mdlFileURL, true);
request.responseType = "blob";
request.send(null);
}
}
exports.MDLMesh = MDLMesh;