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

Fixes issue with framing a quantized GLTF #2463

Merged
2 commits merged into from
Jun 10, 2021
Merged
Changes from 1 commit
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
37 changes: 33 additions & 4 deletions packages/model-viewer/src/three-components/ModelUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,33 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import {Object3D, Vector3} from 'three';
import {BufferAttribute, InterleavedBufferAttribute, Object3D, Vector3} from 'three';


/**
* Gets a scale value to perform inverse quantization of a vertex value
* Reference:
* https://github.com/KhronosGroup/glTF/tree/master/extensions/2.0/Khronos/KHR_mesh_quantization#encoding-quantized-data
* @param buffer A gltf vertex buffer
* @returns A scale value based on KHR_mesh_quantization or 1 if the buffer is
* not quantized.
*/
export const getNormalizedComponentScale =
(buffer: BufferAttribute|InterleavedBufferAttribute) => {
const array: ArrayLike<number> = buffer.array;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

May need to check buffer.normalized here - the array could be Uint8Array but not normalized.


if (array instanceof Int8Array) {
return 1 / 127;
} else if (array instanceof Uint8Array) {
return 1 / 255;
} else if (array instanceof Int16Array) {
return 1 / 32767;
} else if (array instanceof Uint16Array) {
return 1 / 65535;
}

return 1;
};
/**
* Moves Three.js objects from one parent to another
*/
Expand Down Expand Up @@ -49,17 +74,21 @@ export const reduceVertices = <T>(
for (i = 0, l = vertices.length; i < l; i++) {
vertex.copy(vertices[i]);
vertex.applyMatrix4(object.matrixWorld);

value = func(value, vertex);
}

} else if (geometry.isBufferGeometry) {
const {position} = geometry.attributes;

if (position !== undefined) {
const scale = getNormalizedComponentScale(position);

for (i = 0, l = position.count; i < l; i++) {
vertex.fromBufferAttribute(position, i)
.applyMatrix4(object.matrixWorld);
vertex.fromBufferAttribute(position, i);
if (position.normalized) {
This conversation was marked as resolved.
Show resolved Hide resolved
vertex.multiplyScalar(scale);
}
vertex.applyMatrix4(object.matrixWorld);

value = func(value, vertex);
}
Expand Down