Files
cocos4/cocos/2d/renderer/render-entity.ts
T
2022-07-23 15:28:25 +08:00

223 lines
6.5 KiB
TypeScript

import { JSB } from 'internal:constants';
import { NativeRenderEntity } from './native-2d';
import { Batcher2D } from './batcher-2d';
import { RenderData } from './render-data';
import { RenderDrawInfo } from './render-draw-info';
import { color, Color, director, Material, Node } from '../../core';
import { EmitLocation } from '../../particle/enum';
import { Stage } from './stencil-manager';
export enum RenderEntityType {
STATIC,
DYNAMIC,
CROSSED,
}
export enum RenderEntityFloatSharedBufferView {
localOpacity,
count,
}
export enum RenderEntityUInt8SharedBufferView {
colorR,
colorG,
colorB,
colorA,
maskMode,
count,
}
export enum RenderEntityBoolSharedBufferView{
colorDirty,
enabled,
useLocal,
count,
}
export enum MaskMode {
NONE,
MASK,
MASK_INVERTED,
MASK_NODE,
MASK_NODE_INVERTED
}
export class RenderEntity {
private _renderEntityType: RenderEntityType = RenderEntityType.STATIC;
private _dynamicDrawInfoArr: RenderDrawInfo[] = [];
protected _node: Node | null = null;
protected _stencilStage: Stage = Stage.DISABLED;
protected _useLocal = false;
protected _maskMode = MaskMode.NONE;
protected declare _floatSharedBuffer: Float32Array;
protected declare _uint8SharedBuffer: Uint8Array;
protected declare _boolSharedBuffer:Uint8Array;
private declare _nativeObj: NativeRenderEntity;
get nativeObj () {
return this._nativeObj;
}
get renderDrawInfoArr () {
return this._dynamicDrawInfoArr;
}
get renderEntityType () {
return this._renderEntityType;
}
// set renderEntityType (val:RenderEntityType) {
// this._renderEntityType = val;
// }
protected _color: Color = Color.WHITE;
get color () {
return this._color;
}
set color (val: Color) {
this._color = val;
if (JSB) {
this._uint8SharedBuffer[RenderEntityUInt8SharedBufferView.colorR] = val.r;
this._uint8SharedBuffer[RenderEntityUInt8SharedBufferView.colorG] = val.g;
this._uint8SharedBuffer[RenderEntityUInt8SharedBufferView.colorB] = val.b;
this._uint8SharedBuffer[RenderEntityUInt8SharedBufferView.colorA] = val.a;
}
}
protected _localOpacity = 255;
get localOpacity () {
return this._localOpacity;
}
set localOpacity (val: number) {
this._localOpacity = val;
if (JSB) {
this._floatSharedBuffer[RenderEntityFloatSharedBufferView.localOpacity] = val;
}
}
protected _colorDirty = true;
get colorDirty () {
return this._colorDirty;
}
set colorDirty (val: boolean) {
this._colorDirty = val;
if (JSB) {
this._boolSharedBuffer[RenderEntityBoolSharedBufferView.colorDirty] = val ? 1 : 0;
}
}
protected _enabled = true;
get enabled () {
return this._enabled;
}
set enabled (val: boolean) {
this._enabled = val;
if (JSB) {
this._boolSharedBuffer[RenderEntityBoolSharedBufferView.enabled] = val ? 1 : 0;
}
}
constructor (entityType: RenderEntityType) {
if (JSB) {
if (!this._nativeObj) {
this._nativeObj = new NativeRenderEntity(entityType);
}
this._renderEntityType = entityType;
this.initSharedBuffer();
}
}
public addDynamicRenderDrawInfo (renderDrawInfo: RenderDrawInfo | null) {
if (JSB) {
if (renderDrawInfo) {
this._dynamicDrawInfoArr.push(renderDrawInfo);
this._nativeObj.addDynamicRenderDrawInfo(renderDrawInfo.nativeObj);
}
}
}
public removeDynamicRenderDrawInfo () {
if (JSB) {
this._dynamicDrawInfoArr.pop();
this._nativeObj.removeDynamicRenderDrawInfo();
}
}
public clearDynamicRenderDrawInfos () {
if (JSB) {
this._dynamicDrawInfoArr.length = 0;
this._nativeObj.clearDynamicRenderDrawInfos();
}
}
public setDynamicRenderDrawInfo (renderDrawInfo: RenderDrawInfo | null, index: number) {
if (JSB) {
if (renderDrawInfo) {
if (this._dynamicDrawInfoArr.length < index + 1) {
this._dynamicDrawInfoArr.push(renderDrawInfo);
this._nativeObj.addDynamicRenderDrawInfo(renderDrawInfo.nativeObj);
} else {
this._dynamicDrawInfoArr[index] = renderDrawInfo;
this._nativeObj.setDynamicRenderDrawInfo(renderDrawInfo.nativeObj, index);
}
}
}
}
public setMaskMode (mode: MaskMode) {
if (JSB) {
this._uint8SharedBuffer[RenderEntityUInt8SharedBufferView.maskMode] = mode;
}
this._maskMode = mode;
}
public getStaticRenderDrawInfo (): RenderDrawInfo | null {
if (JSB) {
const nativeDrawInfo = this._nativeObj.getStaticRenderDrawInfo(this._nativeObj.staticDrawInfoSize++);
const drawInfo = new RenderDrawInfo(nativeDrawInfo);
return drawInfo;
}
return null;
}
setNode (node: Node | null) {
if (JSB) {
if (this._node !== node) {
this._nativeObj.node = node;
}
}
this._node = node;
}
setStencilStage (stage: Stage) {
if (JSB) {
if (this._stencilStage !== stage) {
this._nativeObj.stencilStage = stage;
}
}
this._stencilStage = stage;
}
setUseLocal (useLocal: boolean) {
if (JSB) {
this._boolSharedBuffer[RenderEntityBoolSharedBufferView.useLocal] = useLocal ? 1 : 0;
}
this._useLocal = useLocal;
}
private initSharedBuffer () {
if (JSB) {
//this._sharedBuffer = new Float32Array(RenderEntitySharedBufferView.count);
const buffer = this._nativeObj.getEntitySharedBufferForJS();
let offset = 0;
this._floatSharedBuffer = new Float32Array(buffer, offset, RenderEntityFloatSharedBufferView.count);
offset += RenderEntityFloatSharedBufferView.count * 4;
this._uint8SharedBuffer = new Uint8Array(buffer, offset, RenderEntityUInt8SharedBufferView.count);
offset += RenderEntityUInt8SharedBufferView.count * 1;
this._boolSharedBuffer = new Uint8Array(buffer, offset, RenderEntityBoolSharedBufferView.count);
}
}
}