Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature metadata extension (EXT_feature_metadata) #3

Merged
merged 17 commits into from
Feb 24, 2021
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
682 changes: 682 additions & 0 deletions extensions/2.0/Vendor/EXT_feature_metadata/0.0.0/README.md

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
## Texture Metadata Example: Microcosm

This glTF example is a mesh that represents a lot of different types of
terrain in one small tile

This example simulates using per-texel metadata for land cover
classification as well as the Normalized Difference Vegetation Index (NDVI)
values.

The data here is hand-made until we can get more realistic data.

### Script to generate metadata

To generate the metadata buffer, run `node microcosm-metadata.js`. This
will overwrite `microcosm.bin` and print out JSON to add to the bufferViews
of the glTF.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
'use strict';

const fs = require('fs');

/**
* Need to know these lengths:
* - total buffer length
*
* - LULC Name bufferView length and offsets
* - LULC Name offsets bufferView length and offsets
* - LULC Color bufferView length and offsets
*/

// This is how I mapped the color values in GIMP. Kinda haphazard admittedly.
const landCoverValues = {
0: {
name: "Grassland",
color: [118, 163, 11]
},
32: {
name: "Rocky Ground",
color: [110, 110, 110]
},
64: {
name: "Desert",
color: [212, 209, 182]
},
128: {
name: "River",
color: [82, 173, 204]
},
208: {
name: "Forest",
color: [50, 84, 51]
},
255: {
name: "Building",
color: [194, 194, 194]
}
};

const unusedLandCover = {
name: "Unused",
color: [0, 0, 0]
};

const numberOfLandCoverFeatures = 256;
const sizeOfColor = 3;

function makeLandCoverColors() {
const bufferLength = numberOfLandCoverFeatures * sizeOfColor;
const buffer = Buffer.alloc(bufferLength);
for (let i = 0; i < numberOfLandCoverFeatures; i++) {
const feature = landCoverValues[i];
if (feature !== undefined) {
const [r, g, b] = feature.color;
buffer[3 * i] = r;
buffer[3 * i + 1] = g;
buffer[3 * i + 2] = b;
}

// Otherwise the default color of 0, 0, 0 is used,
// but the buffer is allocated with all 0s so no work is needed here.
}
return buffer;
}

function makeLandCoverNames() {
const offsets = new Uint32Array(numberOfLandCoverFeatures + 1);
let nameText = '';
let currentOffset = 0;
for (let i = 0; i < numberOfLandCoverFeatures; i++) {
let feature = landCoverValues[i];
if (feature === undefined) {
feature = unusedLandCover;
}

const name = feature.name;
const nameLength = Buffer.byteLength(name, 'utf8');

offsets[i] = currentOffset;
currentOffset += nameLength;

nameText += name;
}
offsets[numberOfLandCoverFeatures] = currentOffset;

const namesBuffer = Buffer.from(nameText, 'utf8');

// Convert from Uint32Array -> Buffer (which is a Uint8Array)
const offsetsBuffer = Buffer.from(offsets.buffer);
return [namesBuffer, offsetsBuffer];
}

function makeBufferViews() {
const [landCoverNames, landCoverOffsets] = makeLandCoverNames();
const landCoverColor = makeLandCoverColors();

return [
{
name: "LULC Name",
bufferView: landCoverNames
},
{
name: "LULC Name Offsets",
bufferView: landCoverOffsets,
},
{
name: "LULC Color",
bufferView: landCoverColor
}
]
}

function makeBinFile() {
const bufferViewInfos = makeBufferViews();
const bufferViews = [];
const stats = [];
let offset = 0;
for (const bufferViewInfo of bufferViewInfos) {
if (offset % 8 != 0) {
const paddingBytes = 8 - (offset % 8);
console.log(`Offset not aligned, adding ${paddingBytes} of padding`);
const padding = Buffer.alloc(paddingBytes);
bufferViews.push(padding);
offset += paddingBytes;
}

const bufferView = bufferViewInfo.bufferView;
const byteLength = bufferView.byteLength;
stats.push({
name: bufferViewInfo.name,
buffer: 1,
byteLength: byteLength,
byteOffset: offset
});
offset += byteLength;

bufferViews.push(bufferView);
}

const buffer = Buffer.concat(bufferViews);
fs.writeFileSync('microcosm-metadata.bin', buffer, 'utf8');

console.log(JSON.stringify(stats, undefined, 4));
console.log('totalLength:', buffer.byteLength);
}

makeBinFile();
Binary file not shown.
Loading