Files
cocos4/cocos/core/renderer/ui/render-data.ts
T
pandamicro b162e21a93 Update license and API language declaration (#7750)
* Update licenses

* Update license for audio

* Update license for core/3d

* Update license for animation, assets, asset-manager, components, data

* update license for event, geometry, gfx

* update license for core modules

math, memop, pipeline, platform, primitive, renderer, scene-graph, utils, value-types

* Update license for core

* Update license for particle and particle-2d

* Update license for particle part2

* Update license for physics and physics-2d

* Update license for all modules in cocos

* Update license for exports

* Fix API doc language declaration
2020-11-10 17:36:31 +08:00

253 lines
7.3 KiB
TypeScript

/*
Copyright (c) 2019-2020 Xiamen Yaji Software Co., Ltd.
https://www.cocos.com/
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated engine source code (the "Software"), a limited,
worldwide, royalty-free, non-assignable, revocable and non-exclusive license
to use Cocos Creator solely to develop games on your target platforms. You shall
not use Cocos Creator software for developing other software or tools that's
used for developing games. You are not granted to publish, distribute,
sublicense, and/or sell copies of Cocos Creator.
The software or tools in this License Agreement are licensed, not sold.
Xiamen Yaji Software Co., Ltd. reserves all rights not expressly granted to you.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
/**
* @packageDocumentation
* @hidden
*/
import { Material } from '../../assets/material';
import { Color } from '../../math';
import { Pool, RecyclePool } from '../../memop';
export interface IRenderData {
x: number;
y: number;
z: number;
u: number;
v: number;
color: Color;
}
export class BaseRenderData {
public material: Material | null = null;
public vertexCount: number = 0;
public indicesCount: number = 0;
}
export class RenderData extends BaseRenderData {
get dataLength () {
return this._data.length;
}
set dataLength (length: number) {
const data: IRenderData[] = this._data;
if (data.length !== length) {
// // Free extra data
const value = data.length;
let i = 0;
for (i = length; i < value; i++) {
_dataPool.free(data[i]);
}
for (i = value; i < length; i++) {
data[i] = _dataPool.alloc();
}
data.length = length;
}
}
get data () {
return this._data;
}
public static add () {
return _pool.add();
}
public static remove (data: RenderData) {
const idx = _pool.data.indexOf(data);
if (idx === -1){
return;
}
_pool.data[idx].clear();
_pool.removeAt(idx);
}
public vData: Float32Array | null = null;
public uvDirty: boolean = true;
public vertDirty: boolean = true;
private _data: IRenderData[] = [];
private _indices: number[] = [];
private _pivotX: number = 0;
private _pivotY: number = 0;
private _width: number = 0;
private _height: number = 0;
public updateSizeNPivot (width: number, height: number, pivotX: number, pivotY: number) {
if (width !== this._width ||
height !== this._height ||
pivotX !== this._pivotX ||
pivotY !== this._pivotY) {
this._width = width;
this._height = height;
this._pivotX = pivotX;
this._pivotY = pivotY;
this.vertDirty = true;
}
}
public clear () {
this._data.length = 0;
this._indices.length = 0;
this._pivotX = 0;
this._pivotY = 0;
this._width = 0;
this._height = 0;
this.uvDirty = true;
this.vertDirty = true;
this.material = null;
this.vertexCount = 0;
this.indicesCount = 0;
}
}
export class MeshRenderData extends BaseRenderData {
public vData: Float32Array = new Float32Array(256 * 9 * Float32Array.BYTES_PER_ELEMENT);
public iData: Uint16Array = new Uint16Array(256 * 6);
public vertexStart = 0;
public indicesStart = 0;
public byteStart = 0;
public byteCount = 0;
private _formatByte = 9 * Float32Array.BYTES_PER_ELEMENT;
public static add () {
return _meshDataPool.add();
}
public static remove (data: MeshRenderData) {
const idx = _meshDataPool.data.indexOf(data);
if (idx === -1){
return;
}
_meshDataPool.data[idx].reset();
_meshDataPool.removeAt(idx);
}
public request (vertexCount: number, indicesCount: number) {
const byteOffset = this.byteCount + vertexCount * this._formatByte;
this.reserve(vertexCount, indicesCount);
this.vertexCount += vertexCount; // vertexOffset
this.indicesCount += indicesCount; // indicesOffset
this.byteCount = byteOffset; // byteOffset
return true;
}
public reserve(vertexCount: number, indicesCount: number) {
const newVBytes = this.byteCount + vertexCount * this._formatByte;
const newICount = this.indicesCount + indicesCount;
if (vertexCount + this.vertexCount > 65535) {
return false;
}
let byteLength = this.vData!.byteLength;
let indicesLength = this.iData!.length;
let vCount = this.vData.length;
let iCount = this.iData.length;
if (newVBytes > byteLength || newICount > indicesLength) {
while (byteLength < newVBytes || indicesLength < newICount) {
vCount *= 2;
iCount *= 2;
byteLength = vCount * 4;
indicesLength = iCount;
}
this._reallocBuffer(vCount, iCount);
}
}
public advance(vertexCount: number, indicesCount: number) {
this.vertexCount += vertexCount; // vertexOffset
this.indicesCount += indicesCount; // indicesOffset
this.byteCount += vertexCount * this._formatByte;
}
public reset () {
this.vertexCount = 0;
this.indicesCount = 0;
this.byteCount = 0;
this.vertexStart = 0;
this.indicesStart = 0;
this.byteStart = 0;
}
protected _reallocBuffer (vCount, iCount) {
// copy old data
const oldVData = this.vData;
this.vData = new Float32Array(vCount);
this.vData.set(oldVData, 0);
const oldIData = this.iData;
this.iData = new Uint16Array(iCount);
this.iData.set(oldIData, 0);
}
}
export class QuadRenderData extends MeshRenderData {
private _fillQuadBuffer () {
const count = this.iData.length / 6;
const buffer = this.iData;
for (let i = 0, idx = 0; i < count; i++) {
const vId = i * 4;
buffer[idx++] = vId;
buffer[idx++] = vId + 1;
buffer[idx++] = vId + 2;
buffer[idx++] = vId + 1;
buffer[idx++] = vId + 3;
buffer[idx++] = vId + 2;
}
}
protected _reallocBuffer (vCount, iCount) {
// copy old data
super._reallocBuffer(vCount, iCount);
this._fillQuadBuffer();
}
};
const _dataPool = new Pool(() => {
return {
x: 0,
y: 0,
z: 0,
u: 0,
v: 0,
color: Color.WHITE.clone(),
};
}, 128);
const _pool = new RecyclePool(() => {
return new RenderData();
}, 32);
const _meshDataPool: RecyclePool<MeshRenderData> = new RecyclePool(() => {
return new MeshRenderData();
}, 32);