Skip to content

Commit

Permalink
Webworker changes.
Browse files Browse the repository at this point in the history
  • Loading branch information
repalash committed Nov 12, 2020
1 parent f381be4 commit 1d7c4ca
Show file tree
Hide file tree
Showing 9 changed files with 151 additions and 91 deletions.
47 changes: 28 additions & 19 deletions .idea/workspace.xml

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

135 changes: 73 additions & 62 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,41 +1,28 @@
## xatlas-web
## xatlas-web

xatlas-web is a wrapper for xatlas for js. It uses `emcc` to compile WASM from C++ codebase.
xatlas-web is a wrapper for xatlas for js. It uses `emcc` to compile WASM from C++ codebase and can be used as a simple js module or as a webworker with [comlink](https://github.com/GoogleChromeLabs/comlink)

xatlas is a small C++11 library with no external dependencies that generates unique texture coordinates suitable for baking lightmaps or texture painting.
[xatlas](https://github.com/jpcy/xatlas) is a small C++11 library with no external dependencies that generates unique texture coordinates suitable for baking lightmaps or texture painting.
It is an independent fork of [thekla_atlas](https://github.com/Thekla/thekla_atlas), used by [The Witness](https://en.wikipedia.org/wiki/The_Witness_(2016_video_game)).

## Screenshots

#### Example - [Cesium Milk Truck](https://github.com/KhronosGroup/glTF-Sample-Models)
| Viewer | Random packing | Brute force packing |
|---|---|---|
| [![Viewer](https://user-images.githubusercontent.com/3744372/69908461-48cace80-143e-11ea-8b73-efea5a9f036e.png)](https://user-images.githubusercontent.com/3744372/69908460-48323800-143e-11ea-8b18-58087493c8e9.png) | ![Random packing](https://user-images.githubusercontent.com/3744372/68638607-d4db8b80-054d-11ea-8238-845d94789a2d.gif) | ![Brute force packing](https://user-images.githubusercontent.com/3744372/68638614-da38d600-054d-11ea-82d9-43e558c46d50.gif) |

#### Example - [Godot Third Person Shooter demo](https://github.com/godotengine/tps-demo)
[![Godot TPS](https://user-images.githubusercontent.com/3744372/69908463-48cace80-143e-11ea-8035-b669d1a455f6.png)](https://user-images.githubusercontent.com/3744372/69908462-48cace80-143e-11ea-8946-a2c596ec8028.png)

#### [Graphite/Geogram](http://alice.loria.fr/index.php?option=com_content&view=article&id=22)
![Graphite/Geogram](https://user-images.githubusercontent.com/19478253/69903392-c0deb900-1398-11ea-8a52-c211bc7803a9.gif)

## How to use

### Usage for Web and JS

* Add library to your `package.json`
```
```json
"devDependencies": {
"xatlas-web": "git+https://github.com/palashbansal96/xatlas.git#build_v1"
}
```
* Import or require class `XAtlasAPI` (from `dist/xatlas_web.js`) in your codebase and use wrapper functions for xatlas. See comments in `source/web/index.js`.
* Copy the file `dist/xatlas_web.wasm`, eg for webpack, install [CopyPlugin](https://webpack.js.org/plugins/copy-webpack-plugin/) and add this to the config
```
```javascript
new CopyPlugin({
patterns: [
{ from: path.resolve('node_modules', 'xatlas-web','dist','xatlas_web.wasm'), to: path.resolve(BUILD_PATH, 'libs/') },
],
}),
})
```
* Need to `locateFile` parameter function to the `XAtlasAPI` constructor if the `wasm` file is renamed or is not available from the website root.

Expand All @@ -45,65 +32,89 @@ It is an independent fork of [thekla_atlas](https://github.com/Thekla/thekla_atl
* Run `npm run build` - this should generate files in the `dist` folder.
A build is already committed on the `dist` branch.

### Building Native

Premake is used. For CMake support, see [here](https://github.com/cpp-pm/xatlas).

Integration into an existing build is simple, only `xatlas.cpp` and `xatlas.h` are required. They can be found in [source/xatlas](https://github.com/jpcy/xatlas/blob/master/source/xatlas)

#### Windows

Run `build\premake.bat`. Open `build\vs2019\xatlas.sln`.

Note: change the build configuration to "Release". The default - "Debug" - severely degrades performance.

#### Linux

Required packages: `libgl1-mesa-dev libgtk-3-dev xorg-dev`.

Install Premake version 5. Run `premake5 gmake`, `cd build/gmake`, `make`.

### Generate an atlas (JS API)

First import the API class `import {XAtlasAPI} from "xatlas-web"` and create an object.
```javascript
const xAtlas = new XAtlasAPI(()=>{
console.log("on module loadede");
}, (path, dir)=>{
if (path === "xatlas_web.wasm") return "libs/" + path;
return dir + path;
}, (mode, progress)=>{
console.log("on progress ", mode, progress);
}
);
```
Use the object `xAtlas` as:
1. Create an empty atlas with `createAtlas`.
2. Add one or more meshes with `addMesh`.
3. Call `generateAtlas`. Meshes are segmented into charts, which are parameterized and packed into an atlas. The updated vertex and index buffers are returned along with the mesh object.
4. See `source/web/index.js` for complete API and example.
The returned buffers are of different size than the inputs.
Cleanup with `destroyAtlas`. This also does a leak check if enabled in `build-web.sh`. see line 40.

### Generate an atlas (simple API)

1. Create an empty atlas with `xatlas::Create`.
2. Add one or more meshes with `xatlas::AddMesh`.
3. Call `xatlas::Generate`. Meshes are segmented into charts, which are parameterized and packed into an atlas.

The `xatlas::Atlas` instance created in the first step now contains the result: each input mesh added by `xatlas::AddMesh` has a corresponding new mesh with a UV channel. New meshes have more vertices (the UV channel adds seams), but the same number of indices.

Cleanup with `xatlas::Destroy`.

[Example code here.](https://github.com/jpcy/xatlas/blob/master/source/examples/example.cpp)

### Generate an atlas (tools/editor integration API)

Instead of calling `xatlas::Generate`, the following functions can be called in sequence:

1. `xatlas::ComputeCharts`: meshes are segmented into charts and parameterized.
2. `xatlas::PackCharts`: charts are packed into one or more atlases.

All of these functions take a progress callback. Return false to cancel.

You can call any of these functions multiple times, followed by the proceeding functions, to re-generate the atlas. E.g. calling `xatlas::PackCharts` multiple times to tweak options like unit to texel scale and resolution.

See the [viewer](https://github.com/jpcy/xatlas/tree/master/source/examples/viewer) for example code.
### Use as webworker, in JS API.
This should be preferable, does not hang the web browser tab.
Load the xatlas_web.js file as web worker. For webpack, add to config:
```javascript
rules: [
{
test: /\.worker\.js/,
use: {
loader: "worker-loader",
options: { fallback: true }
}
}
]
```
Use in js example:
```javascript
import { wrap, proxy } from "comlink";
import XAtlasWorker from "xatlas-web";
/**
* @class XAtlasAPI
*/
const XAtlasAPI = wrap(new XAtlasWorker());
let xAtlas = null;

// use in function
async () => {
if(xAtlas == null){
xAtlas = await new XAtlasAPI(
proxy(()=>console.log("loaded")),
proxy((path, dir)=>(path === "xatlas_web.wasm" ? "http://localhost:8000/libs/"+path:null)),
proxy((mode, progress)=> console.log("on progress ", mode, progress))
);
}
while (!(await xAtlas.loaded)){
await new Promise(r => setTimeout(r, 500)); // wait for load
}
await xAtlas.createAtlas();
// Add mesh
await xAtlas.addMesh(arguments);
let meshes = await xAtlas.generateAtlas(chartOptions, packOptions, true);
// Process meshes
await xAtlas.destroyAtlas();
}
```

### Pack multiple atlases into a single atlas
### Pack multiple atlases into a single atlas (c++)

1. Create an empty atlas with `xatlas::Create`.
2. Add one or more meshes with `xatlas::AddUvMesh`.
3. Call `xatlas::PackCharts`.

[Example code here.](https://github.com/jpcy/xatlas/blob/master/source/examples/example_uvmesh.cpp)
Full Readme of `xatlas` at its [main repository](https://github.com/jpcy/xatlas/blob/master/README.md).

## Screenshots

#### Example - [Cesium Milk Truck](https://github.com/KhronosGroup/glTF-Sample-Models)
| Viewer | Random packing | Brute force packing |
|---|---|---|
| [![Viewer](https://user-images.githubusercontent.com/3744372/69908461-48cace80-143e-11ea-8b73-efea5a9f036e.png)](https://user-images.githubusercontent.com/3744372/69908460-48323800-143e-11ea-8b18-58087493c8e9.png) | ![Random packing](https://user-images.githubusercontent.com/3744372/68638607-d4db8b80-054d-11ea-8238-845d94789a2d.gif) | ![Brute force packing](https://user-images.githubusercontent.com/3744372/68638614-da38d600-054d-11ea-82d9-43e558c46d50.gif) |


## Technical information / related publications

Expand Down
8 changes: 6 additions & 2 deletions build_web.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,26 @@ echo "============================================="
emcc \
-std=c++1y \
-DXA_MULTITHREADED=0 \
-DNDEBUG \
${OPTIMIZE} \
--bind \
--no-entry \
-s ERROR_ON_UNDEFINED_SYMBOLS=0 \
-s ALLOW_MEMORY_GROWTH=1 \
-s MALLOC=emmalloc \
-s MODULARIZE=1 \
-s ENVIRONMENT='web,worker' \
-s ENVIRONMENT='worker' \
-s EXPORT_NAME="createXAtlasModule" \
-o ./source/web/build/xatlas_web.js \
--js-library ./source/web/jslib.js \
source/web/*.cpp \
source/xatlas/xatlas.cpp \
\
-s ASSERTIONS=1 \
-DNDEBUG \
# -s TOTAL_MEMORY=278mb \
# -D SANITIZE_ADDRESS_CHECK \
# -fsanitize=address \
# -g3
# Uncomment above line for leak checking

# Move artifacts
Expand Down
2 changes: 1 addition & 1 deletion dist/xatlas_web.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/xatlas_web.js.map

Large diffs are not rendered by default.

13 changes: 13 additions & 0 deletions package-lock.json

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

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
"webpack": "^4.44.2",
"webpack-cli": "^3.3.12"
},
"dependencies": {
"comlink": "^4.3.0"
},
"repository": {
"type": "git",
"url": "git+https://github.com/palashbansal96/xatlas.git"
Expand Down
Loading

0 comments on commit 1d7c4ca

Please sign in to comment.