diff --git a/cocos2d/core/CCGame.js b/cocos2d/core/CCGame.js index 4f932f4b30..b3c1f7ea6c 100644 --- a/cocos2d/core/CCGame.js +++ b/cocos2d/core/CCGame.js @@ -731,7 +731,7 @@ var game = { } // WebGL context created successfully - if (cc.game.renderType === game.RENDER_TYPE_WEBGL) { + if (game.renderType === game.RENDER_TYPE_WEBGL) { var opts = { 'stencil': true, 'antialias': false, @@ -740,12 +740,14 @@ var game = { if (isWeChatGame) { opts['preserveDrawingBuffer'] = true; } - renderer.init(localCanvas, opts); + renderer.initWebGL(localCanvas, opts); this._renderContext = renderer.device._gl; } if (!this._renderContext) { - cc.game.renderType = game.RENDER_TYPE_CANVAS; - renderer.init(localCanvas); + game.renderType = game.RENDER_TYPE_CANVAS; + // Could be ignored by module settings + var canvasRenderer = require('./renderer/canvas'); + renderer.initCanvas(localCanvas, canvasRenderer); this._renderContext = renderer.device._ctx; } cc.renderer = renderer; diff --git a/cocos2d/core/platform/CCView.js b/cocos2d/core/platform/CCView.js index a2879e9b6f..f8d3ec0336 100644 --- a/cocos2d/core/platform/CCView.js +++ b/cocos2d/core/platform/CCView.js @@ -768,11 +768,11 @@ View.prototype = { cc.winSize.width = director._winSizeInPoints.width; cc.winSize.height = director._winSizeInPoints.height; - renderer.updateCameraViewport(); - this._originalScaleX = this._scaleX; this._originalScaleY = this._scaleY; cc.visibleRect && cc.visibleRect.init(this._visibleRect); + + renderer.updateCameraViewport(); }, /** diff --git a/cocos2d/core/renderer/canvas/canvas-render-walker.js b/cocos2d/core/renderer/canvas/canvas-render-walker.js new file mode 100644 index 0000000000..7165ea0a12 --- /dev/null +++ b/cocos2d/core/renderer/canvas/canvas-render-walker.js @@ -0,0 +1,72 @@ +/**************************************************************************** + Copyright (c) 2013-2017 Chukong Technologies Inc. + + 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. + Chukong Aipu 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. + ****************************************************************************/ + +const js = require('../../platform/js'); +const renderers = require('./renderers'); +const rendererMap = renderers.map; +const postRendererMap = renderers.postMap; + +let RenderComponentWalker = function (device, defaultCamera) { + this._device = device; + let vx = this._device._vx; + let vy = this._device._vy; + let vh = this._device._vh; + this._camera = defaultCamera; +}; + +RenderComponentWalker.prototype = { + constructor: RenderComponentWalker, + + reset() {}, + + _handleRender (node) { + let comp = node._renderComponent; + let compName = js.getClassName(comp); + if (compName && rendererMap[compName]) { + rendererMap[compName](comp); + } + }, + + _postHandleRender (node) { + let comp = node._renderComponent; + let compName = js.getClassName(comp); + if (compName && postRendererMap[compName]) { + postRendererMap[compName](comp); + } + }, + + _render (comp, renderer) { + let ctx = this._device._ctx; + let cam = this._camera; + ctx.setTransform(cam.a, cam.b, cam.c, cam.d, cam.tx, cam.ty); + let drawCall = renderer(ctx, comp); + this.device._stats.drawcalls += drawCall; + }, + + visit (scene) { + this.device._stats.drawcalls = 0; + scene.walk(this._handleRender, this._postHandleRender); + } +} \ No newline at end of file diff --git a/cocos2d/core/renderer/canvas/forward-renderer.js b/cocos2d/core/renderer/canvas/forward-renderer.js new file mode 100644 index 0000000000..6df0b996a1 --- /dev/null +++ b/cocos2d/core/renderer/canvas/forward-renderer.js @@ -0,0 +1,41 @@ +/**************************************************************************** + Copyright (c) 2013-2017 Chukong Technologies Inc. + + 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. + Chukong Aipu 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. + ****************************************************************************/ + +let ForwardRenderer = function () { +} + +ForwardRenderer.prototype = { + constructor: ForwardRenderer, + + _reset () { + + }, + + render () { + + } +}; + +module.exports = ForwardRenderer; \ No newline at end of file diff --git a/cocos2d/core/renderer/canvas/index.js b/cocos2d/core/renderer/canvas/index.js new file mode 100644 index 0000000000..137da97aac --- /dev/null +++ b/cocos2d/core/renderer/canvas/index.js @@ -0,0 +1,30 @@ +/**************************************************************************** + Copyright (c) 2013-2017 Chukong Technologies Inc. + + 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. + Chukong Aipu 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. + ****************************************************************************/ + +module.exports = { + ForwardRenderer: require('./forward-renderer'), + RenderComponentWalker: require('./canvas-render-walker'), + _renderers: require('./renderers') +}; \ No newline at end of file diff --git a/cocos2d/core/renderer/canvas/renderers/index.js b/cocos2d/core/renderer/canvas/renderers/index.js new file mode 100644 index 0000000000..6aa3aac752 --- /dev/null +++ b/cocos2d/core/renderer/canvas/renderers/index.js @@ -0,0 +1,60 @@ +/**************************************************************************** + Copyright (c) 2013-2017 Chukong Technologies Inc. + + 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. + Chukong Aipu 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. + ****************************************************************************/ + +let js = require('../../../platform/js'); + +let Sprite = require('../../../components/CCSprite'); +let Label = require('../../../components/CCLabel'); +let Mask = require('../../../components/CCMask'); +let RichText = require('../../../components/CCRichText'); +// the following should be placed into their own module folder +// let Graphics = require('../../../graphics/graphics'); +// let MotionStreak = require('../../../../motion-streak/CCMotionStreak'); +// let ParticleSystem = require('../../../../particle/CCParticleSystem'); +// let TiledLayer = require('../../../../tilemap/CCTiledLayer'); +// let Skeleton = require('../../../../../extensions/spine/Skeleton'); +// let Armature = require('../../../../../extensions/dragonbones/ArmatureDisplay'); + +let map = {}; +let postMap = {}; + +function addRenderer (component, handler, postHandler) { + let name = js.getClassName(component); + map[name] = handler; + if (postHandler) { + postMap[name] = postHandler; + } +} + +addRenderer(Sprite, null); +addRenderer(Label, null); +addRenderer(Mask, null); +addRenderer(RichText, null); + +module.exports = { + map, + postMap, + addRenderer +}; \ No newline at end of file diff --git a/cocos2d/core/renderer/canvas/renderers/sprite.js b/cocos2d/core/renderer/canvas/renderers/sprite.js new file mode 100644 index 0000000000..88a7115607 --- /dev/null +++ b/cocos2d/core/renderer/canvas/renderers/sprite.js @@ -0,0 +1,25 @@ +/**************************************************************************** + Copyright (c) 2013-2017 Chukong Technologies Inc. + + 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. + Chukong Aipu 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. + ****************************************************************************/ + diff --git a/cocos2d/core/renderer/index.js b/cocos2d/core/renderer/index.js index 13ff79325c..9eee4ffb9c 100644 --- a/cocos2d/core/renderer/index.js +++ b/cocos2d/core/renderer/index.js @@ -61,6 +61,8 @@ function _initBuiltins(device) { module.exports = { renderEngine: renderEngine, + Texture2D: null, + canvas: null, device: null, scene: null, @@ -71,7 +73,9 @@ module.exports = { _camera: null, _forward: null, - init (canvas, opts) { + initWebGL (canvas, opts) { + this.Texture2D = renderEngine.Texture2D; + this.canvas = canvas; this.device = new renderEngine.Device(canvas, opts); this.scene = new renderEngine.Scene(); @@ -104,24 +108,46 @@ module.exports = { this._forward = new renderEngine.ForwardRenderer(this.device, builtins); }, + initCanvas (canvas, canvasRenderer) { + this.Texture2D = renderEngine.canvas.Texture2D; + + this.canvas = canvas; + this.device = new renderEngine.canvas.Device(canvas); + this._camera = { + a: 1, b: 0, c: 0, d: 1, tx: 0, ty: 0 + }; + this._walker = new canvasRenderer.RenderComponentWalker(this.device, this._camera); + this._forward = new canvasRenderer.ForwardRenderer(); + }, + updateCameraViewport () { // TODO: remove HACK if (!CC_EDITOR && cc.director) { - var ecScene = cc.director.getScene(); + let ecScene = cc.director.getScene(); ecScene.scaleX = ecScene.scaleY = 1; } - let node = this._cameraNode; - let canvas = this.canvas; - let scaleX = cc.view.getScaleX(); - let scaleY = cc.view.getScaleY(); - let zeye = canvas.height / scaleY / 1.1566; - _pos.x = node.x = canvas.width / scaleX / 2; - _pos.y = node.y = canvas.height / scaleY / 2; - node.z = zeye; - _pos.z = 0; - node.lookAt(_pos); - this._camera.dirty = true; + if (cc.game.renderType === cc.game.RENDER_TYPE_CANVAS) { + let vp = cc.view.getViewPortRect(); + this.device.setViewport(vp.x, vp.y, vp.width, vp.height); + this._camera.a = cc.view.getScaleX(); + this._camera.d = -cc.view.getScaleY(); + this._camera.tx = vp.x; + this._camera.ty = vp.y + vp.height; + } + else { + let node = this._cameraNode; + let canvas = this.canvas; + let scaleX = cc.view.getScaleX(); + let scaleY = cc.view.getScaleY(); + let zeye = canvas.height / scaleY / 1.1566; + _pos.x = node.x = canvas.width / scaleX / 2; + _pos.y = node.y = canvas.height / scaleY / 2; + node.z = zeye; + _pos.z = 0; + node.lookAt(_pos); + this._camera.dirty = true; + } }, render (ecScene) { @@ -129,7 +155,7 @@ module.exports = { if (ecScene) { // walk entity component scene to generate models this._walker.visit(ecScene); - this.drawCalls = this.scene.getModelCount(); + this.drawCalls = this.device._stats.drawcalls; // Render models in renderer scene this._forward.render(this.scene); } diff --git a/cocos2d/core/renderer/render-engine.js b/cocos2d/core/renderer/render-engine.js index 66c8872014..34d595339c 100644 --- a/cocos2d/core/renderer/render-engine.js +++ b/cocos2d/core/renderer/render-engine.js @@ -14586,10 +14586,148 @@ module.exports = (function () { } } + class Device$2 { + /** + * @param {HTMLElement} canvasEL + */ + constructor(canvasEL) { + let ctx; + + try { + ctx = canvasEL.getContext('2d'); + } catch (err) { + console.error(err); + return; + } + + // statics + this._canvas = canvasEL; + this._ctx = ctx; + this._caps = {}; // capability + this._stats = { + drawcalls: 0, + }; + + // runtime + this._vx = this._vy = this._vw = this._vh = 0; + this._sx = this._sy = this._sw = this._sh = 0; + } + + _restoreTexture (unit) { + } + + // =============================== + // Immediate Settings + // =============================== + + /** + * @method setViewport + * @param {Number} x + * @param {Number} y + * @param {Number} w + * @param {Number} h + */ + setViewport(x, y, w, h) { + if ( + this._vx !== x || + this._vy !== y || + this._vw !== w || + this._vh !== h + ) { + this._vx = x; + this._vy = y; + this._vw = w; + this._vh = h; + } + } + + /** + * @method setScissor + * @param {Number} x + * @param {Number} y + * @param {Number} w + * @param {Number} h + */ + setScissor(x, y, w, h) { + if ( + this._sx !== x || + this._sy !== y || + this._sw !== w || + this._sh !== h + ) { + this._sx = x; + this._sy = y; + this._sw = w; + this._sh = h; + } + } + + clear(color) { + let ctx = this._ctx; + ctx.clearRect(this._vx, this._vy, this._vw, this._vh); + if (color && (color[0] !== 0 || color[1] !== 0 || color[2] !== 0)) { + ctx.fillStyle = 'rgb(' + color[0] + ',' + color[1] + ',' + color[2] +')'; + ctx.globalAlpha = color[3]; + ctx.fillRect(this._vx, this._vy, this._vw, this._vh); + } + } + } + + class Texture2D$2 { + + /** + * @constructor + * @param {Device} device + * @param {Object} options + * @param {Array} options.images + * @param {Number} options.width + * @param {Number} options.height + */ + constructor(device, options) { + this._device = device; + + this._width = 4; + this._height = 4; + + this._image = null; + + if (options) { + if (options.width !== undefined) { + this._width = options.width; + } + if (options.height !== undefined) { + this._height = options.height; + } + + this.updateImage(options); + } + } + + update (options) { + this.updateImage(options); + } + + updateImage(options) { + if (options.images && options.images[0]) { + let image = options.images[0]; + if (image && image !== this._image) { + this._image = image; + } + } + } + + destroy () { + this._image = null; + } + } + + var canvas = { + Device: Device$2, + Texture2D: Texture2D$2 + }; + // intenral // deps - // import canvas from './lib/canvas'; - const Scene = renderer.Scene; const Camera = renderer.Camera; const View = renderer.View; @@ -14604,6 +14742,9 @@ module.exports = (function () { ForwardRenderer, Texture2D, + // Canvas render support + canvas, + // render scene Scene, Camera, @@ -14644,4 +14785,5 @@ module.exports = (function () { return renderEngine; - }()); \ No newline at end of file + }()); + \ No newline at end of file diff --git a/cocos2d/core/textures/CCTexture2D.js b/cocos2d/core/textures/CCTexture2D.js index 2382204379..70c5ec0535 100644 --- a/cocos2d/core/textures/CCTexture2D.js +++ b/cocos2d/core/textures/CCTexture2D.js @@ -33,7 +33,6 @@ const renderer = require('../renderer'); require('../platform/CCClass'); const gfx = renderEngine.gfx; -const TextureImpl = renderEngine.Texture2D; const GL_NEAREST = 9728; // gl.NEAREST const GL_LINEAR = 9729; // gl.LINEAR @@ -494,7 +493,7 @@ var Texture2D = cc.Class({ opts.wrapT = this._wrapT; if (!this._texture) { - this._texture = new TextureImpl(renderer.device, opts); + this._texture = new renderer.Texture2D(renderer.device, opts); } else { this._texture.update(opts); diff --git a/modules.json b/modules.json index b44e22702b..1d2f383732 100644 --- a/modules.json +++ b/modules.json @@ -18,6 +18,12 @@ "./cocos2d/core/components/CCSprite.js" ] }, + { + "name": "Canvas Renderer", + "entries": [ + "./cocos2d/core/renderer/canvas/index.js" + ] + }, { "name": "Label", "entries": [