mirror of
https://github.com/cocos/cocos4.git
synced 2026-12-03 17:42:41 +08:00
@@ -14,3 +14,4 @@ node_modules
|
||||
npm-debug.log
|
||||
DebugInfos.json
|
||||
/rollup/out
|
||||
/package-lock.json
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
@@ -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'];
|
||||
}
|
||||
}
|
||||
@@ -103,7 +103,7 @@ export class RenderableComponent extends Component {
|
||||
|
||||
}
|
||||
|
||||
private _clearMaterials () {
|
||||
protected _clearMaterials () {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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<string, Node>}
|
||||
*/
|
||||
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);
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -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<string, Node>;
|
||||
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;
|
||||
}
|
||||
@@ -36,7 +36,7 @@ export interface IMemoryImageSource {
|
||||
format: number;
|
||||
}
|
||||
|
||||
type ImageSource = HTMLCanvasElement | HTMLImageElement | IMemoryImageSource;
|
||||
export type ImageSource = HTMLCanvasElement | HTMLImageElement | IMemoryImageSource;
|
||||
|
||||
/**
|
||||
* Class ImageAsset.
|
||||
|
||||
@@ -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.
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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';
|
||||
|
||||
@@ -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,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user