123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011 |
- module BABYLON {
- export class InstanceClassInfo {
- constructor(base: InstanceClassInfo) {
- this._baseInfo = base;
- this._nextOffset = new StringDictionary<number>();
- this._attributes = new Array<InstancePropInfo>();
- }
- mapProperty(propInfo: InstancePropInfo, push: boolean) {
- let curOff = this._nextOffset.getOrAdd(InstanceClassInfo._CurCategories, 0);
- propInfo.instanceOffset.add(InstanceClassInfo._CurCategories, this._getBaseOffset(InstanceClassInfo._CurCategories) + curOff);
- //console.log(`[${InstanceClassInfo._CurCategories}] New PropInfo. Category: ${propInfo.category}, Name: ${propInfo.attributeName}, Offset: ${propInfo.instanceOffset.get(InstanceClassInfo._CurCategories)}, Size: ${propInfo.size / 4}`);
- this._nextOffset.set(InstanceClassInfo._CurCategories, curOff + (propInfo.size / 4));
- if (push) {
- this._attributes.push(propInfo);
- }
- }
- getInstancingAttributeInfos(effect: Effect, categories: string[]): InstancingAttributeInfo[] {
- let catInline = ";" + categories.join(";") + ";";
- let res = new Array<InstancingAttributeInfo>();
- let curInfo: InstanceClassInfo = this;
- while (curInfo) {
- for (let attrib of curInfo._attributes) {
- // Only map if there's no category assigned to the instance data or if there's a category and it's in the given list
- if (!attrib.category || categories.indexOf(attrib.category) !== -1) {
- let index = effect.getAttributeLocationByName(attrib.attributeName);
- if (index === - 1) {
- throw new Error(`Attribute ${attrib.attributeName} was not found in Effect: ${effect.name}. It's certainly no longer used in the Effect's Shaders`);
- }
- let iai = new InstancingAttributeInfo();
- iai.index = index;
- iai.attributeSize = attrib.size / 4; // attrib.size is in byte and we need to store in "component" (i.e float is 1, vec3 is 3)
- iai.offset = attrib.instanceOffset.get(catInline) * 4; // attrib.instanceOffset is in float, iai.offset must be in bytes
- iai.attributeName = attrib.attributeName;
- res.push(iai);
- }
- }
- curInfo = curInfo._baseInfo;
- }
- return res;
- }
- getShaderAttributes(categories: string[]): string[] {
- let res = new Array<string>();
- let curInfo: InstanceClassInfo = this;
- while (curInfo) {
- for (let attrib of curInfo._attributes) {
- // Only map if there's no category assigned to the instance data or if there's a category and it's in the given list
- if (!attrib.category || categories.indexOf(attrib.category) !== -1) {
- res.push(attrib.attributeName);
- }
- }
- curInfo = curInfo._baseInfo;
- }
- return res;
- }
- private _getBaseOffset(categories: string): number {
- let curOffset = 0;
- let curBase = this._baseInfo;
- while (curBase) {
- curOffset += curBase._nextOffset.getOrAdd(categories, 0);
- curBase = curBase._baseInfo;
- }
- return curOffset;
- }
- static _CurCategories: string;
- private _baseInfo: InstanceClassInfo;
- private _nextOffset: StringDictionary<number>;
- private _attributes: Array<InstancePropInfo>;
- }
- export class InstancePropInfo {
- attributeName: string;
- category: string;
- size: number;
- instanceOffset: StringDictionary<number>;
- dataType: ShaderDataType;
- curCategory: string;
- curCategoryOffset: number;
- //uniformLocation: WebGLUniformLocation;
- delimitedCategory: string;
- constructor() {
- this.attributeName = null;
- this.category = null;
- this.size = null;
- this.instanceOffset = new StringDictionary<number>();
- this.dataType = 0;
- this.curCategory = "";
- this.curCategoryOffset = 0;
- }
- setSize(val) {
- if (val instanceof Vector2) {
- this.size = 8;
- this.dataType = ShaderDataType.Vector2;
- return;
- }
- if (val instanceof Vector3) {
- this.size = 12;
- this.dataType = ShaderDataType.Vector3;
- return;
- }
- if (val instanceof Vector4) {
- this.size = 16;
- this.dataType = ShaderDataType.Vector4;
- return;
- }
- if (val instanceof Matrix2D) {
- throw new Error("Matrix2D type is not supported by WebGL Instance Buffer, you have to use four Vector4 properties instead");
- }
- if (typeof (val) === "number") {
- this.size = 4;
- this.dataType = ShaderDataType.float;
- return;
- }
- if (val instanceof Color3) {
- this.size = 12;
- this.dataType = ShaderDataType.Color3;
- return;
- }
- if (val instanceof Color4) {
- this.size = 16;
- this.dataType = ShaderDataType.Color4;
- return;
- }
- if (val instanceof Size) {
- this.size = 8;
- this.dataType = ShaderDataType.Size;
- return;
- } return;
- }
- writeData(array: Float32Array, offset: number, val) {
- switch (this.dataType) {
- case ShaderDataType.Vector2:
- {
- let v = <Vector2>val;
- array[offset + 0] = v.x;
- array[offset + 1] = v.y;
- break;
- }
- case ShaderDataType.Vector3:
- {
- let v = <Vector3>val;
- array[offset + 0] = v.x;
- array[offset + 1] = v.y;
- array[offset + 2] = v.z;
- break;
- }
- case ShaderDataType.Vector4:
- {
- let v = <Vector4>val;
- array[offset + 0] = v.x;
- array[offset + 1] = v.y;
- array[offset + 2] = v.z;
- array[offset + 3] = v.w;
- break;
- }
- case ShaderDataType.Color3:
- {
- let v = <Color3>val;
- array[offset + 0] = v.r;
- array[offset + 1] = v.g;
- array[offset + 2] = v.b;
- break;
- }
- case ShaderDataType.Color4:
- {
- let v = <Color4>val;
- array[offset + 0] = v.r;
- array[offset + 1] = v.g;
- array[offset + 2] = v.b;
- array[offset + 3] = v.a;
- break;
- }
- case ShaderDataType.float:
- {
- let v = <number>val;
- array[offset] = v;
- break;
- }
- case ShaderDataType.Size:
- {
- let s = <Size>val;
- array[offset + 0] = s.width;
- array[offset + 1] = s.height;
- break;
- }
- }
- }
- }
- export function instanceData<T>(category?: string, shaderAttributeName?: string): (target: Object, propName: string | symbol, descriptor: TypedPropertyDescriptor<T>) => void {
- return (target: Object, propName: string | symbol, descriptor: TypedPropertyDescriptor<T>) => {
- let dic = ClassTreeInfo.getOrRegister<InstanceClassInfo, InstancePropInfo>(target, (base) => new InstanceClassInfo(base));
- let node = dic.getLevelOf(target);
- let instanceDataName = <string>propName;
- shaderAttributeName = shaderAttributeName || instanceDataName;
- let info = node.levelContent.get(instanceDataName);
- if (info) {
- throw new Error(`The ID ${instanceDataName} is already taken by another instance data`);
- }
- info = new InstancePropInfo();
- info.attributeName = shaderAttributeName;
- info.category = category || null;
- if (info.category) {
- info.delimitedCategory = ";" + info.category + ";";
- }
- node.levelContent.add(instanceDataName, info);
- descriptor.get = function () {
- return null;
- }
- descriptor.set = function (val) {
- // Check that we're not trying to set a property that belongs to a category that is not allowed (current)
- // Quit if it's the case, otherwise we could overwrite data somewhere...
- if (info.category && InstanceClassInfo._CurCategories.indexOf(info.delimitedCategory) === -1) {
- return;
- }
- let catOffset: number;
- if (info.curCategory === InstanceClassInfo._CurCategories) {
- catOffset = info.curCategoryOffset;
- } else {
- if (!info.size) {
- info.setSize(val);
- node.classContent.mapProperty(info, true);
- } else if (!info.instanceOffset.contains(InstanceClassInfo._CurCategories)) {
- node.classContent.mapProperty(info, false);
- }
- catOffset = info.instanceOffset.get(InstanceClassInfo._CurCategories);
- info.curCategory = InstanceClassInfo._CurCategories;
- info.curCategoryOffset = catOffset;
- }
- let obj: InstanceDataBase = this;
- if (obj.dataBuffer && obj.dataElements) {
- let offset = obj.dataElements[obj.curElement].offset + catOffset;
- info.writeData(obj.dataBuffer.buffer, offset, val);
- }
- }
- }
- }
- export class InstanceDataBase {
- constructor(partId: number, dataElementCount: number) {
- this.id = partId;
- this.curElement = 0;
- this._dataElementCount = dataElementCount;
- this.renderMode = 0;
- this.arrayLengthChanged = false;
- }
- id: number;
- isVisible: boolean;
- @instanceData()
- get zBias(): Vector2 {
- return null;
- }
- set zBias(value: Vector2) {
- }
- @instanceData()
- get transformX(): Vector4 {
- return null;
- }
- set transformX(value: Vector4) {
- }
- @instanceData()
- get transformY(): Vector4 {
- return null;
- }
- set transformY(value: Vector4) {
- }
- // The vector3 is: rendering width, height and 1 if the primitive must be aligned to pixel or 0 otherwise
- @instanceData()
- get renderingInfo(): Vector3 {
- return null;
- }
- set renderingInfo(val: Vector3) {
- }
- @instanceData()
- get opacity(): number {
- return null;
- }
- set opacity(value: number) {
- }
- getClassTreeInfo(): ClassTreeInfo<InstanceClassInfo, InstancePropInfo> {
- if (!this.typeInfo) {
- this.typeInfo = ClassTreeInfo.get<InstanceClassInfo, InstancePropInfo>(Object.getPrototypeOf(this));
- }
- return this.typeInfo;
- }
- allocElements() {
- if (!this.dataBuffer || this.dataElements) {
- return;
- }
- let res = new Array<DynamicFloatArrayElementInfo>(this.dataElementCount);
- for (let i = 0; i < this.dataElementCount; i++) {
- res[i] = this.dataBuffer.allocElement();
- }
- this.dataElements = res;
- }
- freeElements() {
- if (!this.dataElements) {
- return;
- }
- for (let ei of this.dataElements) {
- this.dataBuffer.freeElement(ei);
- }
- this.dataElements = null;
- }
- get dataElementCount(): number {
- return this._dataElementCount;
- }
- set dataElementCount(value: number) {
- if (value === this._dataElementCount) {
- return;
- }
- this.arrayLengthChanged = true;
- this.freeElements();
- this._dataElementCount = value;
- this.allocElements();
- }
- groupInstanceInfo: GroupInstanceInfo;
- arrayLengthChanged: boolean;
- curElement: number;
- renderMode: number;
- dataElements: DynamicFloatArrayElementInfo[];
- dataBuffer: DynamicFloatArray;
- typeInfo: ClassTreeInfo<InstanceClassInfo, InstancePropInfo>;
- private _dataElementCount: number;
- }
- @className("RenderablePrim2D", "BABYLON")
- /**
- * The abstract class for primitive that render into the Canvas2D
- */
- export abstract class RenderablePrim2D extends Prim2DBase {
- static RENDERABLEPRIM2D_PROPCOUNT: number = Prim2DBase.PRIM2DBASE_PROPCOUNT + 5;
- public static isAlphaTestProperty: Prim2DPropInfo;
- public static isTransparentProperty: Prim2DPropInfo;
- @dynamicLevelProperty(Prim2DBase.PRIM2DBASE_PROPCOUNT + 0, pi => RenderablePrim2D.isAlphaTestProperty = pi)
- /**
- * Get/set if the Primitive is from the AlphaTest rendering category.
- * The AlphaTest category is the rendering pass with alpha blend, depth compare and write activated.
- * Primitives that render with an alpha mask should be from this category.
- * The setter should be used only by implementers of new primitive type.
- */
- public get isAlphaTest(): boolean {
- return this._useTextureAlpha() || this._isPrimAlphaTest();
- }
- @dynamicLevelProperty(Prim2DBase.PRIM2DBASE_PROPCOUNT + 1, pi => RenderablePrim2D.isTransparentProperty = pi)
- /**
- * Get/set if the Primitive is from the Transparent rendering category.
- * The setter should be used only by implementers of new primitive type.
- */
- public get isTransparent(): boolean {
- return (this.actualOpacity<1) || this._shouldUseAlphaFromTexture() || this._isPrimTransparent();
- }
- public get renderMode(): number {
- return this._renderMode;
- }
- constructor(settings?: {
- parent ?: Prim2DBase,
- id ?: string,
- origin ?: Vector2,
- isVisible ?: boolean,
- }) {
- super(settings);
- this._transparentPrimitiveInfo = null;
- }
- /**
- * Dispose the primitive and its resources, remove it from its parent
- */
- public dispose(): boolean {
- if (!super.dispose()) {
- return false;
- }
- if (this.renderGroup) {
- this.renderGroup._setCacheGroupDirty();
- }
- if (this._transparentPrimitiveInfo) {
- this.renderGroup._renderableData.removeTransparentPrimitiveInfo(this._transparentPrimitiveInfo);
- this._transparentPrimitiveInfo = null;
- }
- if (this._instanceDataParts) {
- this._cleanupInstanceDataParts();
- }
- if (this._modelRenderCache) {
- this._modelRenderCache.dispose();
- this._modelRenderCache = null;
- }
- if (this._instanceDataParts) {
- this._instanceDataParts.forEach(p => {
- p.freeElements();
- });
- this._instanceDataParts = null;
- }
- return true;
- }
- private _cleanupInstanceDataParts() {
- let gii: GroupInstanceInfo = null;
- for (let part of this._instanceDataParts) {
- part.freeElements();
- gii = part.groupInstanceInfo;
- part.groupInstanceInfo = null;
- }
- if (gii && !gii.isDisposed) {
- let usedCount = 0;
- if (gii.hasOpaqueData) {
- let od = gii.opaqueData[0];
- usedCount += od._partData.usedElementCount;
- gii.opaqueDirty = true;
- }
- if (gii.hasAlphaTestData) {
- let atd = gii.alphaTestData[0];
- usedCount += atd._partData.usedElementCount;
- gii.alphaTestDirty = true;
- }
- if (gii.hasTransparentData) {
- let td = gii.transparentData[0];
- usedCount += td._partData.usedElementCount;
- gii.transparentDirty = true;
- }
- if (usedCount === 0 && gii.modelRenderCache!=null) {
- this.renderGroup._renderableData._renderGroupInstancesInfo.remove(gii.modelRenderCache.modelKey);
- gii.dispose();
- }
- if (this._modelRenderCache) {
- this._modelRenderCache.dispose();
- this._modelRenderCache = null;
- }
- }
- this._instanceDataParts = null;
- }
- public _prepareRenderPre(context: PrepareRender2DContext) {
- super._prepareRenderPre(context);
- // If the model changed and we have already an instance, we must remove this instance from the obsolete model
- if (this._isFlagSet(SmartPropertyPrim.flagModelDirty) && this._instanceDataParts) {
- this._cleanupInstanceDataParts();
- }
- // Need to create the model?
- let setupModelRenderCache = false;
- if (!this._modelRenderCache || this._isFlagSet(SmartPropertyPrim.flagModelDirty)) {
- setupModelRenderCache = this._createModelRenderCache();
- }
- let gii: GroupInstanceInfo = null;
- let newInstance = false;
- // Need to create the instance data parts?
- if (!this._instanceDataParts) {
- // Yes, flag it for later, more processing will have to be done
- newInstance = true;
- gii = this._createModelDataParts();
- }
- // If the ModelRenderCache is brand new, now is the time to call the implementation's specific setup method to create the rendering resources
- if (setupModelRenderCache) {
- this.setupModelRenderCache(this._modelRenderCache);
- }
- if (this._isFlagSet(SmartPropertyPrim.flagModelUpdate)) {
- if (this._modelRenderCache.updateModelRenderCache(this)) {
- this._clearFlags(SmartPropertyPrim.flagModelUpdate);
- }
- }
- // At this stage we have everything correctly initialized, ModelRenderCache is setup, Model Instance data are good too, they have allocated elements in the Instanced DynamicFloatArray.
- // The last thing to do is check if the instanced related data must be updated because a InstanceLevel property had changed or the primitive visibility changed.
- if (this._areSomeFlagsSet(SmartPropertyPrim.flagVisibilityChanged | SmartPropertyPrim.flagNeedRefresh) || context.forceRefreshPrimitive || newInstance || (this._instanceDirtyFlags !== 0) || (this._globalTransformProcessStep !== this._globalTransformStep) || this._mustUpdateInstance()) {
- this._updateInstanceDataParts(gii);
- }
- }
- private _createModelRenderCache(): boolean {
- let setupModelRenderCache = false;
- if (this._modelRenderCache) {
- this._modelRenderCache.dispose();
- }
- this._modelRenderCache = this.owner._engineData.GetOrAddModelCache(this.modelKey, (key: string) => {
- let mrc = this.createModelRenderCache(key);
- setupModelRenderCache = true;
- return mrc;
- });
- this._clearFlags(SmartPropertyPrim.flagModelDirty);
- // if this is still false it means the MRC already exists, so we add a reference to it
- if (!setupModelRenderCache) {
- this._modelRenderCache.addRef();
- }
- return setupModelRenderCache;
- }
- private _createModelDataParts(): GroupInstanceInfo {
- // Create the instance data parts of the primitive and store them
- let parts = this.createInstanceDataParts();
- this._instanceDataParts = parts;
- // Check if the ModelRenderCache for this particular instance is also brand new, initialize it if it's the case
- if (!this._modelRenderCache._partData) {
- this._setupModelRenderCache(parts);
- }
- // The Rendering resources (Effect, VB, IB, Textures) are stored in the ModelRenderCache
- // But it's the RenderGroup that will store all the Instanced related data to render all the primitive it owns.
- // So for a given ModelKey we getOrAdd a GroupInstanceInfo that will store all these data
- let gii = this.renderGroup._renderableData._renderGroupInstancesInfo.getOrAddWithFactory(this.modelKey, k => {
- let res = new GroupInstanceInfo(this.renderGroup, this._modelRenderCache, this._modelRenderCache._partData.length);
- for (let j = 0; j < this._modelRenderCache._partData.length; j++) {
- let part = this._instanceDataParts[j];
- res.partIndexFromId.add(part.id.toString(), j);
- res.usedShaderCategories[j] = ";" + this.getUsedShaderCategories(part).join(";") + ";";
- res.strides[j] = this._modelRenderCache._partData[j]._partDataStride;
- }
- return res;
- });
- // Get the GroupInfoDataPart corresponding to the render category of the part
- let rm = 0;
- let gipd: GroupInfoPartData[] = null;
- if (this.isTransparent) {
- gipd = gii.transparentData;
- rm = Render2DContext.RenderModeTransparent;
- } else if (this.isAlphaTest) {
- gipd = gii.alphaTestData;
- rm = Render2DContext.RenderModeAlphaTest;
- } else {
- gipd = gii.opaqueData;
- rm = Render2DContext.RenderModeOpaque;
- }
- // For each instance data part of the primitive, allocate the instanced element it needs for render
- for (let i = 0; i < parts.length; i++) {
- let part = parts[i];
- part.dataBuffer = gipd[i]._partData;
- part.allocElements();
- part.renderMode = rm;
- part.groupInstanceInfo = gii;
- }
- return gii;
- }
- private _setupModelRenderCache(parts: InstanceDataBase[]) {
- let ctiArray = new Array<ClassTreeInfo<InstanceClassInfo, InstancePropInfo>>();
- this._modelRenderCache._partData = new Array<ModelRenderCachePartData>();
- for (let dataPart of parts) {
- var pd = new ModelRenderCachePartData();
- this._modelRenderCache._partData.push(pd)
- var cat = this.getUsedShaderCategories(dataPart);
- var cti = dataPart.getClassTreeInfo();
- // Make sure the instance is visible other the properties won't be set and their size/offset wont be computed
- let curVisible = this.isVisible;
- this.isVisible = true;
- // We manually trigger refreshInstanceData for the only sake of evaluating each instance property size and offset in the instance data, this can only be made at runtime. Once it's done we have all the information to create the instance data buffer.
- //console.log("Build Prop Layout for " + Tools.getClassName(this._instanceDataParts[0]));
- var joinCat = ";" + cat.join(";") + ";";
- pd._partJoinedUsedCategories = joinCat;
- InstanceClassInfo._CurCategories = joinCat;
- let obj = this.beforeRefreshForLayoutConstruction(dataPart);
- if (!this.refreshInstanceDataPart(dataPart)) {
- console.log(`Layout construction for ${Tools.getClassName(this._instanceDataParts[0])} failed because refresh returned false`);
- }
- this.afterRefreshForLayoutConstruction(dataPart, obj);
- this.isVisible = curVisible;
- var size = 0;
- cti.fullContent.forEach((k, v) => {
- if (!v.category || cat.indexOf(v.category) !== -1) {
- if (v.attributeName === "zBias") {
- pd._zBiasOffset = v.instanceOffset.get(joinCat);
- }
- if (!v.size) {
- console.log(`ERROR: Couldn't detect the size of the Property ${v.attributeName} from type ${Tools.getClassName(cti.type)}. Property is ignored.`);
- } else {
- size += v.size;
- }
- }
- });
- pd._partDataStride = size;
- pd._partUsedCategories = cat;
- pd._partId = dataPart.id;
- ctiArray.push(cti);
- }
- this._modelRenderCache._partsClassInfo = ctiArray;
- }
- protected onZOrderChanged() {
- if (this.isTransparent && this._transparentPrimitiveInfo) {
- this.renderGroup._renderableData.transparentPrimitiveZChanged(this._transparentPrimitiveInfo);
- let gii = this.renderGroup._renderableData._renderGroupInstancesInfo.get(this.modelKey);
- // Flag the transparentData dirty has will have to sort it again
- gii.transparentOrderDirty = true;
- }
- }
- protected _mustUpdateInstance(): boolean {
- return false;
- }
- protected _useTextureAlpha(): boolean {
- return false;
- }
- protected _shouldUseAlphaFromTexture(): boolean {
- return false;
- }
- protected _isPrimAlphaTest(): boolean {
- return false;
- }
- protected _isPrimTransparent(): boolean {
- return false;
- }
- private _updateInstanceDataParts(gii: GroupInstanceInfo) {
- // Fetch the GroupInstanceInfo if we don't already have it
- let rd = this.renderGroup._renderableData;
- if (!gii) {
- gii = rd._renderGroupInstancesInfo.get(this.modelKey);
- }
- if (gii.isDisposed) {
- return;
- }
- let isTransparent = this.isTransparent;
- let isAlphaTest = this.isAlphaTest;
- let wereTransparent = false;
- // Check a render mode change
- let rmChanged = false;
- if (this._instanceDataParts.length>0) {
- let firstPart = this._instanceDataParts[0];
- let partRM = firstPart.renderMode;
- let curRM = this.renderMode;
- if (partRM !== curRM) {
- wereTransparent = partRM === Render2DContext.RenderModeTransparent;
- rmChanged = true;
- let gipd: TransparentGroupInfoPartData[];
- switch (curRM) {
- case Render2DContext.RenderModeTransparent:
- gipd = gii.transparentData;
- break;
- case Render2DContext.RenderModeAlphaTest:
- gipd = gii.alphaTestData;
- break;
- default:
- gipd = gii.opaqueData;
- }
- for (let i = 0; i < this._instanceDataParts.length; i++) {
- let part = this._instanceDataParts[i];
- part.freeElements();
- part.dataBuffer = gipd[i]._partData;
- part.renderMode = curRM;
- }
- }
- }
- // Handle changes related to ZOffset
- let visChanged = this._isFlagSet(SmartPropertyPrim.flagVisibilityChanged);
- if (isTransparent || wereTransparent) {
- // Handle visibility change, which is also triggered when the primitive just got created
- if (visChanged || rmChanged) {
- if (this.isVisible && !wereTransparent) {
- if (!this._transparentPrimitiveInfo) {
- // Add the primitive to the list of transparent ones in the group that render is
- this._transparentPrimitiveInfo = rd.addNewTransparentPrimitiveInfo(this, gii);
- }
- } else {
- if (this._transparentPrimitiveInfo) {
- rd.removeTransparentPrimitiveInfo(this._transparentPrimitiveInfo);
- this._transparentPrimitiveInfo = null;
- }
- }
- gii.transparentOrderDirty = true;
- }
- }
- let rebuildTrans = false;
- // For each Instance Data part, refresh it to update the data in the DynamicFloatArray
- for (let part of this._instanceDataParts) {
- let justAllocated = false;
- // Check if we need to allocate data elements (hidden prim which becomes visible again)
- if (!part.dataElements && (visChanged || rmChanged || this.isVisible)) {
- part.allocElements();
- justAllocated = true;
- }
- InstanceClassInfo._CurCategories = gii.usedShaderCategories[gii.partIndexFromId.get(part.id.toString())];
- // Will return false if the instance should not be rendered (not visible or other any reasons)
- part.arrayLengthChanged = false;
- if (!this.refreshInstanceDataPart(part)) {
- // Free the data element
- if (part.dataElements) {
- part.freeElements();
- }
- // The refresh couldn't succeed, push the primitive to be dirty again for the next render
- if (this.isVisible) {
- rd._primNewDirtyList.push(this);
- }
- }
- rebuildTrans = rebuildTrans || part.arrayLengthChanged || justAllocated;
- }
- this._instanceDirtyFlags = 0;
- // Make the appropriate data dirty
- if (isTransparent) {
- gii.transparentDirty = true;
- if (rebuildTrans) {
- rd._transparentListChanged = true;
- }
- } else if (isAlphaTest) {
- gii.alphaTestDirty = true;
- } else {
- gii.opaqueDirty = true;
- }
- this._clearFlags(SmartPropertyPrim.flagVisibilityChanged); // Reset the flag as we've handled the case
- }
- _updateTransparentSegmentIndices(ts: TransparentSegment) {
- let minOff = Prim2DBase._bigInt;
- let maxOff = 0;
- for (let part of this._instanceDataParts) {
- if (part && part.dataElements) {
- part.dataBuffer.pack();
- for (let el of part.dataElements) {
- minOff = Math.min(minOff, el.offset);
- maxOff = Math.max(maxOff, el.offset);
- }
- ts.startDataIndex = Math.min(ts.startDataIndex, minOff / part.dataBuffer.stride);
- ts.endDataIndex = Math.max(ts.endDataIndex, (maxOff / part.dataBuffer.stride) + 1); // +1 for exclusive
- }
- }
- }
- // This internal method is mainly used for transparency processing
- public _getNextPrimZOrder(): number {
- let length = this._instanceDataParts.length;
- for (let i = 0; i < length; i++) {
- let part = this._instanceDataParts[i];
- if (part) {
- let stride = part.dataBuffer.stride;
- let lastElementOffset = part.dataElements[part.dataElements.length - 1].offset;
- // check if it's the last in the DFA
- if (part.dataBuffer.totalElementCount * stride <= lastElementOffset) {
- return null;
- }
- // Return the Z of the next primitive that lies in the DFA
- return part.dataBuffer[lastElementOffset + stride + this.modelRenderCache._partData[i]._zBiasOffset];
- }
- }
- return null;
- }
- // This internal method is mainly used for transparency processing
- public _getPrevPrimZOrder(): number {
- let length = this._instanceDataParts.length;
- for (let i = 0; i < length; i++) {
- let part = this._instanceDataParts[i];
- if (part) {
- let stride = part.dataBuffer.stride;
- let firstElementOffset = part.dataElements[0].offset;
- // check if it's the first in the DFA
- if (firstElementOffset === 0) {
- return null;
- }
- // Return the Z of the previous primitive that lies in the DFA
- return part.dataBuffer[firstElementOffset - stride + this.modelRenderCache._partData[i]._zBiasOffset];
- }
- }
- return null;
- }
- private static _toz = Size.Zero();
- /**
- * Get the info for a given effect based on the dataPart metadata
- * @param dataPartId partId in part list to get the info
- * @param vertexBufferAttributes vertex buffer attributes to manually add
- * @param uniforms uniforms to manually add
- * @param useInstanced specified if Instanced Array should be used, if null the engine caps will be used (so true if WebGL supports it, false otherwise), but you have the possibility to override the engine capability. However, if you manually set true but the engine does not support Instanced Array, this method will return null
- */
- protected getDataPartEffectInfo(dataPartId: number, vertexBufferAttributes: string[], uniforms: string[] = null, useInstanced: boolean = null): { attributes: string[], uniforms: string[], defines: string } {
- let dataPart = Tools.first(this._instanceDataParts, i => i.id === dataPartId);
- if (!dataPart) {
- return null;
- }
- let instancedArray = this.owner.supportInstancedArray;
- if (useInstanced != null) {
- // Check if the caller ask for Instanced Array and the engine does not support it, return null if it's the case
- if (useInstanced && instancedArray === false) {
- return null;
- }
- // Use the caller's setting
- instancedArray = useInstanced;
- }
- let cti = dataPart.getClassTreeInfo();
- let categories = this.getUsedShaderCategories(dataPart);
- let att = cti.classContent.getShaderAttributes(categories);
- let defines = "";
- categories.forEach(c => { defines += `#define ${c}\n` });
- if (instancedArray) {
- defines += "#define Instanced\n";
- }
- return {
- attributes: instancedArray ? vertexBufferAttributes.concat(att) : vertexBufferAttributes,
- uniforms: instancedArray ? (uniforms != null ? uniforms : []) : ((uniforms != null) ? att.concat(uniforms) : (att!=null ? att : [])),
- defines: defines
- };
- }
- protected get modelRenderCache(): ModelRenderCache {
- return this._modelRenderCache;
- }
- protected createModelRenderCache(modelKey: string): ModelRenderCache {
- return null;
- }
- protected setupModelRenderCache(modelRenderCache: ModelRenderCache) {
- }
- protected createInstanceDataParts(): InstanceDataBase[] {
- return null;
- }
- protected getUsedShaderCategories(dataPart: InstanceDataBase): string[] {
- return [];
- }
- protected beforeRefreshForLayoutConstruction(part: InstanceDataBase): any {
- }
- protected afterRefreshForLayoutConstruction(part: InstanceDataBase, obj: any) {
- }
- protected applyActualScaleOnTransform(): boolean {
- return true;
- }
- protected refreshInstanceDataPart(part: InstanceDataBase): boolean {
- if (!this.isVisible) {
- return false;
- }
- part.isVisible = this.isVisible;
- // Which means, if there's only one data element, we're update it from this method, otherwise it is the responsibility of the derived class to call updateInstanceDataPart as many times as needed, properly (look at Text2D's implementation for more information)
- if (part.dataElementCount === 1) {
- part.curElement = 0;
- this.updateInstanceDataPart(part);
- }
- return true;
- }
- private static _uV = new Vector2(1, 1);
- private static _s = Vector2.Zero();
- private static _r = Quaternion.Identity();
- private static _t = Vector2.Zero();
- private static _iV2 = new Vector2(1, 1); // Must stay identity vector3
- /**
- * Update the instanceDataBase level properties of a part
- * @param part the part to update
- * @param positionOffset to use in multi part per primitive (e.g. the Text2D has N parts for N letter to display), this give the offset to apply (e.g. the position of the letter from the bottom/left corner of the text).
- */
- protected updateInstanceDataPart(part: InstanceDataBase, positionOffset: Vector2 = null) {
- let t = this._globalTransform.multiply(this.renderGroup.invGlobalTransform); // Compute the transformation into the renderGroup's space
- let scl = RenderablePrim2D._s;
- let trn = RenderablePrim2D._t;
- let rot = t.decompose(scl, trn);
- let pas = this.actualScale;
- let canvasScale = this.owner._canvasLevelScale;
- scl.x = pas.x * canvasScale.x * this._postScale.x;
- scl.y = pas.y * canvasScale.y * this._postScale.y;
- t = Matrix2D.Compose(this.applyActualScaleOnTransform() ? scl : RenderablePrim2D._iV2, rot, trn);
- let size = (<Size>this.renderGroup.viewportSize);
- let zBias = this.actualZOffset;
- let offX = 0;
- let offY = 0;
- // If there's an offset, apply the global transformation matrix on it to get a global offset
- if (positionOffset) {
- offX = positionOffset.x * t.m[0] + positionOffset.y * t.m[2];
- offY = positionOffset.x * t.m[1] + positionOffset.y * t.m[3];
- }
- // Have to convert the coordinates to clip space which is ranged between [-1;1] on X and Y axis, with 0,0 being the left/bottom corner
- // Current coordinates are expressed in renderGroup coordinates ([0, renderGroup.actualSize.width|height]) with 0,0 being at the left/top corner
- // So for X:
- // - tx.x = value * 2 / width: is to switch from [0, renderGroup.width] to [0, 2]
- // - tx.w = (value * 2 / width) - 1: w stores the translation in renderGroup coordinates so (value * 2 / width) to switch to a clip space translation value. - 1 is to offset the overall [0;2] to [-1;1].
- // At last we don't forget to apply the actualScale of the Render Group to tx[0] and ty[1] to propagate scaling correctly
- let w = size.width;
- let h = size.height;
- let invZBias = 1 / zBias;
- let tx = new Vector4(t.m[0] * 2 / w, t.m[2] * 2 / w, 0, ((t.m[4] + offX) * 2 / w) - 1);
- let ty = new Vector4(t.m[1] * 2 / h, t.m[3] * 2 / h, 0, ((t.m[5] + offY) * 2 / h) - 1);
- part.renderingInfo = new Vector3(w, h, this.alignToPixel ? 1 : 0);
- part.transformX = tx;
- part.transformY = ty;
- part.opacity = this.actualOpacity;
- // Stores zBias and it's inverse value because that's needed to compute the clip space W coordinate (which is 1/Z, so 1/zBias)
- part.zBias = new Vector2(zBias, invZBias);
- }
- protected _updateRenderMode() {
- if (this.isTransparent) {
- this._renderMode = Render2DContext.RenderModeTransparent;
- } else if (this.isAlphaTest) {
- this._renderMode = Render2DContext.RenderModeAlphaTest;
- } else {
- this._renderMode = Render2DContext.RenderModeOpaque;
- }
- }
- private _modelRenderCache: ModelRenderCache;
- private _transparentPrimitiveInfo: TransparentPrimitiveInfo;
- protected _instanceDataParts: InstanceDataBase[];
- private _renderMode: number;
- }
- }
|