mirror of
https://git.mirrors.martin98.com/https://github.com/google/draco
synced 2025-08-12 21:29:00 +08:00
Merge pull request #45 from customlogic/uvFix
Javascript loader: load texture coordinates (uvs)
This commit is contained in:
commit
b3d372a426
@ -75,7 +75,7 @@ THREE.DRACOLoader.prototype = {
|
|||||||
/*
|
/*
|
||||||
* Example on how to retrieve mesh and attributes.
|
* Example on how to retrieve mesh and attributes.
|
||||||
*/
|
*/
|
||||||
let numFaces, numPoints, numVertexCoordinates, numAttributes;
|
let numFaces, numPoints, numVertexCoordinates, numTextureCoordinates, numAttributes;
|
||||||
// For output basic geometry information.
|
// For output basic geometry information.
|
||||||
let geometryInfoStr;
|
let geometryInfoStr;
|
||||||
if (geometryType == DracoModule.TRIANGULAR_MESH) {
|
if (geometryType == DracoModule.TRIANGULAR_MESH) {
|
||||||
@ -87,6 +87,7 @@ THREE.DRACOLoader.prototype = {
|
|||||||
}
|
}
|
||||||
numPoints = dracoGeometry.num_points();
|
numPoints = dracoGeometry.num_points();
|
||||||
numVertexCoordinates = numPoints * 3;
|
numVertexCoordinates = numPoints * 3;
|
||||||
|
numTextureCoordinates = numPoints * 2;
|
||||||
numAttributes = dracoGeometry.num_attributes();
|
numAttributes = dracoGeometry.num_attributes();
|
||||||
geometryInfoStr = "Number of points loaded: " + numPoints.toString()
|
geometryInfoStr = "Number of points loaded: " + numPoints.toString()
|
||||||
+ ".\n";
|
+ ".\n";
|
||||||
@ -130,6 +131,18 @@ THREE.DRACOLoader.prototype = {
|
|||||||
norAttributeData);
|
norAttributeData);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Get texture coord attributes if exists.
|
||||||
|
const texCoordAttId =
|
||||||
|
wrapper.GetAttributeId(dracoGeometry, Module.TEX_COORD);
|
||||||
|
let textCoordAttributeData;
|
||||||
|
if (texCoordAttId != -1) {
|
||||||
|
geometryInfoStr += "\nLoaded texture coordinate attribute.\n";
|
||||||
|
const texCoordAttribute = wrapper.GetAttribute(dracoGeometry, texCoordAttId);
|
||||||
|
textCoordAttributeData = new DracoModule.DracoFloat32Array();
|
||||||
|
wrapper.GetAttributeFloatForAllPoints(dracoGeometry, texCoordAttribute,
|
||||||
|
textCoordAttributeData);
|
||||||
|
}
|
||||||
|
|
||||||
// Structure for converting to THREEJS geometry later.
|
// Structure for converting to THREEJS geometry later.
|
||||||
const geometryBuffer = {
|
const geometryBuffer = {
|
||||||
indices: [],
|
indices: [],
|
||||||
@ -161,11 +174,23 @@ THREE.DRACOLoader.prototype = {
|
|||||||
norAttributeData.GetValue(i + 2));
|
norAttributeData.GetValue(i + 2));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Add texture coordinates.
|
||||||
|
if (texCoordAttId != -1) {
|
||||||
|
for (let i = 0; i < numTextureCoordinates; i += 2) {
|
||||||
|
geometryBuffer.uvs.push(
|
||||||
|
textCoordAttributeData.GetValue(i),
|
||||||
|
textCoordAttributeData.GetValue(i + 1));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
DracoModule.destroy(posAttributeData);
|
DracoModule.destroy(posAttributeData);
|
||||||
if (colorAttId != -1)
|
if (colorAttId != -1)
|
||||||
DracoModule.destroy(colAttributeData);
|
DracoModule.destroy(colAttributeData);
|
||||||
if (normalAttId != -1)
|
if (normalAttId != -1)
|
||||||
DracoModule.destroy(norAttributeData);
|
DracoModule.destroy(norAttributeData);
|
||||||
|
if (texCoordAttId != -1)
|
||||||
|
DracoModule.destroy(textCoordAttributeData);
|
||||||
|
|
||||||
// For mesh, we need to generate the faces.
|
// For mesh, we need to generate the faces.
|
||||||
if (geometryType == DracoModule.TRIANGULAR_MESH) {
|
if (geometryType == DracoModule.TRIANGULAR_MESH) {
|
||||||
@ -198,6 +223,10 @@ THREE.DRACOLoader.prototype = {
|
|||||||
geometry.addAttribute('normal',
|
geometry.addAttribute('normal',
|
||||||
new THREE.Float32BufferAttribute(geometryBuffer.normals, 3));
|
new THREE.Float32BufferAttribute(geometryBuffer.normals, 3));
|
||||||
}
|
}
|
||||||
|
if (texCoordAttId != -1) {
|
||||||
|
geometry.addAttribute('uv',
|
||||||
|
new THREE.Float32BufferAttribute(geometryBuffer.uvs, 2));
|
||||||
|
}
|
||||||
return geometry;
|
return geometry;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
Loading…
x
Reference in New Issue
Block a user