Home Game Development 3d unit object measurement – Cocos Creator

3d unit object measurement – Cocos Creator

0
3d unit object measurement – Cocos Creator

[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 in search of
some other options?

What do you want 3d object measurement for?

I’ve to maneuver, rotate and scale my 3d object to suit second UI rework. But as you possibly can see on the display screen it doesn’t match properly. In this iteration I’m utilizing collider boundingSphere radius to get measurement (i don’t care a lot about what facet 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)
    digicam: 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.digicam.screenToWorld(newLocal_2);

        // discovering scale
        const screenHeightInModels = this.digicam.orthoHeight * 2;
        const screenWidthInModels = screenHeightInModels * view.getVisibleSize().width / view.getVisibleSize().top

        const xPixelsInUnit = view.getVisibleSize().width / screenWidthInModels
        const yPixelsInUnit = view.getVisibleSize().top / screenHeightInModels

        const contentSize = this.objectUI.getComponent(UITransform).contentSize;

        const radius = this.object3D.getComponent(Collider).boundingSphere.radius

        let newObjectScale: Vec3
        let multiplier: quantity = 1

        const yContentSizeInModels = contentSize.y / yPixelsInUnit
        const xContentSizeInModels = contentSize.x / xPixelsInUnit

        // you'll want to match a circle on the smaller facet of the sprite
        if (yContentSizeInModels <= xContentSizeInModels) {
            multiplier = yContentSizeInModels / (radius * 2)
        } else {
            multiplier = xContentSizeInModels / (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 sure by utilizing:

        const objectUnitSize = new Vec3(this.object3D.getComponent(Collider).worldBounds.halfExtents).multiplyScalar(2)

        let newObjectScale: Vec3
        let multiplier: quantity = 1

        const yContentSizeInModels = contentSize.y / yPixelsInUnit
        const xContentSizeInModels = contentSize.x / xPixelsInUnit

        // you'll want to match a circle on the smaller facet of the sprite
        if (yContentSizeInModels <= xContentSizeInModels) {
            multiplier = yContentSizeInModels / objectUnitSize.y
        } else {
            multiplier = xContentSizeInModels / objectUnitSize.x
        }

Somehow it’s ended up the identical method as utilizing radius

I’m going to share my mission with code in case chances are you’ll want this to check by your self
NewProject_2.zip (1.6 MB)

[ad_2]

LEAVE A REPLY

Please enter your comment!
Please enter your name here