[ad_1]
Hi!
How can I get 3d object measurement in cocos models?
I attempted utilizing getComponent(Collider).worldBounds.halfExtents however propably its not what I’m on the lookout for
every other options?
What do you want 3d object measurement for?
I’ve to maneuver, rotate and scale my 3d object to suit 2nd UI remodel. But as you possibly can see on the display screen it doesn’t match effectively. In this iteration I’m utilizing collider boundingSphere radius to get measurement (i don’t care a lot about what aspect of my 3d object is bigger than others)
I exploit this code:
import { Camera, Collider, Component, Input, enter, Node, Quat, RigidBody, UITransform, Vec3, view, _decorator } from 'cc';
const { ccclass, property } = _decorator;
@ccclass('NewComponent')
export class NewComponent extends Component {
@property(Node)
object3D: Node
@property(Node)
objectUI: Node
@property(Camera)
digital camera: Camera
begin() {
enter.on(Input.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 screenHeightInItems = this.digital camera.orthoHeight * 2;
const screenWidthInItems = screenHeightInItems * view.getVisibleSize().width / view.getVisibleSize().peak
const xPixelsInUnit = view.getVisibleSize().width / screenWidthInItems
const yPixelsInUnit = view.getVisibleSize().peak / screenHeightInItems
const contentSize = this.objectUI.getComponent(UITransform).contentSize;
const radius = this.object3D.getComponent(Collider).boundingSphere.radius
let newObjectScale: Vec3
let multiplier: quantity = 1
const yContentSizeInItems = contentSize.y / yPixelsInUnit
const xContentSizeInItems = contentSize.x / xPixelsInUnit
// you'll want to match a circle on the smaller aspect of the sprite
if (yContentSizeInItems <= xContentSizeInItems) {
multiplier = yContentSizeInItems / (radius * 2)
} else {
multiplier = xContentSizeInItems / (radius * 2)
}
newObjectScale = new Vec3(this.object3D.worldScale).multiplyScalar(multiplier)
this.object3D.worldPosition = newObjectPos
this.object3D.worldScale = newObjectScale
this.object3D.worldRotation = newObjectRotation
}
}
Before transferring:
After transferring:
Btw I can use collider world certain by utilizing:
const objectUnitSize = new Vec3(this.object3D.getComponent(Collider).worldBounds.halfExtents).multiplyScalar(2)
let newObjectScale: Vec3
let multiplier: quantity = 1
const yContentSizeInItems = contentSize.y / yPixelsInUnit
const xContentSizeInItems = contentSize.x / xPixelsInUnit
// you'll want to match a circle on the smaller aspect of the sprite
if (yContentSizeInItems <= xContentSizeInItems) {
multiplier = yContentSizeInItems / objectUnitSize.y
} else {
multiplier = xContentSizeInItems / objectUnitSize.x
}
Somehow it’s ended up the identical approach as utilizing radius
I’m going to share my challenge with code in case you might want this to check by your self
NewProject_2.zip (1.6 MB)
Hi once more
I made some tweeks to make the issue extra life like for my case
const halfSize = this.object3D.getComponent(Collider).worldBounds.halfExtents.multiplyScalar(2)
let newObjectScale: Vec3
let multiplier: quantity = 1
const yContentSizeInItems = contentSize.y / yPixelsInUnit
const xContentSizeInItems = contentSize.x / xPixelsInUnit
// you'll want to match a circle on the smaller aspect of the sprite
if (yContentSizeInItems <= xContentSizeInItems) {
multiplier = yContentSizeInItems / (halfSize.y * 2)
} else {
multiplier = xContentSizeInItems / (halfSize.x * 2)
}
newObjectScale = new Vec3(this.object3D.worldScale).multiplyScalar(multiplier)
It works advantageous with one object:
Working
But received’t work with different:
Not working
I assume the issue is with scaling of my object however anyway I can’t get the consequence I need
NewProject_2.zip (2.3 MB)
[ad_2]