From f05972489c5986ac67b13c3e59eaef837d8009af Mon Sep 17 00:00:00 2001 From: Leslie Leigh Date: Mon, 17 Sep 2018 17:56:21 +0800 Subject: [PATCH] Skeleton asset and SkeletonInstance class (#3251) * 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. --- cocos/3d/assets/animation-clip.js | 71 +- cocos/3d/assets/audio-clip.js | 6 +- cocos/3d/assets/bitmap-font.js | 69 +- cocos/3d/assets/dom-audio-clip.js | 6 +- cocos/3d/assets/effect.js | 30 +- cocos/3d/assets/font.js | 18 +- cocos/3d/assets/index.js | 37 + cocos/3d/assets/joints.js | 21 - cocos/3d/assets/material.js | 14 +- cocos/3d/assets/mesh.js | 117 ++- cocos/3d/assets/opentype-font.js | 10 +- cocos/3d/assets/prefab.js | 26 - cocos/3d/assets/skeleton.js | 49 ++ cocos/3d/assets/sprite.js | 4 +- cocos/3d/assets/texture-2d.js | 10 +- cocos/3d/assets/texture-cube.js | 12 +- cocos/3d/assets/texture-sprite.js | 2 +- cocos/3d/assets/texture.js | 2 +- cocos/3d/assets/utils/gltf-utils.js | 687 ++++++++++-------- cocos/3d/assets/web-audio-clip.js | 6 +- cocos/3d/assets/wx-game-audio-clip.js | 4 +- cocos/3d/framework/index.js | 27 + .../{skeleton.js => skeleton-instance.js} | 46 +- cocos/3d/index.js | 33 + cocos/3d/primitive/box.js | 2 +- cocos/3d/primitive/capsule.js | 2 +- cocos/3d/primitive/cylinder.js | 2 +- cocos/3d/primitive/plane.js | 2 +- cocos/3d/primitive/quad.js | 2 +- cocos/3d/primitive/sphere.js | 2 +- cocos/3d/primitive/torus.js | 2 +- cocos/assets/CCGLTFAsset.js | 2 +- cocos/renderer/gfx/index-buffer.js | 3 +- cocos/renderer/gfx/vertex-buffer.js | 2 +- cocos/scene-graph/node.js | 14 +- 35 files changed, 769 insertions(+), 573 deletions(-) delete mode 100644 cocos/3d/assets/joints.js delete mode 100644 cocos/3d/assets/prefab.js create mode 100644 cocos/3d/assets/skeleton.js create mode 100644 cocos/3d/framework/index.js rename cocos/3d/framework/{skeleton.js => skeleton-instance.js} (76%) diff --git a/cocos/3d/assets/animation-clip.js b/cocos/3d/assets/animation-clip.js index f9f240cdaa..0c8485ae1b 100644 --- a/cocos/3d/assets/animation-clip.js +++ b/cocos/3d/assets/animation-clip.js @@ -22,18 +22,19 @@ 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; +const { ccclass, property } = _decorator; import Asset from "../../assets/CCAsset"; import vec3 from "../../core/vmath/vec3"; import quat from "../../core/vmath/quat"; import { clamp } from "../../core/vmath/utils"; /** - * @typedef {import("../framework/skeleton").SkeletonMask} SkeletonMask - * @typedef {import("../framework/skeleton").default} Skeleton - * @typedef {import("../../scene-graph/CCNode").default} Joint + * @typedef {import("../framework/skeleton-instance").SkeletonMask} SkeletonMask + * @typedef {import("../framework/skeleton-instance").default} SkeletonInstance + * @typedef {import("../../scene-graph/node").default} Joint */ let tmpvec3 = vec3.create(0, 0, 0); @@ -60,32 +61,68 @@ function _binaryIndexOf(array, key) { return lo; } +/** + * @interface + */ @ccclass -export class AnimationClip extends Asset { +export class AnimationResource { + /** + * + * @param {AnimationClip} animationClip + */ + flush(animationClip) { + + } +} + +@ccclass +export default class AnimationClip extends Asset { + /** + * @type {AnimationResource} + */ + @property(AnimationResource) + _resource = null; + + get resource() { + return this._resource; + } + + set resource(value) { + this._resource = value; + this.update(); + } + constructor() { super(); /** - * framesList: [{ - * name: '', - * times: [0.0, ...], - * jionts: [{ id: -1, translations: [], rotations: [], scales: [] }, ...], - * }, ...] + * @type {cc.d3.animation.Frame[]} + */ + this._framesList = []; + + /** + * @type {number} */ - this._framesList = null; this._length = 0.0; // TODO: // this._events = [] } + /** + * + */ + flush() { + this._resource.flush(this); + } + get length() { return this._length; } /** * - * @param {Skeleton} skeleton + * @param {SkeletonInstance} skeleton * @param {number} t */ sample(skeleton, t) { @@ -202,7 +239,7 @@ export class AnimationClip extends Asset { } if (jointFrames.scales) { - jointState.blendScale(jointFrames.scale[0], weight); + jointState.blendScale(jointFrames.scales[0], weight); } } } @@ -257,11 +294,11 @@ export class AnimationClip extends Asset { export class SamplingState { /** * - * @param {Skeleton} skeleton + * @param {SkeletonInstance} skeleton */ constructor(skeleton) { /** - * @type {Skeleton} + * @type {SkeletonInstance} * @ignore */ this._skeleton = skeleton; @@ -370,4 +407,6 @@ class SamplingStateJointState { if (this._sumRotWeight < 1.0) this.blendRotation(this._originalRot, 1.0 - this._sumRotWeight); } -} \ No newline at end of file +} + +cc.AnimationClip = AnimationClip; \ No newline at end of file diff --git a/cocos/3d/assets/audio-clip.js b/cocos/3d/assets/audio-clip.js index b096d1fe6a..11d04be712 100644 --- a/cocos/3d/assets/audio-clip.js +++ b/cocos/3d/assets/audio-clip.js @@ -60,7 +60,7 @@ export const AudioSourceType = { }; /** - * @typedef {AudioBuffer|HTMLAudioElement} AudioSource + * @typedef {AudioBuffer|HTMLAudioElement|any} AudioSource */ /** @@ -115,4 +115,6 @@ export class AudioClip extends Asset { getState() { return this._state; } -} \ No newline at end of file +} + +cc.AudioClip = AudioClip; \ No newline at end of file diff --git a/cocos/3d/assets/bitmap-font.js b/cocos/3d/assets/bitmap-font.js index b5755e0de3..b95b784add 100644 --- a/cocos/3d/assets/bitmap-font.js +++ b/cocos/3d/assets/bitmap-font.js @@ -24,50 +24,45 @@ ****************************************************************************/ // @ts-check import { _decorator } from "../../core/data/index"; -const {ccclass} = _decorator; +const { ccclass } = _decorator; import Font from "./font"; import vec2 from "../../core/vmath/vec2"; import Texture2D from "./texture-2d"; -/** - * @typedef {{char: string, x: number, y: number, width: number, height: number, xoffset: number, yoffset: number, xadvance: number, uvs: vec2[]}} BitmapFontGlyph - */ - @ccclass export class BitmapFont extends Font { - /** - * @type {Texture2D} - */ - _texture = null; - - /** - * @type {string} - */ - _face = ""; - - /** - * @type {BitmapFontGlyph} - */ - _defaultGlyph = { - char: "", - x: 0, - y: 0, - width: 16, - height: 16, - xoffset: 0, - yoffset: 0, - xadvance: 16, - uvs: [ - vec2.create(0, 0), - vec2.create(0, 0), - vec2.create(0, 0), - vec2.create(0, 0) - ], - }; - constructor() { super(); super._type = "bitmap"; + /** + * @type {Texture2D} + */ + this._texture = null; + + /** + * @type {string} + */ + this._face = ""; + + /** + * @type {cc.d3.font.BitmapFontGlyph} + */ + this._defaultGlyph = { + char: "", + x: 0, + y: 0, + width: 16, + height: 16, + xoffset: 0, + yoffset: 0, + xadvance: 16, + uvs: [ + vec2.create(0, 0), + vec2.create(0, 0), + vec2.create(0, 0), + vec2.create(0, 0) + ], + }; } /** @@ -83,4 +78,6 @@ export class BitmapFont extends Font { get face() { return this._face; } -} \ No newline at end of file +} + +cc.BitmapFont = BitmapFont; \ No newline at end of file diff --git a/cocos/3d/assets/dom-audio-clip.js b/cocos/3d/assets/dom-audio-clip.js index 3a76160546..3055407bbc 100644 --- a/cocos/3d/assets/dom-audio-clip.js +++ b/cocos/3d/assets/dom-audio-clip.js @@ -48,7 +48,7 @@ export class DOMAudioClip extends AudioClip { * @type {boolean} */ _oneShoting = false; - + constructor() { super(); @@ -172,4 +172,6 @@ export class DOMAudioClip extends AudioClip { getLoop() { return this._loop; } -} \ No newline at end of file +} + +cc.DOMAudioClip = DOMAudioClip; \ No newline at end of file diff --git a/cocos/3d/assets/effect.js b/cocos/3d/assets/effect.js index 7dc4c84d5c..956c8902b6 100644 --- a/cocos/3d/assets/effect.js +++ b/cocos/3d/assets/effect.js @@ -25,15 +25,14 @@ // @ts-check import { _decorator } from "../../core/data/index"; const {ccclass, property} = _decorator; -import Technique from "../../renderer/core/technique"; import Asset from "../../assets/CCAsset"; @ccclass export default class Effect extends Asset { /** - * @type {Technique[]} + * @type {cc.d3.effect.Technique[]} */ - @property([Technique]) + @property(Object) _techniques = []; /** @@ -43,31 +42,26 @@ export default class Effect extends Asset { _properties = {}; /** - * @type {Definition[]} + * @type {cc.d3.effect.Definition[]} */ @property([Object]) _defines = []; /** - * @type {Dependency[]} + * @type {cc.d3.effect.Dependency[]} */ @property([Object]) _dependencies = []; /** - * @typedef {{name: string, value: boolean|number|string}} Definition - * @typedef {{define: string, extension: string}} Dependency - */ - - /** - * @param {Technique[]} val + * @param {cc.d3.effect.Technique[]} val */ set techniques(val) { this._techniques = val; } /** - * @return {Technique[]} + * @return {cc.d3.effect.Technique[]} */ get techniques() { return this._techniques; @@ -88,28 +82,28 @@ export default class Effect extends Asset { } /** - * @param {Definition[]} val + * @param {cc.d3.effect.Definition[]} val */ set defines(val) { this._defines = val; } /** - * @return {Definition[]}} + * @return {cc.d3.effect.Definition[]}} */ get defines() { return this._defines; } /** - * @param {Dependency[]} val + * @param {cc.d3.effect.Dependency[]} val */ set dependencies(val) { this._dependencies = val; } /** - * @return {Dependency[]} + * @return {cc.d3.effect.Dependency[]} */ get dependencies() { return this._dependencies; @@ -119,4 +113,6 @@ export default class Effect extends Asset { // TODO: what should we do here ??? return super.destroy(); } -} \ No newline at end of file +} + +cc.Effect = Effect; \ No newline at end of file diff --git a/cocos/3d/assets/font.js b/cocos/3d/assets/font.js index 1286433742..1a6d526f60 100644 --- a/cocos/3d/assets/font.js +++ b/cocos/3d/assets/font.js @@ -27,14 +27,6 @@ import { _decorator } from "../../core/data/index"; const {ccclass} = _decorator; import Asset from "../../assets/CCAsset"; -/** - * @typedef {import("../../core/vmath/index").vec2} vec2 - * @typedef {"unknow" | "bitmap" | "opentype"} FontType - * @typedef {{char: string, x: number, y: number, width: number, height: number, xoffset: number, yoffset: number, xadvance: number, uvs: vec2[]}} BitmapFontGlyph - * @typedef {{id: number, x: number, y: number, width: number, height: number, xoffset: number, yoffset: number, xadvance: number, uvs: vec2[]}} OpentypeFontGlyph - * @typedef {BitmapFontGlyph | OpentypeFontGlyph} Glyph - */ - @ccclass export default class Font extends Asset { /** Font size. @@ -43,7 +35,7 @@ export default class Font extends Asset { _size = 32; /** Font type. - * @type {FontType} + * @type {cc.d3.font.FontType} */ _type = "unknow"; @@ -60,7 +52,7 @@ export default class Font extends Asset { _useKerning = false; /** - * @type {Glyph[]} + * @type {cc.d3.font.Glyph[]} */ _glyphs = []; @@ -79,9 +71,11 @@ export default class Font extends Asset { } /** - * @return {FontType} + * @return {cc.d3.font.FontType} */ get type() { return this._type; } -} \ No newline at end of file +} + +cc.Font = Font; \ No newline at end of file diff --git a/cocos/3d/assets/index.js b/cocos/3d/assets/index.js index e69de29bb2..73b8ab7d67 100644 --- a/cocos/3d/assets/index.js +++ b/cocos/3d/assets/index.js @@ -0,0 +1,37 @@ +/**************************************************************************** + 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. + ****************************************************************************/ + +import "./animation-clip"; +import "./audio-clip"; +import "./dom-audio-clip"; +import "./effect"; +import "./bitmap-font"; +import "./material"; +import "./mesh"; +import "./opentype-font"; +import "./skeleton"; +import "./sprite"; +import "./web-audio-clip"; +import "./wx-game-audio-clip"; \ No newline at end of file diff --git a/cocos/3d/assets/joints.js b/cocos/3d/assets/joints.js deleted file mode 100644 index 6d66faa79a..0000000000 --- a/cocos/3d/assets/joints.js +++ /dev/null @@ -1,21 +0,0 @@ -import Asset from './asset'; -import gltfUtils from '../loaders/utils/gltf-utils'; -import Skeleton from '../framework/skeleton'; - -export default class Joints extends Asset { - constructor() { - super(); - - this._nodes = null; - } - - instantiate() { - let joints = gltfUtils.createNodes(this._nodes); - - // create skeleton - let skeleton = new Skeleton(); - skeleton.setRoot(joints[0]); - - return skeleton; - } -} \ No newline at end of file diff --git a/cocos/3d/assets/material.js b/cocos/3d/assets/material.js index a805bfe150..0e45ae0a06 100644 --- a/cocos/3d/assets/material.js +++ b/cocos/3d/assets/material.js @@ -27,7 +27,7 @@ import { _decorator } from "../../core/data/index"; const { ccclass, property } = _decorator; import Asset from "../../assets/CCAsset"; import Texture from './texture'; -import renderer from '../renderer'; +import renderer from "../../renderer"; import { vec2, vec3, vec4, color3, color4 } from '../../core/vmath/index'; import Effect from "./effect"; @@ -50,12 +50,13 @@ export default class Material extends Asset { _props = {}; /** - * @type {renderer.effect} + * @type {cc.renderer.Effect} */ _effecInst = null; _updateEffectInst() { let techNum = this._effect.techniques.length; + /** @type {cc.renderer.Technique[]} */ let techniques = new Array(techNum); let props = {}; @@ -93,6 +94,7 @@ export default class Material extends Asset { } let passNum = tech.passes.length; + /** @type {cc.renderer.Pass[]} */ let passes = new Array(passNum); for (let k = 0; k < passNum; ++k) { let pass = tech.passes[k]; @@ -148,7 +150,7 @@ export default class Material extends Asset { } /** - * @return {renderer.effect} + * @return {cc.renderer.Effect} */ get effectInst() { return this._effectInst; @@ -197,6 +199,8 @@ export default class Material extends Asset { destroy() { // TODO: what should we do here ??? - super.destroy(); + return super.destroy(); } -} \ No newline at end of file +} + +cc.Material = Material; \ No newline at end of file diff --git a/cocos/3d/assets/mesh.js b/cocos/3d/assets/mesh.js index eb626dcee0..ec82a24d8c 100644 --- a/cocos/3d/assets/mesh.js +++ b/cocos/3d/assets/mesh.js @@ -22,76 +22,72 @@ 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, property } = _decorator; -import { GLTFAsset } from "../../assets/CCGLTFAsset"; import Asset from "../../assets/CCAsset"; -import { default as GLTFUtils } from "./utils/gltf-utils"; -/** @typedef {import("../../core/vmath/index").vec3} vec3 */ + +/** + * @interface + */ +@ccclass +export class MeshResource { + /** + * + * @param {Mesh} mesh + */ + flush(mesh) { + + } +} @ccclass export default class Mesh extends Asset { /** - * @type {GLTFAsset} + * @type {MeshResource} */ - @property(GLTFAsset) - _gltfAsset; + @property(MeshResource) + _resource = null; - /** - * @type {number} - */ - @property(Number) - _gltfIndex = -1; + get resource() { + return this._resource; + } - /** - * @type {renderer.InputAssemblers} - */ - _subMeshes = null; + set resource(value) { + this._resource = value; + this.update(); + } - /** - * @type {{jointIndices, bindposes}} - */ - _skinning = null; + constructor() { + super(); - /** - * @type {vec3} - */ - _minPos = null; + /** + * @type {cc.renderer.InputAssembler[]} + */ + this._subMeshes = null; - /** - * @type {vec3} - */ - _maxPos = null; - - /** - * !#en - * Gets the native data of this mesh. - * @return {GLTFAsset | Null} The native data, or null if this is an empty mesh. - */ - getNativeAsset() { - return this._gltfAsset; + /** + * @type {cc.d3.asset.MeshSkinning} + */ + this._skinning = null; + + /** + * @type {cc.core.math.vec3} + */ + this._minPos = null; + + /** + * @type {cc.core.math.vec3} + */ + this._maxPos = null; } /** - * !#en - * Gets the index into the native data of this mesh. - * @return {number} The index into the native data. + * */ - getNativeAssetIndex() { - return this._gltfIndex; - } - - /** - * !#en - * Sets the native data of this mesh. - * @param {GLTFAsset} gltfAsset The GLTF asset. - * @param {number} gltfIndex An index to the GLTF meshes. - */ - setNative(gltfAsset, gltfIndex) { - this._gltfAsset = gltfAsset; - this._gltfIndex = gltfIndex; - this._update(); + flush() { + this._resource.flush(this); } /** @@ -149,19 +145,6 @@ export default class Mesh extends Asset { // } // } // } +} - _update(app) { - if (!this._gltfAsset) { - return; - } - const result = GLTFUtils.createMesh(app, this._gltfAsset, this._gltfIndex); - if (!result) { - return; - } - this.name = result.name; - this._subMeshes = result.subMeshes; - this._minPos = result.minPos; - this._maxPos = result.maxPos; - this._skinning = result.skinning; - } -} \ No newline at end of file +cc.Mesh = Mesh; \ No newline at end of file diff --git a/cocos/3d/assets/opentype-font.js b/cocos/3d/assets/opentype-font.js index 322d5b5eff..db1d9f44a0 100644 --- a/cocos/3d/assets/opentype-font.js +++ b/cocos/3d/assets/opentype-font.js @@ -30,10 +30,6 @@ import vec2 from "../../core/vmath/vec2"; import ShelfPack from "./utils/shelf-pack"; import Texture2D from "./texture-2d"; -/** - * @typedef {{id: number, x: number, y: number, width: number, height: number, xoffset: number, yoffset: number, xadvance: number, uvs: vec2[]}} OpentypeFontGlyph - */ - @ccclass export default class OpentypeFont extends Font { /** @@ -73,7 +69,7 @@ export default class OpentypeFont extends Font { _fontAtlas; /** - * @type {OpentypeFontGlyph} + * @type {cc.d3.font.OpentypeFontGlyph} */ _defaultGlyph = { id: 32, // space charCode @@ -269,4 +265,6 @@ export default class OpentypeFont extends Font { this._fontAtlas.commit(); } } -} \ No newline at end of file +} + +cc.OpentypeFont = OpentypeFont; \ No newline at end of file diff --git a/cocos/3d/assets/prefab.js b/cocos/3d/assets/prefab.js deleted file mode 100644 index 82f0b423d0..0000000000 --- a/cocos/3d/assets/prefab.js +++ /dev/null @@ -1,26 +0,0 @@ -import Asset from './asset'; -import ecsUtils from '../loaders/utils/ecs-utils'; - -export default class Prefab extends Asset { - constructor() { - super(); - - this._app = null; - this._json = null; - this._assets = null; - } - - unload() { - if (!this._loaded) { - return; - } - - // TODO: unload all assets referenced by the prefab ?? - - super.unload(); - } - - instantiate(modifications, level = null) { - return ecsUtils.createPrefab(this._app, this._json, modifications, level); - } -} \ No newline at end of file diff --git a/cocos/3d/assets/skeleton.js b/cocos/3d/assets/skeleton.js new file mode 100644 index 0000000000..4673e0f283 --- /dev/null +++ b/cocos/3d/assets/skeleton.js @@ -0,0 +1,49 @@ +/**************************************************************************** + 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, instantiate } from "../../core/data"; +const { ccclass, property } = _decorator; +import Asset from "../../assets/CCAsset"; +import SkeletonInstance from '../framework/skeleton-instance'; +import { Node } from "../../scene-graph"; + +@ccclass +export default class Skeleton extends Asset { + /** + * @type {Node} + */ + @property(Node) + _rootNode = null; + + instantiate() { + let rootNode = instantiate(this._rootNode, undefined); + let skeleton = new SkeletonInstance(); + skeleton.setRoot(rootNode); + return skeleton; + } +} + +cc.Skeleton = Skeleton; \ No newline at end of file diff --git a/cocos/3d/assets/sprite.js b/cocos/3d/assets/sprite.js index 6b7bcdd880..6e1dbcb2d3 100644 --- a/cocos/3d/assets/sprite.js +++ b/cocos/3d/assets/sprite.js @@ -198,4 +198,6 @@ export default class Sprite extends Asset { vec3.transformMat4(this._uvs[i], this._uvs[i], _textureMatrix); } } -} \ No newline at end of file +} + +cc.Sprite = Sprite; \ No newline at end of file diff --git a/cocos/3d/assets/texture-2d.js b/cocos/3d/assets/texture-2d.js index e30fe977cc..98b2f5ecfb 100644 --- a/cocos/3d/assets/texture-2d.js +++ b/cocos/3d/assets/texture-2d.js @@ -25,7 +25,7 @@ // @ts-check import { _decorator } from "../../core/data/index"; const { ccclass } = _decorator; -import { Texture } from './texture'; +import Texture from './texture'; import gfx from '../gfx'; import { gfxFilters, gfxWraps, gfxTextureFmts } from '../misc/mappings'; @@ -102,13 +102,9 @@ export default class Texture2D extends Texture { this._premultiplyAlpha = false; } - unload() { - if (!this._loaded) { - return; - } - + destroy() { this._texture.destroy(); - super.unload(); + return super.destroy(); } setImage(level, img) { diff --git a/cocos/3d/assets/texture-cube.js b/cocos/3d/assets/texture-cube.js index 2376867107..1d1e7ce597 100644 --- a/cocos/3d/assets/texture-cube.js +++ b/cocos/3d/assets/texture-cube.js @@ -25,7 +25,7 @@ // @ts-check import { _decorator } from "../../core/data/index"; const { ccclass } = _decorator; -import { Texture } from './texture'; +import Texture from './texture'; import gfx from '../gfx'; import { gfxFilters, gfxWraps, gfxTextureFmts } from '../misc/mappings'; @@ -77,13 +77,9 @@ export class TextureCube extends Texture { this._mipFilter = 'linear'; } - unload() { - if (!this._loaded) { - return; - } - - this._texture.destroy(); - super.unload(); + destroy() { + this._texture.destory(); + return super.destroy(); } setImages(imgs) { diff --git a/cocos/3d/assets/texture-sprite.js b/cocos/3d/assets/texture-sprite.js index 3d1fe39fd6..66fbe46c64 100644 --- a/cocos/3d/assets/texture-sprite.js +++ b/cocos/3d/assets/texture-sprite.js @@ -48,6 +48,6 @@ export default class TextureSprite extends Texture2D { for (let name in this._sprites) { this._sprites[name].destroy(); } - super.destroy(); + return super.destroy(); } } diff --git a/cocos/3d/assets/texture.js b/cocos/3d/assets/texture.js index 9404347f6b..cd016035bf 100644 --- a/cocos/3d/assets/texture.js +++ b/cocos/3d/assets/texture.js @@ -28,7 +28,7 @@ const { ccclass } = _decorator; import Asset from "../../assets/CCAsset"; @ccclass -export class Texture extends Asset { +export default class Texture extends Asset { constructor(device) { super(); diff --git a/cocos/3d/assets/utils/gltf-utils.js b/cocos/3d/assets/utils/gltf-utils.js index 2e87faf84e..ad82e17958 100644 --- a/cocos/3d/assets/utils/gltf-utils.js +++ b/cocos/3d/assets/utils/gltf-utils.js @@ -1,10 +1,307 @@ -/* @flow */ -import gfx from '../../gfx'; -import renderer from '../../renderer'; -import { vec3, quat, mat4 } from '../../vmath'; -import { Node } from '../../scene-graph'; -import AnimationClip from '../../assets/animation-clip'; -import Joints from '../../assets/joints'; +// @ts-check +import { _decorator } from "../../../core/data/index"; +const { ccclass, property } = _decorator; +import gfx from "../../../renderer/gfx"; +import renderer from "../../../renderer"; +import { vec3, quat, mat4 } from "../../../core/vmath"; +import Node from "../../../scene-graph/node"; +import { MeshResource } from "../mesh"; +import { AnimationResource } from "../animation-clip"; +import GLTFAsset from "../../../assets/CCGLTFAsset"; +import Skeleton from "../skeleton"; + +/** + * @typedef {import("../../../../types/glTF/glTF").GlTf} GLTFFormat + * @typedef {import("../../../../types/glTF/glTF").Node} GLTFNode + */ + +@ccclass +export class GltfMeshResource extends MeshResource { + /** + * @type {GLTFAsset} + */ + @property(GLTFAsset) + gltfAsset; + + /** + * @type {number} + */ + @property(Number) + gltfIndex = -1; + + /** + * Update mesh asset's content to the GlTf asset's specified mesh. + * The GlTf mesh is conditionally supported. + * All attributes of a GlTf primitive shall be recognized; + * they shall have same amount of elements, and + * shall be tighly interleaved, with no extra head data, in same buffer view. + * @param {cc.d3.asset.Mesh} mesh + */ + flush(mesh) { + if (!this.gltfAsset) { + return; + } + + /** @type GLTFFormat */ + const gltf = this.gltfAsset.description.json; + if (this.gltfIndex >= gltf.meshes.length) { + return; + } + + const gltfMesh = gltf.meshes[this.gltfIndex]; + const gltfAccessors = gltf.accessors; + + mesh.name = gltfMesh.name; + + mesh._subMeshes = new Array(gltfMesh.primitives.length); + for (let i = 0; i < gltfMesh.primitives.length; ++i) { + let primitive = gltfMesh.primitives[i]; + + let iVertexBufferView = -1; + let vertexCount = -1; + let expectedOffset = 0; + const vfmt = []; + for (const gltfAttribName in primitive.attributes) { + const gfxAttribName = _gltfAttribMap[gltfAttribName]; + if (gfxAttribName === undefined) { + console.error(`Found unacceptable GlTf attribute ${gltfAttribName}.`); + return; + } + const gltfAccessor = gltfAccessors[primitive.attributes[gltfAttribName]]; + + if (iVertexBufferView !== -1 && iVertexBufferView !== gltfAccessor.bufferView) { + console.error(`All attributes of one GlTf primitive should reference to the same buffer view.`); + return; + } else { + iVertexBufferView = gltfAccessor.bufferView; + } + + if (vertexCount !== -1 && vertexCount !== gltfAccessor.count) { + console.error(`All attributes of one GlTf primitive should have same amount of elements.`); + return; + } else { + vertexCount = gltfAccessor.count; + } + + if (gltfAccessor.byteOffset !== expectedOffset) { + console.error(`Attributes of one GlTf primitive should be interleaved.`); + return; + } + expectedOffset += gltfAccessor.byteOffset; + + if (gltfAttribName === "POSITION") { + mesh._minPos = vec3.create(gltfAccessor.min[0], gltfAccessor.min[1], gltfAccessor.min[2]); + mesh._maxPos = vec3.create(gltfAccessor.max[0], gltfAccessor.max[1], gltfAccessor.max[2]); + } + vfmt.push({ name: gfxAttribName, type: gltfAccessor.componentType, num: _type2size[gltfAccessor.type] }); + } + + const vertexBufferView = gltf.bufferViews[iVertexBufferView]; + if (expectedOffset !== vertexBufferView.byteStride) { + console.warn(`There is extra data resides in GlTf primitive's buffer.`); + return; + } + + const vbData = new Uint8Array(this.gltfAsset.buffers[vertexBufferView.buffer].data, vertexBufferView.byteOffset, vertexBufferView.byteLength); + const vb = new gfx.VertexBuffer( + app.device, + new gfx.VertexFormat(vfmt), + gfx.USAGE_STATIC, + vbData, + vertexCount + ); + + let ib = null; + if (primitive.indices !== undefined) { + let ibAcc = gltfAccessors[primitive.indices]; + let ibView = gltf.bufferViews[ibAcc.bufferView]; + let ibData = new Uint8Array(this.gltfAsset.buffers[ibView.buffer].data, ibView.byteOffset, ibView.byteLength); + + ib = new gfx.IndexBuffer( + app.device, + ibAcc.componentType, + gfx.USAGE_STATIC, + ibData, + ibAcc.count + ); + } + + mesh._subMeshes[i] = new renderer.InputAssembler(vb, ib); + } + + // create skinning if we found + if (gltf.skins !== undefined) { + const iSkin = gltf.skins.findIndex(skin => skin.name === gltfMesh.name); + if (iSkin >= 0) { + mesh._skinning = this.createSkinning(iSkin); + } + } + } + + /** + * @param {number} index + * @return {cc.d3.asset.MeshSkinning} + */ + createSkinning(index) { + /** @type GLTFFormat */ + const gltf = this.gltfAsset.description.json; + if (index >= gltf.skins.length) { + return null; + } + + let gltfSkin = gltf.skins[index]; + + // extract bindposes mat4 data + let accessor = gltf.accessors[gltfSkin.inverseBindMatrices]; + let bufView = gltf.bufferViews[accessor.bufferView]; + let data = new Float32Array(this.gltfAsset.buffers[bufView.buffer].data, bufView.byteOffset + accessor.byteOffset, accessor.count * 16); + let bindposes = new Array(accessor.count); + + for (let i = 0; i < accessor.count; ++i) { + bindposes[i] = mat4.create( + data[16 * i + 0], data[16 * i + 1], data[16 * i + 2], data[16 * i + 3], + data[16 * i + 4], data[16 * i + 5], data[16 * i + 6], data[16 * i + 7], + data[16 * i + 8], data[16 * i + 9], data[16 * i + 10], data[16 * i + 11], + data[16 * i + 12], data[16 * i + 13], data[16 * i + 14], data[16 * i + 15] + ); + } + + return { + jointIndices: gltfSkin.joints, + bindposes, + }; + } +} + +@ccclass +export class GltfAnimationResource extends AnimationResource { + /** + * @type {GLTFAsset} + */ + @property(GLTFAsset) + gltfAsset; + + /** + * @type {number} + */ + @property(Number) + gltfIndex = -1; + + /** + * + * @param {cc.d3.asset.AnimationClip} animationClip + */ + flush(animationClip) { + if (!this.gltfAsset) { + return; + } + + /** @type GLTFFormat */ + const gltf = this.gltfAsset.description.json; + if (this.gltfIndex >= gltf.animations.length) { + return; + } + + const bin = this.gltfAsset.buffers[0].data; + let gltfAnimation = gltf.animations[this.gltfIndex]; + /** @type {cc.d3.animation.Frame[]} */ + let framesList = []; + let maxLength = -1; + + for (let i = 0; i < gltfAnimation.channels.length; ++i) { + let gltfChannel = gltfAnimation.channels[i]; + let inputAcc = gltf.accessors[gltfChannel.input]; + + // find frames by input name + /** @type {cc.d3.animation.Frame} */ + let frames; + for (let j = 0; j < framesList.length; ++j) { + if (framesList[j].name === inputAcc.name) { + frames = framesList[j]; + break; + } + } + + // if not found, create one + if (!frames) { + let inArray = _createArray(gltf, bin, gltfChannel.input); + /** @type {number[]} */ + let inputs = new Array(inArray.length); + for (let i = 0; i < inArray.length; ++i) { + let t = inArray[i]; + inputs[i] = t; + + if (maxLength < t) { + maxLength = t; + } + } + + frames = { + name: inputAcc.name, + times: inputs, + joints: [], + }; + framesList.push(frames); + } + + // find output frames by node id + /** @type {cc.d3.animation.JointFrame} */ + let jointFrames; + for (let j = 0; j < frames.joints.length; ++j) { + if (frames.joints[j].id === gltfChannel.node) { + jointFrames = frames.joints[j]; + break; + } + } + + // if not found, create one + if (!jointFrames) { + jointFrames = { + id: gltfChannel.node + }; + frames.joints.push(jointFrames); + } + + let outArray = _createArray(gltf, bin, gltfChannel.output); + if (gltfChannel.path === 'translation') { + let cnt = outArray.length / 3; + jointFrames.translations = new Array(cnt); + for (let i = 0; i < cnt; ++i) { + jointFrames.translations[i] = vec3.create( + outArray[3 * i + 0], + outArray[3 * i + 1], + outArray[3 * i + 2] + ); + } + } else if (gltfChannel.path === 'rotation') { + let cnt = outArray.length / 4; + jointFrames.rotations = new Array(cnt); + for (let i = 0; i < cnt; ++i) { + jointFrames.rotations[i] = quat.create( + outArray[4 * i + 0], + outArray[4 * i + 1], + outArray[4 * i + 2], + outArray[4 * i + 3] + ); + } + } else if (gltfChannel.path === 'scale') { + let cnt = outArray.length / 3; + jointFrames.scales = new Array(cnt); + for (let i = 0; i < cnt; ++i) { + jointFrames.scales[i] = vec3.create( + outArray[3 * i + 0], + outArray[3 * i + 1], + outArray[3 * i + 2] + ); + } + } + } + + animationClip.name = gltfAnimation.name; + animationClip._framesList = framesList; + animationClip._length = maxLength; + } +} const _type2size = { SCALAR: 1, @@ -26,12 +323,24 @@ const _compType2Array = { 5126: Float32Array, }; +const _gltfAttribMap = { + NORMAL: gfx.ATTR_NORMAL, + TANGENT: gfx.ATTR_TANGENT, + COLOR_0: gfx.ATTR_COLOR0, + TEXCOORD_0: gfx.ATTR_UV0, + TEXCOORD_1: gfx.ATTR_UV1, + TEXCOORD_2: gfx.ATTR_UV2, + TEXCOORD_3: gfx.ATTR_UV3, + JOINTS_0: gfx.ATTR_JOINTS, + WEIGHTS_0: gfx.ATTR_WEIGHTS +}; + /** - * @param {object} gltf + * @param {GLTFFormat} gltf * @param {ArrayBuffer} bin * @param {number} accessorID */ -function createArray(gltf, bin, accessorID) { +function _createArray(gltf, bin, accessorID) { let acc = gltf.accessors[accessorID]; let bufView = gltf.bufferViews[acc.bufferView]; @@ -43,40 +352,13 @@ function createArray(gltf, bin, accessorID) { } /** - * @param {Array} gltfNodes + * @param {GLTFNode[]} gltfNodes */ -function createNodes(gltfNodes) { +export function createNodes(gltfNodes) { let nodes = new Array(gltfNodes.length); for (let i = 0; i < gltfNodes.length; ++i) { - let gltfNode = gltfNodes[i]; - let node = new Node(gltfNode.name); - - if (gltfNode.translation) { - node.setLocalPos( - gltfNode.translation[0], - gltfNode.translation[1], - gltfNode.translation[2] - ); - } - - if (gltfNode.rotation) { - node.setLocalRot( - gltfNode.rotation[0], - gltfNode.rotation[1], - gltfNode.rotation[2], - gltfNode.rotation[3] - ); - } - - if (gltfNode.scale) { - node.setLocalScale( - gltfNode.scale[0], - gltfNode.scale[1], - gltfNode.scale[2] - ); - } - + let node = _createNode(gltfNodes[i]); nodes[i] = node; } @@ -96,10 +378,10 @@ function createNodes(gltfNodes) { } /** - * @param {App} app + * @param {*} app * @param {Array} gltfNodes */ -function createEntities(app, gltfNodes) { +export function createEntities(app, gltfNodes) { let nodes = new Array(gltfNodes.length); for (let i = 0; i < gltfNodes.length; ++i) { @@ -150,293 +432,72 @@ function createEntities(app, gltfNodes) { } /** - * @param {App} app * @param {GLTFAsset} gltfAsset * @param {number} index - * @return {{ name: string, subMeshes: any[], minPos: vec3, maxPos: vec3, skinning: any }} */ -function createMesh(app, gltfAsset, index) { - /** @type {import("../../../../types/glTF/glTF").GlTf} */ - const gltf = gltfAsset._description; - if (index >= gltf.meshes.length) { - return null; +export function createSkeleton(gltfAsset, index) { + /** @type GLTFFormat */ + const gltf = gltfAsset.description.json; + + if (!gltf.skins || index >= gltf.skins.length) { + return; } - const gltfMesh = gltf.meshes[index]; - const bin = gltfAsset.buffers[0].data; - const accessors = gltf.accessors; - const attributes = gltfMesh.primitives[0].attributes; - let vbView = null; - - // create mesh-asset - const name = gltfMesh.name; - const subMeshes = new Array(gltfMesh.primitives.length); - let minPos = null; - let maxPos = null; - - // create vertex-format - let vfmt = []; - let vcount = 0; - - if (attributes.POSITION !== undefined) { - let acc = accessors[attributes.POSITION]; - vfmt.push({ name: gfx.ATTR_POSITION, type: acc.componentType, num: _type2size[acc.type] }); - - vcount = acc.count; - vbView = gltf.bufferViews[acc.bufferView]; - - minPos = vec3.create(acc.min[0], acc.min[1], acc.min[2]); - maxPos = vec3.create(acc.max[0], acc.max[1], acc.max[2]); - } - - if (attributes.NORMAL !== undefined) { - let acc = accessors[attributes.NORMAL]; - vfmt.push({ name: gfx.ATTR_NORMAL, type: acc.componentType, num: _type2size[acc.type] }); - } - - if (attributes.TANGENT !== undefined) { - let acc = accessors[attributes.TANGENT]; - vfmt.push({ name: gfx.ATTR_TANGENT, type: acc.componentType, num: _type2size[acc.type] }); - } - - if (attributes.COLOR_0 !== undefined) { - let acc = accessors[attributes.COLOR_0]; - vfmt.push({ name: gfx.ATTR_COLOR0, type: acc.componentType, num: _type2size[acc.type] }); - } - - if (attributes.TEXCOORD_0 !== undefined) { - let acc = accessors[attributes.TEXCOORD_0]; - vfmt.push({ name: gfx.ATTR_UV0, type: acc.componentType, num: _type2size[acc.type] }); - } - - if (attributes.TEXCOORD_1 !== undefined) { - let acc = accessors[attributes.TEXCOORD_1]; - vfmt.push({ name: gfx.ATTR_UV1, type: acc.componentType, num: _type2size[acc.type] }); - } - - if (attributes.TEXCOORD_2 !== undefined) { - let acc = accessors[attributes.TEXCOORD_2]; - vfmt.push({ name: gfx.ATTR_UV2, type: acc.componentType, num: _type2size[acc.type] }); - } - - if (attributes.TEXCOORD_3 !== undefined) { - let acc = accessors[attributes.TEXCOORD_3]; - vfmt.push({ name: gfx.ATTR_UV3, type: acc.componentType, num: _type2size[acc.type] }); - } - - if (attributes.JOINTS_0 !== undefined) { - let acc = accessors[attributes.JOINTS_0]; - vfmt.push({ name: gfx.ATTR_JOINTS, type: acc.componentType, num: _type2size[acc.type] }); - } - - if (attributes.WEIGHTS_0 !== undefined) { - let acc = accessors[attributes.WEIGHTS_0]; - vfmt.push({ name: gfx.ATTR_WEIGHTS, type: acc.componentType, num: _type2size[acc.type] }); - } - - // create vertex-buffer - let vbData = new Uint8Array(bin, vbView.byteOffset, vbView.byteLength); - let vb = new gfx.VertexBuffer( - app.device, - new gfx.VertexFormat(vfmt), - gfx.USAGE_STATIC, - vbData, - vcount - ); - - // create index-buffer - for (let i = 0; i < gltfMesh.primitives.length; ++i) { - let primitive = gltfMesh.primitives[i]; - let ib = null; - - if (primitive.indices !== undefined) { - let ibAcc = accessors[primitive.indices]; - let ibView = gltf.bufferViews[ibAcc.bufferView]; - let ibData = new Uint8Array(bin, ibView.byteOffset, ibView.byteLength); - - ib = new gfx.IndexBuffer( - app.device, - ibAcc.componentType, - gfx.USAGE_STATIC, - ibData, - ibAcc.count - ); - } - - subMeshes[i] = new renderer.InputAssembler(vb, ib); - } - - // create skinning if we found - let skinning = null; - if (gltf.skins) { - for (let i = 0; i < gltf.skins.length; ++i) { - if (gltf.skins[i].name === gltfMesh.name) { - skinning = createSkinning(gltf, bin, i); - } - } - } - - return { name, subMeshes, minPos, maxPos, skinning}; + const gltfSkin = gltf.skins[index]; + const skeleton = new Skeleton(); + skeleton.name = gltfSkin.name; + const rootNode = _createNodeRecursive(gltf, gltfSkin.skeleton); + skeleton._rootNode = rootNode; + return skeleton; } /** - * @param {Object} gltf - * @param {ArrayBuffer} bin - * @param {number} index + * + * @param {GLTFFormat} gltf + * @param {number} index */ -function createSkinning(gltf, bin, index) { - if (index >= gltf.skins.length) { - return null; +function _createNodeRecursive(gltf, index) { + let gltfNode = gltf.nodes[index]; + let node = _createNode(gltfNode); + if (gltfNode.children) { + gltfNode.children.forEach((childGLTFNode) => { + let childNode = _createNodeRecursive(gltf, childGLTFNode); + node.addChild(childNode); + }); } + return node; +} - let gltfSkin = gltf.skins[index]; +/** + * + * @param {GLTFNode} gltfNode + */ +function _createNode(gltfNode) { + let node = new Node(gltfNode.name); - // extract bindposes mat4 data - let accessor = gltf.accessors[gltfSkin.inverseBindMatrices]; - let bufView = gltf.bufferViews[accessor.bufferView]; - let data = new Float32Array(bin, bufView.byteOffset + accessor.byteOffset, accessor.count * 16); - let bindposes = new Array(accessor.count); - - for (let i = 0; i < accessor.count; ++i) { - bindposes[i] = mat4.create( - data[16 * i + 0], data[16 * i + 1], data[16 * i + 2], data[16 * i + 3], - data[16 * i + 4], data[16 * i + 5], data[16 * i + 6], data[16 * i + 7], - data[16 * i + 8], data[16 * i + 9], data[16 * i + 10], data[16 * i + 11], - data[16 * i + 12], data[16 * i + 13], data[16 * i + 14], data[16 * i + 15] + if (gltfNode.translation) { + node.setLocalPos( + gltfNode.translation[0], + gltfNode.translation[1], + gltfNode.translation[2] ); } - return { - jointIndices: gltfSkin.joints, - bindposes, - }; -} - -/** - * @param {Object} gltf - * @param {ArrayBuffer} bin - * @param {number} index - */ -function createAnimationClip(gltf, bin, index) { - if (index >= gltf.animations.length) { - return null; + if (gltfNode.rotation) { + node.setLocalRot( + gltfNode.rotation[0], + gltfNode.rotation[1], + gltfNode.rotation[2], + gltfNode.rotation[3] + ); } - let gltfAnimation = gltf.animations[index]; - let framesList = []; - let maxLength = -1; - - for (let i = 0; i < gltfAnimation.channels.length; ++i) { - let gltfChannel = gltfAnimation.channels[i]; - let inputAcc = gltf.accessors[gltfChannel.input]; - - // find frames by input name - let frames; - for (let j = 0; j < framesList.length; ++j) { - if (framesList[j].name === inputAcc.name) { - frames = framesList[j]; - break; - } - } - - // if not found, create one - if (!frames) { - let inArray = createArray(gltf, bin, gltfChannel.input); - let inputs = new Array(inArray.length); - for (let i = 0; i < inArray.length; ++i) { - let t = inArray[i]; - inputs[i] = t; - - if (maxLength < t) { - maxLength = t; - } - } - - frames = { - name: inputAcc.name, - times: inputs, - joints: [], - }; - framesList.push(frames); - } - - // find output frames by node id - let jointFrames; - for (let j = 0; j < frames.joints.length; ++j) { - if (frames.joints[j].id === gltfChannel.node) { - jointFrames = frames.joints[j]; - break; - } - } - - // if not found, create one - if (!jointFrames) { - jointFrames = { - id: gltfChannel.node - }; - frames.joints.push(jointFrames); - } - - let outArray = createArray(gltf, bin, gltfChannel.output); - if (gltfChannel.path === 'translation') { - let cnt = outArray.length / 3; - jointFrames.translations = new Array(cnt); - for (let i = 0; i < cnt; ++i) { - jointFrames.translations[i] = vec3.create( - outArray[3 * i + 0], - outArray[3 * i + 1], - outArray[3 * i + 2] - ); - } - } else if (gltfChannel.path === 'rotation') { - let cnt = outArray.length / 4; - jointFrames.rotations = new Array(cnt); - for (let i = 0; i < cnt; ++i) { - jointFrames.rotations[i] = quat.create( - outArray[4 * i + 0], - outArray[4 * i + 1], - outArray[4 * i + 2], - outArray[4 * i + 3] - ); - } - } else if (gltfChannel.path === 'scale') { - let cnt = outArray.length / 3; - jointFrames.scales = new Array(cnt); - for (let i = 0; i < cnt; ++i) { - jointFrames.scales[i] = vec3.create( - outArray[3 * i + 0], - outArray[3 * i + 1], - outArray[3 * i + 2] - ); - } - } + if (gltfNode.scale) { + node.setLocalScale( + gltfNode.scale[0], + gltfNode.scale[1], + gltfNode.scale[2] + ); } - - let animClip = new AnimationClip(); - animClip._name = gltfAnimation.name; - animClip._framesList = framesList; - animClip._length = maxLength; - - return animClip; -} - -/** - * @param {object} gltf - */ -function createJoints(gltf) { - let joints = new Joints(); - joints._name = gltf.joints[0].name; - joints._nodes = gltf.joints; - - return joints; -} - -export default { - createArray, - createNodes, - createEntities, - createMesh, - createSkinning, - createAnimationClip, - createJoints, -}; \ No newline at end of file + return node; +} \ No newline at end of file diff --git a/cocos/3d/assets/web-audio-clip.js b/cocos/3d/assets/web-audio-clip.js index c168aaba71..5e0ae8dc72 100644 --- a/cocos/3d/assets/web-audio-clip.js +++ b/cocos/3d/assets/web-audio-clip.js @@ -50,7 +50,7 @@ export default class WebAudioClip extends AudioClip { _loop; /** - * @type {WindowTimers} + * @type {number} */ _currentTimer; @@ -190,4 +190,6 @@ export default class WebAudioClip extends AudioClip { getLoop() { return this._loop; } -} \ No newline at end of file +} + +cc.WebAudioClip = WebAudioClip; \ No newline at end of file diff --git a/cocos/3d/assets/wx-game-audio-clip.js b/cocos/3d/assets/wx-game-audio-clip.js index 0d0be1354b..c49150dd8c 100644 --- a/cocos/3d/assets/wx-game-audio-clip.js +++ b/cocos/3d/assets/wx-game-audio-clip.js @@ -151,4 +151,6 @@ export default class WxGameAudioClip extends AudioClip { this._loop = val; if (this._audio) this._audio.loop = val; } -} \ No newline at end of file +} + +cc.WxGameAudioClip = WxGameAudioClip; \ No newline at end of file diff --git a/cocos/3d/framework/index.js b/cocos/3d/framework/index.js new file mode 100644 index 0000000000..79b7947035 --- /dev/null +++ b/cocos/3d/framework/index.js @@ -0,0 +1,27 @@ +/**************************************************************************** + Copyright (c) 2013-2016 Chukong Technologies Inc. + 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. + ****************************************************************************/ + + \ No newline at end of file diff --git a/cocos/3d/framework/skeleton.js b/cocos/3d/framework/skeleton-instance.js similarity index 76% rename from cocos/3d/framework/skeleton.js rename to cocos/3d/framework/skeleton-instance.js index aae76f4536..d404ac94b1 100644 --- a/cocos/3d/framework/skeleton.js +++ b/cocos/3d/framework/skeleton-instance.js @@ -1,13 +1,33 @@ -import { utils } from '../scene-graph'; -import { vec3, quat, mat4 } from '../vmath'; -/** - * @typedef {import("../../scene-graph/CCNode").default} Joint - */ +// @ts-check +import { vec3, quat, mat4 } from '../../core/vmath'; let _v3_tmp = vec3.create(); let _qt_tmp = quat.create(); -export default class Skeleton { +/** + * + * @param {cc.Node} node + * @param {(node: cc.Node)=>void} fx + */ +function _walk(node, fx) { + fx(node); + for (let i = 0; i < node.childrenCount; ++i) { + _walk(node.children[i], fx); + } +} + +/** + * + * @param {cc.Node} node + * @param {cc.Node[]} result + */ +function _flat(node, result) { + _walk(node, (childNode) => { + result.push(childNode); + }); +} + +export default class SkeletonInstance { constructor() { this._root = null; this._joints = null; @@ -16,13 +36,14 @@ export default class Skeleton { setRoot(root) { /** - * @type {Joint} + * @type {cc.Node} */ this._root = root; /** - * @type {Joint[]} + * @type {cc.Node[]} */ - this._joints = utils.flat(this._root); + this._joints = []; + _flat(this._root, this._joints); /** * @type {mat4[]} */ @@ -72,8 +93,8 @@ export default class Skeleton { } clone() { - let newSkeleton = new Skeleton(); - newSkeleton.setRoot(utils.deepClone(this._root)); + let newSkeleton = new SkeletonInstance(); + newSkeleton.setRoot(cc.instantiate(this._root)); return newSkeleton; } @@ -85,6 +106,7 @@ export default class Skeleton { export class SkeletonMask { constructor(skeleton) { + /** @type {SkeletonInstance} */ this._skeleton = skeleton; this._isMaskedArray = new Array(skeleton._joints.length); @@ -103,7 +125,7 @@ export class SkeletonMask { } this._isMaskedArray[jointIndex] = masked; - utils.walk(this._skeleton._joints[jointIndex], (joint) => { + _walk(this._skeleton._joints[0], joint => { let idx = this._skeleton._joints.indexOf(joint); if (idx >= 0) { this._isMaskedArray[idx] = masked; diff --git a/cocos/3d/index.js b/cocos/3d/index.js index e69de29bb2..c2974cbee7 100644 --- a/cocos/3d/index.js +++ b/cocos/3d/index.js @@ -0,0 +1,33 @@ +/**************************************************************************** + 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. + ****************************************************************************/ + +import "./assets"; +import "./builtin"; +import "./debugger"; +import "./framework"; +import "./geom-utils"; +import "./memop"; +import "./misc"; +import "./primitive"; \ No newline at end of file diff --git a/cocos/3d/primitive/box.js b/cocos/3d/primitive/box.js index 90589f604f..e9aa1b0873 100644 --- a/cocos/3d/primitive/box.js +++ b/cocos/3d/primitive/box.js @@ -1,6 +1,6 @@ 'use strict'; -import { vec3 } from '../vmath'; +import { vec3 } from '../../core/vmath'; let temp1 = vec3.create(0, 0, 0); let temp2 = vec3.create(0, 0, 0); diff --git a/cocos/3d/primitive/capsule.js b/cocos/3d/primitive/capsule.js index 6dbf28b79b..6755f095c1 100644 --- a/cocos/3d/primitive/capsule.js +++ b/cocos/3d/primitive/capsule.js @@ -1,6 +1,6 @@ 'use strict'; -import { vec3 } from '../vmath'; +import { vec3 } from '../../core/vmath'; let temp1 = vec3.create(0, 0, 0); let temp2 = vec3.create(0, 0, 0); diff --git a/cocos/3d/primitive/cylinder.js b/cocos/3d/primitive/cylinder.js index 009d374794..7d2ff36119 100644 --- a/cocos/3d/primitive/cylinder.js +++ b/cocos/3d/primitive/cylinder.js @@ -1,6 +1,6 @@ 'use strict'; -import { vec3 } from '../vmath'; +import { vec3 } from '../../core/vmath'; let temp1 = vec3.create(0, 0, 0); let temp2 = vec3.create(0, 0, 0); diff --git a/cocos/3d/primitive/plane.js b/cocos/3d/primitive/plane.js index 12dbc0e419..3146619908 100644 --- a/cocos/3d/primitive/plane.js +++ b/cocos/3d/primitive/plane.js @@ -1,6 +1,6 @@ 'use strict'; -import { vec3 } from '../vmath'; +import { vec3 } from '../../core/vmath'; let temp1 = vec3.create(0, 0, 0); let temp2 = vec3.create(0, 0, 0); diff --git a/cocos/3d/primitive/quad.js b/cocos/3d/primitive/quad.js index 5381a3a36a..503702c5a0 100644 --- a/cocos/3d/primitive/quad.js +++ b/cocos/3d/primitive/quad.js @@ -1,6 +1,6 @@ 'use strict'; -import { vec3 } from '../vmath'; +import { vec3 } from '../../core/vmath'; let positions = [ -0.5, -0.5, 0, // bottom-left diff --git a/cocos/3d/primitive/sphere.js b/cocos/3d/primitive/sphere.js index c918208491..8747f1feea 100644 --- a/cocos/3d/primitive/sphere.js +++ b/cocos/3d/primitive/sphere.js @@ -1,6 +1,6 @@ 'use strict'; -import { vec3 } from '../vmath'; +import { vec3 } from '../../core/vmath'; /** * @param {Number} radius diff --git a/cocos/3d/primitive/torus.js b/cocos/3d/primitive/torus.js index 7ce371b11a..4fbad98c6a 100644 --- a/cocos/3d/primitive/torus.js +++ b/cocos/3d/primitive/torus.js @@ -1,6 +1,6 @@ 'use strict'; -import { vec3 } from '../vmath'; +import { vec3 } from '../../core/vmath'; /** * @param {Number} radius * @param {Number} tube diff --git a/cocos/assets/CCGLTFAsset.js b/cocos/assets/CCGLTFAsset.js index ac1f110bb7..7c3913d5f4 100644 --- a/cocos/assets/CCGLTFAsset.js +++ b/cocos/assets/CCGLTFAsset.js @@ -40,7 +40,7 @@ import Asset from "./CCAsset"; * @extends Asset */ @ccclass -export class GLTFAsset extends Asset { +export default class GLTFAsset extends Asset { /** * @type {JsonAsset} */ diff --git a/cocos/renderer/gfx/index-buffer.js b/cocos/renderer/gfx/index-buffer.js index 9c18c6a2c3..a2e711ce45 100644 --- a/cocos/renderer/gfx/index-buffer.js +++ b/cocos/renderer/gfx/index-buffer.js @@ -6,7 +6,7 @@ export default class IndexBuffer { * @param {Device} device * @param {INDEX_FMT_*} format * @param {USAGE_*} usage - * @param {ArrayBuffer} data + * @param {ArrayBuffer | Uint8Array} data * @param {Number} numIndices */ constructor(device, format, usage, data, numIndices) { @@ -66,6 +66,7 @@ export default class IndexBuffer { return; } + /** @type{WebGLRenderingContext} */ let gl = this._device._gl; let glUsage = this._usage; diff --git a/cocos/renderer/gfx/vertex-buffer.js b/cocos/renderer/gfx/vertex-buffer.js index 617131c5cd..5042631389 100644 --- a/cocos/renderer/gfx/vertex-buffer.js +++ b/cocos/renderer/gfx/vertex-buffer.js @@ -4,7 +4,7 @@ export default class VertexBuffer { * @param {Device} device * @param {VertexFormat} format * @param {USAGE_*} usage - * @param {ArrayBuffer} data + * @param {ArrayBuffer | Uint8Array} data * @param {Number} numVertices */ constructor(device, format, usage, data, numVertices) { diff --git a/cocos/scene-graph/node.js b/cocos/scene-graph/node.js index 2b86511ee0..bbf4ba7b6e 100644 --- a/cocos/scene-graph/node.js +++ b/cocos/scene-graph/node.js @@ -134,8 +134,8 @@ export default class Node extends BaseNode { /** * set local position * @param {vec3|number} val the new local position, or the x component of it - * @param {?number} y the y component of the new local position - * @param {?number} z the z component of the new local position + * @param {?number} [y] the y component of the new local position + * @param {?number} [z] the z component of the new local position */ setLocalPos(val, y, z) { if (arguments.length === 1) { @@ -163,9 +163,9 @@ export default class Node extends BaseNode { /** * set local rotation * @param {quat|number} val the new local rotation, or the x component of it - * @param {?number} y the y component of the new local rotation - * @param {?number} z the z component of the new local rotation - * @param {?number} w the w component of the new local rotation + * @param {?number} [y] the y component of the new local rotation + * @param {?number} [z] the z component of the new local rotation + * @param {?number} [w] the w component of the new local rotation */ setLocalRot(val, y, z, w) { if (arguments.length === 1) { @@ -205,8 +205,8 @@ export default class Node extends BaseNode { /** * set local scale * @param {vec3|number} val the new local scale, or the x component of it - * @param {?number} y the y component of the new local scale - * @param {?number} z the z component of the new local scale + * @param {?number} [y] the y component of the new local scale + * @param {?number} [z] the z component of the new local scale */ setLocalScale(val, y, z) { if (arguments.length === 1) {