首页 > 其他分享 >Cocos Creator 3.x之点击选择3D物体

Cocos Creator 3.x之点击选择3D物体

时间:2024-01-02 23:31:31浏览次数:27  
标签:Cocos Creator curSelect3DNode void Material private camera3D tempScreenV2 3D

一, 场景设计

Cocos Creator 3.x之点击选择3D物体_选择

二, SelectView.ts

import {
    _decorator,
    Component,
    Node,
    Camera,
    MeshRenderer,
    Mesh,
    EventTouch,
    input,
    Input,
    Vec2,
    v2,
    geometry,
    PhysicsSystem,
    PhysicsRayResult,
    Material,
    isValid
} from "cc";

const {ccclass, property} = _decorator;

@ccclass("SelectView")
export class SelectView extends Component {
    @property({type: Material, tooltip: "没有选中的材质"})
    private unSelectMtl: Material = null;
    @property({type: Material, tooltip: "选中的材质"})
    private selectMtl: Material = null;

    private tempScreenV2: Vec2;
    private tempScreenRay: geometry.Ray;
    private curSelect3DNode: Node;
    private camera3D: Camera = null;

    public init(camera3D: Camera): void {
        this.camera3D = camera3D;
    }

    protected onl oad(): void {
        this.tempScreenV2 = v2();
        this.tempScreenRay = new geometry.Ray();
    }

    protected onEnable(): void {
        this.listener(true);
    }

    protected onDisable(): void {
        this.listener(false);
    }

    private listener(isAdd: boolean): void {
        if (isAdd) {
            input.on(Input.EventType.TOUCH_END, this.onTouchHandler, this);
        } else {
            input.off(Input.EventType.TOUCH_END, this.onTouchHandler, this);
        }
    }

    private onTouchHandler(e: EventTouch): void {
        switch (e.type) {
            case Input.EventType.TOUCH_END:
                this.select3DObject(e);
                break;
        }
    }

    private setMaterial(mtl: Material): void{
        if(!isValid(this.curSelect3DNode)) return;
        let meshRenderer: MeshRenderer = this.curSelect3DNode.getComponent(MeshRenderer);
        if(!meshRenderer) return;
        meshRenderer.material = mtl;
    }

    private select3DObject(e: EventTouch): void {
        this.tempScreenV2 = e.getLocation();//获得屏幕坐标
        this.camera3D.screenPointToRay(this.tempScreenV2.x, this.tempScreenV2.y, this.tempScreenRay);//构建一条射线
        if (PhysicsSystem.instance.raycastClosest(this.tempScreenRay, PhysicsSystem.PhysicsGroup.DEFAULT) == true) {
            let ressult: PhysicsRayResult = PhysicsSystem.instance.raycastClosestResult;//最近的一个碰撞体
            this.setMaterial(this.unSelectMtl);
            this.curSelect3DNode = ressult.collider.node;
            let name: string = ressult.collider.node.name;//名称
            this.setMaterial(this.selectMtl);
        } else {
            if (!this.curSelect3DNode) return;
            this.setMaterial(this.unSelectMtl);
            this.curSelect3DNode = null;
        }
    }
}


标签:Cocos,Creator,curSelect3DNode,void,Material,private,camera3D,tempScreenV2,3D
From: https://blog.51cto.com/aonaufly/9073686

相关文章

  • Cocos Creator 3.x之旋转基础
    一,场景设计二,整体场景效果三,QuatView.ts代码import{_decorator,Component,EventTouch,Input,input,Node,Quat,v3,Vec3,Vec2}from"cc";const{ccclass,property}=_decorator;@ccclass("QuatView")exportclassQuatViewextendsComponent{......
  • PPT-平滑及3D功能
    一、平滑功能如果想要实现两个图形的转场效果;可以利用动画效果来实现; 上面可以基本实现动画移动的效果,但是存在以下问题:1、想要在动画后图形不一样?;可以切换---平滑效果来实现 简单了解平滑功能后,做一个带有3D图形的平滑效果......
  • Unity3d Vertex/Fragment Shader添加Shadow
    Shader"ZX/BaseVertFragmentShadow"{Properties{_DiffuseTexture("DiffuseTexture",2D)="white"{}_DiffuseTint("DiffuseTint",Color)=(1,1,1,1)}SubShader{......
  • Meta3D -- 开源的Web3D低代码平台
    大家好,Meta3D是开源的Web3D低代码平台,快速搭建Web3D编辑器,共建开放互助的web3d生态Github进入平台功能演示加入UIControl加入Action脚本运行“完整的编辑器”模板Meta3D的开发历程我之前用了4年多的时间开发过Wonder:WebGL3D引擎和编辑器,它可以看作是Web版的Unity,对......
  • cocos 自定义property
    exportenumAviaBC2CountryEnum{GBR=1,USA=2,}//定义一个图片类型的类@ccclass("SpriteTypes")//这个名字一定要写,而且还注意不能取下面的类名不然cocosCreator会一直报警告classSpriteType{//语言@property({type:cc.Enum(AviaBC2Cou......
  • mac m1 编译cocos2d-x 在模拟器上运行 一些问题汇总
     如果涉及到侵权请联系本人删除 1  》〉/Users/yzfhkms-m/Library/Developer/Xcode/DerivedData/formi-dlcfwgxcmidqefdkxnvnfwfprpfs/Build/Products/Debug-iphonesimulator/formi-mobile.appisnotavalidpathtoanexecutablefile.Pleaserebuildtheprojectto......
  • 【Lidar】Open3D点云K-Means聚类算法:基于距离的点云聚类(单木分割)附Python代码
    ​ 1K-Means算法介绍        K-means聚类算法是一种无监督学习算法,主要用于数据聚类。该算法的主要目标是找到一个数据点的划分,使得每个数据点与其所在簇的质心(即该簇所有数据点的均值)之间的平方距离之和最小。        在K-means聚类算法中,首先需要预定义簇......
  • Unity3D 如何提升游戏运行效率详解
    前言Unity3D是一款非常强大的游戏引擎,但是在处理复杂场景和大量资源时,游戏运行效率可能会遇到一些问题。本文将详细介绍如何提升Unity3D游戏的运行效率,包括技术详解和代码实现。对惹,这里有一个游戏开发交流小组,希望大家可以点击进来一起交流一下开发经验呀使用合适的资源压缩......
  • Unity3D Shader在GPU上是如何执行的详解
    Unity3D是一款广泛应用于游戏开发的跨平台开发引擎,它提供了丰富的功能和工具来帮助开发者创建高质量的游戏。其中一个重要的功能就是Shader,它可以用来控制对象的渲染效果。在Unity3D中,Shader是在GPU上执行的,那么它是如何工作的呢?本文将详细解释Unity3DShader在GPU上的执行过程,并......
  • Unity3D Shader Compute Shader基于GPU的并发计算详解
    在游戏开发中,计算密集型的任务通常需要耗费大量的CPU资源,这可能导致游戏性能下降,影响玩家的游戏体验。为了解决这个问题,Unity3D引入了ShaderComputeShader技术,它使用GPU进行并发计算,将一些计算密集型任务从CPU转移到GPU上执行,以提高游戏的性能和效率。本文将详细介绍Unity3DSha......