support original gltf animation;use creator's texture2d instead (#3265)

* Modify part of asset classes

* Update import

* Update

* mesh-loader

* Update the import statement of JsonAsset as default import

* Rename files

* Remove old asset and asset-mng

* Copyrights

* Fix some bugs

* Fix imports of vmath classes; Fix errors of decorator property's ts comment notation;Adjust the order of member declarations for better IDE intelligense

* Transplant loader of animation clip; Unify export statement style according rules

* Skeleton asset and SkeletonInstance class

* Rename

* Fix material annotation

* fix import path

* fix annotation error

* Rename update to flush, fix index.js files.

* support original gltf animation;use creator's texture2d instead
This commit is contained in:
Leslie Leigh
2018-09-18 17:14:35 +08:00
committed by pandamicro
parent 423132c567
commit a1ed40ad47
8 changed files with 59 additions and 270 deletions
+12 -6
View File
@@ -138,7 +138,7 @@ export default class AnimationClip extends Asset {
if (idx == 0) {
for (let j = 0; j < frames.joints.length; ++j) {
let jointFrames = frames.joints[j];
let joint = skeleton._joints[jointFrames.id];
let joint = skeleton._joints[skeleton.getJointIndexFromOrignalNodeIndex(jointFrames.id)];
if (!joint) {
continue;
}
@@ -162,7 +162,7 @@ export default class AnimationClip extends Asset {
for (let j = 0; j < frames.joints.length; ++j) {
let jointFrames = frames.joints[j];
let joint = skeleton._joints[jointFrames.id];
let joint = skeleton._joints[skeleton.getJointIndexFromOrignalNodeIndex(jointFrames.id)];
if (!joint) {
continue;
}
@@ -221,11 +221,12 @@ export default class AnimationClip extends Asset {
if (idx == 0) {
for (let j = 0; j < frames.joints.length; ++j) {
let jointFrames = frames.joints[j];
if (isJointMaksed(jointFrames.id)) {
const realJointID = state.skeleton.getJointIndexFromOrignalNodeIndex(jointFrames.id);
if (isJointMaksed(realJointID)) {
continue;
}
let jointState = state._jointStates[jointFrames.id];
let jointState = state._jointStates[realJointID];
if (!jointState) {
continue;
}
@@ -250,11 +251,12 @@ export default class AnimationClip extends Asset {
for (let j = 0; j < frames.joints.length; ++j) {
let jointFrames = frames.joints[j];
if (isJointMaksed(jointFrames.id)) {
const realJointID = state.skeleton.getJointIndexFromOrignalNodeIndex(jointFrames.id);
if (isJointMaksed(realJointID)) {
continue;
}
let jointState = state._jointStates[jointFrames.id];
let jointState = state._jointStates[realJointID];
if (!jointState) {
continue;
}
@@ -311,6 +313,10 @@ export class SamplingState {
this._jointStates[i] = new SamplingStateJointState(skeleton._joints[i]);
}
get skeleton() {
return this._skeleton;
}
/**
* Resets this state to get sampling start.
*/
+8 -6
View File
@@ -24,20 +24,22 @@
****************************************************************************/
// @ts-check
import { _decorator } from "../../core/data/index";
const { ccclass } = _decorator;
const { ccclass, property } = _decorator;
import Font from "./font";
import vec2 from "../../core/vmath/vec2";
import Texture2D from "./texture-2d";
import Texture2D from "../../assets/CCTexture2D";
@ccclass
export class BitmapFont extends Font {
/**
* @type {Texture2D}
*/
@property(Texture2D)
_texture = null;
constructor() {
super();
super._type = "bitmap";
/**
* @type {Texture2D}
*/
this._texture = null;
/**
* @type {string}
+8 -10
View File
@@ -28,7 +28,7 @@ const {ccclass} = _decorator;
import Font from "./font";
import vec2 from "../../core/vmath/vec2";
import ShelfPack from "./utils/shelf-pack";
import Texture2D from "./texture-2d";
import Texture2D from "../../assets/CCTexture2D";
@ccclass
export default class OpentypeFont extends Font {
@@ -99,9 +99,10 @@ export default class OpentypeFont extends Font {
this._packCanvas.width = width;
this._packCanvas.height = height;
this._fontAtlas = new Texture2D(device, width, height);
this._fontAtlas.mipmap = false;
this._fontAtlas.premultiplyAlpha = true;
this._fontAtlas = new Texture2D();
this._fontAtlas.initWithData(null, Texture2D.PixelFormat.RGBA8888, width, height);
this._fontAtlas.setMipmap(false);
this._fontAtlas.setPremultiplyAlpha(true);
}
/**
@@ -172,8 +173,7 @@ export default class OpentypeFont extends Font {
this._packer.clear();
let ctx = this._packCanvas.getContext('2d');
ctx.clearRect(0, 0, this._packCanvas.width, this._packCanvas.height);
this._fontAtlas.setImages([this._packCanvas]);
this._fontAtlas.commit();
this._fontAtlas.initWithElement(this._packCanvas);
}
_addTextToFontAtlas(text) {
@@ -235,8 +235,7 @@ export default class OpentypeFont extends Font {
if (fontAtlasDirty) {
// update font atlas
this._fontAtlas.setImages([this._packCanvas]);
this._fontAtlas.commit();
this._fontAtlas.initWithElement(this._packCanvas);
}
}
@@ -261,8 +260,7 @@ export default class OpentypeFont extends Font {
if (fontAtlasDirty) {
// update font atlas
this._fontAtlas.setImages([this._packCanvas]);
this._fontAtlas.commit();
this._fontAtlas.initWithElement(this._packCanvas);
}
}
}
+3
View File
@@ -38,6 +38,9 @@ export default class Skeleton extends Asset {
@property(Node)
_rootNode = null;
@property(Number)
_indexDelta = 0;
instantiate() {
let rootNode = instantiate(this._rootNode, undefined);
let skeleton = new SkeletonInstance();
-234
View File
@@ -1,234 +0,0 @@
/****************************************************************************
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
http://www.cocos.com
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated engine source code (the "Software"), a limited,
worldwide, royalty-free, non-assignable, revocable and non-exclusive license
to use Cocos Creator solely to develop games on your target platforms. You shall
not use Cocos Creator software for developing other software or tools that's
used for developing games. You are not granted to publish, distribute,
sublicense, and/or sell copies of Cocos Creator.
The software or tools in this License Agreement are licensed, not sold.
Xiamen Yaji Software Co., Ltd. reserves all rights not expressly granted to you.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
// @ts-check
import { _decorator } from "../../core/data/index";
const { ccclass } = _decorator;
import Texture from './texture';
import gfx from '../gfx';
import { gfxFilters, gfxWraps, gfxTextureFmts } from '../misc/mappings';
let _opts = {
images: [],
mipmap: true,
width: 2,
height: 2,
format: gfx.TEXTURE_FMT_RGBA8,
anisotropy: 1,
wrapS: gfx.WRAP_REPEAT,
wrapT: gfx.WRAP_REPEAT,
minFilter: gfx.FILTER_LINEAR,
magFilter: gfx.FILTER_LINEAR,
mipFilter: gfx.FILTER_LINEAR,
premultiplyAlpha: false,
};
function _updateOpts(out, texture) {
if (texture._writable) {
out.images = [texture._data];
} else {
out.images = texture._images;
}
out.mipmap = texture._mipmap;
out.width = texture._width;
out.height = texture._height;
out.format = gfxTextureFmts[texture._format];
out.anisotropy = texture._anisotropy;
out.wrapS = gfxWraps[texture._wrapS];
out.wrapT = gfxWraps[texture._wrapT];
out.minFilter = gfxFilters[texture._minFilter];
out.magFilter = gfxFilters[texture._magFilter];
out.mipFilter = gfxFilters[texture._mipFilter];
out.premultiplyAlpha = texture._premultiplyAlpha;
}
function _allocData(texture) {
if (texture._format === 'a8') {
return new Uint8Array(texture._width * texture._height);
} else if (texture._format === 'rgb8') {
return new Uint8Array(texture._width * texture._height * 3);
} else if (texture._format === 'rgba8') {
return new Uint8Array(texture._width * texture._height * 4);
} else if (texture._format === 'rgba32f') {
return new Float32Array(texture._width * texture._height * 4);
}
return null;
}
@ccclass
export default class Texture2D extends Texture {
constructor(device, width = 2, height = 2, fmt = 'rgba8') {
super(device);
//
this._writable = false;
this._data = null;
// opts
this._images = [];
this._mipmap = true;
this._width = width;
this._height = height;
this._format = fmt;
this._anisotropy = 1;
this._wrapS = 'repeat';
this._wrapT = 'repeat';
this._minFilter = 'linear';
this._magFilter = 'linear';
this._mipFilter = 'linear';
this._premultiplyAlpha = false;
}
destroy() {
this._texture.destroy();
return super.destroy();
}
setImage(level, img) {
if (
img instanceof HTMLCanvasElement ||
img instanceof HTMLImageElement ||
img instanceof HTMLVideoElement
) {
this._images[level] = img;
if (level === 0) {
this.resize(img.width, img.height);
}
// TODO: if writable, get data by canvas.drawImage(), canvas.getImageData();
}
}
setImages(imgs) {
this._images = imgs;
this.resize(imgs[0].width, imgs[0].height);
}
resize(width, height) {
this._width = width;
this._height = height;
if (this._writable) {
this._data = _allocData(this);
}
}
get width() {
return this._width;
}
get height() {
return this._height;
}
get format() {
return this._format;
}
get data() {
return this._data;
}
set writable(val) {
this._writable = val;
if (this._writable) {
this._data = _allocData(this);
} else {
this._data = null;
}
}
get writable() {
return this._writable;
}
set mipmap(val) {
this._mipmap = val;
}
get mipmap() {
return this._mipmap;
}
set anisotropy(val) {
this._anisotropy = val;
}
get anisotropy() {
return this._anisotropy;
}
set wrapS(val) {
this._wrapS = val;
}
get wrapS() {
return this._wrapS;
}
set wrapT(val) {
this._wrapT = val;
}
get wrapT() {
return this._wrapT;
}
set minFilter(val) {
this._minFilter = val;
}
get minFilter() {
return this._minFilter;
}
set magFilter(val) {
this._magFilter = val;
}
get magFilter() {
return this._magFilter;
}
set mipFilter(val) {
this._mipFilter = val;
}
get mipFilter() {
return this._mipFilter;
}
set premultiplyAlpha(val) {
this._premultiplyAlpha = val;
}
get premultiplyAlpha() {
return this._premultiplyAlpha;
}
commit() {
_updateOpts(_opts, this);
if (!this._texture) {
this._texture = new gfx.Texture2D(this._device, _opts);
} else {
this._texture.update(_opts);
}
this._images = [];
}
}
+1 -5
View File
@@ -25,7 +25,7 @@
// @ts-check
import { _decorator } from "../../core/data/index";
const { ccclass, property } = _decorator;
import Texture2D from './texture-2d';
import Texture2D from '../../assets/CCTexture2D';
import Sprite from "./sprite";
@ccclass
@@ -36,10 +36,6 @@ export default class TextureSprite extends Texture2D {
@property([Sprite])
_sprites = [];
constructor(device) {
super(device);
}
get sprites() {
return this._sprites;
}
+16 -8
View File
@@ -210,7 +210,12 @@ export class GltfAnimationResource extends AnimationResource {
for (let i = 0; i < gltfAnimation.channels.length; ++i) {
let gltfChannel = gltfAnimation.channels[i];
let inputAcc = gltf.accessors[gltfChannel.input];
if (gltfChannel.target.node === undefined) {
// When node isn't defined, channel should be ignored.
continue;
}
let gltfSampler = gltf.samplers[gltfChannel.sampler];
let inputAcc = gltf.accessors[gltfSampler.input];
// find frames by input name
/** @type {cc.d3.animation.Frame} */
@@ -224,7 +229,7 @@ export class GltfAnimationResource extends AnimationResource {
// if not found, create one
if (!frames) {
let inArray = _createArray(gltf, bin, gltfChannel.input);
let inArray = _createArray(gltf, bin, gltfSampler.input);
/** @type {number[]} */
let inputs = new Array(inArray.length);
for (let i = 0; i < inArray.length; ++i) {
@@ -248,7 +253,7 @@ export class GltfAnimationResource extends AnimationResource {
/** @type {cc.d3.animation.JointFrame} */
let jointFrames;
for (let j = 0; j < frames.joints.length; ++j) {
if (frames.joints[j].id === gltfChannel.node) {
if (frames.joints[j].id === gltfChannel.target.node) {
jointFrames = frames.joints[j];
break;
}
@@ -257,13 +262,13 @@ export class GltfAnimationResource extends AnimationResource {
// if not found, create one
if (!jointFrames) {
jointFrames = {
id: gltfChannel.node
id: gltfChannel.target.node
};
frames.joints.push(jointFrames);
}
let outArray = _createArray(gltf, bin, gltfChannel.output);
if (gltfChannel.path === 'translation') {
if (gltfChannel.target.path === 'translation') {
let cnt = outArray.length / 3;
jointFrames.translations = new Array(cnt);
for (let i = 0; i < cnt; ++i) {
@@ -273,7 +278,7 @@ export class GltfAnimationResource extends AnimationResource {
outArray[3 * i + 2]
);
}
} else if (gltfChannel.path === 'rotation') {
} else if (gltfChannel.target.path === 'rotation') {
let cnt = outArray.length / 4;
jointFrames.rotations = new Array(cnt);
for (let i = 0; i < cnt; ++i) {
@@ -284,7 +289,7 @@ export class GltfAnimationResource extends AnimationResource {
outArray[4 * i + 3]
);
}
} else if (gltfChannel.path === 'scale') {
} else if (gltfChannel.target.path === 'scale') {
let cnt = outArray.length / 3;
jointFrames.scales = new Array(cnt);
for (let i = 0; i < cnt; ++i) {
@@ -339,6 +344,7 @@ const _gltfAttribMap = {
* @param {GLTFFormat} gltf
* @param {ArrayBuffer} bin
* @param {number} accessorID
* @return {Int8Array | Uint8Array | Int16Array | Uint16Array | Int32Array | Uint32Array | Float32Array}
*/
function _createArray(gltf, bin, accessorID) {
let acc = gltf.accessors[accessorID];
@@ -446,8 +452,10 @@ export function createSkeleton(gltfAsset, index) {
const gltfSkin = gltf.skins[index];
const skeleton = new Skeleton();
skeleton.name = gltfSkin.name;
const rootNode = _createNodeRecursive(gltf, gltfSkin.skeleton);
const rootIndex = gltfSkin.skeleton === undefined ? 0 : gltfSkin.skeleton;
const rootNode = _createNodeRecursive(gltf, rootIndex);
skeleton._rootNode = rootNode;
skeleton._indexDelta = rootIndex;
return skeleton;
}
+11 -1
View File
@@ -34,7 +34,7 @@ export default class SkeletonInstance {
this._matrices = null;
}
setRoot(root) {
setRoot(root, indexDelta = 0) {
/**
* @type {cc.Node}
*/
@@ -52,6 +52,12 @@ export default class SkeletonInstance {
for (let i = 0; i < this._joints.length; ++i) {
this._matrices[i] = mat4.create();
}
/**
* @type {number}
*/
this._indexDelta = indexDelta;
this.updateMatrices();
}
@@ -92,6 +98,10 @@ export default class SkeletonInstance {
return -1;
}
getJointIndexFromOrignalNodeIndex(index) {
return index - this._indexDelta;
}
clone() {
let newSkeleton = new SkeletonInstance();
newSkeleton.setRoot(cc.instantiate(this._root));