Skip to content

Commit

Permalink
Added Sandbox package for API examples
Browse files Browse the repository at this point in the history
  • Loading branch information
LucasDower committed Oct 10, 2023
1 parent a783830 commit 06a915d
Show file tree
Hide file tree
Showing 8 changed files with 200 additions and 76 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,7 @@
/Editor/node_modules
/Editor/.firebase
/Editor/.firebaserc
/Editor/webpack/
/Editor/webpack/

# Sandbox
/Sandbox/node_modules
71 changes: 0 additions & 71 deletions Core/examples/pipeline.ts

This file was deleted.

15 changes: 12 additions & 3 deletions Core/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
export function testExport(x: number, y: number) {
return x + y;
}
import { ImporterFactory } from './src/importers/importers';
import { OtS_Texture } from './src/ots_texture';
import { OtS_VoxelMesh } from './src/ots_voxel_mesh';
import { OtS_VoxelMesh_Converter } from './src/ots_voxel_mesh_converter';

export default {
getImporter: ImporterFactory.GetImporter,
texture: OtS_Texture,

voxelMeshConverter: OtS_VoxelMesh_Converter,
voxelMesh: OtS_VoxelMesh,
};
2 changes: 1 addition & 1 deletion Core/src/importers/importers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { ObjImporter } from './obj_importer';
export type TImporters = 'obj' | 'gltf';


export class ImporterFactor {
export class ImporterFactory {
public static GetImporter(importer: TImporters): IImporter {
switch (importer) {
case 'obj':
Expand Down
44 changes: 44 additions & 0 deletions Sandbox/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { strict as assert } from 'node:assert';
import path from 'node:path';
import OTS from 'ots-core';

import { createOtSTexture, createReadableStream } from './src/util';

(async () => {
// 1. Import a mesh
const pathModel = path.join(__dirname, '../res/samples/skull.obj');
const readableStream = createReadableStream(pathModel);

const importer = OTS.getImporter('obj');
const mesh = await importer.import(readableStream);

// 2. Assign materials
const pathTexture = path.join(__dirname, '../res/samples/skull.jpg');
const texture = createOtSTexture(pathTexture);
assert(texture !== undefined, `Could not parse ${pathTexture}`);

// Update the 'skull' material
const success = mesh.setMaterial({
type: 'textured',
name: 'skull',
texture: texture,
});
assert(success, 'Could not update skull material');

// 3. Construct a voxel mesh from the mesh
const converter = new OTS.voxelMeshConverter();
converter.setConfig({
constraintAxis: 'y',
size: 380,
multisampling: false,
replaceMode: 'keep',
});

const voxelMesh = converter.process(mesh);

// 4. Construct a block mesh from the block
// TODO

// 5. Export the block mesh to a file
// TODO
})();
79 changes: 79 additions & 0 deletions Sandbox/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions Sandbox/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"name": "ots-sandbox",
"private": true,
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"jpeg-js": "^0.4.4",
"ots-core": "file:../Core",
"pngjs": "^7.0.0"
}
}
42 changes: 42 additions & 0 deletions Sandbox/src/util.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import fs from 'node:fs';
import path from 'node:path';

import OTS from 'ots-core';

import { PNG } from 'pngjs';
import { decode as jpegDecode } from 'jpeg-js';

export function createReadableStream(p: fs.PathLike) {
return new ReadableStream({
async start(controller) {
const readStream = fs.createReadStream(p);

readStream.on('data', (chunk) => {
controller.enqueue(chunk);
});

readStream.on('end', () => {
controller.close();
});

readStream.on('error', (err) => {
throw err;
});
},
});
}

export function createOtSTexture(p: fs.PathLike) {
const ext = path.extname(p.toString());
switch (ext) {
case '.jpg':
case '.jpeg': {
var jpegData = fs.readFileSync(p);
const jpeg = jpegDecode(jpegData, {
maxMemoryUsageInMB: undefined,
formatAsRGBA: true,
});
return new OTS.texture(Uint8ClampedArray.from(jpeg.data), jpeg.width, jpeg.width, 'nearest', 'repeat');
}
}
}

0 comments on commit 06a915d

Please sign in to comment.