fix conflicts

This commit is contained in:
jerrywwl
2019-04-24 16:16:02 +08:00
23 changed files with 521 additions and 283 deletions
+12 -12
View File
File diff suppressed because one or more lines are too long
+13 -14
View File
@@ -154,22 +154,21 @@ export class SkinningModelComponent extends ModelComponent {
const skeleton = this._skeleton;
const skinningTarget = this._skinningTarget;
mat4.invert(_m4_tmp2, this.node.worldMatrix);
this._skeleton.joints.forEach((joint, index) => {
// If target joint doesn't exists in scene graph, skip it.
const targetNode = skinningTarget.get(joint);
const isTextureStorage = skinningModel.isTextureStorage();
for (let iJoint = 0; iJoint < this._skeleton.joints.length; ++iJoint) {
const targetNode = skinningTarget.get(this._skeleton.joints[iJoint]);
if (!targetNode) {
return;
continue;
}
// 1. transform mesh to joint's local space
// 2. transform from joint' local space to world space
// 3. because it has been in world space, just cancel this mesh's original local-world transform
const bindpose = skeleton.bindposes[index];
const jointMatrix = _m4_tmp;
mat4.multiply(jointMatrix, _m4_tmp2, targetNode.worldMatrix);
mat4.multiply(jointMatrix, jointMatrix, bindpose);
skinningModel.updateJointMatrix(index, jointMatrix);
});
if (isTextureStorage) {
skinningModel.updateJointMatrix(iJoint, targetNode.worldMatrix);
} else {
const bindpose = skeleton.bindposes[iJoint];
const jointMatrix = _m4_tmp;
mat4.multiply(jointMatrix, targetNode.worldMatrix, bindpose);
skinningModel.updateJointMatrix(iJoint, jointMatrix);
}
}
skinningModel.commitJointMatrices();
}
+1 -1
View File
@@ -160,7 +160,7 @@ export const bmfontUtils = {
this._updateProperties();
this._updateContent();
// _comp._actualFontSize = _fontSize;
_comp.actualFontSize = _fontSize;
_comp.node.setContentSize(_contentSize);
_comp.renderData!.vertDirty = _comp.renderData!.uvDirty = false;
+1 -1
View File
@@ -490,7 +490,7 @@ export const letterFont = {
this._updateContent();
// _comp._actualFontSize = _fontSize;
_comp.actualFontSize = _fontSize;
_comp.node.setContentSize(_contentSize);
_comp.renderData!.vertDirty = _comp.renderData!.uvDirty = false;
+3 -2
View File
@@ -30,6 +30,7 @@ import { Color, Size } from '../../../../core/value-types';
import { HorizontalTextAlignment, LabelComponent, VerticalTextAlignment } from '../../components/label-component';
import { LabelOutlineComponent } from '../../components/label-outline-component';
import { CanvasPool, ISharedLabelData } from './font-utils';
import { LetterRenderTexture } from './letter-font';
const Overflow = LabelComponent.Overflow;
const WHITE = Color.WHITE;
@@ -37,7 +38,7 @@ const OUTLINE_SUPPORTED = cc.js.isChildClassOf(LabelOutlineComponent, Component)
let _context: CanvasRenderingContext2D | null = null;
let _canvas: HTMLCanvasElement | null = null;
let _texture: SpriteFrame | null = null;
let _texture: SpriteFrame | LetterRenderTexture | null = null;
let _fontDesc = '';
let _string = '';
@@ -102,7 +103,7 @@ export const ttfUtils = {
this._calculateTextBaseline();
this._updateTexture();
// comp._actualFontSize = _fontSize;
comp.actualFontSize = _fontSize;
comp.node.setContentSize(_canvasSize);
this.updateVerts(comp);
+25 -8
View File
@@ -139,17 +139,18 @@ export class CanvasComponent extends Component {
* @default new cc.Size(960, 640)
*/
@property
private _designResolution = cc.size(960, 640);
protected _designResolution = cc.size(960, 640);
@property
private _fitWidth = false;
protected _fitWidth = false;
@property
private _fitHeight = true;
protected _fitHeight = true;
@property
private _priority = 0;
protected _priority = 0;
private _thisOnResized: () => void;
protected _thisOnResized: () => void;
private _camera: Camera | null = null;
protected _camera: Camera | null = null;
private _pos = new Vec3();
constructor () {
super();
@@ -218,10 +219,14 @@ export class CanvasComponent extends Component {
public alignWithScreen () {
let nodeSize;
let designSize;
this.node.getPosition(this._pos);
if (CC_EDITOR) {
// nodeSize = designSize = cc.engine.getDesignResolutionSize();
nodeSize = designSize = this._designResolution;
this.node.setPosition(designSize.width * 0.5, designSize.height * 0.5, 1);
_tempPos.x = designSize.width * 0.5;
_tempPos.y = designSize.height * 0.5;
_tempPos.z = 0;
}
else {
const canvasSize = cc.visibleRect;
@@ -235,7 +240,14 @@ export class CanvasComponent extends Component {
offsetX = (designSize.width - canvasSize.width) * 0.5;
offsetY = (designSize.height - canvasSize.height) * 0.5;
}
this.node.setPosition(canvasSize.width * 0.5 + offsetX, canvasSize.height * 0.5 + offsetY, 1);
_tempPos.x = canvasSize.width * 0.5 + offsetX;
_tempPos.y = canvasSize.height * 0.5 + offsetY;
_tempPos.z = 0;
}
if (this._pos.equals(_tempPos)) {
this.node.setPosition(_tempPos);
}
if (this.node.width !== nodeSize.width) {
@@ -275,6 +287,11 @@ export class CanvasComponent extends Component {
// cc.engine.setDesignResolutionSize(designRes.width, designRes.height);
}
else {
const root = cc.director.root;
if (root && root.ui && root.ui.debugScreen && root.ui.debugScreen === this ){
return;
}
cc.view.setDesignResolutionSize(designRes.width, designRes.height, policy);
}
}
@@ -0,0 +1,63 @@
import { Vec3 } from '../../../core/value-types';
import { CanvasComponent } from './canvas-component';
const _worldPos = new Vec3();
export class DebugCanvasComponent extends CanvasComponent{
constructor (){
super();
this._thisOnResized = this.alignWithScreen.bind(this);
}
// don't remove
public onEnable (){
}
// don't remove
public onDisable (){
}
public onDestroy (){
if (CC_EDITOR) {
cc.director.off(cc.Director.EVENT_AFTER_UPDATE, this.alignWithScreen, this);
}
cc.view.off('design-resolution-changed', this._thisOnResized);
}
public alignWithScreen (){
const canvasSize = cc.visibleRect;
const nodeSize = canvasSize;
const designSize = cc.view.getDesignResolutionSize();
const clipTopRight = !this.fitHeight && !this.fitWidth;
let offsetX = 0;
let offsetY = 0;
if (clipTopRight) {
// offset the canvas to make it in the center of screen
offsetX = (designSize.width - canvasSize.width) * 0.5;
offsetY = (designSize.height - canvasSize.height) * 0.5;
}
this.node.setPosition(canvasSize.width * 0.5 + offsetX, canvasSize.height * 0.5 + offsetY, 1);
if (this.node.width !== nodeSize.width) {
this.node.width = nodeSize.width;
}
if (this.node.height !== nodeSize.height) {
this.node.height = nodeSize.height;
}
this.node.getWorldPosition(_worldPos);
if (this._camera) {
const size = cc.view.getVisibleSize();
this._camera.resize(size.width, size.height);
this._camera.orthoHeight = this._camera.height / 2;
this._camera.node.setPosition(_worldPos.x, _worldPos.y, 1000);
this._camera.update();
}
}
// don't remove
public applySettings (){
}
}
+1 -1
View File
@@ -209,7 +209,7 @@ export class GraphicsComponent extends UIRenderComponent {
super.__preload();
}
this._flushAssembler();
// this._flushAssembler();
this.impl = this._assembler && (this._assembler as IAssembler).createImpl!(this);
}
+5 -2
View File
@@ -238,12 +238,15 @@ export class LabelComponent extends UIRenderComponent {
@property({
readonly: true,
displayName: 'Actual Font Size',
visible: false,
})
get actualFontSize () {
return this._actualFontSize;
}
set actualFontSize (value) {
this._actualFontSize = value;
}
/**
* !#en Font size of label.
* !#zh 文本字体大小。
@@ -672,7 +675,7 @@ export class LabelComponent extends UIRenderComponent {
}
protected _canRender () {
if (!super._canRender){
if (!super._canRender()){
return false;
}
+1 -1
View File
@@ -251,7 +251,7 @@ export class MaskComponent extends UIRenderComponent {
}
public onLoad (){
this._flushAssembler();
// this._flushAssembler();
this._createGraphics();
}
+2 -2
View File
@@ -32,7 +32,7 @@ import { clampf } from '../../../core/utils/misc';
import { Vec2 } from '../../../core/value-types';
import { ccenum } from '../../../core/value-types/enum';
import { UI } from '../../../renderer/ui/ui';
import { InstanceMaterialType, UIRenderComponent } from './ui-render-component';
import { UIRenderComponent } from './ui-render-component';
/**
* !#en Enum for sprite type.
@@ -431,7 +431,7 @@ export class SpriteComponent extends UIRenderComponent {
}
}
this._flushAssembler();
// this._flushAssembler();
this._activateMaterial();
}
+1 -1
View File
@@ -27,7 +27,7 @@ import { EventHandler as ComponentEventHandler } from '../../../components/compo
import { ccclass, executeInEditMode, executionOrder, menu, property } from '../../../core/data/class-decorator';
import { ButtonComponent} from './button-component';
import { SpriteComponent} from './sprite-component';
import { ToggleContainerComponent} from './toggle-group-component';
import { ToggleContainerComponent} from './toggle-container-component';
/**
* !#en The toggle component is a CheckBox, when it used together with a ToggleGroup, it
@@ -39,7 +39,7 @@ import { ToggleComponent} from './toggle-component';
@ccclass('cc.ToggleContainerComponent')
@executionOrder(100)
@menu('UI/ToggleGroup')
@menu('UI/ToggleContainer')
@executeInEditMode
export class ToggleContainerComponent extends Component {
@property({
+15 -15
View File
@@ -12,12 +12,12 @@ import { UIComponent } from './ui-component';
@menu('UI/Model')
export class UIModelComponent extends UIComponent {
private _modelComponent: RenderableComponent | null = null;
public get modelComponent () {
return this._modelComponent;
}
private _modelComponent: RenderableComponent | null = null;
public onLoad () {
this._modelComponent = this.getComponent(cc.RenderableComponent) as RenderableComponent;
(this._modelComponent as any)._sceneGetter = cc.director.root.ui.getRenderSceneGetter();
@@ -27,6 +27,19 @@ export class UIModelComponent extends UIComponent {
this._fitUIRenderQueue();
}
public updateAssembler (render: UI) {
if (this._modelComponent) {
render.commitComp.call(render, this);
return true;
}
return false;
}
public update () {
this._fitUIRenderQueue();
}
private _fitUIRenderQueue () {
if (!this._modelComponent) {
return;
@@ -58,17 +71,4 @@ export class UIModelComponent extends UIComponent {
}
}
}
public updateAssembler (render: UI) {
if (this._modelComponent) {
render.commitComp.call(render, this);
return true;
}
return false;
}
public update () {
this._fitUIRenderQueue();
}
}
@@ -207,6 +207,9 @@ export class UIRenderComponent extends UIComponent {
public __preload (){
this._instanceMaterial();
if (this._flushAssembler){
this._flushAssembler();
}
}
public onEnable () {
@@ -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 { CanvasComponent } from './canvas-component';
const _vec2a = new Vec2();
const _vec2b = new Vec2();
@@ -379,7 +380,7 @@ export class UITransformComponent extends Component {
// 获取被渲染相机的 visibility
while (parent) {
if (parent) {
const canvasComp = parent.getComponent(cc.CanvasComponent);
const canvasComp = parent.getComponent('cc.CanvasComponent') as CanvasComponent;
if (canvasComp) {
visibility = canvasComp.visibility;
break;
+2 -1
View File
@@ -13,7 +13,7 @@ export { ScrollViewComponent } from './components/scroll-view-component';
export { SliderComponent } from './components/slider-component';
export { SpriteComponent } from './components/sprite-component';
export { ToggleComponent } from './components/toggle-component';
export { ToggleContainerComponent } from './components/toggle-group-component';
export { ToggleContainerComponent } from './components/toggle-container-component';
export { UIModelComponent } from './components/ui-model-component';
export { UIRenderComponent } from './components/ui-render-component';
export { UITransformComponent } from './components/ui-transfrom-component';
@@ -21,3 +21,4 @@ export { ViewGroupComponent } from './components/view-group-component';
export { WebviewComponent } from './components/webview/webView-component';
export { WidgetComponent } from './components/widget-component';
export { LabelOutlineComponent } from './components/label-outline-component';
export { DebugCanvasComponent } from './components/debug-canvas-component';
+15 -1
View File
@@ -150,6 +150,20 @@ export default class Touch {
return out;
}
/**
* !#en Returns the start touch location in OpenGL coordinates.
* !#zh 获获取触点落下时的位置对象,对象包含 x 和 y 属性。
*/
public getUIStartLocation (out?: Vec2) {
if (!out) {
out = new Vec2();
}
vec2.set(out, this._startPoint.x, this._startPoint.y);
cc.view._convertPointWithScale(out);
return out;
}
/**
* !#en Returns the delta distance from the previous touche to the current one in screen coordinates.
* !#zh 获取触点距离上一次事件移动的距离对象,对象包含 x 和 y 属性。
@@ -235,7 +249,7 @@ export default class Touch {
this._id = id;
if (!this._startPointCaptured) {
this._startPoint = new Vec2(this._point);
cc.view._convertPointWithScale(this._startPoint);
// cc.view._convertPointWithScale(this._startPoint);
this._startPointCaptured = true;
}
}
+4 -4
View File
@@ -24,7 +24,7 @@
THE SOFTWARE.
****************************************************************************/
import { CanvasComponent} from '../../../3d/ui/components/canvas-component';
import { DebugCanvasComponent } from '../../../3d/ui/components/debug-canvas-component';
import { LabelComponent } from '../../../3d/ui/components/label-component';
import { UITransformComponent } from '../../../3d/ui/components/ui-transfrom-component';
import { WidgetComponent } from '../../../3d/ui/components/widget-component';
@@ -171,8 +171,8 @@ function generateNode () {
_rootNode = new Node('PROFILER-NODE');
_rootNode.addComponent(UITransformComponent);
_rootNode.addComponent(CanvasComponent);
cc.director.root.ui.debugScreen = _rootNode;
const screen = _rootNode.addComponent(DebugCanvasComponent);
cc.director.root.ui.debugScreen = screen;
cc.game.addPersistRootNode(_rootNode);
// const camera = cc.director.root.
@@ -214,7 +214,7 @@ function generateNode () {
rightLabel.horizontalAlign = cc.LabelComponent.HorizontalAlign.RIGHT;
rightLabel.font = _atlas;
rightLabel.fontSize = _fontSize;
rightLabel.lineHeight = _fontSize;
rightLabel.lineHeight = _fontSize + 1;
}
_label[stringLeft] = leftLabel;
+19 -4
View File
@@ -49,7 +49,8 @@ export enum UniformBinding {
UBO_LOCAL = MAX_BINDING_SUPPORTED - 3,
UBO_FORWARD_LIGHTS = MAX_BINDING_SUPPORTED - 4,
UBO_SKINNING = MAX_BINDING_SUPPORTED - 5,
UBO_UI = MAX_BINDING_SUPPORTED - 6,
UBO_SKINNING_TEXTURE_CASE = MAX_BINDING_SUPPORTED - 6,
UBO_UI = MAX_BINDING_SUPPORTED - 7,
// samplers
SAMPLER_JOINTS = MAX_BINDING_SUPPORTED + 1,
}
@@ -173,14 +174,12 @@ export const JointUniformCapacity = 65;
export class UBOSkinning {
public static MAT_JOINT_OFFSET: number = 0;
public static JOINTS_TEXTURE_SIZE_OFFSET: number = UBOSkinning.MAT_JOINT_OFFSET + JointUniformCapacity * 12;
public static COUNT: number = UBOSkinning.JOINTS_TEXTURE_SIZE_OFFSET + 4;
public static COUNT: number = JointUniformCapacity * 12;
public static SIZE: number = UBOSkinning.COUNT * 4;
public static BLOCK: GFXUniformBlock = {
binding: UniformBinding.UBO_SKINNING, name: 'CCSkinning', members: [
{ name: 'cc_matJoint', type: GFXType.FLOAT4, count: JointUniformCapacity * 3 }, // mat 4 * 3
{ name: 'cc_jointsTextureSize', type: GFXType.FLOAT4, count: 1 },
],
};
}
@@ -189,6 +188,22 @@ localBindingsDesc.set(UBOSkinning.BLOCK.name, {
blockInfo: UBOSkinning.BLOCK,
});
export class UBOSkinningTextureCase {
public static JOINTS_TEXTURE_SIZE_OFFSET: number = 0;
public static COUNT: number = 4;
public static SIZE: number = UBOSkinningTextureCase.COUNT * 4;
public static BLOCK: GFXUniformBlock = {
binding: UniformBinding.UBO_SKINNING_TEXTURE_CASE, name: 'CCSkinningTextureCase', members: [
{ name: 'cc_jointsTextureSize', type: GFXType.FLOAT4, count: 1 },
],
};
}
localBindingsDesc.set(UBOSkinningTextureCase.BLOCK.name, {
type: GFXBindingType.UNIFORM_BUFFER,
blockInfo: UBOSkinningTextureCase.BLOCK,
});
export const UNIFORM_JOINTS_TEXTURE: GFXUniformSampler = {
binding: UniformBinding.SAMPLER_JOINTS, name: 'cc_jointsTexture', type: GFXType.SAMPLER2D, count: 1,
};
+277 -194
View File
@@ -3,11 +3,11 @@
import Skeleton from '../../3d/assets/skeleton';
import { Texture2D } from '../../assets/texture-2d';
import { Filter, PixelFormat, WrapMode } from '../../assets/texture-base';
import { mat4 } from '../../core/vmath';
import { mat4, vec3, vec4 } from '../../core/vmath';
import { GFXBuffer } from '../../gfx/buffer';
import { GFXBufferUsageBit, GFXMemoryUsageBit } from '../../gfx/define';
import { GFXAPI, GFXDevice, GFXFeature } from '../../gfx/device';
import { JointUniformCapacity, UBOSkinning, UNIFORM_JOINTS_TEXTURE } from '../../pipeline/define';
import { JointUniformCapacity, UBOSkinning, UBOSkinningTextureCase, UNIFORM_JOINTS_TEXTURE } from '../../pipeline/define';
import { Node } from '../../scene-graph/node';
import { Pass } from '../core/pass';
import { samplerLib } from '../core/sampler-lib';
@@ -22,51 +22,38 @@ export enum JointStorageKind {
uniform,
}
interface IFJointTextureStorage {
kind: JointStorageKind.textureRGBA32F;
nativeData: Float32Array;
texture: Texture2D;
}
interface IBJointTextureStorage {
kind: JointStorageKind.textureRGBA8;
nativeData: Uint8Array;
texture: Texture2D;
}
interface IJointUniformsStorage {
interface IUniformStorage {
kind: JointStorageKind.uniform;
nativeData: Float32Array;
}
type JointStorage = IFJointTextureStorage | IBJointTextureStorage | IJointUniformsStorage;
function isTextureStorage (storage: JointStorage): storage is (IFJointTextureStorage | IBJointTextureStorage) {
return storage.kind === JointStorageKind.textureRGBA8 || storage.kind === JointStorageKind.textureRGBA32F;
interface ITextureStorage {
kind: JointStorageKind.textureRGBA32F | JointStorageKind.textureRGBA8;
texture: MatrixTexture;
}
const pixelsPerJointOfFJointTexture = 4;
const pixelsPerJointOfBJointTexture = 16;
type JointStorage = ITextureStorage | IUniformStorage;
function isTextureStorage (storage: JointStorage): storage is ITextureStorage {
return storage.kind === JointStorageKind.textureRGBA8 || storage.kind === JointStorageKind.textureRGBA32F;
}
export const __FORCE_USE_UNIFORM_STORAGE__ = false;
export class SkinningModel extends Model {
private _jointStorage: JointStorage | null = null;
private _skinningUBO: GFXBuffer;
private _binded: null | {
jointStorage: JointStorage,
skinningUBO: GFXBuffer,
skinningUBOBinding: number,
} = null;
constructor (scene: RenderScene, node: Node) {
super(scene, node);
this._type = 'skinning';
this._skinningUBO = this._device.createBuffer({
usage: GFXBufferUsageBit.UNIFORM | GFXBufferUsageBit.TRANSFER_DST,
memUsage: GFXMemoryUsageBit.HOST | GFXMemoryUsageBit.DEVICE,
size: UBOSkinning.SIZE,
stride: UBOSkinning.SIZE,
});
}
get storageKind () {
return this._jointStorage ? this._jointStorage.kind : null;
public isTextureStorage () {
return this._binded && isTextureStorage(this._binded.jointStorage);
}
public bindSkeleton (skeleton: Skeleton) {
@@ -75,77 +62,104 @@ export class SkinningModel extends Model {
if (storageKind === JointStorageKind.textureRGBA32F ||
storageKind === JointStorageKind.textureRGBA8) {
const jointsTexture = _createJointsTexture(skeleton, storageKind);
textureSizeBuffer[0] = jointsTexture.width;
this._skinningUBO.update(
textureSizeBuffer, UBOSkinning.JOINTS_TEXTURE_SIZE_OFFSET, textureSizeBuffer.byteLength);
if (storageKind === JointStorageKind.textureRGBA32F) {
this._jointStorage = {
kind: JointStorageKind.textureRGBA32F,
texture: jointsTexture,
nativeData: new Float32Array(jointsTexture.width * jointsTexture.height * pixelsPerJointOfFJointTexture * 4),
};
} else {
this._jointStorage = {
kind: JointStorageKind.textureRGBA8,
texture: jointsTexture,
nativeData: new Uint8Array(jointsTexture.width * jointsTexture.height * pixelsPerJointOfBJointTexture * 4),
};
const mat4TextureKind = storageKind === JointStorageKind.textureRGBA32F ? Mat4TextureKind.rgba32f : Mat4TextureKind.rgba8888;
const mat4Texture = new MatrixTexture(skeleton.joints.length * 2, mat4TextureKind);
for (let iJoint = 0; iJoint < skeleton.bindposes.length; ++iJoint) {
mat4Texture.set(iJoint * 2 + 0, skeleton.bindposes[iJoint]);
}
this._binded = {
skinningUBOBinding: UBOSkinningTextureCase.BLOCK.binding,
skinningUBO: this._device.createBuffer({
usage: GFXBufferUsageBit.UNIFORM | GFXBufferUsageBit.TRANSFER_DST,
memUsage: GFXMemoryUsageBit.HOST | GFXMemoryUsageBit.DEVICE,
size: UBOSkinningTextureCase.SIZE,
stride: UBOSkinningTextureCase.SIZE,
}),
jointStorage: {
kind: storageKind,
texture: mat4Texture,
},
};
textureSizeBuffer[0] = mat4Texture.texture.width;
this._binded.skinningUBO.update(
textureSizeBuffer, UBOSkinningTextureCase.JOINTS_TEXTURE_SIZE_OFFSET, textureSizeBuffer.byteLength);
} else {
if (skeleton.joints.length > JointUniformCapacity) {
console.error(
`Skeleton ${skeleton.name} has ${skeleton.joints.length} joints ` +
`which exceeds Cocos's max allowed joint count(${JointUniformCapacity}).`);
}
this._jointStorage = {
kind: JointStorageKind.uniform,
nativeData: new Float32Array(skeleton.joints.length * 12),
this._binded = {
skinningUBOBinding: UBOSkinning.BLOCK.binding,
skinningUBO: this._device.createBuffer({
usage: GFXBufferUsageBit.UNIFORM | GFXBufferUsageBit.TRANSFER_DST,
memUsage: GFXMemoryUsageBit.HOST | GFXMemoryUsageBit.DEVICE,
size: UBOSkinning.SIZE,
stride: UBOSkinning.SIZE,
}),
jointStorage: {
kind: JointStorageKind.uniform,
nativeData: new Float32Array(skeleton.joints.length * 12),
},
};
}
}
public updateJointMatrix (iMatrix: number, matrix: mat4) {
if (!this._jointStorage) {
if (!this._binded) {
return;
}
_setJointMatrix(this._jointStorage.nativeData, iMatrix, matrix, this._jointStorage.kind);
const { jointStorage, skinningUBO } = this._binded;
if (isTextureStorage(jointStorage)) {
jointStorage.texture.set(iMatrix * 2 + 1, matrix);
} else {
setMat4InUniform(jointStorage.nativeData, iMatrix, matrix);
}
}
public commitJointMatrices () {
if (!this._jointStorage) {
if (!this._binded) {
return;
}
if (isTextureStorage(this._jointStorage)) {
this._jointStorage.texture.directUpdate(this._jointStorage!.nativeData.buffer);
const { jointStorage, skinningUBO } = this._binded;
if (isTextureStorage(jointStorage)) {
jointStorage.texture.commit();
} else {
this._skinningUBO.update(this._jointStorage.nativeData, UBOSkinning.MAT_JOINT_OFFSET);
skinningUBO.update(jointStorage.nativeData, UBOSkinning.MAT_JOINT_OFFSET);
}
}
protected _doCreatePSO (pass: Pass) {
const pso = super._doCreatePSO(pass);
pso.pipelineLayout.layouts[0].bindBuffer(UBOSkinning.BLOCK.binding, this._skinningUBO);
if (this._jointStorage && isTextureStorage(this._jointStorage)) {
const jointTexture = this._jointStorage.texture;
const view = jointTexture.getGFXTextureView();
const sampler = samplerLib.getSampler(this._device, jointTexture.getGFXSamplerInfo());
if (view && sampler) {
pso.pipelineLayout.layouts[0].bindTextureView(UNIFORM_JOINTS_TEXTURE.binding, view);
pso.pipelineLayout.layouts[0].bindSampler(UNIFORM_JOINTS_TEXTURE.binding, sampler);
if (this._binded) {
const { jointStorage, skinningUBO, skinningUBOBinding } = this._binded;
pso.pipelineLayout.layouts[0].bindBuffer(skinningUBOBinding, skinningUBO);
if (isTextureStorage(jointStorage)) {
const jointTexture = jointStorage.texture.texture;
const view = jointTexture.getGFXTextureView();
const sampler = samplerLib.getSampler(this._device, jointTexture.getGFXSamplerInfo());
if (view && sampler) {
pso.pipelineLayout.layouts[0].bindTextureView(UNIFORM_JOINTS_TEXTURE.binding, view);
pso.pipelineLayout.layouts[0].bindSampler(UNIFORM_JOINTS_TEXTURE.binding, sampler);
}
}
}
return pso;
}
private _destroyJointStorage () {
if (!this._jointStorage) {
return;
if (this._binded) {
const { jointStorage, skinningUBO } = this._binded;
if (isTextureStorage(jointStorage)) {
jointStorage.texture.destroy();
}
skinningUBO.destroy();
this._binded = null;
}
if (isTextureStorage(this._jointStorage)) {
this._jointStorage.texture.destroy();
}
this._jointStorage = null;
}
private _selectStorageKind (): JointStorageKind {
@@ -165,84 +179,135 @@ export function selectStorageKind (device: GFXDevice): JointStorageKind {
}
}
function _setJointMatrix (out: Float32Array | Uint8Array, iMatrix: number, matrix: mat4, kind: JointStorageKind) {
if (kind === JointStorageKind.uniform) {
const base = 12 * iMatrix;
// Discard the last row
out[base + 0] = matrix.m00;
out[base + 1] = matrix.m01;
out[base + 2] = matrix.m02;
out[base + 3] = matrix.m04;
out[base + 4] = matrix.m05;
out[base + 5] = matrix.m06;
out[base + 6] = matrix.m08;
out[base + 7] = matrix.m09;
out[base + 8] = matrix.m10;
out[base + 9] = matrix.m12;
out[base + 10] = matrix.m13;
out[base + 11] = matrix.m14;
} else if (kind === JointStorageKind.textureRGBA32F) {
mat4.array(out, matrix, iMatrix * pixelsPerJointOfFJointTexture * 4);
} else {
const base = iMatrix * 16 * 4;
const o = out as Uint8Array;
encode32(matrix.m00, o, base + 0 * 4);
encode32(matrix.m01, o, base + 1 * 4);
encode32(matrix.m02, o, base + 2 * 4);
encode32(matrix.m03, o, base + 3 * 4);
encode32(matrix.m04, o, base + 4 * 4);
encode32(matrix.m05, o, base + 5 * 4);
encode32(matrix.m06, o, base + 6 * 4);
encode32(matrix.m07, o, base + 7 * 4);
encode32(matrix.m08, o, base + 8 * 4);
encode32(matrix.m09, o, base + 9 * 4);
encode32(matrix.m10, o, base + 10 * 4);
encode32(matrix.m11, o, base + 11 * 4);
encode32(matrix.m12, o, base + 12 * 4);
encode32(matrix.m13, o, base + 13 * 4);
encode32(matrix.m14, o, base + 14 * 4);
encode32(matrix.m15, o, base + 15 * 4);
}
function setMat4InUniform (out: Float32Array, iMatrix: number, matrix: mat4) {
const base = 12 * iMatrix;
// Discard the last row
out[base + 0] = matrix.m00;
out[base + 1] = matrix.m01;
out[base + 2] = matrix.m02;
out[base + 3] = matrix.m04;
out[base + 4] = matrix.m05;
out[base + 5] = matrix.m06;
out[base + 6] = matrix.m08;
out[base + 7] = matrix.m09;
out[base + 8] = matrix.m10;
out[base + 9] = matrix.m12;
out[base + 10] = matrix.m13;
out[base + 11] = matrix.m14;
}
function createTointTextureCapacityTable (maxCapacity: number, pixelsPerJoint: number) {
const result: Array<{ textureExtent: number; capacity: number; }> = [];
for (let i = 0; ; ++i) {
const textureExtent = 1 << i;
const capacity = (textureExtent * textureExtent) / pixelsPerJoint;
if (capacity < 1) {
continue;
}
result.push({ textureExtent, capacity });
if (capacity >= maxCapacity) {
break;
}
}
return result;
enum Mat4TextureKind {
rgba8888,
rgba32f,
}
const fJointTextureCapacityTable = createTointTextureCapacityTable(1024, pixelsPerJointOfFJointTexture);
const bJointTextureCapacityTable = createTointTextureCapacityTable(1024, pixelsPerJointOfBJointTexture);
class MatrixTexture {
function _createJointsTexture (skinning: { joints: any[]; }, kind: JointStorageKind.textureRGBA8 | JointStorageKind.textureRGBA32F) {
const flt = (kind === JointStorageKind.textureRGBA32F);
const jointCount = skinning.joints.length;
let textureExtent = -1;
for (const item of (flt ? fJointTextureCapacityTable : bJointTextureCapacityTable)) {
if (item.capacity >= jointCount) {
textureExtent = item.textureExtent;
break;
get texture () {
return this._texture;
}
private static Mat4TextureAttributes = {
[Mat4TextureKind.rgba8888]: {
localStorage: Uint8Array,
pixelsPerMatrix: 16,
capacityTable: MatrixTexture.createCapacityTable(1024, 16),
pixelFormat: PixelFormat.RGBA8888,
},
[Mat4TextureKind.rgba32f]: {
localStorage: Float32Array,
pixelsPerMatrix: 4,
capacityTable: MatrixTexture.createCapacityTable(1024, 4),
pixelFormat: PixelFormat.RGBA32F,
},
};
private static createCapacityTable (maxCapacity: number, pixelsPerJoint: number) {
const result: Array<{ textureExtent: number; capacity: number; }> = [];
for (let i = 0; ; ++i) {
const textureExtent = 1 << i;
if (textureExtent % pixelsPerJoint !== 0) {
continue;
}
const rowCapacity = textureExtent / pixelsPerJoint;
if (rowCapacity < 1) {
continue;
}
const capacity = textureExtent * rowCapacity;
result.push({ textureExtent, capacity });
if (capacity >= maxCapacity) {
break;
}
}
return result;
}
private _texture: Texture2D;
private _nativeData: Float32Array | Uint8Array;
private _kind: Mat4TextureKind;
/**
* Creates a texture which is able to store matrices(4x4).
* The matrices is directly emplaced (in textureRGBA32F case) or encoded (in textureRGBA8888 case),
* one by one, in column major order, into texture pixels, without any spacing pixels.
* Every row of texture is guaranteed to store integer multiple of matrix count.
* This means when retrive a single matrix from such a texture, every matrix element has a same texcoord 'v',
* and their texcoord 'u' is incremented by '1 step'.
* @param capacity The count of matrices this texture is able store.
* @param kind The texture format.
* @throws Throws error if matrix count is too large.
*/
constructor (capacity: number, kind: Mat4TextureKind) {
this._kind = kind;
const attribute = MatrixTexture.Mat4TextureAttributes[kind];
let textureExtent = -1;
for (const item of attribute.capacityTable) {
if (item.capacity >= capacity) {
textureExtent = item.textureExtent;
break;
}
}
if (textureExtent < 0) {
throw new Error('Too many joints.');
}
this._texture = new Texture2D();
this._texture.create(textureExtent, textureExtent, attribute.pixelFormat);
this._texture.setFilters(Filter.NEAREST, Filter.NEAREST);
this._texture.setWrapMode(WrapMode.CLAMP_TO_EDGE, WrapMode.CLAMP_TO_EDGE);
this._nativeData = new (attribute.localStorage)(this._texture.width * this._texture.height * attribute.pixelsPerMatrix * 4);
}
public destroy () {
this._texture.destroy();
}
public set (iMatrix: number, matrix: mat4) {
if (this._kind === Mat4TextureKind.rgba32f) {
mat4.array(this._nativeData, matrix, iMatrix * 4 * 4);
} else {
const base = iMatrix * 16 * 4;
const o = this._nativeData as Uint8Array;
encode32(matrix.m00, o, base + 0 * 4);
encode32(matrix.m01, o, base + 1 * 4);
encode32(matrix.m02, o, base + 2 * 4);
encode32(matrix.m03, o, base + 3 * 4);
encode32(matrix.m04, o, base + 4 * 4);
encode32(matrix.m05, o, base + 5 * 4);
encode32(matrix.m06, o, base + 6 * 4);
encode32(matrix.m07, o, base + 7 * 4);
encode32(matrix.m08, o, base + 8 * 4);
encode32(matrix.m09, o, base + 9 * 4);
encode32(matrix.m10, o, base + 10 * 4);
encode32(matrix.m11, o, base + 11 * 4);
encode32(matrix.m12, o, base + 12 * 4);
encode32(matrix.m13, o, base + 13 * 4);
encode32(matrix.m14, o, base + 14 * 4);
encode32(matrix.m15, o, base + 15 * 4);
}
}
if (textureExtent < 0) {
throw new Error('Too many joints.');
}
const texture = new Texture2D();
texture.create(textureExtent, textureExtent, (flt ? PixelFormat.RGBA32F : PixelFormat.RGBA8888));
texture.setFilters(Filter.NEAREST, Filter.NEAREST);
texture.setWrapMode(WrapMode.CLAMP_TO_EDGE, WrapMode.CLAMP_TO_EDGE);
return texture;
public commit () {
this._texture.directUpdate(this._nativeData.buffer);
}
}
const encode32 = (() => {
@@ -252,71 +317,81 @@ const encode32 = (() => {
const EXP2_XX_4 = exp2(23.0);
const EXP2_XX_5 = exp2(-15.0);
function decode32 (r: number, g: number, b: number, a: number) {
const Sign = 1.0 - step(128.0, r) * 2.0;
const Exponent = 2.0 * mod(r, 128.0) + step(128.0, g) - 127.0;
// From Arjan's answer.
if (Exponent === -127) {
return 0;
}
const Mantissa = mod(g, 128.0) * 65536.0 + b * 256.0 + a + (0x800000);
// Original
// const Result = Sign * exp2(Exponent) * (Mantissa * exp2(-23.0 ));
// return Result;
// From Arjan's answer.
return Sign * exp2(Exponent - 23.0) * Mantissa;
}
return (f: number, output: Uint8Array, offset: number) => {
// https://stackoverflow.com/questions/7059962/how-do-i-convert-a-vec4-rgba-value-to-a-float
// http://www.shaderific.com/glsl-functions/
const e = 5.0;
const F = Math.abs(f);
f = numberToFloat32(f);
// From Arjan's answer.
if (F === 0) {
output[offset + 0] = 0;
output[offset + 1] = 0;
output[offset + 2] = 0;
output[offset + 3] = 0;
return;
}
// https://stackoverflow.com/questions/7059962/how-do-i-convert-a-vec4-rgba-value-to-a-float
// http://www.shaderific.com/glsl-functions/
const e = 5.0;
const F = Math.abs(f);
const Sign = step(0.0, -f);
let Exponent = Math.floor(Math.log2(F));
// From Arjan's answer.
if (F === 0) {
output[offset + 0] = 0;
output[offset + 1] = 0;
output[offset + 2] = 0;
output[offset + 3] = 0;
return;
}
// Original.
// const Mantissa = (exp2(- Exponent) * F);
// Exponent = Math.floor(Math.log2(F) + 127.0) + Math.floor(Math.log2(Mantissa));
const Sign = step(0.0, -f);
let Exponent = Math.floor(Math.log2(F));
// From Arjan's answer.
const Mantissa = F / exp2(Exponent);
if (Mantissa < 1) {
Exponent -= 1;
}
Exponent += 127;
// Original.
// const Mantissa = (exp2(- Exponent) * F);
// Exponent = Math.floor(Math.log2(F) + 127.0) + Math.floor(Math.log2(Mantissa));
const r = 128.0 * Sign + Math.floor(Exponent * EXP2_XX_1);
const g = 128.0 * mod(Exponent, 2.0) + mod(Math.floor(Mantissa * 128.0), 128.0);
const b = Math.floor(mod(Math.floor(Mantissa * EXP2_XX_2), EXP2_XX_3));
const a = Math.floor(EXP2_XX_4 * mod(Mantissa, EXP2_XX_5));
// From Arjan's answer.
const Mantissa = F / exp2(Exponent);
if (Mantissa < 1) {
Exponent -= 1;
}
Exponent += 127;
// Verification.
// const g = decode32(r, g, b, a);
// if (Math.abs(g - f) > 0.00001) {
// f = g;
// }
const r = 128.0 * Sign + Math.floor(Exponent * EXP2_XX_1);
const g = 128.0 * mod(Exponent, 2.0) + mod(Math.floor(Mantissa * 128.0), 128.0);
const b = Math.floor(mod(Math.floor(Mantissa * EXP2_XX_2), EXP2_XX_3));
const a = Math.floor(EXP2_XX_4 * mod(Mantissa, EXP2_XX_5));
output[offset + 0] = r;
output[offset + 1] = g;
output[offset + 2] = b;
output[offset + 3] = a;
// Verification.
// const x = decode32(r, g, b, a);
// if (Math.abs(x - f) > 0.00001) {
// f = x;
// }
output[offset + 0] = r;
output[offset + 1] = g;
output[offset + 2] = b;
output[offset + 3] = a;
};
})();
function decode32 (r: number, g: number, b: number, a: number) {
const Sign = 1.0 - step(128.0, r) * 2.0;
const Exponent = 2.0 * mod(r, 128.0) + step(128.0, g) - 127.0;
// From Arjan's answer.
if (Exponent === -127) {
return 0;
}
const Mantissa = mod(g, 128.0) * 65536.0 + b * 256.0 + a + (0x800000);
// Original
// const Result = Sign * exp2(Exponent) * (Mantissa * exp2(-23.0 ));
// return Result;
// From Arjan's answer.
return Sign * exp2(Exponent - 23.0) * Mantissa;
}
const numberToFloat32 = (() => {
const f32storage = new Float32Array(1);
return (x: number) => {
f32storage[0] = x;
return f32storage[0];
};
})();
function step (edge: number, x: number): number {
return x < edge ? 0.0 : 1.0;
@@ -329,3 +404,11 @@ function mod (x: number, y: number): number {
function exp2 (x: number): number {
return Math.pow(2, x);
}
function int (x: number) {
return Math.trunc(x);
}
function fract (x: number) {
return x - Math.floor(x);
}
+6 -8
View File
@@ -80,12 +80,6 @@ export class UI {
set debugScreen (value){
this._debugScreen = value;
if (this._debugScreen){
const screen = this.getScreen(this._debugScreen.visibility);
if (screen) {
this.removeScreen(screen.visibility);
}
}
}
public device: GFXDevice;
@@ -187,9 +181,11 @@ export class UI {
public addScreen (comp: CanvasComponent) {
this._screens.push(comp);
if (comp.camera) {
comp.camera.view.visibility = this._screens.length;
}
}
public getScreen (visibility: number) {
@@ -280,7 +276,7 @@ export class UI {
}
}
public commitComp (comp: UIComponent, frame: GFXTextureView | null, assembler: IAssembler) {
public commitComp (comp: UIComponent, frame: GFXTextureView | null = null, assembler?: IAssembler) {
if (comp instanceof UIRenderComponent) {
const renderComp = comp as UIRenderComponent;
const texView = frame;
@@ -294,7 +290,9 @@ export class UI {
this._currCanvas = renderComp.visibility;
}
assembler.fillBuffers(renderComp, this);
if (assembler) {
assembler.fillBuffers(renderComp, this);
}
} else {
// if the last comp is spriteComp, previous comps should be batched.
if (this._currMaterial !== this._emptyMaterial) {
+49 -9
View File
@@ -18,6 +18,7 @@ enum NodeSpace {
LOCAL,
WORLD,
}
const TRANFORM_ON = 1 << 0;
@ccclass('cc.Node')
class Node extends BaseNode {
@@ -56,6 +57,7 @@ class Node extends BaseNode {
protected _eulerDirty = false;
protected _eventProcessor;
protected _eventMask = 0;
private _uiTransfromComp: UITransformComponent | null = null;
@property({ type: Vec3 })
@@ -192,6 +194,7 @@ class Node extends BaseNode {
quat.copy(this._rot, this._lrot);
vec3.copy(this._scale, this._lscale);
this._eulerDirty = this._dirty = this._hasChanged = true;
this._eventMask = 0;
for (const child of this._children) {
child._onBatchCreated();
}
@@ -221,7 +224,9 @@ class Node extends BaseNode {
vec3.copy(this._pos, this._lpos);
this.invalidateChildren();
this.emit(EventType.TRANSFORM_CHANGED, EventType.POSITION_PART);
if (this._eventMask & TRANFORM_ON) {
this.emit(EventType.TRANSFORM_CHANGED, EventType.POSITION_PART);
}
}
/**
@@ -420,7 +425,9 @@ class Node extends BaseNode {
vec3.copy(this._pos, this._lpos);
this.invalidateChildren();
this.emit(EventType.TRANSFORM_CHANGED, EventType.POSITION_PART);
if (this._eventMask & TRANFORM_ON) {
this.emit(EventType.TRANSFORM_CHANGED, EventType.POSITION_PART);
}
}
/**
@@ -465,7 +472,9 @@ class Node extends BaseNode {
this._eulerDirty = true;
this.invalidateChildren();
this.emit(EventType.TRANSFORM_CHANGED, EventType.ROTATION_PART);
if (this._eventMask & TRANFORM_ON) {
this.emit(EventType.TRANSFORM_CHANGED, EventType.ROTATION_PART);
}
}
/**
@@ -481,7 +490,9 @@ class Node extends BaseNode {
quat.copy(this._rot, this._lrot);
this.invalidateChildren();
this.emit(EventType.TRANSFORM_CHANGED, EventType.ROTATION_PART);
if (this._eventMask & TRANFORM_ON) {
this.emit(EventType.TRANSFORM_CHANGED, EventType.ROTATION_PART);
}
}
/**
@@ -530,7 +541,9 @@ class Node extends BaseNode {
vec3.copy(this._scale, this._lscale);
this.invalidateChildren();
this.emit(EventType.TRANSFORM_CHANGED, EventType.SCALE_PART);
if (this._eventMask & TRANFORM_ON) {
this.emit(EventType.TRANSFORM_CHANGED, EventType.SCALE_PART);
}
}
/**
@@ -588,7 +601,9 @@ class Node extends BaseNode {
}
this.invalidateChildren();
this.emit(EventType.TRANSFORM_CHANGED, EventType.POSITION_PART);
if (this._eventMask & TRANFORM_ON) {
this.emit(EventType.TRANSFORM_CHANGED, EventType.POSITION_PART);
}
}
public getWorldPosition<Out extends vec3 = Vec3> (out: Out): Out;
@@ -651,7 +666,9 @@ class Node extends BaseNode {
this._eulerDirty = true;
this.invalidateChildren();
this.emit(EventType.TRANSFORM_CHANGED, EventType.ROTATION_PART);
if (this._eventMask & TRANFORM_ON) {
this.emit(EventType.TRANSFORM_CHANGED, EventType.ROTATION_PART);
}
}
/**
@@ -671,7 +688,9 @@ class Node extends BaseNode {
this._eulerDirty = true;
this.invalidateChildren();
this.emit(EventType.TRANSFORM_CHANGED, EventType.ROTATION_PART);
if (this._eventMask & TRANFORM_ON) {
this.emit(EventType.TRANSFORM_CHANGED, EventType.ROTATION_PART);
}
}
public getWorldRotation<Out extends quat = Quat> (out: Out): Out;
@@ -731,7 +750,9 @@ class Node extends BaseNode {
}
this.invalidateChildren();
this.emit(EventType.TRANSFORM_CHANGED, EventType.SCALE_PART);
if (this._eventMask & TRANFORM_ON) {
this.emit(EventType.TRANSFORM_CHANGED, EventType.SCALE_PART);
}
}
public getWorldScale<Out extends vec3 = Vec3> (out: Out): Out;
@@ -825,11 +846,26 @@ class Node extends BaseNode {
// Event: maybe remove
public on (type: string | EventType, callback: Function, target?: Object, useCapture?: any) {
switch (type) {
case EventType.TRANSFORM_CHANGED:
this._eventMask |= TRANFORM_ON;
break;
}
this._eventProcessor.on(type, callback, target, useCapture);
}
public off (type: string, callback: Function, target?: Object, useCapture?: any) {
this._eventProcessor.off(type, callback, target, useCapture);
var hasListeners = this._eventProcessor.hasEventListener(type);
// All listener removed
if (!hasListeners) {
switch (type) {
case EventType.TRANSFORM_CHANGED:
this._eventMask &= ~TRANFORM_ON;
break;
}
}
}
public once (type: string, callback: Function, target?: Object, useCapture?: any) {
@@ -850,6 +886,10 @@ class Node extends BaseNode {
public targetOff (target: string | Object) {
this._eventProcessor.targetOff(target);
// Check for event mask reset
if ((this._eventMask & TRANFORM_ON) && !this._eventProcessor.hasEventListener(EventType.TRANSFORM_CHANGED)) {
this._eventMask &= ~TRANFORM_ON;
}
}
public pauseSystemEvents (recursive: boolean) {