Hello!
How can I get 3d object dimension in cocos items?
I attempted utilizing getComponent(Collider).worldBounds.halfExtents however propably its not what I’m in search of
some other options?
What do you want 3d object dimension for?
I’ve to maneuver, rotate and scale my 3d object to suit second UI rework. However as you’ll be able to 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 take advantage of this code:
import { Digicam, Collider, Element, Enter, enter, Node, Quat, RigidBody, UITransform, Vec3, view, _decorator } from 'cc';
const { ccclass, property } = _decorator;
@ccclass('NewComponent')
export class NewComponent extends Element {
@property(Node)
object3D: Node
@property(Node)
objectUI: Node
@property(Digicam)
digital camera: Digicam
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.digital camera.screenToWorld(newLocal_2);
// discovering scale
const screenHeightInUnits = this.digital camera.orthoHeight * 2;
const screenWidthInUnits = screenHeightInUnits * view.getVisibleSize().width / view.getVisibleSize().top
const xPixelsInUnit = view.getVisibleSize().width / screenWidthInUnits
const yPixelsInUnit = view.getVisibleSize().top / 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's essential to 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's essential to 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 method as utilizing radius
I’m going to share my venture with code in case it’s possible you’ll want this to check by your self
NewProject_2.zip (1.6 MB)