Merge pull request #2375 from pandamicro/next

Add canvas renderer basic architecture
This commit is contained in:
pandamicro
2018-02-28 10:40:12 +08:00
committed by GitHub
11 changed files with 428 additions and 25 deletions
+6 -4
View File
@@ -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;
+2 -2
View File
@@ -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();
},
/**
@@ -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);
}
}
@@ -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;
+30
View File
@@ -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')
};
@@ -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
};
@@ -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.
****************************************************************************/
+40 -14
View File
@@ -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);
}
+145 -3
View File
@@ -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;
}());
}());
+1 -2
View File
@@ -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);
+6
View File
@@ -18,6 +18,12 @@
"./cocos2d/core/components/CCSprite.js"
]
},
{
"name": "Canvas Renderer",
"entries": [
"./cocos2d/core/renderer/canvas/index.js"
]
},
{
"name": "Label",
"entries": [