Skip to content

Commit

Permalink
Merge pull request #35 from xeokit/webgl-refactorings
Browse files Browse the repository at this point in the history
Remove textures and metadata from DTX
  • Loading branch information
xeolabs authored Jul 8, 2024
2 parents 0fb9b87 + 2906b65 commit aa58a92
Show file tree
Hide file tree
Showing 8 changed files with 6 additions and 273 deletions.
54 changes: 1 addition & 53 deletions packages/dtx/src/DTXData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,38 +7,6 @@
*/
export interface DTXData {

/**
* Arbitrary metadata JSON for the [DTX](https://xeokit.github.io/sdk/docs/pages/GLOSSARY.html#dtx) file.
*/
metadata: any;

/**
* Combined data for all textures in the [DTX](https://xeokit.github.io/sdk/docs/pages/GLOSSARY.html#dtx) file.
*/
textureData: Uint8Array;

/**
* For each texture, a pointer to its portion in {@link DTXData.textureData}.
*/
eachTextureDataPortion: Uint32Array;

/**
* For each texture, a set of attributes.
*
* The attributes for each texture are:
*
* * SceneTexture compressed? - 0 (no) or 1 (yes)
* * {@link @xeokit/scene!SceneTexture.mediaType}
* * {@link @xeokit/scene!SceneTexture.width}
* * {@link @xeokit/scene!SceneTexture.height}
* * {@link @xeokit/scene!SceneTexture.minFilter}
* * {@link @xeokit/scene!SceneTexture.magFilter}
* * {@link @xeokit/scene!SceneTexture.wrapS}
* * {@link @xeokit/scene!SceneTexture.wrapT}
* * {@link @xeokit/scene!SceneTexture.wrapR}
*/
eachTextureAttributes: Uint16Array;

/**
* Combined vertex positions array for entire [DTX](https://xeokit.github.io/sdk/docs/pages/GLOSSARY.html#dtx) file.
*/
Expand All @@ -49,11 +17,6 @@ export interface DTXData {
*/
colors: Uint8Array;

/**
* Combined vertex UV coordinates for entire [DTX](https://xeokit.github.io/sdk/docs/pages/GLOSSARY.html#dtx) file.
*/
uvs: Float32Array;

/**
* Combined 8-bit indices for entire [DTX](https://xeokit.github.io/sdk/docs/pages/GLOSSARY.html#dtx) file.
*/
Expand Down Expand Up @@ -84,11 +47,6 @@ export interface DTXData {
*/
edgeIndices32Bit: Uint32Array;

/**
* For each texture set, a set of pointers into eachTextureDataPortion.
*/
eachTextureSetTextures: Int32Array;

/**
* Combined list of all positions dequantization matrices.
*/
Expand All @@ -104,11 +62,6 @@ export interface DTXData {
*/
eachBucketColorsPortion: Uint32Array;

/**
* For each geometry bucket, a pointer to the base of its portion in {@link DTXData.uvs}.
*/
eachBucketUVsPortion: Uint32Array;

/**
* For each geometry bucket, a pointer to the base of its portion in {@link DTXData.indices8Bit}, {@link DTXData.indices16Bit} or {@link DTXData.indices32Bit}.
*/
Expand Down Expand Up @@ -164,11 +117,6 @@ export interface DTXData {
*/
eachMeshOriginsPortion: Uint32Array;

/**
* For each mesh, a pointer to its texture set in {@link DTXData.textureSets}.
*/
eachMeshTextureSet: Int32Array; // Allow -1 values, to indicate no SceneTextureSet

/**
* For each mesh, a set of sixe material attribute values.
*
Expand Down Expand Up @@ -202,4 +150,4 @@ export interface DTXData {
* For each object, a pointer to its forst mesh in DTXata.eachMesh*
*/
eachObjectMeshesPortion: Uint32Array;
}
}
9 changes: 1 addition & 8 deletions packages/dtx/src/DTXDataDeflated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,8 @@
* @private
*/
export interface DTXDataDeflated {
metadata: Buffer;
textureData: Buffer;
eachTextureDataPortion: Buffer;
eachTextureAttributes: Buffer;
positions: Buffer;
colors: Buffer;
uvs: Buffer;
indices8Bit: Buffer;
indices16Bit: Buffer;
indices32Bit: Buffer;
Expand All @@ -19,7 +14,6 @@ export interface DTXDataDeflated {
decodeMatrices: Buffer;
eachBucketPositionsPortion: Buffer;
eachBucketColorsPortion: Buffer;
eachBucketUVsPortion: Buffer;
eachBucketIndicesPortion: Buffer;
eachBucketEdgeIndicesPortion: Buffer;
eachBucketIndicesBitness: Buffer;
Expand All @@ -31,10 +25,9 @@ export interface DTXDataDeflated {
eachMeshGeometriesPortion: Buffer;
eachMeshMatricesPortion: Buffer;
eachMeshOriginsPortion: Buffer;
eachMeshTextureSet: Buffer;
eachMeshMaterialAttributes: Buffer;
eachGeometryId: Buffer;
eachMeshId: Buffer;
eachObjectId: Buffer;
eachObjectMeshesPortion: Buffer;
}
}
19 changes: 1 addition & 18 deletions packages/dtx/src/deflateDTX.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,18 @@ import * as pako from "pako";
* @private
*/
export function deflateDTX(dtxData: DTXData): DTXDataDeflated {
const deflatedJSON = deflateJSON(dtxData.metadata);
const metaModelBytes = pako.deflate(deflatedJSON); // TODO
return <DTXDataDeflated>{
metadata: metaModelBytes,
textureData: pako.deflate(dtxData.textureData.buffer),
eachTextureDataPortion: pako.deflate(dtxData.eachTextureDataPortion.buffer),
eachTextureAttributes: pako.deflate(dtxData.eachTextureAttributes.buffer),
positions: pako.deflate(dtxData.positions.buffer),
positions: pako.deflate(dtxData.positions.buffer),
colors: pako.deflate(dtxData.colors.buffer),
uvs: pako.deflate(dtxData.uvs.buffer),
indices8Bit: pako.deflate(dtxData.indices8Bit.buffer),
indices16Bit: pako.deflate(dtxData.indices16Bit.buffer),
indices32Bit: pako.deflate(dtxData.indices32Bit.buffer),
edgeIndices8Bit: pako.deflate(dtxData.edgeIndices8Bit.buffer),
edgeIndices16Bit: pako.deflate(dtxData.edgeIndices16Bit.buffer),
edgeIndices32Bit: pako.deflate(dtxData.edgeIndices32Bit.buffer),
eachTextureSetTextures: pako.deflate(dtxData.eachTextureSetTextures.buffer),
decodeMatrices: pako.deflate(dtxData.decodeMatrices.buffer),
eachBucketPositionsPortion: pako.deflate(dtxData.eachBucketPositionsPortion.buffer),
eachBucketColorsPortion: pako.deflate(dtxData.eachBucketColorsPortion.buffer),
eachBucketUVsPortion: pako.deflate(dtxData.eachBucketUVsPortion.buffer),
eachBucketIndicesPortion: pako.deflate(dtxData.eachBucketIndicesPortion.buffer),
eachBucketEdgeIndicesPortion: pako.deflate(dtxData.eachBucketEdgeIndicesPortion.buffer),
eachBucketIndicesBitness: pako.deflate(dtxData.eachBucketIndicesBitness.buffer),
Expand All @@ -38,7 +29,6 @@ export function deflateDTX(dtxData: DTXData): DTXDataDeflated {
eachMeshGeometriesPortion: pako.deflate(dtxData.eachMeshGeometriesPortion.buffer),
eachMeshMatricesPortion: pako.deflate(dtxData.eachMeshMatricesPortion.buffer),
eachMeshOriginsPortion: pako.deflate(dtxData.eachMeshOriginsPortion.buffer),
eachMeshTextureSet: pako.deflate(dtxData.eachMeshTextureSet.buffer),
eachMeshMaterialAttributes: pako.deflate(dtxData.eachMeshMaterialAttributes.buffer),
eachGeometryId: pako.deflate(JSON.stringify(dtxData.eachGeometryId)
.replace(/[\u007F-\uFFFF]/g, function (chr) { // Produce only ASCII-chars, so that the data can be inflated later
Expand All @@ -55,10 +45,3 @@ export function deflateDTX(dtxData: DTXData): DTXDataDeflated {
eachObjectMeshesPortion: pako.deflate(dtxData.eachObjectMeshesPortion.buffer)
};
}

function deflateJSON(strings: string[]): string {
return JSON.stringify(strings)
.replace(/[\u007F-\uFFFF]/g, function (chr) { // Produce only ASCII-chars, so that the data can be inflated later
return "\\u" + ("0000" + chr.charCodeAt(0).toString(16)).substr(-4)
});
}
99 changes: 0 additions & 99 deletions packages/dtx/src/dtxToModel.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import {
JPEGMediaType,
LinesPrimitive,
PNGMediaType,
PointsPrimitive,
SolidPrimitive,
SurfacePrimitive,
Expand All @@ -12,8 +10,6 @@ import type {SceneGeometryBucketParams, SceneGeometryCompressedParams, SceneMode
import type {DTXData} from "./DTXData";
import type {FloatArrayParam} from "@xeokit/math";

const NUM_TEXTURE_ATTRIBUTES = 9;

/**
* @private
*/
Expand All @@ -25,16 +21,7 @@ export function dtxToModel(params: {

const dtxData = params.dtxData;
const sceneModel = params.sceneModel;
const dataModel = params.dataModel;

if (dataModel) {
if (dtxData.metadata) {
dataModel.fromJSON(<DataModelParams>dtxData.metadata);
}
}

const numTextures = dtxData.eachTextureDataPortion.length;
const numTextureSets = dtxData.eachTextureSetTextures.length / 5;
const numBuckets = dtxData.eachBucketPositionsPortion.length;
const numMeshes = dtxData.eachMeshGeometriesPortion.length;
const numObjects = dtxData.eachObjectMeshesPortion.length;
Expand All @@ -46,89 +33,6 @@ export function dtxToModel(params: {
[key: string]: boolean
} = {};

// Create textures

for (let textureIndex = 0; textureIndex < numTextures; textureIndex++) {

const atLastTexture = (textureIndex === (numTextures - 1));
const textureDataPortionStart = dtxData.eachTextureDataPortion[textureIndex];
const textureDataPortionEnd = atLastTexture ? dtxData.textureData.length : (dtxData.eachTextureDataPortion[textureIndex + 1]);
const textureDataPortionSize = textureDataPortionEnd - textureDataPortionStart;
const textureDataPortionExists = (textureDataPortionSize > 0);
const textureAttrBaseIdx = (textureIndex * NUM_TEXTURE_ATTRIBUTES);

const compressed = (dtxData.eachTextureAttributes[textureAttrBaseIdx] === 1);
const mediaType = dtxData.eachTextureAttributes[textureAttrBaseIdx + 1];
const width = dtxData.eachTextureAttributes[textureAttrBaseIdx + 2];
const height = dtxData.eachTextureAttributes[textureAttrBaseIdx + 3];
const minFilter = dtxData.eachTextureAttributes[textureAttrBaseIdx + 4];
const magFilter = dtxData.eachTextureAttributes[textureAttrBaseIdx + 5]; // LinearFilter | NearestFilter
const wrapS = dtxData.eachTextureAttributes[textureAttrBaseIdx + 6]; // ClampToEdgeWrapping | MirroredRepeatWrapping | RepeatWrapping
const wrapT = dtxData.eachTextureAttributes[textureAttrBaseIdx + 7]; // ClampToEdgeWrapping | MirroredRepeatWrapping | RepeatWrapping
const wrapR = dtxData.eachTextureAttributes[textureAttrBaseIdx + 8]; // ClampToEdgeWrapping | MirroredRepeatWrapping | RepeatWrapping

if (textureDataPortionExists) {

const imageDataSubarray = new Uint8Array(dtxData.textureData.subarray(textureDataPortionStart, textureDataPortionEnd));
const arrayBuffer = imageDataSubarray.buffer;
const textureId = `texture-${textureIndex}`;

if (compressed) {

sceneModel.createTexture({
id: textureId,
buffers: [arrayBuffer],
minFilter,
magFilter,
wrapS,
wrapT,
wrapR
});

} else {

const mimeType = mediaType === JPEGMediaType ? "image/jpeg" : (mediaType === PNGMediaType ? "image/png" : "image/gif");
const blob = new Blob([arrayBuffer], {type: mimeType});
const urlCreator = window.URL || window.webkitURL;
const imageUrl = urlCreator.createObjectURL(blob);
const img = document.createElement('img');
img.src = imageUrl;

sceneModel.createTexture({
id: textureId,
image: img,
mediaType,
minFilter,
magFilter,
wrapS,
wrapT,
wrapR
});
}
}
}

// Create texture sets

for (let textureSetIndex = 0; textureSetIndex < numTextureSets; textureSetIndex++) {

const eachTextureSetTexturesIndex = textureSetIndex * 5; // Five textures per set
const textureSetId = `textureSet-${textureSetIndex}`;
const colorTextureIndex = dtxData.eachTextureSetTextures[eachTextureSetTexturesIndex];
const metallicRoughnessTextureIndex = dtxData.eachTextureSetTextures[eachTextureSetTexturesIndex + 1];
const normalsTextureIndex = dtxData.eachTextureSetTextures[eachTextureSetTexturesIndex + 2];
const emissiveTextureIndex = dtxData.eachTextureSetTextures[eachTextureSetTexturesIndex + 3];
const occlusionTextureIndex = dtxData.eachTextureSetTextures[eachTextureSetTexturesIndex + 4];

sceneModel.createTextureSet({
id: textureSetId,
colorTextureId: colorTextureIndex >= 0 ? `texture-${colorTextureIndex}` : undefined,
normalsTextureId: normalsTextureIndex >= 0 ? `texture-${normalsTextureIndex}` : undefined,
metallicRoughnessTextureId: metallicRoughnessTextureIndex >= 0 ? `texture-${metallicRoughnessTextureIndex}` : undefined,
emissiveTextureId: emissiveTextureIndex >= 0 ? `texture-${emissiveTextureIndex}` : undefined,
occlusionTextureId: occlusionTextureIndex >= 0 ? `texture-${occlusionTextureIndex}` : undefined
});
}

// Iterate objects

Expand All @@ -148,8 +52,6 @@ export function dtxToModel(params: {

const geometryIndex = dtxData.eachMeshGeometriesPortion[meshIndex];
const atLastGeometry = (geometryIndex === (numGeometries - 1));
const textureSetIndex = dtxData.eachMeshTextureSet[meshIndex];
const textureSetId = (textureSetIndex >= 0) ? `textureSet-${textureSetIndex}` : undefined;

const meshColor = decompressColor(dtxData.eachMeshMaterialAttributes.subarray((meshIndex * 6), (meshIndex * 6) + 3));
const meshOpacity = dtxData.eachMeshMaterialAttributes[(meshIndex * 6) + 3] / 255.0;
Expand Down Expand Up @@ -255,7 +157,6 @@ export function dtxToModel(params: {
sceneModel.createMesh({
id: meshId,
geometryId,
textureSetId,
matrix: meshMatrix,
color: meshColor,
metallic: meshMetallic,
Expand Down
12 changes: 2 additions & 10 deletions packages/dtx/src/inflateDTX.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,17 @@ export function inflateDTX(dtxDataDeflated: DTXDataDeflated): DTXData {
}

return <DTXData>{
metadata: JSON.parse(pako.inflate(dtxDataDeflated.metadata, {to: 'string'})),
textureData: new Uint8Array(inflate(dtxDataDeflated.textureData)),
eachTextureDataPortion: new Uint32Array(inflate(dtxDataDeflated.eachTextureDataPortion)),
eachTextureAttributes: new Uint16Array(inflate(dtxDataDeflated.eachTextureAttributes)),
positions: new Uint16Array(inflate(dtxDataDeflated.positions)),
colors: new Uint8Array(inflate(dtxDataDeflated.colors)),
uvs: new Float32Array(inflate(dtxDataDeflated.uvs)),
indices8Bit: new Uint8Array(inflate(dtxDataDeflated.indices8Bit)),
indices16Bit: new Uint16Array(inflate(dtxDataDeflated.indices16Bit)),
indices32Bit: new Uint32Array(inflate(dtxDataDeflated.indices32Bit)),
edgeIndices8Bit: new Uint8Array(inflate(dtxDataDeflated.edgeIndices8Bit)),
edgeIndices16Bit: new Uint16Array(inflate(dtxDataDeflated.edgeIndices16Bit)),
edgeIndices32Bit: new Uint32Array(inflate(dtxDataDeflated.edgeIndices32Bit)),
eachTextureSetTextures: new Int32Array(inflate(dtxDataDeflated.eachTextureSetTextures)),
decodeMatrices: new Float32Array(inflate(dtxDataDeflated.decodeMatrices)),
decodeMatrices: new Float32Array(inflate(dtxDataDeflated.decodeMatrices)),
eachBucketPositionsPortion: new Uint32Array(inflate(dtxDataDeflated.eachBucketPositionsPortion)),
eachBucketColorsPortion: new Uint32Array(inflate(dtxDataDeflated.eachBucketColorsPortion)),
eachBucketUVsPortion: new Uint32Array(inflate(dtxDataDeflated.eachBucketUVsPortion)),
eachBucketIndicesPortion: new Uint32Array(inflate(dtxDataDeflated.eachBucketIndicesPortion)),
eachBucketEdgeIndicesPortion: new Uint32Array(inflate(dtxDataDeflated.eachBucketEdgeIndicesPortion)),
eachBucketIndicesBitness: new Uint8Array(inflate(dtxDataDeflated.eachBucketIndicesBitness)),
Expand All @@ -41,11 +34,10 @@ export function inflateDTX(dtxDataDeflated: DTXDataDeflated): DTXData {
eachMeshGeometriesPortion: new Uint32Array(inflate(dtxDataDeflated.eachMeshGeometriesPortion)),
eachMeshMatricesPortion: new Uint32Array(inflate(dtxDataDeflated.eachMeshMatricesPortion)),
eachMeshOriginsPortion: new Uint32Array(inflate(dtxDataDeflated.eachMeshOriginsPortion)),
eachMeshTextureSet: new Int32Array(inflate(dtxDataDeflated.eachMeshTextureSet)),
eachMeshMaterialAttributes: new Uint8Array(inflate(dtxDataDeflated.eachMeshMaterialAttributes)),
eachGeometryId: JSON.parse(pako.inflate(dtxDataDeflated.eachGeometryId, {to: 'string'})),
eachMeshId: JSON.parse(pako.inflate(dtxDataDeflated.eachMeshId, {to: 'string'})),
eachObjectId: JSON.parse(pako.inflate(dtxDataDeflated.eachObjectId, {to: 'string'})),
eachObjectMeshesPortion: new Uint32Array(inflate(dtxDataDeflated.eachObjectMeshesPortion)),
};
}
}
Loading

0 comments on commit aa58a92

Please sign in to comment.