Repository navigation
How to get rid of KHR_Texture_Transform extension? #1095
|
Hey, Also, because of KHR_Texture_Transform, meshopt also refuses to apply it's compression on the given attribute / primitive as seen from the error logs |
Replies: 2 comments
|
It's certainly possible to modify vertex data in glTF Transform, by reading an element (e.g. the of UV coordinates for a vertex) from an Accessor, applying the transform, and writing the result back to the accessor. You may need to be careful to understand why KHR_texture_transform is there, though... e.g. if different materials use the same mesh data with different texture transforms, then you'd need to make copies of the mesh data before applying changes to the UVs. Do I understand correctly what you're trying to do?
I'm not aware of any such constraint. Are you using glTF Transform to do meshopt compression, or something else? What error do you see in the logs? If you're able to share a model related to these problems, that almost always saves time figuring out the solution. |
|
To remove import { Material } from '@gltf-transform/core';
import { Transform } from '@gltf-transform/extensions';
import { prune } from '@gltf-transform/functions';
import { vec3, mat3 } from 'gl-matrix';
const matrixMap = new Map();
// Compute UV transform matrix for each material's base color texture.
for (const material of document.getRoot().listMaterials()) {
const info = material.getBaseColorTextureInfo();
const transform = info.getExtension('KHR_texture_transform');
if (!transform) continue;
const matrix = mat3.create();
mat3.translate(matrix, matrix, transform.getOffset());
mat3.rotate(matrix, matrix, -transform.getRotation());
mat3.scale(matrix, matrix, transform.getScale());
matrixMap.set(material, matrix);
}
// Transform UVs.
for (const mesh of document.getRoot().listMeshes()) {
for (const prim of mesh.listPrimitives()) {
const matrix = matrixMap.get(prim.getMaterial());
if (!matrix) continue;
const uv = prim.getAttribute('TEXCOORD_0').clone();
for (let i = 0, el = [0, 0, 1], il = uv.getCount(); i < il; i++) {
uv.getElement(i, el);
vec3.transformMat3(el, el, matrix);
uv.setElement(i, el);
}
prim.setAttribute('TEXCOORD_0', uv);
}
}
// Remove KHR_texture_transform extension.
for (const extension of document.getRoot().listExtensionsUsed()) {
if (extension.extensionName === 'KHR_texture_transform') {
extension.dispose();
}
}
// Clean up.
await document.transform(prune());This code makes a number of assumptions and simplifications:
When processing models that don't meet those assumptions, or arbitrary models from unknown users, adjustments would need to be made. |
To remove
KHR_texture_transformand apply its transforms to each mesh primitive: