cocos creator 笔记-Asset Bundle

cocos creator实现使用Asset Bundle加载远端模型,点击模型切换操作

编译器版本 3.5.2

生成远端包

https://docs.cocos.com/creator/3.5/manual/zh/asset/bundle.html

新建一个子包工程,在资源下新建文件夹,同时放置模型和新建场景(如果想直接在引用的工程里面直接渲染的话),勾选配置为Bundle,编译,文件在 build\web-desktop\remote,使用nginx映射文件目录。

主工程使用

 第一人称相机自身操作:

cocos creator 笔记-模型转动_暮雪...的博客-CSDN博客_cocos 模型

https://blog.csdn.net/qq_22071421/article/details/126447808?spm=1001.2014.3001.5501

  loadModel(){

        let that=this
        assetManager.loadBundle('http://192.168.0.107/data/cocosbundle/myBundle/remote/fashi', (err, bundle) => {
            bundle.load('xxx/xxx',Prefab,function (err, prefab) {
                that.loadNode = instantiate(prefab);
                that.loadNode.setPosition(new Vec3(0,0,10))
                that.loadNode.setRotationFromEuler(new Vec3(0, -180, 0));
                that.loadNode.setScale(0.5,1,0.5)
                that.loadNode.addComponent(RigidBody);
                that.loadNode.getComponent(RigidBody).useGravity=false
                that.loadNode.addComponent(BoxCollider);
                that.loadNode.getComponent(BoxCollider).enabled=true
                that.loadNode.getComponent(BoxCollider).size=new Vec3(3,12,3)
                director.getScene().addChild(that.loadNode);
            });
        });
    }

直接加载场景

    loadModel(){
        assetManager.loadBundle('http://192.168.0.107/data/cocosbundle/myBundle/remote/fashi', (err, bundle) => {
            bundle.loadScene('fashi', function (err, scene) {
                director.runScene(scene);
             });
        
        });
}

可能是因为模型制作不规范的原因,添加BoxCollider时要指定size,不然不能完整点击模型

点击切换模型与自身操作

简单使用变量 actionState记录操作对象

屏幕上模型点击监听

    //入口引入
        input.on(Input.EventType.TOUCH_START, this.onMouseTouchStart, this);
   

    @property(Camera)
    private fristCamera: Camera = null!;

    onMouseTouchStart(touch) {
        let touchPos = touch.getLocation();
        let ray = this.fristCamera.screenPointToRay(touchPos.x,touchPos.y);
        if (PhysicsSystem.instance.raycastClosest(ray)) {
            let res = PhysicsSystem.instance.raycastClosestResult;
            let hitNode = res.collider.node;
            commonUtil.actionState=!commonUtil.actionState
        }
    }

模型响应后,使用键盘进行转动

    onKeyDown(event: EventKeyboard) {
        if(!commonUtil.actionState){
            moveUtil.movePosition(this.node, this.position_dis, event.keyCode)
        }else{
            if(!this.loadNode){
                return
            }
            viewUtil.viewModeDefault(this.loadNode, event)
        }
    }

  /**
     * 转动角色
     * @param node node
     * @param event 键盘事件
     * @param rotax x单元角度
     * @param rotay y单元角度
     */
    static viewMode(node: Node, event: EventKeyboard, rotax: number, rotay: number) {
        let rota_target = new Vec3();
        let rota_move = new Quat();
        let rota_cur = new Quat();
        node.getRotation(rota_cur);
        //四元数转欧拉角
        Quat.toEuler(rota_move, rota_cur);
        switch (event.keyCode) {
            case KeyCode.KEY_W:
                if (rota_move.x <= 0 && rota_move.x >= -180) {
                    if (rota_move.x - rotax < -180) {
                        rota_target = new Vec3(rota_move.x - rotax + 360, rota_move.y, 0);
                    } else {
                        rota_target = new Vec3(rota_move.x - rotax, rota_move.y, 0);
                    }
                } else {
                    rota_target = new Vec3(rota_move.x - rotax, rota_move.y, 0);
                }
                break;
            case KeyCode.KEY_S:
                if (rota_move.x >= 0 && rota_move.x <= 180) {
                    if (rota_move.x + rotax > 180) {
                        rota_target = new Vec3(rotax + rota_move.x - 360, rota_move.y, 0);
                    } else {
                        rota_target = new Vec3(rota_move.x + rotax, rota_move.y, 0);
                    }
                } else {
                    rota_target = new Vec3(rota_move.x + rotax, rota_move.y, 0);
                }
                break;
            case KeyCode.KEY_A:
                if (rota_move.y <= 0 && rota_move.y >= -180) {
                    if (rota_move.y - rotay > -180) {
                        rota_target = new Vec3(rota_move.x, rota_move.y - rotay + 360, 0);
                    } else {
                        rota_target = new Vec3(rota_move.x, rota_move.y - rotay, 0);
                    }
                } else {
                    rota_target = new Vec3(rota_move.x, rota_move.y - rotay, 0);
                }
                break;
            case KeyCode.KEY_D:
                if (rota_move.y <= 0 && rota_move.y <= 180) {
                    if (rota_move.y + rotay > 180) {
                        rota_target = new Vec3(rota_move.x, rota_move.y + rotay - 360, 0);
                    } else {
                        rota_target = new Vec3(rota_move.x, rota_move.y + rotay, 0);
                    }
                } else {
                    rota_target = new Vec3(rota_move.x, rota_move.y + rotay, 0);
                }
                break;
        }
        node.setRotationFromEuler(rota_target);
    }

单元角度可以随意指定,

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

暮雪...

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值