From 2c366812cc7236ca6fdd0a32c37f4842e7aadf7b Mon Sep 17 00:00:00 2001 From: Weiliang Wang Date: Thu, 18 Jul 2019 13:12:50 +0800 Subject: [PATCH 1/4] fix null value read when model without bounding box (#4908) --- cocos/pipeline/culling.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/cocos/pipeline/culling.ts b/cocos/pipeline/culling.ts index e8f1ad5bd5..94b30c1be9 100644 --- a/cocos/pipeline/culling.ts +++ b/cocos/pipeline/culling.ts @@ -19,11 +19,12 @@ export function cullDirectionalLight (light: DirectionalLight, model: Model) { } export function cullSphereLight (light: SphereLight, model: Model) { - return cullLight(light, model) || !intersect.aabb_aabb(model.worldBounds, light.aabb); + return cullLight(light, model) || (model.worldBounds && !intersect.aabb_aabb(model.worldBounds, light.aabb)); } export function cullSpotLight (light: SpotLight, model: Model) { - return cullLight(light, model) || !intersect.aabb_aabb(model.worldBounds, light.aabb) || !intersect.aabb_frustum(model.worldBounds, light.frustum); + return cullLight(light, model) || (model.worldBounds && + (!intersect.aabb_aabb(model.worldBounds, light.aabb) || !intersect.aabb_frustum(model.worldBounds, light.frustum))); } export const cullSceneWithDirectionalLight = (() => { From 117b76bb35de954a1912c34a6ae71229b6632d38 Mon Sep 17 00:00:00 2001 From: "jiaxin.lai" Date: Thu, 18 Jul 2019 14:20:42 +0800 Subject: [PATCH 2/4] Fix bug of samepleEvents in animation-state.ts (#4909) --- cocos/animation/animation-state.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/cocos/animation/animation-state.ts b/cocos/animation/animation-state.ts index 5c1fab951e..3fb0b20448 100644 --- a/cocos/animation/animation-state.ts +++ b/cocos/animation/animation-state.ts @@ -652,6 +652,7 @@ export class AnimationState extends Playable { } private _sampleEvents (wrapInfo: WrappedInfo) { + const length = this._clip.eventGroups.length; let direction = wrapInfo.direction; let eventIndex = this._clip.getEventGroupIndexAtRatio(wrapInfo.ratio); if (eventIndex < 0) { From 68d6ee70fdf1cac871796fa95b352538809fdda3 Mon Sep 17 00:00:00 2001 From: Yun Hsiao Wu Date: Thu, 18 Jul 2019 17:53:59 +0800 Subject: [PATCH 3/4] fix batched skinning model (#4912) --- .../batched-skinning-model-component.ts | 30 +++++++------------ cocos/animation/skeletal-animation-clip.ts | 2 +- cocos/renderer/models/skinning-model.ts | 2 +- 3 files changed, 12 insertions(+), 22 deletions(-) diff --git a/cocos/3d/framework/batched-skinning-model-component.ts b/cocos/3d/framework/batched-skinning-model-component.ts index dd317c76b0..57c6e2d3c6 100644 --- a/cocos/3d/framework/batched-skinning-model-component.ts +++ b/cocos/3d/framework/batched-skinning-model-component.ts @@ -42,6 +42,7 @@ import { IMeshStruct, Mesh } from '../assets/mesh'; import { Skeleton } from '../assets/skeleton'; import { LCA, mapBuffer } from '../misc/utils'; import { SkinningModelComponent } from './skinning-model-component'; +import { AnimationComponent } from '../../components'; const repeat = (n: number) => n - Math.floor(n); const batch_id: IGFXAttribute = { name: GFXAttributeName.ATTR_BATCH_ID, format: GFXFormat.R32F, isNormalized: false }; @@ -55,8 +56,6 @@ export class SkinningModelUnit { public mesh: Mesh | null = null; @property(Skeleton) public skeleton: Skeleton | null = null; - @property(Node) - public skinningRoot: Node | null = null; @property(Material) public material: Material | null = null; @property @@ -85,7 +84,6 @@ export class SkinningModelUnit { if (!comp) { return; } this.mesh = comp.mesh; this.skeleton = comp.skeleton; - this.skinningRoot = comp.skinningRoot; this.material = comp.getSharedMaterial(0); } get copyFrom () { @@ -209,25 +207,19 @@ export class BatchedSkinningModelComponent extends SkinningModelComponent { } public cookSkeletons () { - // find lowest common ancestor as the new skinning root - let lca: Node | null = null; - for (const unit of this.units) { - if (!unit || !unit.skinningRoot) { continue; } - const cur = unit.skinningRoot; - if (!lca) { lca = cur; continue; } - lca = LCA(lca, cur); - } - this._skinningRoot = lca; - if (!lca) { console.warn('illegal skinning roots'); return; } + // find the lowest AnimationComponent node as the new skinning root + let root: Node | null = this.node; + while (root && !root.getComponent(AnimationComponent)) { root = root.parent; } + if (!root) { console.warn('skinning model component must be put under animation component'); return; } + this._skinningRoot = root; // merge joints accordingly const skeleton = new Skeleton(); const bindposes: Mat4[] = []; for (const unit of this.units) { - if (!unit || !unit.skeleton || !unit.skinningRoot) { continue; } + if (!unit || !unit.skeleton) { continue; } const partial = unit.skeleton; - const prefix = getPrefix(lca, unit.skinningRoot); for (let i = 0; i < partial.joints.length; i++) { - const path = concatPath(prefix, partial.joints[i]); + const path = partial.joints[i]; const idx = skeleton.joints.findIndex((p) => p === path); if (idx >= 0) { continue; } skeleton.joints.push(path); @@ -276,11 +268,9 @@ export class BatchedSkinningModelComponent extends SkinningModelComponent { const unitLen = this.units.length; for (let i = 0; i < unitLen; i++) { const unit = this.units[i]; - if (!unit || !unit.skeleton || !unit.skinningRoot) { continue; } - const prefix = getPrefix(this._skinningRoot, unit.skinningRoot); + if (!unit || !unit.skeleton) { continue; } jointIndexMap[i] = unit.skeleton.joints.map((j) => { - const path = concatPath(prefix, j); - return this._skeleton!.joints.findIndex((ref) => path === ref); + return this._skeleton!.joints.findIndex((ref) => j === ref); }); } diff --git a/cocos/animation/skeletal-animation-clip.ts b/cocos/animation/skeletal-animation-clip.ts index 340dd4e54b..913b780180 100644 --- a/cocos/animation/skeletal-animation-clip.ts +++ b/cocos/animation/skeletal-animation-clip.ts @@ -8,7 +8,7 @@ import { SkinningModelComponent } from '../3d/framework/skinning-model-component import { ccclass } from '../core/data/class-decorator'; import { getClassName } from '../core/utils/js'; import { Quat, Vec3 } from '../core/value-types'; -import { mat4, quat, vec3 } from '../core/vmath'; +import { quat, vec3 } from '../core/vmath'; import { Node } from '../scene-graph'; import { AnimationClip, ICurveData, IObjectCurveData, IPropertyCurve } from './animation-clip'; import { IPropertyCurveData } from './animation-curve'; diff --git a/cocos/renderer/models/skinning-model.ts b/cocos/renderer/models/skinning-model.ts index a2d9d9d6da..04d31c7514 100644 --- a/cocos/renderer/models/skinning-model.ts +++ b/cocos/renderer/models/skinning-model.ts @@ -82,7 +82,7 @@ export class SkinningModel extends Model { public bindSkeleton (skeleton: Skeleton | null, skinningRoot: Node | null) { this._skeleton = skeleton; if (!skeleton || !skinningRoot) { return; } - this._transform = skinningRoot.parent!; + this._transform = skinningRoot; this._applyJointTexture(skeleton.getBindposeTexture(this._device)); } From c17f28201dbc725a559d930650e3cf70fa6acbb9 Mon Sep 17 00:00:00 2001 From: Leslie Leigh Date: Fri, 19 Jul 2019 16:12:45 +0800 Subject: [PATCH 4/4] module division 2 (#4907) * module division * update * update * small fix * allow co-existing of physics modules * update * upgrade rollup-plugin-node-resolve * refine build engine cli * fix * upgrade rollup plugin commonjs * fix * mark physics module as required --- cocos/3d/index.ts | 9 -- cocos/3d/physics/cannon/instantiate.ts | 14 +++ cocos/3d/physics/cocos/instantiate.ts | 15 +++ cocos/3d/physics/impl-selector.ts | 24 ++--- .../animation-component.ts | 7 +- cocos/animation/index.ts | 1 + cocos/components/index.ts | 1 - cocos/gfx/index.ts | 4 - exports/animation.ts | 27 +++++ exports/base.ts | 20 ++++ exports/gfx-webgl.ts | 3 + exports/gfx-webgl2.ts | 3 + exports/physics-ammo.ts | 2 + exports/physics-builtin.ts | 3 + exports/physics-cannon.ts | 3 + exports/primitives.ts | 6 ++ exports/tween.ts | 2 + index.ts | 63 ++---------- package.json | 5 +- rollup/src/build-engine-cli.ts | 90 ++++++++++++++--- rollup/src/build-engine.ts | 72 ++++++++++---- rollup/tsconfig.json | 2 +- scripts/module-division/division-config.json | 60 ++++++++++++ .../tools/division-config-schema.json | 98 +++++++++++++++++++ .../module-division/tools/division-config.ts | 68 +++++++++++++ .../module-division/tools/generate-schema.js | 6 ++ 26 files changed, 488 insertions(+), 120 deletions(-) create mode 100644 cocos/3d/physics/cannon/instantiate.ts create mode 100644 cocos/3d/physics/cocos/instantiate.ts rename cocos/{components => animation}/animation-component.ts (98%) create mode 100644 exports/animation.ts create mode 100644 exports/base.ts create mode 100644 exports/gfx-webgl.ts create mode 100644 exports/gfx-webgl2.ts create mode 100644 exports/physics-ammo.ts create mode 100644 exports/physics-builtin.ts create mode 100644 exports/physics-cannon.ts create mode 100644 exports/primitives.ts create mode 100644 exports/tween.ts create mode 100644 scripts/module-division/division-config.json create mode 100644 scripts/module-division/tools/division-config-schema.json create mode 100644 scripts/module-division/tools/division-config.ts create mode 100644 scripts/module-division/tools/generate-schema.js diff --git a/cocos/3d/index.ts b/cocos/3d/index.ts index f7c6bfa54b..55f5ce97cd 100644 --- a/cocos/3d/index.ts +++ b/cocos/3d/index.ts @@ -33,21 +33,12 @@ export * from './framework'; export * from './memop'; import * as geometry from './geom-utils'; import * as utils from './misc/utils'; -import * as primitives from './primitive'; export * from './ui'; -// hidden -// export { createRaycastResult } from './physics/instance'; - export { utils, - primitives, geometry, }; cc.geometry = geometry; -cc.primitives = primitives; cc.utils = utils; - -/// PHYSICS EXPORT /// -export * from './physics/export-api'; diff --git a/cocos/3d/physics/cannon/instantiate.ts b/cocos/3d/physics/cannon/instantiate.ts new file mode 100644 index 0000000000..f59ca06aaa --- /dev/null +++ b/cocos/3d/physics/cannon/instantiate.ts @@ -0,0 +1,14 @@ +import { instantiate } from '../impl-selector'; +import { CannonRigidBody } from './cannon-body'; +import { CannonWorld } from './cannon-world'; +import { CannonBoxShape } from './shapes/cannon-box-shape'; +import { CannonSphereShape } from './shapes/cannon-sphere-shape'; + +if (CC_PHYSICS_CANNON) { + instantiate( + CannonBoxShape, + CannonSphereShape, + CannonRigidBody, + CannonWorld, + ); +} diff --git a/cocos/3d/physics/cocos/instantiate.ts b/cocos/3d/physics/cocos/instantiate.ts new file mode 100644 index 0000000000..13401e5c6c --- /dev/null +++ b/cocos/3d/physics/cocos/instantiate.ts @@ -0,0 +1,15 @@ + +import { instantiate } from '../impl-selector'; +import { BuiltInBody } from './builtin-body'; +import { BuiltInWorld } from './builtin-world'; +import { BuiltinBoxShape } from './shapes/builtin-box-shape'; +import { BuiltinSphereShape } from './shapes/builtin-sphere-shape'; + +if (CC_PHYSICS_BUILT_IN) { + instantiate( + BuiltinBoxShape, + BuiltinSphereShape, + BuiltInBody, + BuiltInWorld, + ); +} diff --git a/cocos/3d/physics/impl-selector.ts b/cocos/3d/physics/impl-selector.ts index fae1ebca8b..30cc73b741 100644 --- a/cocos/3d/physics/impl-selector.ts +++ b/cocos/3d/physics/impl-selector.ts @@ -24,9 +24,6 @@ import { CannonWorld } from './cannon/cannon-world'; import { CannonBoxShape } from './cannon/shapes/cannon-box-shape'; import { CannonSphereShape } from './cannon/shapes/cannon-sphere-shape'; -// Ammo -// import { AmmoBoxShape, AmmoRaycastResult, AmmoRigidBody, AmmoSphereShape, AmmoWorld, } from './ammo-impl'; - // built-in import { BuiltInBody } from './cocos/builtin-body'; import { BuiltInWorld } from './cocos/builtin-world'; @@ -38,16 +35,13 @@ export let SphereShape: typeof CannonSphereShape | typeof BuiltinSphereShape; export let RigidBody: typeof CannonRigidBody | typeof BuiltInBody; export let PhysicsWorld: typeof CannonWorld | typeof BuiltInWorld; -if (CC_PHYSICS_CANNON) { - BoxShape = CannonBoxShape; - SphereShape = CannonSphereShape; - RigidBody = CannonRigidBody; - PhysicsWorld = CannonWorld; -} else if (CC_PHYSICS_AMMO) { - // TODO : 选择AMMO -} else if (CC_PHYSICS_BUILT_IN) { - BoxShape = BuiltinBoxShape; - SphereShape = BuiltinSphereShape; - RigidBody = BuiltInBody; - PhysicsWorld = BuiltInWorld; +export function instantiate ( + boxShape: typeof BoxShape, + sphereShape: typeof SphereShape, + body: typeof RigidBody, + world: typeof PhysicsWorld) { + BoxShape = boxShape; + SphereShape = sphereShape; + RigidBody = body; + PhysicsWorld = world; } diff --git a/cocos/components/animation-component.ts b/cocos/animation/animation-component.ts similarity index 98% rename from cocos/components/animation-component.ts rename to cocos/animation/animation-component.ts index fc3eb7dd23..70a0a4ac91 100644 --- a/cocos/components/animation-component.ts +++ b/cocos/animation/animation-component.ts @@ -3,8 +3,7 @@ * @category animation */ -import { AnimationClip, AnimationState } from '../animation'; -import { CrossFade } from '../animation/cross-fade'; +import { Component } from '../components/component'; import { ccclass, executeInEditMode, executionOrder, menu, property } from '../core/data/class-decorator'; import { Event, EventTarget } from '../core/event'; import { CallbacksInvoker, ICallbackTable } from '../core/event/callbacks-invoker'; @@ -13,7 +12,9 @@ import { warnID } from '../core/platform/CCDebug'; import * as ArrayUtils from '../core/utils/array'; import { createMap } from '../core/utils/js-typed'; import { ccenum } from '../core/value-types/enum'; -import { Component } from './component'; +import { AnimationClip } from './animation-clip'; +import { AnimationState } from './animation-state'; +import { CrossFade } from './cross-fade'; /** * @en The event type supported by Animation diff --git a/cocos/animation/index.ts b/cocos/animation/index.ts index dbee23fde0..9265b74592 100644 --- a/cocos/animation/index.ts +++ b/cocos/animation/index.ts @@ -12,3 +12,4 @@ export * from './skeletal-animation-clip'; export * from './animation-manager'; export * from './animation-state'; export * from './cubic-spline-value'; +export * from './animation-component'; diff --git a/cocos/components/index.ts b/cocos/components/index.ts index d5431e2ca7..21aae5348f 100644 --- a/cocos/components/index.ts +++ b/cocos/components/index.ts @@ -31,4 +31,3 @@ export { Component } from './component'; export { EventHandler } from './component-event-handler'; export {default as MissingScript} from './missing-script'; -export { AnimationComponent } from './animation-component'; diff --git a/cocos/gfx/index.ts b/cocos/gfx/index.ts index f2753118c4..84d43290e9 100644 --- a/cocos/gfx/index.ts +++ b/cocos/gfx/index.ts @@ -16,8 +16,6 @@ import { GFXSampler } from './sampler'; import { GFXShader } from './shader'; import { GFXTexture } from './texture'; import { GFXTextureView } from './texture-view'; -import { WebGLGFXDevice } from './webgl/webgl-device'; -import { WebGL2GFXDevice } from './webgl2/webgl2-device'; export { GFXAttributeName, GFXFormat, GFXPrimitiveMode } from './define'; @@ -34,7 +32,5 @@ cc.GFXPipelineLayout = GFXPipelineLayout; cc.GFXPipelineState = GFXPipelineState; cc.GFXCommandBuffer = GFXCommandBuffer; cc.GFXQueue = GFXQueue; -cc.WebGLGFXDevice = WebGLGFXDevice; -cc.WebGL2GFXDevice = WebGL2GFXDevice; Object.assign(cc, GFXDefines); diff --git a/exports/animation.ts b/exports/animation.ts new file mode 100644 index 0000000000..726312c686 --- /dev/null +++ b/exports/animation.ts @@ -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. + */ + +export * from '../cocos/animation'; diff --git a/exports/base.ts b/exports/base.ts new file mode 100644 index 0000000000..83df23184f --- /dev/null +++ b/exports/base.ts @@ -0,0 +1,20 @@ +import '../predefine'; +import '../cocos/legacy'; + +// LOAD ENGINE CORE +export * from '../cocos/core'; +export * from '../cocos/scene-graph'; +import * as renderer from '../cocos/renderer'; +export { renderer }; +cc.renderer = renderer; + +export * from '../cocos/assets'; +export * from '../cocos/load-pipeline'; +export * from '../cocos/components'; +export * from '../cocos/3d'; +export * from '../cocos/gfx'; +import '../cocos/pipeline'; +export * from '../extensions/ccpool/node-pool'; + +const cclegacy = cc; +export { cclegacy }; diff --git a/exports/gfx-webgl.ts b/exports/gfx-webgl.ts new file mode 100644 index 0000000000..1263693e8d --- /dev/null +++ b/exports/gfx-webgl.ts @@ -0,0 +1,3 @@ + +import { WebGLGFXDevice } from '../cocos/gfx/webgl/webgl-device'; +cc.WebGLGFXDevice = WebGLGFXDevice; diff --git a/exports/gfx-webgl2.ts b/exports/gfx-webgl2.ts new file mode 100644 index 0000000000..d349925719 --- /dev/null +++ b/exports/gfx-webgl2.ts @@ -0,0 +1,3 @@ + +import { WebGL2GFXDevice } from '../cocos/gfx/webgl2/webgl2-device'; +cc.WebGL2GFXDevice = WebGL2GFXDevice; diff --git a/exports/physics-ammo.ts b/exports/physics-ammo.ts new file mode 100644 index 0000000000..9916f417cb --- /dev/null +++ b/exports/physics-ammo.ts @@ -0,0 +1,2 @@ + +// import '../cocos/3d/physics/ammo/instantiate'; diff --git a/exports/physics-builtin.ts b/exports/physics-builtin.ts new file mode 100644 index 0000000000..33da7c6b03 --- /dev/null +++ b/exports/physics-builtin.ts @@ -0,0 +1,3 @@ + +import '../cocos/3d/physics/cocos/instantiate'; +export * from '../cocos/3d/physics/export-api'; diff --git a/exports/physics-cannon.ts b/exports/physics-cannon.ts new file mode 100644 index 0000000000..6f7e9df8fa --- /dev/null +++ b/exports/physics-cannon.ts @@ -0,0 +1,3 @@ + +import '../cocos/3d/physics/cannon/instantiate'; +export * from '../cocos/3d/physics/export-api'; diff --git a/exports/primitives.ts b/exports/primitives.ts new file mode 100644 index 0000000000..f341d93a75 --- /dev/null +++ b/exports/primitives.ts @@ -0,0 +1,6 @@ + +import * as primitives from '../cocos/3d/primitive'; +export { + primitives, +}; +cc.primitives = primitives; diff --git a/exports/tween.ts b/exports/tween.ts new file mode 100644 index 0000000000..b01f0efc0e --- /dev/null +++ b/exports/tween.ts @@ -0,0 +1,2 @@ + +export * from '../cocos/tween'; diff --git a/index.ts b/index.ts index f655d4c84d..963440c856 100644 --- a/index.ts +++ b/index.ts @@ -1,56 +1,9 @@ -/* - 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. - */ -// PREDEFINE - -/** - * @hidden - */ -import './predefine'; - -import './cocos/legacy'; - -// LOAD ENGINE CORE -export * from './cocos/core'; -export * from './cocos/scene-graph'; -import * as renderer from './cocos/renderer'; -export { renderer }; -cc.renderer = renderer; - -export * from './cocos/assets'; -export * from './cocos/animation'; -export * from './cocos/load-pipeline'; -export * from './cocos/components'; -export * from './cocos/3d'; -export * from './cocos/gfx'; -import './cocos/pipeline'; -export * from './extensions/ccpool/node-pool'; - -const cclegacy = cc; -export { cclegacy }; - -export * from './cocos/tween'; - -// export default cc; +export * from './exports/base'; +export * from './exports/animation'; +export * from './exports/gfx-webgl'; +export * from './exports/gfx-webgl2'; +export * from './exports/primitives'; +export * from './exports/tween'; +export * from './exports/physics-builtin'; +export * from './exports/physics-cannon'; diff --git a/package.json b/package.json index 729b15ac81..e45c001815 100644 --- a/package.json +++ b/package.json @@ -72,9 +72,10 @@ "require-dir": "0.3.0", "rollup": "^1.15.6", "rollup-plugin-babel": "^4.1.0-1", - "rollup-plugin-commonjs": "^9.1.6", + "rollup-plugin-commonjs": "10.0.1", "rollup-plugin-json": "^3.1.0", - "rollup-plugin-node-resolve": "^3.4.0", + "rollup-plugin-multi-entry": "^2.1.0", + "rollup-plugin-node-resolve": "5.2.0", "rollup-plugin-uglify": "^6.0.2", "rollup-watch": "^4.3.1", "tfig": "^1.0.20", diff --git a/rollup/src/build-engine-cli.ts b/rollup/src/build-engine-cli.ts index 362b5947ff..fef5465960 100644 --- a/rollup/src/build-engine-cli.ts +++ b/rollup/src/build-engine-cli.ts @@ -2,15 +2,48 @@ import * as fs from 'fs-extra'; import * as ps from 'path'; import yargs from 'yargs'; -import { build, IBuildOptions, IFlags, Physics, Platform } from './build-engine'; +import { build, enumeratePhysicsReps, enumeratePlatformReps, IBuildOptions, IFlags, parsePhysics, parsePlatform } from './build-engine'; -yargs.option('platform', { type: 'string', alias: 'p' }); -yargs.option('physics', { type: 'string', alias: 'py' }); -yargs.option('flags', { type: 'array', alias: 'f' }); -yargs.option('destination', { type: 'string', alias: 'd' }); -yargs.option('excludes', { type: 'array', alias: 'e' }); -yargs.options('sourcemap', {}); -yargs.boolean('compress'); +yargs.help(); +yargs.option('platform', { + type: 'string', + alias: 'p', + description: 'Target platform.', + demandOption: true, + choices: enumeratePlatformReps(), +}); +yargs.option('physics', { + type: 'string', + alias: 'py', + description: 'Physics engine to use.', + choices: enumeratePhysicsReps(), +}); +yargs.option('flags', { + type: 'array', + alias: 'f', + description: 'Engine flags.', +}); +yargs.option('destination', { + type: 'string', + alias: 'd', + description: 'Output path.', +}); +yargs.option('excludes', { + type: 'array', + alias: 'e', + description: '(Expired!)', +}); +yargs.options('sourcemap', { + choices: [ + 'inline', + true, + ], + description: 'Source map generation options', +}); +yargs.option('compress', { + type: 'boolean', + description: 'Whether to compress compiled engine.', +}); const flags: IFlags = {}; const argvFlags = yargs.argv.flags as (string[] | undefined); @@ -20,23 +53,32 @@ if (argvFlags) { const sourceMap = yargs.argv.sourcemap === 'inline' ? 'inline' : !!yargs.argv.sourcemap; +const moduleEntries = yargs.argv._; +if (moduleEntries.length === 0) { + console.log(`No module entry specified, default module entries will be used.`); + moduleEntries.push(...getDefaultModuleEntries()); +} + const options: IBuildOptions = { + moduleEntries, compress: yargs.argv.compress as (boolean | undefined), - inputPath: './index.ts', outputPath: yargs.argv.destination as string, excludes: yargs.argv.excludes as string[], sourcemap: sourceMap, flags, - platform: yargs.argv.platform as (Platform | undefined), - physics: yargs.argv.physics as (Physics | undefined), }; +if (yargs.argv.platform) { + options.platform = parsePlatform(yargs.argv.platform as unknown as string); +} +if (yargs.argv.physics) { + options.physics = parsePhysics(yargs.argv.physics as unknown as string); +} build(options).then( (result) => { console.log(`Build successful.`); fs.ensureDirSync(ps.dirname(options.outputPath)); fs.writeFileSync(options.outputPath, result.code); - console.log(`With map? ${!!result.map}`); if (result.map) { fs.writeFileSync(`${options.outputPath}.map`, result.map); } @@ -45,3 +87,27 @@ build(options).then( console.error(`Build failed, reason:\n ${reason.stack}`); }, ); + +function getDefaultModuleEntries () { + type ModuleDivision = any; // import('../../scripts/module-division/tools/division-config').ModuleDivision; + type GroupItem = any; // import('../../scripts/module-division/tools/division-config').GroupItem; + type Item = any; // import('../../scripts/module-division/tools/division-config').Item; + + const isGroupItem = (item: Item): item is GroupItem => { + return 'options' in item; + }; + + const divisionConfig: ModuleDivision = require('../../scripts/module-division/division-config.json'); + const result = []; + for (const item of divisionConfig.items) { + if (item.required || item.default) { + if (isGroupItem(item)) { + result.push(item.options[item.defaultOption || 0].entry); + } else { + // @ts-ignore + result.push(item.entry); + } + } + } + return result; +} diff --git a/rollup/src/build-engine.ts b/rollup/src/build-engine.ts index 3d0d903288..92cbb059b0 100644 --- a/rollup/src/build-engine.ts +++ b/rollup/src/build-engine.ts @@ -1,4 +1,5 @@ -import { dirname, relative } from 'path'; +import { existsSync } from 'fs'; +import { dirname, join, normalize, relative } from 'path'; import { rollup } from 'rollup'; // @ts-ignore import babel from 'rollup-plugin-babel'; @@ -7,17 +8,16 @@ import commonjs from 'rollup-plugin-commonjs'; // @ts-ignore import json from 'rollup-plugin-json'; // @ts-ignore +import multiEntry from 'rollup-plugin-multi-entry'; +// @ts-ignore import resolve from 'rollup-plugin-node-resolve'; // @ts-ignore import { uglify } from 'rollup-plugin-uglify'; - -const { excludes } = require('../plugin/rollup-plugin-excludes'); +// @ts-ignore +import { excludes } from '../plugin/rollup-plugin-excludes'; interface IBaseOptions { - /** - * 引擎入口模块。 - */ - inputPath: string; + moduleEntries: string[]; /** * 指定输出路径。 @@ -79,18 +79,22 @@ export async function build (options: IBuildOptions) { return await _internalBuild(Object.assign(options, {globalDefines})); } +function resolveModuleEntry (moduleEntry: string) { + return normalize(`${__dirname}/../../exports/${moduleEntry}.ts`); +} + async function _internalBuild (options: IAdvancedOptions) { console.log(`Build-engine options: ${JSON.stringify(options, undefined, 2)}`); const doUglify = !!options.compress; const rollupPlugins = [ + multiEntry(), + excludes({ modules: options.excludes, }), resolve({ extensions: ['.js', '.ts', '.json'], - jsnext: true, - main: true, }), json({ @@ -161,8 +165,14 @@ async function _internalBuild (options: IAdvancedOptions) { const outputPath = options.outputPath; const sourcemapFile = options.sourcemapFile || `${options.outputPath}.map`; + const moduleEntries = options.moduleEntries.map(resolveModuleEntry); + for (const moduleEntry of moduleEntries) { + if (!existsSync(moduleEntry)) { + console.error(`Cannot find engine module ${moduleEntry}`); + } + } const rollupBuild = await rollup({ - input: options.inputPath, + input: moduleEntries, plugins: rollupPlugins, }); const generated = await rollupBuild.generate({ @@ -190,9 +200,35 @@ async function _internalBuild (options: IAdvancedOptions) { } } -export type Platform = 'universal' | 'editor' | 'preview' | 'build' | 'test'; +export enum Platform { + universal, + editor, + preview, + build, + test, +} -export type Physics = 'cannon' | 'ammo' | 'builtin'; +export function enumeratePlatformReps () { + return Object.values(Platform).filter((value) => typeof value === 'string'); +} + +export function parsePlatform (rep: string) { + return Reflect.get(Platform, rep); +} + +export enum Physics { + cannon, + ammo, + builtin, +} + +export function enumeratePhysicsReps () { + return Object.values(Physics).filter((value) => typeof value === 'string'); +} + +export function parsePhysics (rep: string) { + return Reflect.get(Physics, rep); +} export interface IFlags { jsb?: boolean; @@ -231,14 +267,14 @@ interface IGlobaldefines { // tslint:disable-next-line: no-shadowed-variable function getGlobalDefs (platform?: Platform, physics?: Physics, flags?: IFlags): object { - platform = platform || 'universal'; + platform = platform || Platform.universal; const PLATFORM_MACROS = ['CC_EDITOR', 'CC_PREVIEW', 'CC_BUILD', 'CC_TEST']; const FLAGS = ['jsb', 'runtime', 'wechatgame', 'wechatgameSub', 'qqplay', 'debug', 'nativeRenderer']; - const platformMacro = 'CC_' + platform.toUpperCase(); - if (PLATFORM_MACROS.indexOf(platformMacro) === -1 && platform !== 'universal') { + const platformMacro = ('CC_' + Platform[platform]).toUpperCase(); + if (PLATFORM_MACROS.indexOf(platformMacro) === -1 && platform !== Platform.universal) { throw new Error(`Unknown platform ${platform}.`); } const result: IGlobaldefines = {}; @@ -270,19 +306,19 @@ function getGlobalDefs (platform?: Platform, physics?: Physics, flags?: IFlags): result.CC_PHYSICS_BUILT_IN = false; switch (physics) { - case 'cannon': + case Physics.cannon: result.CC_PHYSICS_CANNON = true; result.CC_PHYSICS_AMMO = false; result.CC_PHYSICS_BUILT_IN = false; break; - case 'ammo': + case Physics.ammo: result.CC_PHYSICS_CANNON = false; result.CC_PHYSICS_AMMO = true; result.CC_PHYSICS_BUILT_IN = false; break; - case 'builtin': + case Physics.builtin: result.CC_PHYSICS_CANNON = false; result.CC_PHYSICS_AMMO = false; result.CC_PHYSICS_BUILT_IN = true; diff --git a/rollup/tsconfig.json b/rollup/tsconfig.json index 505f79138c..66516d4732 100644 --- a/rollup/tsconfig.json +++ b/rollup/tsconfig.json @@ -12,7 +12,7 @@ "sourceMap": true, /* Generates corresponding '.map' file. */ // "outFile": "./", /* Concatenate and emit output to single file. */ "outDir": "./out", /* Redirect output structure to the directory. */ - // "rootDir": "./", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */ + "rootDir": "./src", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */ // "composite": true, /* Enable project compilation */ // "removeComments": true, /* Do not emit comments to output. */ // "noEmit": true, /* Do not emit outputs. */ diff --git a/scripts/module-division/division-config.json b/scripts/module-division/division-config.json new file mode 100644 index 0000000000..8678a17221 --- /dev/null +++ b/scripts/module-division/division-config.json @@ -0,0 +1,60 @@ +{ + "$schema": "./tools/division-config-schema.json", + "items": [ + { + "name": "Base", + "required": true, + "entry": "base", + "default": true + }, + { + "name": "Animation", + "entry": "animation", + "default": true + }, + { + "name": "GFX", + "required": true, + "options": [ + { + "name": "WebGL 1.0", + "entry": "gfx-webgl" + }, + { + "name": "WebGL 2.0", + "entry": "gfx-webgl2" + } + ], + "default": true, + "defaultOption": 0 + }, + { + "name": "Primitives", + "entry": "primitives", + "default": true + }, + { + "name": "Physics", + "options": [ + { + "name": "Built-in", + "entry": "physics-builtin" + }, + { + "name": "cannon.js", + "entry": "physics-cannon" + }, + { + "name": "ammo.js", + "entry": "physics-ammo" + } + ], + "required": true, + "defaultOption": 0 + }, + { + "name": "tween.js Adapter", + "entry": "tween" + } + ] +} \ No newline at end of file diff --git a/scripts/module-division/tools/division-config-schema.json b/scripts/module-division/tools/division-config-schema.json new file mode 100644 index 0000000000..b0a766b13a --- /dev/null +++ b/scripts/module-division/tools/division-config-schema.json @@ -0,0 +1,98 @@ +{ + "$schema": "http://json-schema.org/draft-07/schema#", + "additionalProperties": false, + "definitions": { + "GroupItem": { + "additionalProperties": false, + "properties": { + "default": { + "description": "If true, this field indicates the item is marked as bundled by default.", + "type": "boolean" + }, + "defaultOption": { + "description": "Indicates the n-th option is selected by default if the item is marked as bundled.\nDefaults to 0.", + "type": "number" + }, + "description": { + "description": "Item description.", + "type": "string" + }, + "name": { + "description": "Item name.", + "type": "string" + }, + "options": { + "description": "Options.", + "items": { + "additionalProperties": false, + "properties": { + "description": { + "description": "Option description.", + "type": "string" + }, + "entry": { + "description": "Entry of the option.", + "type": "string" + }, + "name": { + "description": "Name of the option.", + "type": "string" + } + }, + "type": "object" + }, + "type": "array" + }, + "required": { + "description": "Whether if this item is required to provide.", + "type": "boolean" + } + }, + "type": "object" + }, + "SimpleItem": { + "additionalProperties": false, + "properties": { + "default": { + "description": "If true, this field indicates the item is marked as bundled by default.", + "type": "boolean" + }, + "description": { + "description": "Item description.", + "type": "string" + }, + "entry": { + "description": "Entry to the module.", + "type": "string" + }, + "name": { + "description": "Item name.", + "type": "string" + }, + "required": { + "description": "Whether if this item is required to provide.", + "type": "boolean" + } + }, + "type": "object" + } + }, + "properties": { + "items": { + "description": "Items.", + "items": { + "anyOf": [ + { + "$ref": "#/definitions/SimpleItem" + }, + { + "$ref": "#/definitions/GroupItem" + } + ] + }, + "type": "array" + } + }, + "type": "object" +} + diff --git a/scripts/module-division/tools/division-config.ts b/scripts/module-division/tools/division-config.ts new file mode 100644 index 0000000000..1c6328f1ec --- /dev/null +++ b/scripts/module-division/tools/division-config.ts @@ -0,0 +1,68 @@ + +// tslint:disable:interface-name + +export interface ModuleDivision { + /** + * Items. + */ + items: Item[]; +} + +interface BaseItem { + /** + * Item name. + */ + name: string; + + /** + * Item description. + */ + description?: string; + + /** + * Whether if this item is required to provide. + */ + required?: boolean; + + /** + * If true, this field indicates the item is marked as bundled by default. + */ + default?: boolean; +} + +export interface SimpleItem extends BaseItem { + /** + * Entry to the module. + */ + entry: string; +} + +export interface GroupItem extends BaseItem { + /** + * Options. + */ + options: Array<{ + /** + * Name of the option. + */ + name: string; + + /** + * Entry of the option. + */ + entry: string; + + /** + * Option description. + */ + description?: string; + }>; + + /** + * Indicates the n-th option is selected by default if the item is marked as bundled. + * Defaults to 0. + */ + defaultOption?: number; +} + +export type Item = SimpleItem | GroupItem; diff --git a/scripts/module-division/tools/generate-schema.js b/scripts/module-division/tools/generate-schema.js new file mode 100644 index 0000000000..b31d8a9605 --- /dev/null +++ b/scripts/module-division/tools/generate-schema.js @@ -0,0 +1,6 @@ +const { execSync } = require('child_process'); +const { join } = require('path'); + +const input = join(__dirname, 'division-config.ts'); +const output = join(__dirname, 'division-config-schema.json'); +execSync(`typescript-json-schema ${input} ModuleDivision -o ${output} --noExtraProps`);