-
-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added
Sandbox
package for API examples
- Loading branch information
1 parent
a783830
commit 06a915d
Showing
8 changed files
with
200 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
})(); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
} | ||
} | ||
} |