Laya3判断模型的某些部分是否在屏幕内/是否在屏幕上特定区域

给在屏幕内的特定模型更换高亮材质,不在屏幕内的不做处理


import Camera = Laya.Camera;
import Scene3D = Laya.Scene3D;
import Event = Laya.Event;
import Sprite = Laya.Sprite;
import MeshRenderer = Laya.MeshRenderer;
import Vector3 = Laya.Vector3;
import Sprite3D = Laya.Sprite3D;


export class CheckOnscreen extends Laya.Script {
    private scene: Scene3D;
    private camera: Camera;
    private rectLine: Sprite;

    private rectMinX: number = 0;
    private rectMinY: number = 0;
    private rectMaxX: number = 0;
    private rectMaxY: number = 0;
    private isMouseDown: boolean = false;

    private mat: Laya.PBRStandardMaterial;

    onAwake(): void {
        this.scene = this.owner as Scene3D;
        this.camera = this.owner.getChildByName("Main Camera") as Camera;

        // 框定屏幕内的特定区域(便于调试)
        this.rectLine = this.scene.parent.addChild(new Laya.Sprite());

        Laya.loader.load("resources/Models/gaoliang.lmat").then((mat) => {
            this.mat = mat;
        });
    }

    onEnable() {
        return
        Laya.stage.on(Event.MOUSE_DOWN, this, this.mouseDown);
        Laya.stage.on(Event.MOUSE_UP, this, this.mouseUp);
        Laya.stage.on(Event.MOUSE_MOVE, this, this.mouseMove);
    }
    onDisable() {
        return
        Laya.stage.off(Event.MOUSE_DOWN, this, this.mouseDown);
        Laya.stage.off(Event.MOUSE_UP, this, this.mouseUp);
        Laya.stage.off(Event.MOUSE_MOVE, this, this.mouseMove);
    }

    mouseDown(e: any): void {
        this.rectMinX = Laya.stage.mouseX;
        this.rectMinY = Laya.stage.mouseY;

        this.isMouseDown = true;
    }
    mouseMove(e: any) {
        if (this.isMouseDown) {
            this.rectMaxX = Laya.stage.mouseX;
            this.rectMaxY = Laya.stage.mouseY;
            this.checkOnRect();
        }
    }
    mouseUp(e: any): void {
        this.rectMaxX = Laya.stage.mouseX;
        this.rectMaxY = Laya.stage.mouseY;
        this.isMouseDown = false;
    }
    checkOnScreen(): void {
        this.rectMinX = 0;
        this.rectMinY = 0;
        this.rectMaxX = Laya.stage.width;
        this.rectMaxY = Laya.stage.height;
        this.checkOnRect();
    }
    checkOnRect(): void {
        this.clearLine();
        let w = this.rectMaxX - this.rectMinX;
        let h = this.rectMaxY - this.rectMinY;
        let x = this.rectMinX;
        let y = this.rectMinY;
        this.rectLine.graphics.drawRect(x, y, w, h, "rgba(255,255,255,0)", "#ffff00");// 辅助线

        let checkObjList: any[] = [];
        let tushuguan = this.scene.getChildByName('modelBox').getChildByName('floor3') as any;
        for (let i = 0; i < tushuguan._children.length; i++) {
            let item = tushuguan._children[i];
            if (item.name.toLowerCase().includes("shelf.")) {// 模型特定part
                let pos = this.getObjScreenPos(item.transform.position);// 由于模型位置是变化的,当检测的时候需要再次获取各个part的屏幕坐标
                item.screenpos = pos;
                checkObjList.push(item);
            }
        }

        let minx = Math.min(this.rectMinX, this.rectMaxX);
        let maxx = Math.max(this.rectMinX, this.rectMaxX);
        let miny = Math.min(this.rectMinY, this.rectMaxY);
        let maxy = Math.max(this.rectMinY, this.rectMaxY);

        for (let i = 0; i < checkObjList.length; i++) {
            let checkItem = checkObjList[i];
            if (checkItem.screenpos.x > minx && checkItem.screenpos.x < maxx && checkItem.screenpos.y > miny && checkItem.screenpos.y < maxy) {
                // 检测部分位于屏幕、特定区域内
                checkItem.getComponent(Laya.MeshRenderer).material = this.mat;
            }
        }
    }

    getObjScreenPos(position: Laya.Vector3): Laya.Vector4 {
        var screenPos1 = new Laya.Vector4();
        this.camera.worldToViewportPoint(position, screenPos1);
        return screenPos1;
    }

    clearLine() {
        this.rectLine.graphics.clear();
    }

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值