mirror of
https://github.com/cocos/cocos4.git
synced 2026-12-05 19:12:40 +08:00
Merge pull request #2519 from minggo/optimize
Add Effect, Technique, Pass and Model
This commit is contained in:
@@ -0,0 +1,76 @@
|
||||
let config = renderer.config;
|
||||
|
||||
class Effect {
|
||||
/**
|
||||
* @param {Array} techniques
|
||||
*/
|
||||
constructor(techniques, properties = {}, defines = []) {
|
||||
this._techniques = techniques;
|
||||
this._properties = properties;
|
||||
this._defines = defines;
|
||||
|
||||
// TODO: check if params is valid for current technique???
|
||||
}
|
||||
|
||||
clear() {
|
||||
this._techniques.length = 0;
|
||||
this._properties = null;
|
||||
this._defines.length = 0;
|
||||
}
|
||||
|
||||
getTechnique(stage) {
|
||||
let stageID = config.stageID(stage);
|
||||
for (let i = 0; i < this._techniques.length; ++i) {
|
||||
let tech = this._techniques[i];
|
||||
if (tech.stageIDs & stageID) {
|
||||
return tech;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
getProperty(name) {
|
||||
return this._properties[name];
|
||||
}
|
||||
|
||||
setProperty(name, value) {
|
||||
// TODO: check if params is valid for current technique???
|
||||
this._properties[name] = value;
|
||||
}
|
||||
|
||||
getDefine(name) {
|
||||
for (let i = 0; i < this._defines.length; ++i) {
|
||||
let def = this._defines[i];
|
||||
if ( def.name === name ) {
|
||||
return def.value;
|
||||
}
|
||||
}
|
||||
|
||||
console.warn(`Failed to get define ${name}, define not found.`);
|
||||
return null;
|
||||
}
|
||||
|
||||
define(name, value) {
|
||||
for (let i = 0; i < this._defines.length; ++i) {
|
||||
let def = this._defines[i];
|
||||
if ( def.name === name ) {
|
||||
def.value = value;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
console.warn(`Failed to set define ${name}, define not found.`);
|
||||
}
|
||||
|
||||
extractDefines(out = {}) {
|
||||
for (let i = 0; i < this._defines.length; ++i) {
|
||||
let def = this._defines[i];
|
||||
out[def.name] = def.value;
|
||||
}
|
||||
|
||||
return out;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = Effect;
|
||||
+101
@@ -0,0 +1,101 @@
|
||||
class Model {
|
||||
constructor() {
|
||||
this._poolID = -1;
|
||||
this._node = null;
|
||||
this._inputAssemblers = [];
|
||||
this._effects = [];
|
||||
this._defines = [];
|
||||
this._dynamicIA = false;
|
||||
this._viewID = -1;
|
||||
|
||||
// TODO: we calculate aabb based on vertices
|
||||
// this._aabb
|
||||
}
|
||||
|
||||
get inputAssemblerCount() {
|
||||
return this._inputAssemblers.length;
|
||||
}
|
||||
|
||||
get dynamicIA() {
|
||||
return this._dynamicIA;
|
||||
}
|
||||
|
||||
get drawItemCount() {
|
||||
return this._dynamicIA ? 1 : this._inputAssemblers.length;
|
||||
}
|
||||
|
||||
setNode(node) {
|
||||
this._node = node;
|
||||
}
|
||||
|
||||
setDynamicIA(enabled) {
|
||||
this._dynamicIA = enabled;
|
||||
}
|
||||
|
||||
addInputAssembler(ia) {
|
||||
if (this._inputAssemblers.indexOf(ia) !== -1) {
|
||||
return;
|
||||
}
|
||||
this._inputAssemblers.push(ia);
|
||||
}
|
||||
|
||||
clearInputAssemblers() {
|
||||
this._inputAssemblers.length = 0;
|
||||
}
|
||||
|
||||
addEffect(effect) {
|
||||
if (this._effects.indexOf(effect) !== -1) {
|
||||
return;
|
||||
}
|
||||
this._effects.push(effect);
|
||||
|
||||
//
|
||||
let defs = Object.create(null);
|
||||
effect.extractDefines(defs);
|
||||
this._defines.push(defs);
|
||||
}
|
||||
|
||||
clearEffects() {
|
||||
this._effects.length = 0;
|
||||
this._defines.length = 0;
|
||||
}
|
||||
|
||||
extractDrawItem(out, index) {
|
||||
if (this._dynamicIA) {
|
||||
out.model = this;
|
||||
out.node = this._node;
|
||||
out.ia = null;
|
||||
out.effect = this._effects[0];
|
||||
out.defines = out.effect.extractDefines(this._defines[0]);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if (index >= this._inputAssemblers.length ) {
|
||||
out.model = null;
|
||||
out.node = null;
|
||||
out.ia = null;
|
||||
out.effect = null;
|
||||
out.defines = null;
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
out.model = this;
|
||||
out.node = this._node;
|
||||
out.ia = this._inputAssemblers[index];
|
||||
|
||||
let effect, defines;
|
||||
if (index < this._effects.length) {
|
||||
effect = this._effects[index];
|
||||
defines = this._defines[index];
|
||||
} else {
|
||||
effect = this._effects[this._effects.length-1];
|
||||
defines = this._defines[this._effects.length-1];
|
||||
}
|
||||
out.effect = effect;
|
||||
out.defines = effect.extractDefines(defines);
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = Model;
|
||||
+115
@@ -0,0 +1,115 @@
|
||||
class Pass {
|
||||
constructor(name) {
|
||||
this._programName = name;
|
||||
|
||||
// cullmode
|
||||
this._cullMode = gfx.CULL_BACK;
|
||||
|
||||
// blending
|
||||
this._blend = false;
|
||||
this._blendEq = gfx.BLEND_FUNC_ADD;
|
||||
this._blendAlphaEq = gfx.BLEND_FUNC_ADD;
|
||||
this._blendSrc = gfx.BLEND_ONE;
|
||||
this._blendDst = gfx.BLEND_ZERO;
|
||||
this._blendSrcAlpha = gfx.BLEND_ONE;
|
||||
this._blendDstAlpha = gfx.BLEND_ZERO;
|
||||
this._blendColor = 0xffffffff;
|
||||
|
||||
// depth
|
||||
this._depthTest = false;
|
||||
this._depthWrite = false;
|
||||
this._depthFunc = gfx.DS_FUNC_LESS,
|
||||
|
||||
// stencil
|
||||
this._stencilTest = false;
|
||||
// front
|
||||
this._stencilFuncFront = gfx.DS_FUNC_ALWAYS;
|
||||
this._stencilRefFront = 0;
|
||||
this._stencilMaskFront = 0xff;
|
||||
this._stencilFailOpFront = gfx.STENCIL_OP_KEEP;
|
||||
this._stencilZFailOpFront = gfx.STENCIL_OP_KEEP;
|
||||
this._stencilZPassOpFront = gfx.STENCIL_OP_KEEP;
|
||||
this._stencilWriteMaskFront = 0xff;
|
||||
// back
|
||||
this._stencilFuncBack = gfx.DS_FUNC_ALWAYS;
|
||||
this._stencilRefBack = 0;
|
||||
this._stencilMaskBack = 0xff;
|
||||
this._stencilFailOpBack = gfx.STENCIL_OP_KEEP;
|
||||
this._stencilZFailOpBack = gfx.STENCIL_OP_KEEP;
|
||||
this._stencilZPassOpBack = gfx.STENCIL_OP_KEEP;
|
||||
this._stencilWriteMaskBack = 0xff;
|
||||
}
|
||||
|
||||
setCullMode(cullMode) {
|
||||
this._cullMode = cullMode;
|
||||
}
|
||||
|
||||
setBlend(
|
||||
blendEq = gfx.BLEND_FUNC_ADD,
|
||||
blendSrc = gfx.BLEND_ONE,
|
||||
blendDst = gfx.BLEND_ZERO,
|
||||
blendAlphaEq = gfx.BLEND_FUNC_ADD,
|
||||
blendSrcAlpha = gfx.BLEND_ONE,
|
||||
blendDstAlpha = gfx.BLEND_ZERO,
|
||||
blendColor = 0xffffffff
|
||||
) {
|
||||
this._blend = true;
|
||||
this._blendEq = blendEq;
|
||||
this._blendSrc = blendSrc;
|
||||
this._blendDst = blendDst;
|
||||
this._blendAlphaEq = blendAlphaEq;
|
||||
this._blendSrcAlpha = blendSrcAlpha;
|
||||
this._blendDstAlpha = blendDstAlpha;
|
||||
this._blendColor = blendColor;
|
||||
}
|
||||
|
||||
setDepth(
|
||||
depthTest = false,
|
||||
depthWrite = false,
|
||||
depthFunc = gfx.DS_FUNC_LESS
|
||||
) {
|
||||
this._depthTest = depthTest;
|
||||
this._depthWrite = depthWrite;
|
||||
this._depthFunc = depthFunc;
|
||||
}
|
||||
|
||||
setStencilFront(
|
||||
stencilFunc = gfx.DS_FUNC_ALWAYS,
|
||||
stencilRef = 0,
|
||||
stencilMask = 0xff,
|
||||
stencilFailOp = gfx.STENCIL_OP_KEEP,
|
||||
stencilZFailOp = gfx.STENCIL_OP_KEEP,
|
||||
stencilZPassOp = gfx.STENCIL_OP_KEEP,
|
||||
stencilWriteMask = 0xff
|
||||
) {
|
||||
this._stencilTest = true;
|
||||
this._stencilFuncFront = stencilFunc;
|
||||
this._stencilRefFront = stencilRef;
|
||||
this._stencilMaskFront = stencilMask;
|
||||
this._stencilFailOpFront = stencilFailOp;
|
||||
this._stencilZFailOpFront = stencilZFailOp;
|
||||
this._stencilZPassOpFront = stencilZPassOp;
|
||||
this._stencilWriteMaskFront = stencilWriteMask;
|
||||
}
|
||||
|
||||
setStencilBack(
|
||||
stencilFunc = gfx.DS_FUNC_ALWAYS,
|
||||
stencilRef = 0,
|
||||
stencilMask = 0xff,
|
||||
stencilFailOp = gfx.STENCIL_OP_KEEP,
|
||||
stencilZFailOp = gfx.STENCIL_OP_KEEP,
|
||||
stencilZPassOp = gfx.STENCIL_OP_KEEP,
|
||||
stencilWriteMask = 0xff
|
||||
) {
|
||||
this._stencilTest = true;
|
||||
this._stencilFuncBack = stencilFunc;
|
||||
this._stencilRefBack = stencilRef;
|
||||
this._stencilMaskBack = stencilMask;
|
||||
this._stencilFailOpBack = stencilFailOp;
|
||||
this._stencilZFailOpBack = stencilZFailOp;
|
||||
this._stencilZPassOpBack = stencilZPassOp;
|
||||
this._stencilWriteMaskBack = stencilWriteMask;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = Pass;
|
||||
+75
-2
@@ -59,7 +59,18 @@ renderer.CLEAR_COLOR = 1;
|
||||
renderer.CLEAR_DEPTH = 2;
|
||||
renderer.CLEAR_STENCIL = 4;
|
||||
|
||||
renderer.addStage = renderer.Config.addStage;
|
||||
//renderer.addStage = renderer.Config.addStage;
|
||||
|
||||
renderer.config = {
|
||||
addStage: renderer.addStage,
|
||||
// stageIDs: renderer.stageIDs,
|
||||
stageIDs: renderer.stageIDs,
|
||||
stageID: renderer.stageID
|
||||
};
|
||||
renderer.Effect = require('./Effect');
|
||||
renderer.Technique = require('./Technique');
|
||||
renderer.Pass = require('./Pass');
|
||||
renderer.Model = require('./Model');
|
||||
|
||||
// ForwardRenderer adapter
|
||||
var _p;
|
||||
@@ -74,11 +85,73 @@ _p = renderer.InputAssembler.prototype;
|
||||
_p._ctor = function(vb, ib, pt = gfx.PT_TRIANGLES) {
|
||||
this.init(vb, ib, pt);
|
||||
};
|
||||
|
||||
cc.defineGetterSetter(_p, "_vertexBuffer", _p.getVertexBuffer, _p.setVertexBuffer);
|
||||
cc.defineGetterSetter(_p, "_indexBuffer", _p.getIndexBuffer, _p.setIndexBuffer);
|
||||
cc.defineGetterSetter(_p, "_primitiveType", _p.getPrimitiveType, _p.setPrimitiveType);
|
||||
cc.defineGetterSetter(_p, "_start", _p.getStart, _p.setStart);
|
||||
cc.defineGetterSetter(_p, "_count", _p.getCount, _p.setCount);
|
||||
|
||||
// Scene
|
||||
_p = renderer.Scene.prototype;
|
||||
// _p.addModel = function(model) {
|
||||
// // var modelNative = new renderer.ModelNative();
|
||||
// // var effects = model._effects;
|
||||
// // var effectNatives = [];
|
||||
// // for (var i = 0, len = effects.length; i < len; ++i) {
|
||||
// // var effect = effects[i];
|
||||
// // var techniques = effect._techniques;
|
||||
// // var techniqueNatives = [];
|
||||
// // for (var j = 0, len1 = techniques.length; j < len1; ++j) {
|
||||
// // var technique = techniques[j];
|
||||
// // var passesNatives = [];
|
||||
// // var passes = technique.passes;
|
||||
// // for (var k =0, len2 = passes.length; k < len2; ++k) {
|
||||
// // var pass = passes[k];
|
||||
// // var passNative = new renderer.PassNative(pass._programName);
|
||||
// // passNative.setCullMode(pass._cullMode);
|
||||
// // passNative.setBlend(pass._blendEq,
|
||||
// // pass._blendSrc,
|
||||
// // pass._blendDst,
|
||||
// // pass._blendAlphaEq,
|
||||
// // pass._blendSrcAlpha,
|
||||
// // pass._blendDstAlpha,
|
||||
// // pass._blendColor);
|
||||
// // passNative.setDepth(pass._depthTest, pass._depthWrite, pass._depthFunc);
|
||||
// // passNative.setStencilFront(pass._stencilFuncFront,
|
||||
// // pass._stencilRefFront,
|
||||
// // pass._stencilMaskFront,
|
||||
// // pass._stencilFailOpFront,
|
||||
// // pass._stencilZFailOpFront,
|
||||
// // pass._stencilZPassOpFront,
|
||||
// // pass._stencilWriteMaskFront);
|
||||
// // passNative.setStencilBack(pass._stencilFuncBack,
|
||||
// // pass._stencilRefBack,
|
||||
// // pass._stencilMaskBack,
|
||||
// // pass._stencilFailOpBack,
|
||||
// // pass._stencilZFailOpBack,
|
||||
// // pass._stencilZPassOpBack,
|
||||
// // pass._stencilWriteMaskBack);
|
||||
// // passesNatives.push(passNative);
|
||||
// // }
|
||||
// // var techniqueNative = new renderer.TechniqueNative(technique._stages, technique._parameters, passesNatives, technique._layer);
|
||||
// // techniqueNatives.push(techniqueNative);
|
||||
// // }
|
||||
// // var effectNative = new renderer.EffectNative(techniqueNatives, effect._properties, effect._defines);
|
||||
// // modelNative.addEffect(effectNative);
|
||||
// // }
|
||||
|
||||
// // modelNative.setDynamicIA(model._dynamicIA);
|
||||
// // modelNative.setViewId(model._viewID);
|
||||
// // modelNative.setNode(model._node);
|
||||
|
||||
// // var inputAssemblers = model._inputAssemblers;
|
||||
// // for (var k = 0, len2 = inputAssemblers.length; k < len2; ++k) {
|
||||
// // modelNative.addInputAssembler(inputAssemblers[k]);
|
||||
// // }
|
||||
|
||||
// // this.addModelNative(modelNative);
|
||||
// this.addModelNative(model);
|
||||
// };
|
||||
_p.removeModel = function() {}
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,38 @@
|
||||
let config = renderer.config;
|
||||
|
||||
let _genID = 0;
|
||||
|
||||
class Technique {
|
||||
/**
|
||||
* @param {Array} stages
|
||||
* @param {Array} parameters
|
||||
* @param {Array} passes
|
||||
* @param {Number} layer
|
||||
*/
|
||||
constructor(stages, parameters, passes, layer = 0) {
|
||||
this._id = _genID++;
|
||||
this._stageIDs = config.stageIDs(stages);
|
||||
this.stageIDs = this._stageIDs;
|
||||
this._parameters = parameters; // {name, type, size, val}
|
||||
this._passes = passes;
|
||||
this.passes = this._passes;
|
||||
this._layer = layer;
|
||||
this._stages = stages;
|
||||
// TODO: this._version = 'webgl' or 'webgl2' // ????
|
||||
}
|
||||
|
||||
setStages(stages) {
|
||||
this._stageIDs = config.stageIDs(stages);
|
||||
this._stages = stages;
|
||||
}
|
||||
|
||||
// get passes() {
|
||||
// return this._passes;
|
||||
// }
|
||||
|
||||
// get stageIDs() {
|
||||
// return this._stageIDs;
|
||||
// }
|
||||
}
|
||||
|
||||
module.exports = Technique;
|
||||
Reference in New Issue
Block a user