diff --git a/cocos/3d/framework/camera-component.js b/cocos/3d/framework/camera-component.js index 5052e32573..38208e3bac 100644 --- a/cocos/3d/framework/camera-component.js +++ b/cocos/3d/framework/camera-component.js @@ -1,198 +1,212 @@ -import { Component } from '../ecs'; -import renderer from '../renderer'; -import { toRadian } from '../vmath'; +import RenderableComponent from './renderable-component'; +import _decorator from '../../core/data/class-decorator'; +import renderer from '../../renderer'; +import { toRadian, color4 } from '../../core/vmath'; +import Rect from '../../core/value-types/rect'; +const { ccclass, property } = _decorator; -export default class CameraComponent extends Component { - constructor() { - super(); +@ccclass('CameraComponent') +export default class CameraComponent extends RenderableComponent { - this._camera = new renderer.Camera(); - } - onInit() { - this._camera.setStages([ - 'opaque', - 'transparent' - ]); - this._camera.setNode(this._entity); + constructor() { + super(); + this._projection = 'perspective'; + this._priority = 0; + this._fov = 45; + this._orthoHeight = 10; + this._near = 0.01; + this._far = 1000.0; + this._color = new color4(0.2, 0.3, 0.47, 1.0); + this._depth = 1.0; + this._stencil = 0; + this._clearFlags = renderer.CLEAR_COLOR | renderer.CLEAR_DEPTH; + this._rect = new Rect(0, 0, 1, 1); + this._camera = new renderer.Camera(); + } - /** - * **@schema** The projection type of the camera - * @type {string} - */ - this.projection = this._projection; - /** - * **@schema** The camera priority - * @type {number} - */ - this.priority = this._priority; - /** - * **@schema** The camera field of view - * @type {number} - */ - this.fov = this._fov; - /** - * **@schema** The camera height when in orthogonal mode - * @type {number} - */ - this.orthoHeight = this._orthoHeight; - /** - * **@schema** The near clipping distance of the camera - * @type {number} - */ - this.near = this._near; - /** - * **@schema** The far clipping distance of the camera - * @type {number} - */ - this.far = this._far; - /** - * **@schema** The clearing color of the camera - * @type {color4} - */ - this.color = this._color; - /** - * **@schema** The clearing depth value of the camera - * @type {number} - */ - this.depth = this._depth; - /** - * **@schema** The clearing stencil value of the camera - * @type {number} - */ - this.stencil = this._stencil; - /** - * **@schema** The clearing flags of this camera - * @type {number} - */ - this.clearFlags = this._clearFlags; - /** - * **@schema** The screen rect of the camera - * @type {Object} - */ - this.rect = this._rect; - } + @property + get projection() { + return this._projection; + } + set projection(val) { + this._projection = val; + let type = renderer.PROJ_PERSPECTIVE; + if (this._projection === 'ortho') { + type = renderer.PROJ_ORTHO; + } + this._camera.setType(this._projection); + } - onEnable() { - this._app.scene.addCamera(this._camera); - } + @property + get priority() { + return this._priority; + } + set priority(val) { + this._priority = val; + this._camera.setPriority(this._priority); + } - onDisable() { - this._app.scene.removeCamera(this._camera); - } + @property + get fov() { + return this._fov; + } + set fov(val) { + this._fov = val; + this._camera.setFov(toRadian(this._fov)); + } + + @property + get orthoHeight() { + return this._orthoHeight; + } + set orthoHeight(val) { + this._orthoHeight = val; + this._camera.setOrthoHeight(this._orthoHeight); + } + + @property + get near() { + return this._near; + } + + set near(val) { + this._near = val; + this._camera.setNear(this._near); + } + + @property + get far() { + return this._far; + } + set far(val) { + this._far = val; + this._camera.setFar(this._far); + } + + @property + get color() { + return this._color; + } + + set color(val) { + this._color.r = val.r; + this._color.g = val.g; + this._color.b = val.b; + this._color.a = val.a; + let color = this._color; + this._camera.setColor(color.r, color.g, color.b, color.a); + } + + @property + get depth() { + return this._depth; + } + set depth(val) { + this._depth = val; + this._camera.setDepth(this._depth); + } + + @property + get stencil() { + return this._stencil; + } + + set stencil(val) { + this._stencil = val; + this._camera.setStencil(this._stencil); + } + + @property + get clearFlags() { + return this._clearFlags; + } + + set clearFlags(val) { + this._clearFlags = val; + this._camera.setClearFlags(this._clearFlags); + } + + @property + get rect() { + return this._rect; + } + + set rect(val) { + this._rect.set(val); + this._camera.setRect(this._rect); + } + + onLoad() { + this._camera.setStages([ + 'opaque', + 'transparent' + ]); + this._camera.setNode(this.node); + + /** + * **@schema** The projection type of the camera + * @type {string} + */ + this.projection = this._projection; + /** + * **@schema** The camera priority + * @type {number} + */ + this.priority = this._priority; + /** + * **@schema** The camera field of view + * @type {number} + */ + this.fov = this._fov; + /** + * **@schema** The camera height when in orthogonal mode + * @type {number} + */ + this.orthoHeight = this._orthoHeight; + /** + * **@schema** The near clipping distance of the camera + * @type {number} + */ + this.near = this._near; + /** + * **@schema** The far clipping distance of the camera + * @type {number} + */ + this.far = this._far; + /** + * **@schema** The clearing color of the camera + * @type {color4} + */ + this.color = this._color; + /** + * **@schema** The clearing depth value of the camera + * @type {number} + */ + this.depth = this._depth; + /** + * **@schema** The clearing stencil value of the camera + * @type {number} + */ + this.stencil = this._stencil; + /** + * **@schema** The clearing flags of this camera + * @type {number} + */ + this.clearFlags = this._clearFlags; + /** + * **@schema** The screen rect of the camera + * @type {Object} + */ + this.rect = this._rect; + } + + onEnable() { + this.scene.addCamera(this._camera); + } + + onDisable() { + this.scene.removeCamera(this._camera); + } } -CameraComponent.schema = { - projection: { - type: 'enums', - default: 'perspective', - options: ['ortho', 'perspective'], - set(val) { - this._projection = val; - let type = renderer.PROJ_PERSPECTIVE; - if (this._projection === 'ortho') { - type = renderer.PROJ_ORTHO; - } - this._camera.setType(type); - } - }, - - priority: { - type: 'number', - default: 0, - set(val) { - this._priority = val; - this._camera.setPriority(val); - } - }, - - // fov (in angle) - fov: { - type: 'number', - default: 45, - set(val) { - this._fov = val; - this._camera.setFov(toRadian(val)); - } - }, - - // orthoHeight - orthoHeight: { - type: 'number', - default: 10, - set(val) { - this._orthoHeight = val; - this._camera.setOrthoHeight(val); - } - }, - - // near - near: { - type: 'number', - default: 0.01, - set(val) { - this._near = val; - this._camera.setNear(val); - } - }, - - // far - far: { - type: 'number', - default: 1000.0, - set(val) { - this._far = val; - this._camera.setFar(val); - } - }, - - // color - color: { - type: 'color4', - default: [0.2, 0.3, 0.47, 1], - set(val) { - this._color = val; - this._camera.setColor(val.r, val.g, val.b, val.a); - } - }, - - // depth - depth: { - type: 'number', - default: 1, - set(val) { - this._depth = val; - this._camera.setDepth(val); - } - }, - - // stencil - stencil: { - type: 'number', - default: 0, - set(val) { - this._stencil = val; - this._camera.setStencil(val); - } - }, - - // clearFlags - clearFlags: { - type: 'number', - default: 3, // enums.CLEAR_COLOR | enums.CLEAR_DEPTH; - set(val) { - this._clearFlags = val; - this._camera.setClearFlags(val); - } - }, - - // rect - rect: { - type: 'rect', - default: [0, 0, 1, 1], - set(val) { - this._rect = val; - this._camera.setRect(val[0], val[1], val[2], val[3]); - } - }, -}; \ No newline at end of file diff --git a/cocos/3d/framework/render-system.js b/cocos/3d/framework/render-system.js new file mode 100644 index 0000000000..5e51e478bc --- /dev/null +++ b/cocos/3d/framework/render-system.js @@ -0,0 +1,27 @@ +import render from '../../renderer'; +export default class RenderSystem { + constructor () { + this._scene = new render.Scene(); + } + + init () { + + } + + add (comp) { + + } + + remove (comp) { + + } + + update (dt) { + cc.render.render(this._scene); + this._scene.reset(); + } + + get scene () { + return this._scene; + } +} diff --git a/cocos/3d/framework/renderable-component.js b/cocos/3d/framework/renderable-component.js new file mode 100644 index 0000000000..036befdfd8 --- /dev/null +++ b/cocos/3d/framework/renderable-component.js @@ -0,0 +1,18 @@ +import Component from '../../Components/CCComponent'; + +export default class RenderableComponent extends Component { + constructor() { + this._system = RenderableComponent.system; + this._scene = this._system.scene; + } + + // todo add implementation + get renderSystem() { + return this._system; + } + + // todo add scene + get scene() { + return this._scene; + } +}; diff --git a/cocos/core/CCDirector.js b/cocos/core/CCDirector.js index 62aeac906a..5652f8310d 100644 --- a/cocos/core/CCDirector.js +++ b/cocos/core/CCDirector.js @@ -30,11 +30,12 @@ import eventManager from './platform/event-manager/CCEventManager'; import ccobject from './data/object'; import game from './CCGame'; import Scheduler from './CCScheduler'; -import {autoRelease} from '../load-pipeline/auto-release-utils'; +import { autoRelease } from '../load-pipeline/auto-release-utils'; import ComponentScheduler from '../scene-graph/component-scheduler'; import NodeActivator from '../scene-graph/node-activator'; // import AnimationSystem from '../3d/framework/animation-system'; // import SkinningModelSystem from '../3d/framework/skinning-model-system'; +import RenderSystem from '../3d/framework/render-system'; import { getClassByName } from './utils/js'; // const ComponentScheduler = require('./component-scheduler'); @@ -176,6 +177,14 @@ class Director extends EventTarget { // this.registerSystem('animation', AnimationSystem, ['AnimationComponent'], 200); // this.registerSystem('skinning-model', SkinningModelSystem, ['SkinningModelComponent'], 100); + // init renderSystem + { + this._renderSystem = new RenderSystem(); + let renderComps = ['CameraComponent']; + renderComps.forEach((compName) => { + getClassByName(compName).system = this._renderSystem; + }); + } cc.loader.init(this); } @@ -319,7 +328,7 @@ class Director extends EventTarget { if (eventManager) eventManager.setEnabled(false); - cc.renderer.clear(); + // cc.renderer.clear(); if (!CC_EDITOR) { if (cc.isValid(this._scene)) { @@ -785,7 +794,7 @@ class Director extends EventTarget { * @param {number} priority * @return {System} */ - registerSystem(name, cls, compClsNames, priority) { + registerSystem (name, cls, compClsNames, priority) { let sys = new cls(); sys._id = name; sys._priority = priority; @@ -804,7 +813,7 @@ class Director extends EventTarget { * @param {string} name * @return {System} */ - getSystem(name) { + getSystem (name) { return this._systems.find((sys) => { return sys._id === name; }); @@ -882,8 +891,7 @@ class Director extends EventTarget { // Render this.emit(cc.Director.EVENT_BEFORE_DRAW); - renderer.render(this._scene); - + this._renderSystem.update(this._deltaTime); // After draw this.emit(cc.Director.EVENT_AFTER_DRAW); diff --git a/cocos/core/CCGame.js b/cocos/core/CCGame.js index 1232807324..36a5df12ee 100644 --- a/cocos/core/CCGame.js +++ b/cocos/core/CCGame.js @@ -27,7 +27,10 @@ import EventTarget from './event/event-target'; import inputManager from './platform/event-manager/CCInputManager'; import debug from './platform/CCDebug'; -import {addon} from './utils/js'; +import { addon } from './utils/js'; + +import renderer from '../renderer'; +import gfx from '../renderer/gfx'; /** * @module cc @@ -80,7 +83,7 @@ var game = { EVENT_GAME_INITED: "game_inited", /** - * Event triggered after engine inited, at this point you will be able to use all engine classes. + * Event triggered after engine inited, at this point you will be able to use all engine classes. * It was defined as EVENT_RENDERER_INITED in cocos creator v1.x and renamed in v2.0 * @property EVENT_ENGINE_INITED * @constant @@ -231,9 +234,9 @@ var game = { */ onStart: null, -//@Public Methods + //@Public Methods -// @Game play control + // @Game play control /** * !#en Set frame rate of game. * !#zh 设置游戏帧率。 @@ -340,7 +343,7 @@ var game = { close(); }, -// @Game loading + // @Game loading _initEngine () { if (this._rendererInitialized) { @@ -473,7 +476,7 @@ var game = { this.prepare(game.onStart && game.onStart.bind(game)); }, -// @ Persist root node section + // @ Persist root node section /** * !#en * Add a persistent root node to the game, the persistent node won't be destroyed during scene transition.
@@ -496,7 +499,7 @@ var game = { if (!node.parent) { node.parent = scene; } - else if ( !(node.parent instanceof cc.Scene) ) { + else if (!(node.parent instanceof cc.Scene)) { cc.warnID(3801); return; } @@ -535,9 +538,9 @@ var game = { return node._persistNode; }, -//@Private Methods + //@Private Methods -// @Time ticker section + // @Time ticker section _setAnimFrame: function () { this._lastTime = new Date(); var frameRate = game.config.frameRate; @@ -555,34 +558,34 @@ var game = { } else { window.requestAnimFrame = window.requestAnimationFrame || - window.webkitRequestAnimationFrame || - window.mozRequestAnimationFrame || - window.oRequestAnimationFrame || - window.msRequestAnimationFrame || - this._stTime; + window.webkitRequestAnimationFrame || + window.mozRequestAnimationFrame || + window.oRequestAnimationFrame || + window.msRequestAnimationFrame || + this._stTime; window.cancelAnimFrame = window.cancelAnimationFrame || - window.cancelRequestAnimationFrame || - window.msCancelRequestAnimationFrame || - window.mozCancelRequestAnimationFrame || - window.oCancelRequestAnimationFrame || - window.webkitCancelRequestAnimationFrame || - window.msCancelAnimationFrame || - window.mozCancelAnimationFrame || - window.webkitCancelAnimationFrame || - window.oCancelAnimationFrame || - this._ctTime; + window.cancelRequestAnimationFrame || + window.msCancelRequestAnimationFrame || + window.mozCancelRequestAnimationFrame || + window.oCancelRequestAnimationFrame || + window.webkitCancelRequestAnimationFrame || + window.msCancelAnimationFrame || + window.mozCancelAnimationFrame || + window.webkitCancelAnimationFrame || + window.oCancelAnimationFrame || + this._ctTime; } } }, - _stTime: function(callback){ + _stTime: function (callback) { var currTime = new Date().getTime(); var timeToCall = Math.max(0, game._frameTime - (currTime - game._lastTime)); - var id = window.setTimeout(function() { callback(); }, + var id = window.setTimeout(function () { callback(); }, timeToCall); game._lastTime = currTime + timeToCall; return id; }, - _ctTime: function(id){ + _ctTime: function (id) { window.clearTimeout(id); }, //Run game. @@ -609,7 +612,7 @@ var game = { self._paused = false; }, -// @Game loading section + // @Game loading section _initConfig (config) { // Configs adjustment if (typeof config.debugMode !== 'number') { @@ -644,11 +647,11 @@ var game = { _determineRenderType () { let config = this.config, userRenderMode = parseInt(config.renderMode) || 0; - + // Determine RenderType this.renderType = this.RENDER_TYPE_CANVAS; let supportRender = false; - + if (userRenderMode === 0) { if (cc.sys.capabilities['opengl']) { this.renderType = this.RENDER_TYPE_WEBGL; @@ -667,7 +670,7 @@ var game = { this.renderType = this.RENDER_TYPE_WEBGL; supportRender = true; } - + if (!supportRender) { throw new Error(debug.getError(3820, userRenderMode)); } @@ -756,14 +759,27 @@ var game = { if (isWeChatGame || isQQPlay) { opts['preserveDrawingBuffer'] = true; } - renderer.initWebGL(localCanvas, opts); - this._renderContext = renderer.device._gl; + + { + // todo, adjust opts here + let device = this._renderContext = new gfx.Device(localCanvas, {}); + // todo, fix builtins here + this._renderer = new renderer.ForwardRenderer(device); + } + // renderer.initWebGL(localCanvas, opts); + // this._renderContext = renderer.device._gl; } if (!this._renderContext) { + // todo fix here for wechat game + console.error('can not support canvas rendering in 3D'); + this._renderer = null; + this._renderContext = null; this.renderType = this.RENDER_TYPE_CANVAS; + return; + // this.renderType = this.RENDER_TYPE_CANVAS; // Could be ignored by module settings - renderer.initCanvas(localCanvas); - this._renderContext = renderer.device._ctx; + // renderer.initCanvas(localCanvas); + // this._renderContext = renderer.device._ctx; } this.canvas.oncontextmenu = function () { @@ -859,4 +875,4 @@ EventTarget.call(game); addon(game, EventTarget.prototype); cc.game = game; -export default game; \ No newline at end of file +export default game; diff --git a/cocos/core/utils/array.js b/cocos/core/utils/array.js index f485280f0b..3ddd316a48 100644 --- a/cocos/core/utils/array.js +++ b/cocos/core/utils/array.js @@ -28,8 +28,6 @@ * @static */ -export {default as MutableForwardIterator} from './mutable-forward-iterator'; - /** * Removes the array item at the specified index. * @method removeAt @@ -99,7 +97,7 @@ export function fastRemove (array, value) { export function verifyType (array, type) { if (array && array.length > 0) { for (var i = 0; i < array.length; i++) { - if (!(array[i] instanceof type)) { + if (!(array[i] instanceof type)) { cc.logID(1300); return false; } @@ -167,4 +165,4 @@ export function copy (array) { for (i = 0; i < len; i += 1) arr_clone[i] = array[i]; return arr_clone; -} \ No newline at end of file +} diff --git a/cocos/core/utils/js.js b/cocos/core/utils/js.js index 928325ea97..3d5902c92e 100644 --- a/cocos/core/utils/js.js +++ b/cocos/core/utils/js.js @@ -24,6 +24,7 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ****************************************************************************/ +import IdGenerater from './id-generater'; import * as jsarray from './array'; import IDGenerater from './id-generater'; diff --git a/cocos/scene-graph/scene.js b/cocos/scene-graph/scene.js index 4d1f13fa06..8c7d34b180 100644 --- a/cocos/scene-graph/scene.js +++ b/cocos/scene-graph/scene.js @@ -41,8 +41,6 @@ const { ccclass, property } = _decorator; export default class Scene extends Node { constructor(name) { super(name); - this._anchorPoint.x = 0.0; - this._anchorPoint.y = 0.0; this._activeInHierarchy = false; this._inited = !cc.game._isCloning; diff --git a/playground/files.js b/playground/files.js new file mode 100644 index 0000000000..af6dc0f92c --- /dev/null +++ b/playground/files.js @@ -0,0 +1,82 @@ +window.files = { + "aa": [ + "simple" + ], +// "animation": [ +// "spec-skeleton" +// ], +// "audio": [ +// "spec-audio" +// ], +// "enabled": [ +// "spec-comp-disabled", +// "spec-entity-disabled", +// "spec-prefab-disabled", +// ], +// "model": [ +// "spec-blood-and-fire", +// "spec-mesh-asset", +// "spec-primitives" +// ], +// "prefab": [ +// "spec-multiple-children", +// "spec-prefab-instantiate", +// "spec-prefab-skeleton-instantiate", +// "spec-use-default-transform", +// "spec-use-new-name" +// ], +// "preload": [ +// "spec-mix-preload", +// "spec-no-prefab-preload" +// ], +// "script": [ +// "spec-script-comps", +// "spec-script-materials", +// "spec-script-mesh", +// "spec-script-refs", +// "spec-script-simple", +// "spec-script-sprite" +// ], +// "subasset": [ +// "spec-model-subasset" +// ], +// "ui": [ +// "spec-bitmap-font", +// "spec-button", +// "spec-mask-nested", +// "spec-mask-same-level", +// "spec-opentype-font", +// "spec-simple", +// "spec-system-font", +// "spec-screen-scaler", +// "spec-toggle-and-group", +// "spec-grid-layout", +// "spec-scroll-bar", +// "spec-scroll-view", +// "spec-slider", +// "spec-edit-box", +// "spec-transition-state", +// ], +// "particle": [ +// "spec-particlesystem-box", +// "spec-particlesystem-circle", +// "spec-particlesystem-colorovertime", +// "spec-particlesystem-cone", +// "spec-particlesystem-forceovertime", +// "spec-particlesystem-hemisphere", +// "spec-particlesystem-limitvelocity", +// "spec-particlesystem-renderer", +// "spec-particlesystem-rotationovertime", +// "spec-particlesystem-sizeovertime", +// "spec-particlesystem-sphere", +// "spec-particlesystem-texturesheet", +// "spec-particlesystem-velocityovertime", +// "spec-particlesystem-performance" +// ], +// "physics": [ +// "spec-boxes", +// "spec-spheres", +// "spec-body-params", +// "spec-skiing" +// ] +}; diff --git a/playground/index.html b/playground/index.html new file mode 100644 index 0000000000..65280a941b --- /dev/null +++ b/playground/index.html @@ -0,0 +1,446 @@ + + + + engine-3d / examples + + + + + + +
+

engine-3d / examples

+ + + + + +
+ + x +
+
+
+ + + + + + + + diff --git a/playground/simple.html b/playground/simple.html new file mode 100644 index 0000000000..0331799fd7 --- /dev/null +++ b/playground/simple.html @@ -0,0 +1,42 @@ + + + + + spec-scroll-view + + + + + + + + + + + + + diff --git a/predefine.js b/predefine.js index 34abdd1dfa..3862294262 100644 --- a/predefine.js +++ b/predefine.js @@ -128,10 +128,10 @@ var _global = typeof window === 'undefined' ? global : window; * @module cc * @main cc */ -cc = _global.cc || {}; +let cc = _global.cc = _global.cc || {}; // For internal usage -_cc = _global._cc || {}; +_global._cc = _global._cc || {}; function defineMacro (name, defaultValue) { @@ -202,4 +202,4 @@ if (CC_DEV) { const engineVersion = '2.0.0 alpha'; _global['CocosEngine'] = cc.ENGINE_VERSION = engineVersion; -export default cc; \ No newline at end of file +export default cc;