import { _decorator,Node, Rect, rect, Mat4, mat4, Component, Camera, director, View, view, UITransform, Vec3 } from "cc"; const {ccclass, property, menu, requireComponent, executeInEditMode, playOnFocus} = _decorator; let _rect_temp_visible: Rect = rect(); let _rect_temp_child: Rect = rect(); let _mat4_temp: Mat4 = mat4(); let _mat4_camera_temp: Mat4 = mat4(); @ccclass export default class VisibleRangeClipping extends Component { @property(Camera) camera : Camera = null; left:Vec3 = new Vec3(); right:Vec3 = new Vec3(); mid:Vec3 = new Vec3(); private _checkContentNode: Node; // LIFE-CYCLE CALLBACKS: protected onLoad(): void { if(this.camera){ this._initCheck(); } } SetCamera(camera:Camera){ this.camera = camera; this._initCheck(); } private _initCheck() { this._checkContentNode = this.node; } private _updateOpacity () { let viewNode = this._checkContentNode; if (!viewNode) return; _rect_temp_visible = this.camera.node.getChildByName("CameraRect").getComponent(UITransform).getBoundingBoxToWorld(); let children = viewNode.children; let isShow = true; for (let i = 0, ilen = children?.length; i < ilen; i++) { // 参考 getBoundingBox 算法计算包围盒. let c = children[i]; //if (c.name == 'tipCenter' || c['clip']) continue; if (c.getComponent(VisibleRangeClipping)) continue; _rect_temp_child = c.getComponent(UITransform).getBoundingBoxToWorld(); isShow = _rect_temp_visible.intersects(_rect_temp_child); CNodeUtils.setOpacity(children[i],isShow ? 255 : 0); } } update (dt) { if (!(director.getTotalFrames() % 6)) { this._updateOpacity(); } } }