mirror of
https://github.com/cocos/cocos4.git
synced 2026-12-01 07:42:38 +08:00
Add method ' raycastUI '、' raycastUINode ' to rander-scene; (#4422)
Add method ' getComputeAABB ' to ui-transform-component; Add UI layer;
This commit is contained in:
committed by
Weiliang Wang
parent
955f414a60
commit
283edb8d22
@@ -4,6 +4,7 @@ import { EventType } from '../../../core/platform/event-manager/event-enum';
|
||||
import { EventListener, ILinstenerMask } from '../../../core/platform/event-manager/event-listener';
|
||||
import { Mat4, Rect, Size, Vec2, Vec3 } from '../../../core/value-types';
|
||||
import * as vmath from '../../../core/vmath';
|
||||
import { aabb } from '../../geom-utils';
|
||||
import { CanvasComponent } from './canvas-component';
|
||||
|
||||
const _vec2a = new Vec2();
|
||||
@@ -35,7 +36,7 @@ export class UITransformComponent extends Component {
|
||||
}
|
||||
|
||||
let clone: Size;
|
||||
if (CC_EDITOR){
|
||||
if (CC_EDITOR) {
|
||||
clone = new Size(this._contentSize);
|
||||
}
|
||||
|
||||
@@ -77,7 +78,7 @@ export class UITransformComponent extends Component {
|
||||
}
|
||||
|
||||
set height (value) {
|
||||
if (this.contentSize.height === value){
|
||||
if (this.contentSize.height === value) {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -149,6 +150,7 @@ export class UITransformComponent extends Component {
|
||||
|
||||
public __preload () {
|
||||
this.node.uiTransfromComp = this;
|
||||
this.node.layer = cc.Layers.UI;
|
||||
}
|
||||
|
||||
public onDestroy () {
|
||||
@@ -165,7 +167,7 @@ export class UITransformComponent extends Component {
|
||||
* node.setContentSize(cc.size(100, 100));
|
||||
* node.setContentSize(100, 100);
|
||||
*/
|
||||
public setContentSize (size: Size|number, height?: number) {
|
||||
public setContentSize (size: Size | number, height?: number) {
|
||||
const locContentSize = this._contentSize;
|
||||
let clone: Size;
|
||||
if (height === undefined) {
|
||||
@@ -174,7 +176,7 @@ export class UITransformComponent extends Component {
|
||||
return;
|
||||
}
|
||||
|
||||
if (CC_EDITOR){
|
||||
if (CC_EDITOR) {
|
||||
clone = new Size(this._contentSize);
|
||||
}
|
||||
|
||||
@@ -193,10 +195,10 @@ export class UITransformComponent extends Component {
|
||||
locContentSize.height = height;
|
||||
}
|
||||
|
||||
if (CC_EDITOR){
|
||||
if (CC_EDITOR) {
|
||||
// @ts-ignore
|
||||
this.node.emit(EventType.SIZE_CHANGED, clone);
|
||||
}else{
|
||||
} else {
|
||||
this.node.emit(EventType.SIZE_CHANGED);
|
||||
}
|
||||
}
|
||||
@@ -425,7 +427,25 @@ export class UITransformComponent extends Component {
|
||||
return rect;
|
||||
}
|
||||
|
||||
private _getVisibility (){
|
||||
/**
|
||||
* compute the corresponding aabb in world space for raycast
|
||||
*/
|
||||
public getComputeAABB (out?: aabb) {
|
||||
const rectWorld = this.getBoundingBoxToWorld();
|
||||
const px = rectWorld.center.x;
|
||||
const py = rectWorld.center.y;
|
||||
const pz = this.node.getWorldPosition().z;
|
||||
const w = rectWorld.width / 2;
|
||||
const h = rectWorld.height / 2;
|
||||
const l = 0.01;
|
||||
if (out != null) {
|
||||
aabb.set(out, px, py, pz, w, h, l);
|
||||
} else {
|
||||
return new aabb(px, py, pz, w, h, l);
|
||||
}
|
||||
}
|
||||
|
||||
private _getVisibility () {
|
||||
let visibility = -1;
|
||||
let parent = this.node;
|
||||
// 获取被渲染相机的 visibility
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { IBArray } from '../../3d/assets/mesh';
|
||||
import { intersect, ray, triangle } from '../../3d/geom-utils';
|
||||
import { aabb, intersect, ray, triangle } from '../../3d/geom-utils';
|
||||
import { RecyclePool } from '../../3d/memop';
|
||||
import { Root } from '../../core/root';
|
||||
import { Mat4, Vec3 } from '../../core/value-types';
|
||||
@@ -96,7 +96,7 @@ export class RenderScene {
|
||||
|
||||
constructor (root: Root) {
|
||||
this._root = root;
|
||||
this._ambient = new Ambient (this);
|
||||
this._ambient = new Ambient(this);
|
||||
this._defaultMainLightNode = new Node('Main Light');
|
||||
this._mainLight = new DirectionalLight(this, 'Main Light', this._defaultMainLightNode);
|
||||
this._mainLight.illuminance = Ambient.SUN_ILLUM;
|
||||
@@ -247,6 +247,61 @@ export class RenderScene {
|
||||
results.length = pool.length;
|
||||
return results;
|
||||
}
|
||||
|
||||
/**
|
||||
* Cast a ray into the scene, record all the intersected ui aabb in the result array
|
||||
* @param worldRay - the testing ray
|
||||
* @param mask - the layer mask to filter the ui aabb
|
||||
* @returns the results array
|
||||
*/
|
||||
public raycastUI (worldRay: ray, mask: number = Layers.UI): IRaycastResult[] {
|
||||
poolUI.reset();
|
||||
const canvasComs = cc.director.getScene().getComponentsInChildren(cc.CanvasComponent);
|
||||
if (canvasComs != null) {
|
||||
const canvasNode = canvasComs[0].node as Node;
|
||||
if (canvasNode != null && canvasNode.active) {
|
||||
this._raycastUINodeRecursiveChildren(worldRay, mask, canvasNode);
|
||||
}
|
||||
}
|
||||
resultUIs.length = poolUI.length;
|
||||
return resultUIs;
|
||||
}
|
||||
|
||||
/**
|
||||
* Before you raycast the ui node, make sure the node is not null
|
||||
* @param worldRay - the testing ray
|
||||
* @param mask - the layer mask to filter the models
|
||||
* @param uiNode - the ui node
|
||||
* @returns IRaycastResult | undefined
|
||||
*/
|
||||
public raycastUINode (worldRay: ray, mask: number = Layers.UI, uiNode: Node) {
|
||||
const uiTransfrom = uiNode.uiTransfromComp;
|
||||
if (uiTransfrom == null || !cc.Layers.check(uiNode.layer, mask)) { return; }
|
||||
uiTransfrom.getComputeAABB(aabbUI);
|
||||
// broadphase
|
||||
distance = intersect.ray_aabb(worldRay, aabbUI);
|
||||
|
||||
if (distance <= 0) {
|
||||
return;
|
||||
} else {
|
||||
const r = poolUI.add();
|
||||
r.node = uiNode;
|
||||
r.distance = distance * vec3.magnitude(vec3.mul(v3, worldRay.d, uiNode._scale));
|
||||
return r;
|
||||
}
|
||||
}
|
||||
|
||||
private _raycastUINodeRecursiveChildren (worldRay: ray, mask: number, parent: Node) {
|
||||
const result = this.raycastUINode(worldRay, mask, parent);
|
||||
if (result != null) {
|
||||
resultUIs[poolUI.length - 1] = result;
|
||||
}
|
||||
for (const node of parent.children) {
|
||||
if (node != null && node.active) {
|
||||
this._raycastUINodeRecursiveChildren(worldRay, mask, node);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const modelRay = ray.create();
|
||||
@@ -258,6 +313,12 @@ const pool = new RecyclePool<IRaycastResult>(() => {
|
||||
return { node: null!, distance: Infinity };
|
||||
}, 8);
|
||||
const results: IRaycastResult[] = [];
|
||||
/** UI raycast result pool */
|
||||
const aabbUI = new aabb();
|
||||
const poolUI = new RecyclePool<IRaycastResult>(() => {
|
||||
return { node: null!, distance: Infinity };
|
||||
}, 8);
|
||||
const resultUIs: IRaycastResult[] = [];
|
||||
|
||||
const narrowphase = (vb: Float32Array, ib: IBArray, pm: GFXPrimitiveMode, sides?: boolean) => {
|
||||
distance = Infinity;
|
||||
|
||||
@@ -9,6 +9,7 @@ export class Layers {
|
||||
public static IgnoreRaycast = (1 << 1);
|
||||
public static Gizmos = (1 << 2);
|
||||
public static Editor = (1 << 3);
|
||||
public static UI = (1 << 4);
|
||||
// masks
|
||||
public static All = Layers.makeExclusiveMask([Layers.Gizmos, Layers.Editor]);
|
||||
public static RaycastMask = Layers.makeExclusiveMask([Layers.Gizmos, Layers.Editor, Layers.IgnoreRaycast]);
|
||||
|
||||
Reference in New Issue
Block a user