Explorar o código

Adding new feature RenderPipeline.
Adding new tool make array.

michael-korbas %!s(int64=11) %!d(string=hai) anos
pai
achega
58d27ae75e

+ 221 - 0
Babylon/PostProcess/RenderPipeline/babylon.renderEffect.js

@@ -0,0 +1,221 @@
+"use strict";
+
+var BABYLON = BABYLON || {};
+
+(function () {
+	BABYLON.RenderEffect = function RenderEffect(engine, name, postProcessType, ratio, samplingMode, singleInstance) {
+		this._engine = engine;
+
+		this._name = name;
+
+		this._singleInstance = singleInstance || true;
+
+		this._postProcesses = [];
+
+		this._postProcessType = postProcessType;
+
+		this._ratio = ratio;
+		this._samplingMode = samplingMode || null;
+
+		this._cameras = [];
+		this._indicesForCamera = [];
+
+		this._renderPasses = [];
+		this._renderEffectAsPasses = [];
+
+		this.parameters = function () { };
+
+	};
+
+	BABYLON.RenderEffect.prototype.addPass = function(renderPass) {
+		this._renderPasses[renderPass._name] = renderPass;
+
+		this._linkParameters();
+	};
+
+	BABYLON.RenderEffect.prototype.removePass = function (renderPass) {
+		delete this._renderPasses[renderPass._name];
+
+		this._linkParameters();
+	};
+
+	BABYLON.RenderEffect.prototype.addRenderEffectAsPass = function(renderEffect) {
+		this._renderEffectAsPasses[renderEffect._name] = renderEffect;
+
+		this._linkParameters();
+	};
+
+	BABYLON.RenderEffect.prototype.getPass = function (passName) {
+	    for (var renderPassName in this._renderPasses) {
+	        if (renderPassName == passName) {
+	            return this._renderPasses[passName];
+	        }
+	    }
+	};
+
+	BABYLON.RenderEffect.prototype.emptyPasses = function () {
+		this._renderPasses.length = 0;
+
+		this._linkParameters();
+	};
+
+	BABYLON.RenderEffect.prototype.attachCameras = function (cameras) {
+		var postProcess = null;
+
+		cameras = BABYLON.Tools.MakeArray(cameras || this._cameras);
+
+		for (var i = 0; i < cameras.length; i++) {
+			if (this._singleInstance) {
+			    postProcess = this._postProcesses[0] || eval(BABYLON.RenderEffect.getStringToInstantiate(this._postProcessType, this._ratio, this._samplingMode));
+				this._postProcesses[0] = postProcess;
+			}
+			else {
+			    postProcess = this._postProcesses[cameras[i]] || eval(BABYLON.RenderEffect.getStringToInstantiate(this._postProcessType, this._ratio, this._samplingMode));
+				this._postProcesses[cameras[i].name] = postProcess;
+			}
+
+			var index = cameras[i].attachPostProcess(postProcess);
+
+			if (this._indicesForCamera[cameras[i].name] == null) {
+				this._indicesForCamera[cameras[i].name] = [];
+			}
+
+			this._indicesForCamera[cameras[i].name].push(index);
+
+			if (this._cameras.indexOf(cameras[i].name) == -1) {
+				this._cameras.push(cameras[i].name);
+			}
+		}
+
+		this._linkParameters();
+	};
+
+	BABYLON.RenderEffect.prototype.detachCameras = function (cameras) {
+		cameras = BABYLON.Tools.MakeArray(cameras || this._cameras);
+
+		for (var i = 0; i < cameras.length; i++) {
+		    if (this._singleInstance) {
+			    cameras[i].detachPostProcess(this._postProcesses[0], this._indicesForCamera[cameras[i].name]);
+			}
+			else {
+				cameras[i].detachPostProcess(this._postProcesses[cameras[i].name], this._indicesForCamera[cameras[i].name]);
+			}
+
+			this._indicesForCamera.splice(cameras[i].name, 1);
+			this._cameras.splice(this._cameras.indexOf(cameras[i].name), 1);
+		}
+	};
+
+	BABYLON.RenderEffect.prototype._linkParameters = function () {
+		var that = this;
+		for (var index in this._postProcesses) {
+		    this._postProcesses[index].onApply = function (effect) {
+		        that.parameters(effect);
+		        that._linkTextures(effect);
+		    };
+		}
+	};
+
+	BABYLON.RenderEffect.prototype._linkTextures = function () {
+        for (var renderPassName in this._renderPasses) {
+            effect.setTexture(renderPassName, this._renderPasses[renderPassName].getRenderTexture());
+        }
+      
+        for (var renderEffectName in this._renderEffectAsPasses) {
+            effect.setTextureFromPostProcess(renderEffectName + "Sampler", this._renderEffectAsPasses[renderEffectName].getPostProcess());
+        }
+	};
+
+    
+	BABYLON.RenderEffect.prototype._update = function () {
+		for (var renderPassName in this._renderPasses) {
+			this._renderPasses[renderPassName]._update();
+		}
+	};
+
+	BABYLON.RenderEffect.prototype.enable = function (cameras) {
+	    cameras = BABYLON.Tools.MakeArray(cameras || this._cameras);
+
+		for (var i = 0; i < cameras.length; i++) {
+			for (var j = 0; j < this._indicesForCamera[cameras[i].name].length; j++) {
+				if (cameras[i]._postProcesses[this._indicesForCamera[cameras[i].name][j]] === undefined) {
+					if (this._singleInstance) {
+						cameras[i].attachPostProcess(this._postProcesses[0], this._indicesForCamera[cameras[i].name][j]);
+					}
+					else {
+						cameras[i].attachPostProcess(this._postProcesses[cameras[i].name], this._indicesForCamera[cameras[i].name][j]);
+					}
+				}
+			}
+		}
+	};
+
+	BABYLON.RenderEffect.prototype.disable = function (cameras) {
+		cameras = BABYLON.Tools.MakeArray(cameras || this._cameras);
+
+		for (var i = 0; i < cameras.length; i++) {
+		    if (this._singleInstance) {
+				cameras[i].detachPostProcess(this._postProcesses[0], this._indicesForCamera[cameras[i].name]);
+			}
+			else {
+				cameras[i].detachPostProcess(this._postProcesses[cameras[i].name], this._indicesForCamera[cameras[i].name]);
+			}
+		}
+	};
+
+
+	BABYLON.RenderEffect.prototype.getPostProcess = function(camera) {
+		return this._postProcess;
+	};
+
+	BABYLON.RenderEffect.getStringToInstantiate = function (postProcessType, ratio, samplingMode) {
+	    var stringToInstantiate = "new " + postProcessType + "(";
+
+	    var parameters = BABYLON.RenderEffect.getParametersNames(postProcessType);
+	    for (var i = 0; i < parameters.length; i++) {
+	        switch (parameters[i]) {
+	            case "name":
+	                stringToInstantiate += "\"" + postProcessType + "\"";
+	                break;
+	            case "ratio":
+	                stringToInstantiate += ratio;
+	                break;
+	            case "camera":
+	                stringToInstantiate += "null";
+	                break;
+	            case "samplingMode":
+	                stringToInstantiate += samplingMode;
+	                break;
+	            case "engine":
+	                stringToInstantiate += "this._engine";
+	                break;
+	            case "reusable":
+	                stringToInstantiate += "true";
+	                break;
+	            default:
+	                stringToInstantiate += "null";
+	                break;
+	        }
+
+	        if (i + 1 < parameters.length) {
+	            stringToInstantiate += ",";
+	        }
+	    }
+
+	    stringToInstantiate += ")";
+
+	    return stringToInstantiate;
+	};
+
+	BABYLON.RenderEffect.getParametersNames = function (func) {
+	    var commentsRegex = /((\/\/.*$)|(\/\*[\s\S]*?\*\/))/mg;
+	    var functWithoutComments = eval(func).toString().replace(commentsRegex, '');
+
+	    var parameters = functWithoutComments.slice(functWithoutComments.indexOf('(') + 1, functWithoutComments.indexOf(')')).match(/([^\s,]+)/g);
+
+	    if (parameters === null)
+	        parameters = [];
+	    return parameters;
+	};
+
+})();

+ 49 - 0
Babylon/PostProcess/RenderPipeline/babylon.renderPass.js

@@ -0,0 +1,49 @@
+"use strict";
+
+var BABYLON = BABYLON || {};
+
+(function () {
+	BABYLON.RenderPass = function RenderPass(scene, name, size, renderList, beforeRender, afterRender) {
+		this._name = name;
+		this._enabled = true;
+
+		this._renderList = renderList;
+
+		this._renderTexture = new BABYLON.RenderTargetTexture(name, size, scene);
+		this.setRenderList(renderList);
+
+		this._renderTexture.onBeforeRender = beforeRender;
+		this._renderTexture.onAfterRender = afterRender;
+
+		this._refCount = 0;
+
+	};
+
+	BABYLON.RenderPass.prototype.incRefCount = function () {
+	    if (this._refCount == 0) {
+	        scene.customRenderTargets.push(this._renderTexture);
+	    }
+
+	    this._refCount++;
+	};
+
+	BABYLON.RenderPass.prototype.decRefCount = function () {
+	    this._refCount--;
+
+	    if (this._refCount <= 0) {
+	        scene.customRenderTargets.splice(scene.customRenderTargets.indexOf(this._renderTexture), 1);
+	    }
+	};
+
+	BABYLON.RenderPass.prototype.setRenderList = function (renderList) {
+		this._renderTexture.renderList = renderList;
+	};
+
+	BABYLON.RenderPass.prototype.getRenderTexture = function () {
+		return this._renderTexture;
+	};
+
+	BABYLON.RenderPass.prototype._update = function () {
+		this.setRenderList(this._renderList);
+	};
+})();

+ 138 - 0
Babylon/PostProcess/RenderPipeline/babylon.renderPipeline.js

@@ -0,0 +1,138 @@
+"use strict";
+
+var BABYLON = BABYLON || {};
+
+(function () {
+	BABYLON.RenderPipeline = function RenderPipeline(engine, name) {
+		this._engine = engine;
+
+		this._name = name;
+
+		this._renderEffects = [];
+		this._renderEffectsPasses = [];
+
+		this._cameras = [];
+	};
+
+	BABYLON.RenderPipeline.prototype.addEffect = function (renderEffect) {
+		this._renderEffects[renderEffect._name] = renderEffect;
+	};
+
+	BABYLON.RenderPipeline.prototype.enableEffect = function (renderEffectName, cameras) {
+	    cameras = BABYLON.Tools.MakeArray(cameras || this._cameras);
+
+		var renderEffects = this._renderEffects[renderEffectName];
+
+		if (!renderEffects) {
+			return;
+		}
+
+		renderEffects.enable(cameras);
+	};
+
+	BABYLON.RenderPipeline.prototype.disableEffect = function (renderEffectName, cameras) {
+	    cameras = BABYLON.Tools.MakeArray(cameras || this._cameras);
+
+		var renderEffects = this._renderEffects[renderEffectName];
+
+		if (!renderEffects) {
+			return;
+		}
+
+		renderEffects.disable(cameras);
+	};
+
+	BABYLON.RenderPipeline.prototype.attachCameras = function (cameras, unique) {
+		cameras = BABYLON.Tools.MakeArray(cameras || this._cameras);
+		
+		var indicesToDelete = [];
+
+		for (var i = 0; i < cameras.length; i++) {
+			if (this._cameras.indexOf(cameras[i]) == -1) {
+				this._cameras.push(cameras[i]);
+			}
+			else if(unique) {
+				indicesToDelete.push(i);
+			}
+		}
+
+		for (var i = 0; i < indicesToDelete.length; i++) {
+			cameras.splice(indicesToDelete[i], 1);
+		}
+
+		for(var renderEffectName in this._renderEffects) {
+			this._renderEffects[renderEffectName].attachCameras(cameras);
+		}
+	};
+
+	BABYLON.RenderPipeline.prototype.detachCameras = function (cameras) {
+		cameras = BABYLON.Tools.MakeArray(cameras || this._cameras);
+
+		for (var renderEffectName in this._renderEffects) {
+			this._renderEffects[renderEffectName].detachCameras(cameras);
+		}
+
+		for (var i = 0; i < cameras.length; i++) {
+			this._cameras.splice(this._cameras.indexOf(cameras[i]), 1);
+		}
+	};
+
+	BABYLON.RenderPipeline.prototype.enableDisplayOnlyPass = function (passName, cameras) {
+		cameras = BABYLON.Tools.MakeArray(cameras || this._cameras);
+
+		var pass = null;
+
+		for (var renderEffectName in this._renderEffects) {
+			pass = this._renderEffects[renderEffectName].getPass(passName);
+
+			if (pass != null) {
+				break;
+			}
+		}
+
+		if (pass == null) {
+			return;
+		}
+		
+		for (var renderEffectName in this._renderEffects) {
+			this._renderEffects[renderEffectName].disable(cameras);
+		}
+		
+		pass._name = BABYLON.RenderPipeline.PASS_SAMPLER_NAME;
+		
+		for (var i = 0; i < cameras.length; i++) {
+			this._renderEffectsPasses[cameras[i].name] = this._renderEffectsPasses[cameras[i].name] || new BABYLON.RenderEffect(this._engine, BABYLON.RenderPipeline.PASS_EFFECT_NAME, "BABYLON.PassPostProcess");
+			this._renderEffectsPasses[cameras[i].name].attachCameras(cameras[i]);
+			this._renderEffectsPasses[cameras[i].name].emptyPasses();
+			this._renderEffectsPasses[cameras[i].name].addPass(pass);
+		}
+	};
+
+	BABYLON.RenderPipeline.prototype.disableDisplayOnlyPass = function (cameras) {
+	    cameras = BABYLON.Tools.MakeArray(cameras || this._cameras);
+
+		for (var i = 0; i < cameras.length; i++) {
+		    this._renderEffectsPasses[cameras[i].name] = this._renderEffectsPasses[cameras[i].name] || new BABYLON.RenderEffect(this._engine, BABYLON.RenderPipeline.PASS_EFFECT_NAME, "BABYLON.DisplayPassPostProcess", 1.0);
+		    this._renderEffectsPasses[cameras[i].name].disable(cameras[i]);
+		}
+
+		for (var renderEffectName in this._renderEffects) {
+			this._renderEffects[renderEffectName].enable(cameras);
+		}
+	};
+
+	BABYLON.RenderPipeline.prototype._update = function () {
+		for (var renderEffectName in this._renderEffects) {
+			this._renderEffects[renderEffectName]._update();
+		}
+
+		for(var i = 0; i < this._cameras.length; i++) {
+			if (this._renderEffectsPasses[this._cameras[i]]) {
+				this._renderEffectsPasses[this._cameras[i]]._update();
+			}
+		}
+	};
+
+	BABYLON.RenderPipeline.PASS_EFFECT_NAME = "passEffect";
+	BABYLON.RenderPipeline.PASS_SAMPLER_NAME = "passSampler";
+})();

+ 81 - 0
Babylon/PostProcess/RenderPipeline/babylon.renderPipelineManager.js

@@ -0,0 +1,81 @@
+"use strict";
+
+var BABYLON = BABYLON || {};
+
+(function () {
+	BABYLON.RenderPipelineManager = function RenderPipelineManager() {
+		this._renderPipelines = [];
+	};
+
+	BABYLON.RenderPipelineManager.prototype.addPipeline = function(renderPipeline) {
+		this._renderPipelines[renderPipeline._name] = renderPipeline;
+	};
+
+	BABYLON.RenderPipelineManager.prototype.update = function () {
+		for (var renderPipelineName in this._renderPipelines) {
+			this._renderPipelines[renderPipelineName]._update();
+		}
+	};
+
+	BABYLON.RenderPipelineManager.prototype.attachCamerasToRenderPipeline = function (renderPipelineName, cameras, unique) {
+		var renderPipeline = this._renderPipelines[renderPipelineName];
+
+		if (!renderPipeline) {
+			return;
+		}
+
+		renderPipeline.attachCameras(cameras, unique);
+	};
+
+	BABYLON.RenderPipelineManager.prototype.detachCamerasFromRenderPipeline = function (renderPipelineName, cameras) {
+		var renderPipeline = this._renderPipelines[renderPipelineName];
+
+		if (!renderPipeline) {
+			return;
+		}
+
+		renderPipeline.detachCameras(cameras);
+	};
+
+
+	BABYLON.RenderPipelineManager.prototype.enableEffectInPipeline = function (renderPipelineName, renderEffectName, cameras) {
+		var renderPipeline = this._renderPipelines[renderPipelineName];
+
+		if (!renderPipeline) {
+			return;
+		}
+
+		renderPipeline.enableEffect(renderEffectName, cameras);
+	};
+
+	BABYLON.RenderPipelineManager.prototype.disableEffectInPipeline = function (renderPipelineName, renderEffectName, cameras) {
+		var renderPipeline = this._renderPipelines[renderPipelineName];
+
+		if (!renderPipeline) {
+			return;
+		}
+
+		renderPipeline.disableEffect(renderEffectName, cameras);
+	};
+
+
+	BABYLON.RenderPipelineManager.prototype.enableDisplayOnlyPassInPipeline = function (renderPipelineName, passName, cameras) {
+		var renderPipeline = this._renderPipelines[renderPipelineName];
+
+		if (!renderPipeline) {
+			return;
+		}
+
+		renderPipeline.enableDisplayOnlyPass(passName, cameras);
+	};
+
+	BABYLON.RenderPipelineManager.prototype.disableDisplayOnlyPassInPipeline = function (renderPipelineName, cameras) {
+		var renderPipeline = this._renderPipelines[renderPipelineName];
+
+		if (!renderPipeline) {
+			return;
+		}
+
+		renderPipeline.disableDisplayOnlyPass(cameras);
+	};
+})();

+ 7 - 0
Babylon/Tools/babylon.tools.js

@@ -51,6 +51,13 @@ var BABYLON = BABYLON || {};
             maximum: maximum
         };
     };
+	
+	BABYLON.Tools.MakeArray = function (obj, allowsNullUndefined) {
+		if (allowsNullUndefined !== true && (obj === undefined || obj == null))
+			return undefined;
+
+		return Array.isArray(obj) ? obj : [obj];
+    };
 
     // Smart array
     BABYLON.Tools.SmartArray = function(capacity) {