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 items?
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 second UI remodel. But as you possibly can see on the display screen it doesn’t match nicely. 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 take advantage of 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 screenHeightInModels = this.digital camera.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 could 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 shifting:

After shifting:

Btw I can use collider world certain through the use of:

        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 could 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 challenge with code in case you could 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 practical for my case

            const halfSize = 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 could match a circle on the smaller facet of the sprite
            if (yContentSizeInModels <= xContentSizeInModels) {
                multiplier = yContentSizeInModels / (halfSize.y * 2)
            } else {
                multiplier = xContentSizeInModels / (halfSize.x * 2)
            }

            newObjectScale = new Vec3(this.object3D.worldScale).multiplyScalar(multiplier)

It works superb with one object:

Working

But received’t work with different:

Not working

I suppose the issue is with scaling of my object however anyway I can’t get the end result I would like
NewProject_2.zip (2.3 MB)

[ad_2]

LEAVE A REPLY

Please enter your comment!
Please enter your name here