From 8a5a2b9342896edc35a1c29ca62c5f503c4bee0c Mon Sep 17 00:00:00 2001 From: Leslie Date: Wed, 23 Jan 2019 15:24:47 +0800 Subject: [PATCH 1/2] skinning-model-component --- cocos/3d/assets/texture-cube.ts | 2 +- ...{model-component.js => model-component.ts} | 148 +++++----- cocos/3d/framework/renderable-component.ts | 2 +- .../3d/framework/skinning-model-component.js | 253 ------------------ .../3d/framework/skinning-model-component.ts | 238 ++++++++++++++++ cocos/assets/image-asset.ts | 2 +- cocos/assets/texture-2d.ts | 11 +- cocos/assets/texture-base.ts | 35 ++- cocos/gfx/webgl/webgl-commands.ts | 2 + cocos/renderer/index.ts | 2 +- cocos/renderer/models/model-uniforms.ts | 10 +- cocos/renderer/models/skinning-model.js | 35 --- cocos/renderer/models/skinning-model.ts | 52 ++++ 13 files changed, 413 insertions(+), 379 deletions(-) rename cocos/3d/framework/{model-component.js => model-component.ts} (66%) delete mode 100644 cocos/3d/framework/skinning-model-component.js create mode 100644 cocos/3d/framework/skinning-model-component.ts delete mode 100644 cocos/renderer/models/skinning-model.js create mode 100644 cocos/renderer/models/skinning-model.ts diff --git a/cocos/3d/assets/texture-cube.ts b/cocos/3d/assets/texture-cube.ts index d3a60944bd..22073f3130 100644 --- a/cocos/3d/assets/texture-cube.ts +++ b/cocos/3d/assets/texture-cube.ts @@ -175,7 +175,7 @@ export class TextureCube extends TextureBase { protected _getTextureCreateInfo () { const result = super._getTextureCreateInfo(); result.arrayLayer = 6; - result.flags = result.flags | GFXTextureFlagBit.CUBEMAP; + result.flags = (result.flags || 0) | GFXTextureFlagBit.CUBEMAP; return result; } diff --git a/cocos/3d/framework/model-component.js b/cocos/3d/framework/model-component.ts similarity index 66% rename from cocos/3d/framework/model-component.js rename to cocos/3d/framework/model-component.ts index a52e2cd0a5..506c278e90 100644 --- a/cocos/3d/framework/model-component.js +++ b/cocos/3d/framework/model-component.ts @@ -22,12 +22,12 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ****************************************************************************/ -import { Model } from '../../renderer/scene/model'; -import { ccclass, property, menu, executionOrder, executeInEditMode } from '../../core/data/class-decorator'; -import { Mesh } from '../assets/mesh'; +import { ccclass, executeInEditMode, executionOrder, menu, property } from '../../core/data/class-decorator'; import Enum from '../../core/value-types/enum'; +import { Model } from '../../renderer/scene/model'; +import { Material } from '../assets/material'; +import { Mesh } from '../assets/mesh'; import { RenderableComponent } from './renderable-component'; -import { builtinResMgr } from '../builtin'; /** * !#en Shadow projection mode @@ -36,7 +36,7 @@ import { builtinResMgr } from '../builtin'; * @static * @enum ModelComponent.ShadowCastingMode */ -let ModelShadowCastingMode = Enum({ +const ModelShadowCastingMode = Enum({ /** * !#en * @@ -88,31 +88,20 @@ let ModelShadowCastingMode = Enum({ @executeInEditMode export class ModelComponent extends RenderableComponent { - /** - * @type {Mesh} - */ - @property({ type: Mesh }) - _mesh = null; - - @property({ type: ModelShadowCastingMode }) - _shadowCastingMode = ModelShadowCastingMode.Off; - - @property - _receiveShadows = false; - - static _builtinMaterial = null; - /** * !#en The mesh of the model * * !#ch 模型网格 * @type {Mesh} */ - @property({ type: Mesh }) - get mesh() { + @property({ + type: Mesh, + }) + get mesh () { return this._mesh; } - set mesh(val) { + + set mesh (val) { this._mesh = val; this._updateModels(true); } @@ -123,11 +112,14 @@ export class ModelComponent extends RenderableComponent { * !#ch 投射阴影方式 * @type {Number} */ - @property({ type: ModelShadowCastingMode }) - get shadowCastingMode() { + @property({ + type: ModelShadowCastingMode, + }) + get shadowCastingMode () { return this._shadowCastingMode; } - set shadowCastingMode(val) { + + set shadowCastingMode (val) { this._shadowCastingMode = val; this._updateCastShadow(); } @@ -139,86 +131,108 @@ export class ModelComponent extends RenderableComponent { * @type {Boolean} */ @property - get receiveShadows() { + get receiveShadows () { return this._receiveShadows; } - set receiveShadows(val) { + set receiveShadows (val) { this._receiveShadows = val; this._updateReceiveShadow(); } - static ShadowCastingMode = ModelShadowCastingMode; - - constructor() { - super(); - /** - * @type {Model} - */ - this._model = null; - } - - get model() { + get model () { return this._model; } - onLoad() { + public static ShadowCastingMode = ModelShadowCastingMode; + + protected _model: Model | null = null; + + @property + private _mesh: Mesh | null = null; + + @property + private _shadowCastingMode = ModelShadowCastingMode.Off; + + @property + private _receiveShadows = false; + + constructor () { + super(); + } + + public onLoad () { } - onEnable() { + public onEnable () { this._updateModels(); this._updateCastShadow(); this._updateReceiveShadow(); - if (this._model) { this._model.enabled = true; } + if (this._model) { + this._model.enabled = true; + } } - onDisable() { - if (this._model) { this._model.enabled = false; } + public onDisable () { + if (this._model) { + this._model.enabled = false; + } } - onDestroy() { - this._getRenderScene().destroyModel(this._model); + public onDestroy () { + if (this._model) { + this._getRenderScene().destroyModel(this._model); + } } - _updateModels(forceUpdate) { + protected _updateModels (forceUpdate: boolean = false) { if (!this.enabled || !this.node._scene || !this._mesh || (this._model && !forceUpdate)) { return; } if (this._model) { this._model.destroy(); - } else { - this._model = this._createModel(); + this._model = null; } - this._model.createBoundingShape(this._mesh.minPosition, this._mesh.maxPosition); + this._model = this._createModel(); + + this._model!.createBoundingShape(this._mesh.minPosition, this._mesh.maxPosition); this._updateModelParams(); } - _createModel() { + protected _createModel () { return this._getRenderScene().createModel(Model, this.node); } - _updateModelParams() { + protected _updateModelParams () { this.node._hasChanged = true; - let meshCount = this._mesh ? this._mesh.subMeshCount : 0; + if (!this._mesh || !this._model) { + return; + } + const meshCount = this._mesh ? this._mesh.subMeshCount : 0; for (let i = 0; i < meshCount; ++i) { - let material = this.getSharedMaterial(i); - let subMeshData = this._mesh.renderingMesh.getSubmesh(i); - this._model.initSubModel(i, subMeshData, material || this._getBuiltinMaterial()); + const material = this.getSharedMaterial(i); + const renderingMesh = this._mesh.renderingMesh; + if (renderingMesh) { + const subMeshData = renderingMesh.getSubmesh(i); + if (subMeshData) { + this._model.initSubModel(i, subMeshData, material || this._getBuiltinMaterial()); + } + } } } - _onMaterialModified(idx, material) { + protected _onMaterialModified (idx: number, material: Material | null) { if (this._model == null) { return; } this._model.setSubModelMaterial(idx, material); } - _clearMaterials() { + protected _clearMaterials () { if (this._model == null) { return; } @@ -227,17 +241,18 @@ export class ModelComponent extends RenderableComponent { } } - _updateCastShadow() { - if (!this.enabled || !this._model) + private _updateCastShadow () { + if (!this.enabled || !this._model) { return; + } if (this._shadowCastingMode === ModelShadowCastingMode.Off) { for (let i = 0; i < this._model.subModelNum; ++i) { - let subModel = this._model.getSubModel(i); + const subModel = this._model.getSubModel(i); subModel.castShadow = false; } } else if (this._shadowCastingMode === ModelShadowCastingMode.On) { for (let i = 0; i < this._model.subModelNum; ++i) { - let subModel = this._model.getSubModel(i); + const subModel = this._model.getSubModel(i); subModel.castShadow = true; } } else { @@ -245,19 +260,20 @@ export class ModelComponent extends RenderableComponent { } } - _updateReceiveShadow() { - if (!this.enabled || !this._model) + private _updateReceiveShadow () { + if (!this.enabled || !this._model) { return; + } for (let i = 0; i < this._model.subModelNum; ++i) { - let subModel = this._model.getSubModel(i); + const subModel = this._model.getSubModel(i); // if (subModel._defines['CC_USE_SHADOW_MAP'] != undefined) { // this.getMaterial(i).define('CC_USE_SHADOW_MAP', this._receiveShadows); // } } } - _getBuiltinMaterial() { + private _getBuiltinMaterial () { // classic ugly pink indicating missing material - return builtinResMgr.get('default-material'); + return cc.BuiltinResMgr['default-material']; } } diff --git a/cocos/3d/framework/renderable-component.ts b/cocos/3d/framework/renderable-component.ts index 108e0ed33b..f75ddcc68a 100644 --- a/cocos/3d/framework/renderable-component.ts +++ b/cocos/3d/framework/renderable-component.ts @@ -103,7 +103,7 @@ export class RenderableComponent extends Component { } - private _clearMaterials () { + protected _clearMaterials () { } } diff --git a/cocos/3d/framework/skinning-model-component.js b/cocos/3d/framework/skinning-model-component.js deleted file mode 100644 index 9e1a4cd65c..0000000000 --- a/cocos/3d/framework/skinning-model-component.js +++ /dev/null @@ -1,253 +0,0 @@ -/**************************************************************************** - Copyright (c) 2013-2016 Chukong Technologies Inc. - Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd. - - http://www.cocos2d-x.org - - Permission is hereby granted, free of charge, to any person obtaining a copy - of this software and associated documentation files (the "Software"), to deal - in the Software without restriction, including without limitation the rights - to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - copies of the Software, and to permit persons to whom the Software is - furnished to do so, subject to the following conditions: - - The above copyright notice and this permission notice shall be included in - all copies or substantial portions of the Software. - - 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. - ****************************************************************************/ - -// @ts-check -import { mat4 } from '../../core/vmath'; -import { ModelComponent } from './model-component'; -import * as renderer from '../../renderer/index'; -import { ccclass, property, executionOrder, menu,executeInEditMode } from '../../core/data/class-decorator'; -import * as utils from '../misc/utils'; -import Skeleton from '../assets/skeleton'; -import { Node } from '../../scene-graph/node'; - -/** - * @typedef {import("../framework/skeleton-instance").default} SkeletonInstance - */ -let _m4_tmp = mat4.create(); - -/** - * !#en The Skinning Model Component - * - * !#ch 皮肤模型组件 - * @class SkinningModelComponent - * @extends ModelComponent - */ -@ccclass('cc.SkinningModelComponent') -@executionOrder(100) -@executeInEditMode -@menu('Components/SkinningModelComponent') -export default class SkinningModelComponent extends ModelComponent { - /** - * @type {Skeleton} - */ - @property(Skeleton) - _skeleton = null; - - /** - * @type {Node} - */ - @property(Node) - _skinningRoot = null; - - constructor() { - super(); - - /** - * @type {Map} - */ - this._skinningTarget = null; - - /** - * @type {import("../../renderer/gfx/texture-2d").default} - */ - this._jointsTexture = null; - - /** - * @type {Float32Array} - */ - this._jointMatricesData = null; - } - - /** - * !#en The bone nodes - * - * !#ch 骨骼节点 - * @type {Skeleton} - */ - @property({type: Skeleton}) - get skeleton() { - return this._skeleton; - } - - set skeleton(val) { - this._skeleton = val; - } - - @property({type: Node}) - get skinningRoot() { - return this._skinningRoot; - } - - set skinningRoot(value) { - this._skinningRoot = value; - this._resetTarget(); - } - - onLoad() { - this._resetTarget(); - this._reInitJointsData(); - this._updateModels(); - this._updateCastShadow(); - this._updateReceiveShadow(); - } - - update(dt) { - this._updateMatrices(); - } - - - onDestroy() { - } - - _updateMatrices() { - if (!this._mesh || !this._skeleton || !this._skinningTarget) { - return; - } - - // /** @type {import("../assets/utils/gltf-utils").GltfMeshResource} */ - // const res = this._mesh.resource; - // const gltf = res.gltfAsset.description; - - // /** @type {Node[]} */ - // const nodes = []; - // gltf.nodes.forEach((gltfNode, index) => nodes[index] = createNode(gltfNode)); - // gltf.nodes.forEach((gltfNode, index) => { - // if (gltfNode.children) { - // const node = nodes[index]; - // gltfNode.children.forEach((childIndex) => { - // nodes[childIndex].setParent(node); - // }); - // } - // }); - - const cancelThisNoTransform = this.node.getWorldMatrix(); - mat4.invert(cancelThisNoTransform, cancelThisNoTransform); - - this._skeleton.joints.forEach((joint, index) => { - // const debugNode = nodes[this._skeleton._debugjoints[index]]; - // let wm = debugNode.getWorldMatrix(); - - const targetNode = this._skinningTarget.get(joint); - if (!targetNode) { - return; - } - const bindpose = this._skeleton.bindposes[index]; - - // 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 jointMatrix = _m4_tmp; - mat4.multiply(jointMatrix, cancelThisNoTransform, targetNode.getWorldMatrix()); - mat4.multiply(jointMatrix, jointMatrix, bindpose); - this._setJointMatrix(index, jointMatrix); - }); - - this._commitJointsData(); - } - - _createModel() { - return new renderer.SkinningModel(); - } - - _updateModelParams() { - super._updateModelParams(); - for (let i = 0; i < this._models.length; ++i) - this._updateModelJointParam(this._models[i]); - } - - _reInitJointsData() { - if (this._jointsTexture) { - this._jointsTexture.destroy(); - } - - if (!this._skeleton) { - this._jointMatricesData = null; - return; - } - - if (cc.game._gfxDevice.allowFloatTexture()) { - this._jointsTexture = utils.createJointsTexture(this._skeleton); - this._jointMatricesData = new Float32Array(this._jointsTexture._width * this._jointsTexture._height * 4); - } else { - this._jointMatricesData = new Float32Array(this._skeleton.joints.length * 16); - } - } - - _commitJointsData() { - if (this._jointsTexture != null) { - this._jointsTexture.updateImage({ - image: this._jointMatricesData, - width: this._jointsTexture._width, - height: this._jointsTexture._height, - level: 0, - flipY: false, - premultiplyAlpha: false - }); - } - } - - _updateModelJointParam(model) { - if (this._jointsTexture != null) { - model.setJointsTexture(this._jointsTexture); - } else { - model.setJointsMatrixArray(this._jointMatricesData); - } - } - - _setJointMatrix(iMatrix, matrix) { - this._jointMatricesData[16 * iMatrix + 0] = matrix.m00; - this._jointMatricesData[16 * iMatrix + 1] = matrix.m01; - this._jointMatricesData[16 * iMatrix + 2] = matrix.m02; - this._jointMatricesData[16 * iMatrix + 3] = matrix.m03; - this._jointMatricesData[16 * iMatrix + 4] = matrix.m04; - this._jointMatricesData[16 * iMatrix + 5] = matrix.m05; - this._jointMatricesData[16 * iMatrix + 6] = matrix.m06; - this._jointMatricesData[16 * iMatrix + 7] = matrix.m07; - this._jointMatricesData[16 * iMatrix + 8] = matrix.m08; - this._jointMatricesData[16 * iMatrix + 9] = matrix.m09; - this._jointMatricesData[16 * iMatrix + 10] = matrix.m10; - this._jointMatricesData[16 * iMatrix + 11] = matrix.m11; - this._jointMatricesData[16 * iMatrix + 12] = matrix.m12; - this._jointMatricesData[16 * iMatrix + 13] = matrix.m13; - this._jointMatricesData[16 * iMatrix + 14] = matrix.m14; - this._jointMatricesData[16 * iMatrix + 15] = matrix.m15; - } - - _resetTarget() { - this._skinningTarget = new Map(); - if (!this._skeleton || !this._skinningRoot) { - return; - } - const rootNode = this._skinningRoot; - this._skeleton.joints.forEach(joint => { - const targetNode = rootNode.getChildByPath(joint); - if (!targetNode) { - console.warn(`Skinning target ${joint} not found in scene graph.`); - return; - } - this._skinningTarget.set(joint, targetNode); - }); - } -} diff --git a/cocos/3d/framework/skinning-model-component.ts b/cocos/3d/framework/skinning-model-component.ts new file mode 100644 index 0000000000..1f80f29aeb --- /dev/null +++ b/cocos/3d/framework/skinning-model-component.ts @@ -0,0 +1,238 @@ +/**************************************************************************** + Copyright (c) 2013-2016 Chukong Technologies Inc. + Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd. + + http://www.cocos2d-x.org + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + 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. + ****************************************************************************/ + +// @ts-check +import { Texture2D } from '../../assets/texture-2d'; +import { ccclass, executeInEditMode, executionOrder, menu, property } from '../../core/data/class-decorator'; +import { mat4 } from '../../core/vmath'; +import { GFXDevice, GFXFeature } from '../../gfx/device'; +import { SkinningModel } from '../../renderer/models/skinning-model'; +import { Node } from '../../scene-graph/node'; +import Skeleton from '../assets/skeleton'; +import * as utils from '../misc/utils'; +import { ModelComponent } from './model-component'; + +const _m4_tmp = mat4.create(); + +interface IJointTextureStorage { + nativeData: Float32Array; + texture: Texture2D; +} + +interface IJointUniformsStorage { + nativeData: Float32Array; +} + +type JointStorage = IJointTextureStorage | IJointUniformsStorage; + +interface ISkinningState { + skinningTarget: Map; + storage: JointStorage; +} + +function isTextureStorage (storage: JointStorage): storage is IJointTextureStorage { + return (storage as any).texture !== undefined; +} + +/** + * !#en The Skinning Model Component + * !#ch 皮肤模型组件 + */ +@ccclass('cc.SkinningModelComponent') +@executionOrder(100) +@executeInEditMode +@menu('Components/SkinningModelComponent') +export default class SkinningModelComponent extends ModelComponent { + @property(Skeleton) + private _skeleton: Skeleton | null = null; + + @property(Node) + private _skinningRoot: Node | null = null; + + private _skinningState: ISkinningState | null = null; + + constructor () { + super(); + } + + /** + * !#en The bone nodes + * + * !#ch 骨骼节点 + */ + @property({type: Skeleton}) + get skeleton () { + return this._skeleton; + } + + set skeleton (val) { + this._skeleton = val; + this._resetSkinningState(); + } + + @property({type: Node}) + get skinningRoot () { + return this._skinningRoot; + } + + set skinningRoot (value) { + this._skinningRoot = value; + this._resetSkinningState(); + } + + public onLoad () { + this._resetSkinningState(); + } + + public update (dt) { + this._tryUpdateMatrices(); + } + + public onDestroy () { + } + + public _tryUpdateMatrices () { + if (!this._skeleton || !this._skinningState) { + return; + } + + const skinningState = this._skinningState!; + const skeleton = this._skeleton!; + + const cancelThisNodeTransform = this.node.getWorldMatrix(); + mat4.invert(cancelThisNodeTransform, cancelThisNodeTransform); + + this._skeleton.joints.forEach((joint, index) => { + // If target joint doesn't exists in scene graph, skip it. + const targetNode = skinningState.skinningTarget.get(joint); + if (!targetNode) { + return; + } + // 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, cancelThisNodeTransform, targetNode.getWorldMatrix()); + mat4.multiply(jointMatrix, jointMatrix, bindpose); + _setJointMatrix(skinningState.storage.nativeData, index, jointMatrix); + }); + + const storage = skinningState.storage; + if (isTextureStorage(storage)) { + storage.texture.directUpdate(storage.nativeData.buffer); + } + } + + public _createModel () { + return this._getRenderScene().createModel(SkinningModel, this.node); + } + + public _updateModelParams () { + super._updateModelParams(); + // Upload joint matrices. + if (this._skinningState !== null) { + const skinningState = this._skinningState; + const skinningModel = this._model as SkinningModel; + if (isTextureStorage(skinningState.storage)) { + const texture = skinningState.storage.texture; + skinningModel.setJointTexture(texture); + } else { + skinningModel.setJointMatrices(skinningState.storage.nativeData); + } + } + } + + private _resetSkinningState () { + if (this._skinningState) { + if (isTextureStorage(this._skinningState.storage)) { + this._skinningState.storage.texture.destroy(); + } + this._skinningState = null; + } + + if (!this._skeleton || !this._skinningRoot) { + return; + } + + let storage: JointStorage; + + // Allocate joint matrices storage. + const device = _getGlobalDevice(); + if (device && device.hasFeature(GFXFeature.TEXTURE_FLOAT)) { + const jointsTexture = utils.createJointsTexture(this._skeleton); + storage = { + texture: jointsTexture, + nativeData: new Float32Array(jointsTexture.width * jointsTexture.height * 4), + } as IJointTextureStorage; + } else { + storage = { + nativeData: new Float32Array(this._skeleton.joints.length * 16), + } as IJointUniformsStorage; + } + + const skinningState: ISkinningState = { + skinningTarget: new Map(), + storage, + }; + + // Collect target to skin. + const rootNode = this._skinningRoot; + this._skeleton.joints.forEach((joint) => { + const targetNode = rootNode.getChildByPath(joint); + if (!targetNode) { + console.warn(`Skinning target ${joint} not found in scene graph.`); + return; + } + skinningState.skinningTarget.set(joint, targetNode); + }); + + this._skinningState = skinningState; + } +} + +function _getGlobalDevice (): GFXDevice | null { + // @ts-ignore + return cc.director.root.device; +} + +function _setJointMatrix (out: Float32Array, iMatrix: number, matrix: mat4) { + out[16 * iMatrix + 0] = matrix.m00; + out[16 * iMatrix + 1] = matrix.m01; + out[16 * iMatrix + 2] = matrix.m02; + out[16 * iMatrix + 3] = matrix.m03; + out[16 * iMatrix + 4] = matrix.m04; + out[16 * iMatrix + 5] = matrix.m05; + out[16 * iMatrix + 6] = matrix.m06; + out[16 * iMatrix + 7] = matrix.m07; + out[16 * iMatrix + 8] = matrix.m08; + out[16 * iMatrix + 9] = matrix.m09; + out[16 * iMatrix + 10] = matrix.m10; + out[16 * iMatrix + 11] = matrix.m11; + out[16 * iMatrix + 12] = matrix.m12; + out[16 * iMatrix + 13] = matrix.m13; + out[16 * iMatrix + 14] = matrix.m14; + out[16 * iMatrix + 15] = matrix.m15; +} diff --git a/cocos/assets/image-asset.ts b/cocos/assets/image-asset.ts index 5cb4c4f2aa..708fc1c0ed 100644 --- a/cocos/assets/image-asset.ts +++ b/cocos/assets/image-asset.ts @@ -36,7 +36,7 @@ export interface IMemoryImageSource { format: number; } -type ImageSource = HTMLCanvasElement | HTMLImageElement | IMemoryImageSource; +export type ImageSource = HTMLCanvasElement | HTMLImageElement | IMemoryImageSource; /** * Class ImageAsset. diff --git a/cocos/assets/texture-2d.ts b/cocos/assets/texture-2d.ts index 95c03611b9..d5a5afd186 100644 --- a/cocos/assets/texture-2d.ts +++ b/cocos/assets/texture-2d.ts @@ -25,7 +25,7 @@ ****************************************************************************/ // @ts-check import { ccclass, property } from '../core/data/class-decorator'; -import { ImageAsset } from './image-asset'; +import { ImageAsset, ImageSource } from './image-asset'; import { TextureBase } from './texture-base'; /** @@ -93,14 +93,13 @@ export class Texture2D extends TextureBase { /** * Updates mipmaps at specified range of levels. - * @param sources The image sources. * @param firstLevel The first level from which the sources update. * @description * If the range specified by [firstLevel, firstLevel + sources.length) exceeds * the actually range of mipmaps this texture contains, only overlaped mipmaps are updated. * Use this method if your mipmap data are modified. */ - public updateMipmaps (firstLevel: number, count?: number) { + public updateMipmaps (firstLevel: number = 0, count?: number) { if (firstLevel >= this._mipmaps.length) { return; } @@ -111,10 +110,14 @@ export class Texture2D extends TextureBase { for (let i = 0; i < nUpdate; ++i) { const level = firstLevel + i; - this._uploadImage(this._mipmaps[level], level); + this._assignImage(this._mipmaps[level], level); } } + public directUpdate (source: HTMLImageElement | HTMLCanvasElement | ArrayBuffer, level: number = 0) { + this._uploadData(source, level); + } + /** * !#en * HTMLElement Object getter, available only on web. diff --git a/cocos/assets/texture-base.ts b/cocos/assets/texture-base.ts index 87406f1567..5f8812de9e 100644 --- a/cocos/assets/texture-base.ts +++ b/cocos/assets/texture-base.ts @@ -36,7 +36,7 @@ import { GFXSampler } from '../gfx/sampler'; import { GFXTexture, IGFXTextureInfo } from '../gfx/texture'; import { GFXTextureView, IGFXTextureViewInfo } from '../gfx/texture-view'; import { Asset } from './asset'; -import { ImageAsset } from './image-asset'; +import { ImageAsset, ImageSource } from './image-asset'; const CHAR_CODE_1 = 49; // '1' @@ -243,6 +243,7 @@ export class TextureBase extends Asset { protected constructor (flipY: boolean = false) { super(); + // @ts-ignore EventTarget.call(this); this._flipY = flipY; @@ -266,7 +267,7 @@ export class TextureBase extends Asset { this._potientialWidth = width; this._potientialHeight = height; this._format = format; - this._mipmapLevel = 1; + this._mipmapLevel = mipmapLevel; this._updateSampler(); this._recreateTexture(); } @@ -495,7 +496,18 @@ export class TextureBase extends Asset { } protected _assignImage (image: ImageAsset, level: number, arrayIndex?: number) { - const upload = () => { this._uploadImage(image, level, arrayIndex); }; + const upload = () => { + if (!image.data) { + return; + } + let source: HTMLCanvasElement | HTMLImageElement | ArrayBuffer; + if (image.data instanceof HTMLElement) { + source = image.data; + } else { + source = image.data.buffer; + } + this._uploadData(source, level, arrayIndex); + }; // @ts-ignore if (image.loaded) { upload(); @@ -507,7 +519,8 @@ export class TextureBase extends Asset { } } - protected _uploadImage (image: ImageAsset, level: number, arrayIndex?: number) { + protected _uploadData ( + source: HTMLCanvasElement | HTMLImageElement | ArrayBuffer, level: number, arrayIndex?: number) { if (!this._texture) { return; } @@ -516,11 +529,6 @@ export class TextureBase extends Asset { return; } - const data = image.data; - if (!data) { - return; - } - const gfxDevice = this._getGlobalDevice(); if (!gfxDevice) { return; @@ -548,10 +556,10 @@ export class TextureBase extends Asset { }, }; - if (data instanceof HTMLElement) { - gfxDevice.copyImageSourceToTexture(data, this._texture, [region]); + if (source instanceof HTMLElement) { + gfxDevice.copyImageSourceToTexture(source, this._texture, [region]); } else { - gfxDevice.copyBufferToTexture(data.buffer, this._texture, [region]); + gfxDevice.copyBufferToTexture(source, this._texture, [region]); } } @@ -621,7 +629,8 @@ export class TextureBase extends Asset { } } -cc.TextureBase = TextureBase; +/* tslint:disable:no-string-literal */ +cc['TextureBase'] = TextureBase; /** * !#zh diff --git a/cocos/gfx/webgl/webgl-commands.ts b/cocos/gfx/webgl/webgl-commands.ts index ec81e36417..2cfe05d36e 100644 --- a/cocos/gfx/webgl/webgl-commands.ts +++ b/cocos/gfx/webgl/webgl-commands.ts @@ -151,6 +151,7 @@ function GFXFormatToWebGLInternalFormat (format: GFXFormat): GLenum { case GFXFormat.LA8: return WebGLRenderingContext.LUMINANCE_ALPHA; case GFXFormat.RGB8: return WebGLRenderingContext.RGB; case GFXFormat.RGBA8: return WebGLRenderingContext.RGBA; + case GFXFormat.RGBA32F: return WebGLRenderingContext.RGBA; case GFXFormat.R5G6B5: return WebGLRenderingContext.RGB565; case GFXFormat.RGB5A1: return WebGLRenderingContext.RGB5_A1; case GFXFormat.RGBA4: return WebGLRenderingContext.RGBA4; @@ -191,6 +192,7 @@ function GFXFormatToWebGLFormat (format: GFXFormat): GLenum { case GFXFormat.LA8: return WebGLRenderingContext.LUMINANCE_ALPHA; case GFXFormat.RGB8: return WebGLRenderingContext.RGB; case GFXFormat.RGBA8: return WebGLRenderingContext.RGBA; + case GFXFormat.RGBA32F: return WebGLRenderingContext.RGBA; case GFXFormat.R5G6B5: return WebGLRenderingContext.RGB; case GFXFormat.RGB5A1: return WebGLRenderingContext.RGBA; case GFXFormat.RGBA4: return WebGLRenderingContext.RGBA; diff --git a/cocos/renderer/index.ts b/cocos/renderer/index.ts index 4a5e1eac1b..b1cd8bc636 100644 --- a/cocos/renderer/index.ts +++ b/cocos/renderer/index.ts @@ -15,4 +15,4 @@ export { Model } from './scene/model'; export { default as LineBatchModel } from './models/line-batch-model'; export { default as SpriteBatchModel } from './models/sprite-batch-model'; export { default as ParticleBatchModel } from './models/particle-batch-model'; -export { default as SkinningModel } from './models/skinning-model'; +export { SkinningModel } from './models/skinning-model'; diff --git a/cocos/renderer/models/model-uniforms.ts b/cocos/renderer/models/model-uniforms.ts index b28f62485a..16ef5e976f 100644 --- a/cocos/renderer/models/model-uniforms.ts +++ b/cocos/renderer/models/model-uniforms.ts @@ -1,10 +1,10 @@ import { GFXType } from '../../gfx/define'; -import { GFXUniformBlock } from '../../gfx/shader'; +import { GFXUniformBlock, GFXUniformSampler } from '../../gfx/shader'; export class SkinningUBO { public static MAT_JOINT_OFFSET: number = 0; - public static JOINTS_TEXTURE_SIZE_OFFSET: number = SkinningUBO.MAT_JOINT_OFFSET + 128 * 4; - public static COUNT: number = SkinningUBO.MAT_JOINT_OFFSET + 4; + public static JOINTS_TEXTURE_SIZE_OFFSET: number = SkinningUBO.MAT_JOINT_OFFSET + 128 * 16; + public static COUNT: number = SkinningUBO.JOINTS_TEXTURE_SIZE_OFFSET + 4; public static SIZE: number = SkinningUBO.COUNT * 4; public static BLOCK: GFXUniformBlock = { @@ -14,5 +14,7 @@ export class SkinningUBO { ], }; - // public view: Float32Array = new Float32Array(SkinningUBO.COUNT); + public static JOINT_TEXTURE: GFXUniformSampler = { + binding: 28, name: 'CCJointTexture', type: GFXType.SAMPLER2D, count: 1, + }; } diff --git a/cocos/renderer/models/skinning-model.js b/cocos/renderer/models/skinning-model.js deleted file mode 100644 index 53a8af9c92..0000000000 --- a/cocos/renderer/models/skinning-model.js +++ /dev/null @@ -1,35 +0,0 @@ -// Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd. - -import { Model } from '../scene/model'; -import { SkinningUBO } from './model-uniforms'; -import { GFXBufferUsageBit, GFXMemoryUsageBit } from '../../gfx/define'; - -export default class SkinningModel extends Model { - constructor() { - super(); - - this._type = 'skinning'; - this._jointsTexture = null; - this._jointsMatrixArray = null; - } - - setJointsTexture(texture) { - this._jointsTexture = texture; - } - - setJointsMatrixArray(a) { - this._jointsMatrixArray = a; - this._skinningUBO = this._device.createBuffer({ - usage: GFXBufferUsageBit.UNIFORM | GFXBufferUsageBit.TRANSFER_DST, - memUsage: GFXMemoryUsageBit.HOST, - size: SkinningUBO.SIZE, - stride: SkinningUBO.SIZE, - }); - this._skinningUBO.update(this._jointsMatrixArray); - } - - _onCreatePSO(pso) { - super._onCreatePSO(pso); - pso.pipelineLayout.layouts[0].bindBuffer(SkinningUBO.BLOCK.binding, this._skinningUBO); - } -} diff --git a/cocos/renderer/models/skinning-model.ts b/cocos/renderer/models/skinning-model.ts new file mode 100644 index 0000000000..95a7a179d2 --- /dev/null +++ b/cocos/renderer/models/skinning-model.ts @@ -0,0 +1,52 @@ +// Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd. + +import { Texture2D } from '../../assets/texture-2d'; +import { GFXBuffer } from '../../gfx/buffer'; +import { GFXBufferUsageBit, GFXMemoryUsageBit } from '../../gfx/define'; +import { GFXPipelineState } from '../../gfx/pipeline-state'; +import { Node } from '../../scene-graph/node'; +import { Model } from '../scene/model'; +import { RenderScene } from '../scene/render-scene'; +import { SkinningUBO } from './model-uniforms'; + +const textureSizeBuffer = new Float32Array(4); + +export class SkinningModel extends Model { + private _skinningUBO: GFXBuffer; + private _jointTexture: Texture2D | null = 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, + size: SkinningUBO.SIZE, + stride: SkinningUBO.SIZE, + }); + } + + public setJointTexture (texture: Texture2D) { + this._jointTexture = texture; + textureSizeBuffer[0] = texture.width; + this._skinningUBO.update( + textureSizeBuffer, SkinningUBO.JOINTS_TEXTURE_SIZE_OFFSET, textureSizeBuffer.byteLength); + } + + public setJointMatrices (data: Float32Array) { + this._skinningUBO.update(data, SkinningUBO.MAT_JOINT_OFFSET); + } + + protected _onCreatePSO (pso: GFXPipelineState) { + super._onCreatePSO(pso); + pso.pipelineLayout.layouts[0].bindBuffer(SkinningUBO.BLOCK.binding, this._skinningUBO); + if (this._jointTexture) { + const view = this._jointTexture.getGFXTextureView(); + const sampler = this._jointTexture.getGFXSampler(); + if (view && sampler) { + pso.pipelineLayout.layouts[0].bindTextureView(SkinningUBO.JOINT_TEXTURE.binding, view); + pso.pipelineLayout.layouts[0].bindSampler(SkinningUBO.JOINT_TEXTURE.binding, sampler); + } + } + } +} From dc0de128b035b63a2f79cdf5f666b93d9c33c5dc Mon Sep 17 00:00:00 2001 From: Leslie Date: Wed, 23 Jan 2019 15:26:24 +0800 Subject: [PATCH 2/2] ignore package-lock.json --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 0dd9da6758..d57d13d67d 100644 --- a/.gitignore +++ b/.gitignore @@ -14,3 +14,4 @@ node_modules npm-debug.log DebugInfos.json /rollup/out +/package-lock.json