给在屏幕内的特定模型更换高亮材质,不在屏幕内的不做处理
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();
}
}