์๋ ํ์ธ์ ์ฌ๋ฌ๋ถ,
CubeGeometry ๋ฐ TextGeometry๋ฅผ ํฌํจํ๋ Object3D ์ธ์คํด์ค๋ฅผ ์ฌ์ฉํฉ๋๋ค. ๋ด ์ง๋ฌธ : ์ป์ ์ ์์ต๋๋ค
์ด ๋ณตํฉ ๊ธฐํํ์ ๊ฒฝ๊ณ ์์? ๋๋ Object3D ํด๋์ค์ boundingBox-Method๊ฐ ์๋ค๋ ๊ฒ์ ๋ณด์ ๊ธฐ ๋๋ฌธ์ ์ด๊ฒ์ ๋ฌป์ต๋๋ค.
Object3D ์ปจํ
์ด๋์ ํฌํจ ๋ ๋ชจ๋ ๊ฐ์ฒด์ ๋ํด ๊ณ์ฐํ ๋ฐฉ๋ฒ์ด ์์ต๋๋ค.
๋ด ์๊ฐ์ Object3D ์ปจํ ์ด๋ centeroid์ ๋ฐ๋ผ ์นด๋ฉ๋ผ๋ฅผ ์ค์์ ๋ฐฐ์นํ๋ ๊ฒ์ด๊ธฐ ๋๋ฌธ์ boundingBox๋ฅผ ์์์ผํฉ๋๋ค.
Roundrobin ์ธ์ฌ๋ง
์ง๊ธ๊น์ง ๊ฐ์ฌํฉ๋๋ค. ์ด์ฉ๋ฉด ๋ด๊ฐ ํ๋ฆฐ ๊ฒ์ ๋ฌป๊ณ , ๋ด ์๋๋ ๋ ๊ฐ์ฒด์ ๊ฒฝ๊ณ ์์๋ฅผ ์ป๋ ๊ฒ์ด ์์ต๋๋ค. ํ๋์ ์ง์ค๋ฉํธ๋ฆฌ์ ๋ํ ๊ฒฝ๊ณ ์์๋ฅผ ๊ณ์ฐํ๋ ๋ฐฉ๋ฒ์ ์๊ณ ์์ง๋ง ๋ณตํฉ 3D ๊ฐ์ฒด๊ฐ์๋ ๊ฒฝ์ฐ ๋ ๊ฐ์ ํ๋ธ์ ๊ฐ์ ๊ฒฝ๊ณ ์์๋ฅผ ๊ณ์ฐํ๋ ๋ฐฉ๋ฒ์ ๋ฌด์์ ๋๊น? ์๋๋ฉด ์ด์ ์ ํ๋์ ์ง์ค๋ฉํธ๋ฆฌ๋ก ๊ฐ์ฒด๋ฅผ ๋ณํฉํด์ผํฉ๋๊น?
๋๋ ๋น์ ์ด ๋ด ์์ ์ ๊ฐ์ง๊ณ ์๊ธฐ๋ฅผ ๋ฐ๋๋๋ค :) ์ค๋ช ํ๊ธฐ๊ฐ ์ด๋ ต์ต๋๋ค.
๋ด๊ฐ ์ฐธ์กฐ. ์ง๊ธ ์ ์ผํ ๋ฐฉ๋ฒ์ ๋ชจ๋ ๊ฒฝ๊ณ ์์๋ฅผ ๋น๊ตํ๊ณ ๋ชจ๋ ๊ฐ์ผ๋ก max()
๋ฐ min()
ํ๋ ๊ฒ์
๋๋ค.
๊ด์ฐฎ ๊ฐ์ฌ. ์ฑ๊ณตํ๋ฉด ๋ด ์๋ฃจ์ ์ ๊ฒ์ํ๊ฒ ์ต๋๋ค.
์ฌ๊ธฐ ๋ด ํด๊ฒฐ์ฑ ์ด ์์ต๋๋ค. ์ด ํจ์์ ๊ฐ์ฒด๋ฅผ ์ ๋ฌํ๊ธฐ ๋งํ๋ฉด ์์์ ํตํด ๊ฐ์ฒด์ ๋์ด / ๋๋น๋ฅผ ๊ณ์ฐํ๊ณ ๋์ด ๋ฐ ๋๋น ์์ฑ์ ๊ฐ์ฒด์ ํ ๋นํฉ๋๋ค.
calculateDimensions(myObject);
...
/*
* Get the size of the compound object by computing the bounding box and getting the max/min of each of its children
*/
function calculateDimensions(_object) {
var absoluteMinX = 0, absoluteMaxX = 0, absoluteMinY = 0, absoluteMaxY = 0, absoluteMinZ = 0, absoluteMaxZ = 0;
for (var i = 0; i < _object.children.length; i++) {
_object.children[i].geometry.computeBoundingBox();
absoluteMinX = Math.min(absoluteMinX,_object.children[i].geometry.boundingBox.min.x);
absoluteMaxX = Math.max(absoluteMaxX,_object.children[i].geometry.boundingBox.max.x);
absoluteMinY = Math.min(absoluteMinY,_object.children[i].geometry.boundingBox.min.y);
absoluteMaxY = Math.max(absoluteMaxY,_object.children[i].geometry.boundingBox.max.y);
absoluteMinZ = Math.min(absoluteMinZ,_object.children[i].geometry.boundingBox.min.z);
absoluteMaxZ = Math.max(absoluteMaxZ,_object.children[i].geometry.boundingBox.max.z);
}
// set generic height and width values
_object.depth = (absoluteMaxX - absoluteMinX) * _object.scale.x;
_object.height = (absoluteMaxY - absoluteMinY) * _object.scale.y;
_object.width = (absoluteMaxZ - absoluteMinZ) * _object.scale.z;
// remember the original dimensions
if (_object.originalDepth === undefined) _object.originalDepth = _object.depth;
if (_object.originalHeight === undefined) _object.originalHeight = _object.height;
if (_object.originalWidth === undefined) _object.originalWidth = _object.width;
console.log("Depth: " + _object.depth + ", Height: " + _object.height + ", Width: " + _object.width);
}
๋๋ ๋๊ฐ์ ๊ฒ์ ์ฐพ๊ณ ์ด๊ฒ์ ๋ฐ๊ฒฌํ๊ณ ๊ฒฐ๊ตญ ์ด๊ฒ์ ์ ์ฐฉํ์ต๋๋ค.
function getCompoundBoundingBox(object3D) {
var box = null;
object3D.traverse(function (obj3D) {
var geometry = obj3D.geometry;
if (geometry === undefined) return;
geometry.computeBoundingBox();
if (box === null) {
box = geometry.boundingBox;
} else {
box.union(geometry.boundingBox);
}
});
return box;
}
@NickLarsen ์ข์ ๋ณด์ธ๋ค!
2 ๋ ์ ๋ผ์ด๋ ๋ก๋น๊ณผ ๋๊ฐ์ ๋ฌธ์ ์ ์์ด๋์ด๊ฐ ์์ต๋๋ค. ๋๋ ๊ทธ๊ฒ์ ์ค๋ช ํ๋ ค๊ณ ํฉ๋๋ค ... @mrdoob ๋งํ๋ฏ์ด @NickLarsen์ ํด๊ฒฐ์ฑ ์ ์ ๋ง ์ข์ ๋ณด์ด์ง๋ง ํฐ ๋ฌธ์ ๊ฐ์๋ค.
๋ด ์ฅ๋ฉด์๋ ํญ์ ๋ชจ๋ ๋ํ์ ํฌํจํ๋ ํ๋์ Object3D๊ฐ ์์ต๋๋ค. ์๋ฅผ ๋ค์ด ๋ฐ๊ฒฝ๊ณผ y ์ขํ๊ฐ ๋ค๋ฅธ 8 ๊ฐ์ ๊ตฌ๊ฐ ์์ต๋๋ค. ๋ชจ๋ x = 0 ๋ฐ z = 0์ ์์ต๋๋ค.
๊ทธ๋ฐ ๋ค์ NickLarsen์ ํจ์๋ก ์ ์ฒด ๊ฒฝ๊ณ ์์๋ฅผ ๊ณ์ฐํ๋ ค๊ณ ํ์ง๋ง ๊ฒฐ๊ณผ๊ฐ ๋งค์ฐ ์ด์ํ์ต๋๋ค. ์ค๋ฅ๋ฅผ ์ฐพ๊ธฐ ์ํด ๋ชจ๋ ๋จ์ผ ๊ฒฝ๊ณ ์์๋ฅผ ์ฝ์์ ๊ธฐ๋กํ์ต๋๋ค.
({์ต์ : {x : -10, y : -10, z : -10}, ์ต๋ : {x : 10, y : 10, z : 10}})
({์ต์ : {x : -20, y : -20, z : -20}, ์ต๋ : {x : 20, y : 20, z : 20}})
({์ต์ : {x : -7, y : -7, z : -7}, ์ต๋ : {x : 7, y : 7, z : 7}})
({์ต์ : {x : -18, y : -18, z : -18}, ์ต๋ : {x : 18, y : 18, z : 18}})
({๋ถ : {x : -15, y : -15, z : -15}, ์ต๋ : {x : 15, y : 15, z : 15}})
({์ต์ : {x : -3, y : -3, z : -3}, ์ต๋ : {x : 3, y : 3, z : 3}})
({์ต์ : {x : -10, y : -10, z : -10}, ์ต๋ : {x : 10, y : 10, z : 10}})
({์ต์ : {x : -25, y : -25, z : -25}, ์ต๋ : {x : 25, y : 25, z : 25}})
๋ณด์๋ค์ํผ ๊ฒฝ๊ณ ์์๋ ํญ์ ๋จ์ผ ๊ตฌ์ ๋ก์ปฌ ์ขํ๊ณ์์ ๊ณ์ฐ๋ฉ๋๋ค. ๊ทธ๋ฆฌ๊ณ ๊ทธ๊ฒ๋ค์ ๊ฒฐํฉํ๋ฉด ๋ง์ด๋์ง ์์ต๋๋ค! ๋ด Object3D์ ์ค์ ๊ฒฝ๊ณ ์์๋ฅผ ์ป๊ธฐ ์ํด ์ธ๊ณ ์ขํ์์ ๊ฒฝ๊ณ ์์๋ฅผ ์ด๋ป๊ฒ ์ป์ ์ ์์ต๋๊น?
r.59dev ๋ธ๋์น์์ Box3.setFromObject( object )
์ฐธ์กฐํ์ญ์์ค.
@PanChan ๋น์ ์ ์ด๊ฒ์ ์คํํ๊ธฐ ์ ์ ๋ณํ์ ์ฌ๊ณผํด์ผํ๋ฉฐ ์๋ ํ๋๋ก ์๋ํฉ๋๋ค. ์ด๊ฒ์ ์ค์ ๋ก ์ฌ์ฉํ๋ฉด์ ๊ฐ ์ค๋ธ์ ํธ์ ์ง์ค๋ฉํธ๋ฆฌ๋ฅผ ๋ณต์ ํ๊ณ ๊ฒฝ๊ณ ์์๋ฅผ ๊ณ์ฐํ๊ธฐ ์ ์ ๋ณํ์ ์ ์ฉํฉ๋๋ค. ์ฌ๊ธฐ์์ ์ค์ ๊ตฌํ์ ๋ณผ ์ ์์ต๋๋ค. ๊ทธ๋๋ ๋๋ ์ด๊ฒ์ ๊ดํด์๋ ๊ฝค ๋ฉ์ฒญํ ์ฌ๋์ด๋ฏ๋ก ํ์๋ก ์ ์ถฉ๋ถํ ๊ฐ์ง๊ณ ๋ด ๊ตฌํ์ ์ทจํ์ญ์์ค.
@NickLarsen ๋๋ ์๋ํ๋ ๊ฒ ๊ฐ์ ๋ ๋ค๋ฅธ ํด๊ฒฐ์ฑ ์ ์ฐพ์์ต๋๋ค. boundingbox๋ฅผ ๊ณ์ฐ ํ ํ ์๋ ์์คํ ์ผ๋ก ๋ณํํฉ๋๋ค.
var bb = geometry.boundingBox;
bb.translate(obj3D.localToWorld( new THREE.Vector3()));
๊ทธ๊ฒ์ด ์ข์ ํด๊ฒฐ์ฑ ์ธ์ง๋ ๋ชจ๋ฅด์ง๋ง ์ง๊ธ์ ์๋ํฉ๋๋ค. ์ ๋ฌธ ์๋ฃจ์ ์ผ๋ก ์๋ก์ด ๊ฐ์ ํ์ ๊ธฐ๋ค๋ฆฌ๊ณ ์๋ค๊ณ ์๊ฐํ์ญ์์ค.) ์ค๋ช ํด ์ฃผ์ ์ ๊ฐ์ฌํฉ๋๋ค!
๋ด ํ๋ก์ ํธ์์ @NickLarsen ์๋ฃจ์ ์ ์ฌ์ฉํ์ฌ ๊ฐ์ฒด์ ๊ฒฝ๊ณ ์์๊ฐ ์ด๋ฏธ ์กด์ฌํ๋ ๊ฒฝ์ฐ์ ๋ํด ์ฝ๊ฐ ์ต์ ํ ๋ ๊ณ์ฐ์ด๋ฏ๋ก ๋ค์ ๊ณ์ฐํ ํ์๊ฐ ์์ต๋๋ค.
function getComplexBoundingBox(object3D) {
var box = null;
object3D.traverse(function(obj3D) {
if (obj3D.matrixWorldNeedsUpdate) obj3D.updateMatrixWorld();
var geometry = obj3D.geometry;
// If current is not a geometry (THREE.Geometry), proceed to the next one
if (geometry === undefined) return null;
// If this object is already bounding box, then use it
if (geometry.boundingBox) {
var workableBox = geometry.boundingBox.clone();
// Move the resulting bounding box to the position of the object itself
workableBox.applyMatrix4(obj3D.matrixWorld);
if (box === null) {
box = workableBox;
} else {
box.union(workableBox);
}
// If there is no bounding box for current object - creating
} else {
var workableGeometry = geometry.clone();
// Move the resulting geometry in the position of the object itself
workableGeometry.applyMatrix(obj3D.matrixWorld);
// Calculate the bounding box for the resulting geometry
workableGeometry.computeBoundingBox();
if (box === null) {
box = workableGeometry.boundingBox;
} else {
box.union(workableGeometry.boundingBox);
}
}
});
return box;
}
5 IF ์กฐ๊ฑด-ํ์คํ ์ข์ ์ฝ๋์ ์๋ ์๋์ง๋ง ๋ฌด์์ํด์ผํ ๊น์? :)
์ด๊ฒ์ ์ ๋ง ์ค๋๋ ์ค๋ ๋์
๋๋ค. ์์ฆ์๋ ์๋ก์ด new THREE.Box3().setFromObject(object3d)
๋๋์ด ๋ฐฉ๋ฒ์ ์๋ ํ ๊ฒ์ด๋ค, ๊ฐ์ฌํฉ๋๋ค!
๋ฌธ์ ์์ด์. ๋๋ ๋ช ๋ฌ ์์ ์ ํ์ฌ์ด ์คํ์ ๊ฒช์ : https://github.com/danielribeiro/three-hub/commit/859f148349ca64ede9ee224d55ad2ea3f51c0da0#diff -0dad6abd7a4801d561b83deddedc1baa
๊ฐ์ฅ ์ ์ฉํ ๋๊ธ
๋๋ ๋๊ฐ์ ๊ฒ์ ์ฐพ๊ณ ์ด๊ฒ์ ๋ฐ๊ฒฌํ๊ณ ๊ฒฐ๊ตญ ์ด๊ฒ์ ์ ์ฐฉํ์ต๋๋ค.