I have .obj file being loaded using
let objLoader: OBJLoader = new OBJLoader();
objLoader.load(
'assets/device.obj',
(object) => {}
)
perspective camera is being made use of and positioning and rotating also happens to uploaded object(THREE.Object3D)
How can i resize the uploaded object to real world 0.27m width regardless of positioning and rotating.
Let size = Box3.setFromObject( yourobject ).getSize(nee THREE.Vector3())
Gets you the size of the object… Then you can yourObject.scale.multiplyScalar(.2/size.x)
2 Likes
I already tried this, it works perfectly for the first time when camera obj is loaded, drawCamera() gets called if riverCamY or riverCamX or riverCamZ value changes. Here please note that X is mapped to Y, Y is mapped to Z, Z is mapped to X
When riverCamX value is changing, camera3DObject size is same but visually it is appearing small and larger, maybe because each time positioning and rotation also changes, now how can I keep camera3DObject visually also same
private drawCamera(): void {
let riverCamY = parseFloat(this.analysisSettingsModel.analysisCameraPositionY?.toString() || "0");
let riverCamX = parseFloat(this.analysisSettingsModel.analysisCameraPositionX?.toString() || "0");
let riverCamZ = parseFloat(this.analysisSettingsModel.analysisCameraPositionZ?.toString() || "0")
let riverCamVisibility = this.analysisSettingsModel.cameraVisible;
const riverCamMeshName = "riverCam_mesh";
if (riverCamVisibility) {
this.camera3DObject.name = riverCamMeshName;
const verticalCrossSectionCenter: number = this.bottom.y;
const diffBetweenCenterAndCamY = Math.abs(verticalCrossSectionCenter - riverCamZ);
this.camera3DObject.position.set(riverCamY, (verticalCrossSectionCenter + diffBetweenCenterAndCamY), -Math.abs(this.bank1?.x - this.bank2?.x) / 2 + riverCamX);
this.camera3DObject.position.z += 0.5;
this.riverCamObject = this.camera3DObject;
if (!this.isValidOBJ(this.camera3DObject)) {
let objLoader: OBJLoader = new OBJLoader();
objLoader.load(
'assets/camera.obj',
(object) => {
object.name = this.camera3DObject.name;
const verticalCrossSectionCenter: number = this.bottom.y;
const diffBetweenCenterAndCamY = Math.abs(verticalCrossSectionCenter - riverCamZ);
object.position.set(riverCamY, (verticalCrossSectionCenter + diffBetweenCenterAndCamY), -Math.abs(this.bank1?.x - this.bank2?.x) / 2 + riverCamX);
object.scale.setScalar(0.001); // converts mm → meters
// Ensure the transformation is applied
object.updateMatrixWorld(true);
this.changeCameraFacing(object);
object.traverse((child) => {
if (child instanceof THREE.Mesh) {
// Change the material color to the specified color
child.material.color.set(0x404040);
child.geometry.computeBoundingBox();
}
});
if (this.savedRotation) {
object.rotation.copy(this.savedRotation);
}
this.camera3DObject = object;
this.camera3DObject.position.z += 0.5;
this.addOrReplaceToSceneByName(riverCamVisibility, riverCamMeshName, object);
return;
},
(xhr) => {
},
(error) => {
console.error('Error loading OBJ:', error);
}
);
}
}
if (this.savedRotation) {
this.camera3DObject.rotation.copy(this.savedRotation);
}
this.changeCameraFacing(this.camera3DObject);
this.addOrReplaceToSceneByName(riverCamVisibility, riverCamMeshName, this.camera3DObject);
}