Skip to content

Instantly share code, notes, and snippets.

@potato-robert
Last active July 30, 2023 02:36
Show Gist options
  • Select an option

  • Save potato-robert/ec153ae228a8ae3f6fe28b143073b669 to your computer and use it in GitHub Desktop.

Select an option

Save potato-robert/ec153ae228a8ae3f6fe28b143073b669 to your computer and use it in GitHub Desktop.
// CODE SNIPPET, NOT THE FULL FILE //
//______________________________________________________________________ Load
function loadModel(OBJFile, MTLFile, SasUrls) {
//Load with MTL + Textures
//Note: The MTLLoader has been customized so that it can have a SAS token specified,
// and that it converts the filenames in the MTL file to lowercase(otherwise Azure Storage URL will be incorrect)
var mtlLoader = new MTLLoader();
mtlLoader.setSasUrls(SasUrls);
mtlLoader.load(MTLFile, function (materials) {
materials.preload();
new OBJLoader()
.setMaterials(materials)
.load(OBJFile, function (object) {
object.traverse(function (child) { // aka setTexture
if (child instanceof THREE.Mesh) {
child.material.side = THREE.DoubleSide;
//The default color space is sRGB, we need Linear (will be too dark otherwise)
//see https://threejs.org/docs/#manual/en/introduction/Color-management
if (child.material.map) child.material.map.encoding = THREE.LinearEncoding;
}
});
object.rotation.y = Math.PI; //rotate 180 degrees to match orientation in Clirio View
//Calculate y center pos, raise model and recenter camera on model
var boundingBox = new THREE.Box3().setFromObject(object);
size = new THREE.Vector3();
boundingBox.getSize(size);
var center = new THREE.Vector3();
boundingBox.getCenter(center);
var centerPos = object.localToWorld(center);
objYCenterPos = size.y / 2 - centerPos.y + gapAboveGrid;
longestSide = Math.max(size.x, size.y, size.z);
object.position.set(centerPos.x, objYCenterPos, centerPos.z);
//for large objects, update view + grid
configureGridBasedOnModelSize();
resetView();
//add to scene, add scale legend, remove spinner
scene.add(object);
addScaleLegend();
scanSpinner.classList.add('hidden');
}, function (item) {
//console.log("Load Progress: " + item.loaded + "/" + item.total);
}, function (error) {
console.error(error);
});
});
animate();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment