Skip to content

Commit c930e39

Browse files
authored
Remove flipv code from glb loader (#6747)
1 parent 5c27524 commit c930e39

File tree

1 file changed

+19
-70
lines changed

1 file changed

+19
-70
lines changed

src/framework/parsers/glb-parser.js

+19-70
Original file line numberDiff line numberDiff line change
@@ -395,53 +395,6 @@ const generateNormals = (sourceDesc, indices) => {
395395
};
396396
};
397397

398-
const flipTexCoordVs = (vertexBuffer) => {
399-
let i, j;
400-
401-
const floatOffsets = [];
402-
const shortOffsets = [];
403-
const byteOffsets = [];
404-
for (i = 0; i < vertexBuffer.format.elements.length; ++i) {
405-
const element = vertexBuffer.format.elements[i];
406-
if (element.name === SEMANTIC_TEXCOORD0 ||
407-
element.name === SEMANTIC_TEXCOORD1) {
408-
switch (element.dataType) {
409-
case TYPE_FLOAT32:
410-
floatOffsets.push({ offset: element.offset / 4 + 1, stride: element.stride / 4 });
411-
break;
412-
case TYPE_UINT16:
413-
shortOffsets.push({ offset: element.offset / 2 + 1, stride: element.stride / 2 });
414-
break;
415-
case TYPE_UINT8:
416-
byteOffsets.push({ offset: element.offset + 1, stride: element.stride });
417-
break;
418-
}
419-
}
420-
}
421-
422-
const flip = (offsets, type, one) => {
423-
const typedArray = new type(vertexBuffer.storage);
424-
for (i = 0; i < offsets.length; ++i) {
425-
let index = offsets[i].offset;
426-
const stride = offsets[i].stride;
427-
for (j = 0; j < vertexBuffer.numVertices; ++j) {
428-
typedArray[index] = one - typedArray[index];
429-
index += stride;
430-
}
431-
}
432-
};
433-
434-
if (floatOffsets.length > 0) {
435-
flip(floatOffsets, Float32Array, 1.0);
436-
}
437-
if (shortOffsets.length > 0) {
438-
flip(shortOffsets, Uint16Array, 65535);
439-
}
440-
if (byteOffsets.length > 0) {
441-
flip(byteOffsets, Uint8Array, 255);
442-
}
443-
};
444-
445398
// given a texture, clone it
446399
// NOTE: CPU-side texture data will be shared but GPU memory will be duplicated
447400
const cloneTexture = (texture) => {
@@ -479,7 +432,7 @@ const cloneTextureAsset = (src) => {
479432
return result;
480433
};
481434

482-
const createVertexBufferInternal = (device, sourceDesc, flipV) => {
435+
const createVertexBufferInternal = (device, sourceDesc) => {
483436
const positionDesc = sourceDesc[SEMANTIC_POSITION];
484437
if (!positionDesc) {
485438
// ignore meshes without positions
@@ -572,16 +525,12 @@ const createVertexBufferInternal = (device, sourceDesc, flipV) => {
572525
}
573526
}
574527

575-
if (flipV) {
576-
flipTexCoordVs(vertexBuffer);
577-
}
578-
579528
vertexBuffer.unlock();
580529

581530
return vertexBuffer;
582531
};
583532

584-
const createVertexBuffer = (device, attributes, indices, accessors, bufferViews, flipV, vertexBufferDict) => {
533+
const createVertexBuffer = (device, attributes, indices, accessors, bufferViews, vertexBufferDict) => {
585534

586535
// extract list of attributes to use
587536
const useAttributes = {};
@@ -630,7 +579,7 @@ const createVertexBuffer = (device, attributes, indices, accessors, bufferViews,
630579
}
631580

632581
// create and store it in the dictionary
633-
vb = createVertexBufferInternal(device, sourceDesc, flipV);
582+
vb = createVertexBufferInternal(device, sourceDesc);
634583
vertexBufferDict[vbKey] = vb;
635584
}
636585

@@ -777,7 +726,7 @@ const createDracoMesh = (device, primitive, accessors, bufferViews, meshVariants
777726
return result;
778727
};
779728

780-
const createMesh = (device, gltfMesh, accessors, bufferViews, flipV, vertexBufferDict, meshVariants, meshDefaultMaterials, assetOptions, promises) => {
729+
const createMesh = (device, gltfMesh, accessors, bufferViews, vertexBufferDict, meshVariants, meshDefaultMaterials, assetOptions, promises) => {
781730
const meshes = [];
782731

783732
gltfMesh.primitives.forEach((primitive) => {
@@ -788,7 +737,7 @@ const createMesh = (device, gltfMesh, accessors, bufferViews, flipV, vertexBuffe
788737
} else {
789738
// handle uncompressed mesh
790739
let indices = primitive.hasOwnProperty('indices') ? getAccessorData(accessors[primitive.indices], bufferViews, true) : null;
791-
const vertexBuffer = createVertexBuffer(device, primitive.attributes, indices, accessors, bufferViews, flipV, vertexBufferDict);
740+
const vertexBuffer = createVertexBuffer(device, primitive.attributes, indices, accessors, bufferViews, vertexBufferDict);
792741
const primitiveType = getPrimitiveType(primitive);
793742

794743
// build the mesh
@@ -1163,7 +1112,7 @@ const extensionIridescence = (data, material, textures) => {
11631112
}
11641113
};
11651114

1166-
const createMaterial = (gltfMaterial, textures, flipV) => {
1115+
const createMaterial = (gltfMaterial, textures) => {
11671116
const material = new StandardMaterial();
11681117

11691118
// glTF doesn't define how to occlude specular
@@ -1692,7 +1641,7 @@ const createSkins = (device, gltf, nodes, bufferViews) => {
16921641
});
16931642
};
16941643

1695-
const createMeshes = (device, gltf, bufferViews, flipV, options) => {
1644+
const createMeshes = (device, gltf, bufferViews, options) => {
16961645
// dictionary of vertex buffers to avoid duplicates
16971646
const vertexBufferDict = {};
16981647
const meshVariants = {};
@@ -1701,7 +1650,7 @@ const createMeshes = (device, gltf, bufferViews, flipV, options) => {
17011650

17021651
const valid = (!options.skipMeshes && gltf?.meshes?.length && gltf?.accessors?.length && gltf?.bufferViews?.length);
17031652
const meshes = valid ? gltf.meshes.map((gltfMesh) => {
1704-
return createMesh(device, gltfMesh, gltf.accessors, bufferViews, flipV, vertexBufferDict, meshVariants, meshDefaultMaterials, options, promises);
1653+
return createMesh(device, gltfMesh, gltf.accessors, bufferViews, vertexBufferDict, meshVariants, meshDefaultMaterials, options, promises);
17051654
}) : [];
17061655

17071656
return {
@@ -1712,7 +1661,7 @@ const createMeshes = (device, gltf, bufferViews, flipV, options) => {
17121661
};
17131662
};
17141663

1715-
const createMaterials = (gltf, textures, options, flipV) => {
1664+
const createMaterials = (gltf, textures, options) => {
17161665
if (!gltf.hasOwnProperty('materials') || gltf.materials.length === 0) {
17171666
return [];
17181667
}
@@ -1725,7 +1674,7 @@ const createMaterials = (gltf, textures, options, flipV) => {
17251674
if (preprocess) {
17261675
preprocess(gltfMaterial);
17271676
}
1728-
const material = process(gltfMaterial, textures, flipV);
1677+
const material = process(gltfMaterial, textures);
17291678
if (postprocess) {
17301679
postprocess(gltfMaterial, material);
17311680
}
@@ -1936,14 +1885,14 @@ const createResources = async (device, gltf, bufferViews, textures, options) =>
19361885
preprocess(gltf);
19371886
}
19381887

1939-
// The original version of FACT generated incorrectly flipped V texture
1940-
// coordinates. We must compensate by flipping V in this case. Once
1941-
// all models have been re-exported we can remove this flag.
1942-
const flipV = gltf.asset && gltf.asset.generator === 'PlayCanvas';
19431888

1944-
// We'd like to remove the flipV code at some point.
1945-
if (flipV) {
1946-
Debug.warn('glTF model may have flipped UVs. Please reconvert.');
1889+
// The very first version of FACT generated incorrectly flipped V texture
1890+
// coordinates. Since this first version was only ever available behind an
1891+
// editor flag there should be very few such GLB models in the wild.
1892+
// Instead of bloating the engine forevermore with code to handle this case,
1893+
// we now issue a warning and prompt user to reconvert their FBX.
1894+
if (gltf.asset && gltf.asset.generator === 'PlayCanvas') {
1895+
Debug.warn(`glTF model may have been generated with flipped UVs. Please reconvert.`);
19471896
}
19481897

19491898
const nodes = createNodes(gltf, options);
@@ -1954,13 +1903,13 @@ const createResources = async (device, gltf, bufferViews, textures, options) =>
19541903

19551904
// buffer data must have finished loading in order to create meshes and animations
19561905
const bufferViewData = await Promise.all(bufferViews);
1957-
const { meshes, meshVariants, meshDefaultMaterials, promises } = createMeshes(device, gltf, bufferViewData, flipV, options);
1906+
const { meshes, meshVariants, meshDefaultMaterials, promises } = createMeshes(device, gltf, bufferViewData, options);
19581907
const animations = createAnimations(gltf, nodes, bufferViewData, options);
19591908

19601909
// textures must have finished loading in order to create materials
19611910
const textureAssets = await Promise.all(textures);
19621911
const textureInstances = textureAssets.map(t => t.resource);
1963-
const materials = createMaterials(gltf, textureInstances, options, flipV);
1912+
const materials = createMaterials(gltf, textureInstances, options);
19641913
const skins = createSkins(device, gltf, nodes, bufferViewData);
19651914

19661915
// create renders to wrap meshes

0 commit comments

Comments
 (0)