Hello!
How can I get 3d object dimension in cocos models?
I attempted utilizing getComponent(Collider).worldBounds.halfExtents however propably its not what I’m on the lookout for
another options?
What do you want 3d object dimension for?
I’ve to maneuver, rotate and scale my 3d object to suit 2nd UI rework. However as you possibly can see on the display it doesn’t match nicely. On this iteration I’m utilizing collider boundingSphere radius to get dimension (i don’t care a lot about what facet of my 3d object is bigger than others)
I exploit this code:
import { Digital camera, Collider, Part, Enter, enter, Node, Quat, RigidBody, UITransform, Vec3, view, _decorator } from 'cc';
const { ccclass, property } = _decorator;
@ccclass('NewComponent')
export class NewComponent extends Part {
@property(Node)
object3D: Node
@property(Node)
objectUI: Node
@property(Digital camera)
digicam: Digital camera
begin() {
enter.on(Enter.EventType.TOUCH_END, this.transfer, this)
}
transfer() {
this.object3D.getComponent(Collider).enabled = false
this.object3D.getComponent(RigidBody).enabled = false
// discovering rotation
const newObjectRotation = new Quat()
Quat.fromEuler(newObjectRotation, 0, 0, 0)
// discovering place
let scaleX = view.getScaleX()
let scaleY = view.getScaleY()
const localPosX = this.objectUI.worldPosition.x * scaleX;
const localPosY = this.objectUI.worldPosition.y * scaleY;
const newLocal_2 = new Vec3(localPosX, localPosY, 0.5);
const newObjectPos = this.digicam.screenToWorld(newLocal_2);
// discovering scale
const screenHeightInUnits = this.digicam.orthoHeight * 2;
const screenWidthInUnits = screenHeightInUnits * view.getVisibleSize().width / view.getVisibleSize().peak
const xPixelsInUnit = view.getVisibleSize().width / screenWidthInUnits
const yPixelsInUnit = view.getVisibleSize().peak / screenHeightInUnits
const contentSize = this.objectUI.getComponent(UITransform).contentSize;
const radius = this.object3D.getComponent(Collider).boundingSphere.radius
let newObjectScale: Vec3
let multiplier: quantity = 1
const yContentSizeInUnits = contentSize.y / yPixelsInUnit
const xContentSizeInUnits = contentSize.x / xPixelsInUnit
// it is advisable match a circle on the smaller facet of the sprite
if (yContentSizeInUnits <= xContentSizeInUnits) {
multiplier = yContentSizeInUnits / (radius * 2)
} else {
multiplier = xContentSizeInUnits / (radius * 2)
}
newObjectScale = new Vec3(this.object3D.worldScale).multiplyScalar(multiplier)
this.object3D.worldPosition = newObjectPos
this.object3D.worldScale = newObjectScale
this.object3D.worldRotation = newObjectRotation
}
}
Earlier than shifting:
After shifting:
Btw I can use collider world sure by utilizing:
const objectUnitSize = new Vec3(this.object3D.getComponent(Collider).worldBounds.halfExtents).multiplyScalar(2)
let newObjectScale: Vec3
let multiplier: quantity = 1
const yContentSizeInUnits = contentSize.y / yPixelsInUnit
const xContentSizeInUnits = contentSize.x / xPixelsInUnit
// it is advisable match a circle on the smaller facet of the sprite
if (yContentSizeInUnits <= xContentSizeInUnits) {
multiplier = yContentSizeInUnits / objectUnitSize.y
} else {
multiplier = xContentSizeInUnits / objectUnitSize.x
}
One way or the other it’s ended up the identical manner as utilizing radius
I’m going to share my undertaking with code in case it’s possible you’ll want this to check by your self
NewProject_2.zip (1.6 MB)
Hello once more
I made some tweeks to make the issue extra practical for my case
const halfSize = this.object3D.getComponent(Collider).worldBounds.halfExtents.multiplyScalar(2)
let newObjectScale: Vec3
let multiplier: quantity = 1
const yContentSizeInUnits = contentSize.y / yPixelsInUnit
const xContentSizeInUnits = contentSize.x / xPixelsInUnit
// it is advisable match a circle on the smaller facet of the sprite
if (yContentSizeInUnits <= xContentSizeInUnits) {
multiplier = yContentSizeInUnits / (halfSize.y * 2)
} else {
multiplier = xContentSizeInUnits / (halfSize.x * 2)
}
newObjectScale = new Vec3(this.object3D.worldScale).multiplyScalar(multiplier)
It really works fantastic with one object:
Working
However gained’t work with different:
Not working
I assume the issue is with scaling of my object however anyway I can’t get the end result I need
NewProject_2.zip (2.3 MB)