Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Clean up

  • Loading branch information...
commit dae952a5786da3c44fa9da1dc9713c952866d34d 1 parent 1f906d4
@mortennobel authored
Showing with 116 additions and 191 deletions.
  1. +116 −191 src/js/material.js
View
307 src/js/material.js
@@ -1005,7 +1005,7 @@ KICK.namespace = function (ns_string) {
* @param {Object} engineUniforms
* @param {KICK.scene.Transform) transform
*/
- material.Shader.prototype.bindUniform = function(material, engineUniforms, transform){
+ material.Shader.prototype.bindUniform = function (material, engineUniforms, transform) {
var lookupUniform = this.lookupUniform,
gl = this.gl,
modelMatrix = lookupUniform._m,
@@ -1087,30 +1087,13 @@ KICK.namespace = function (ns_string) {
* @param {Object} config
* @extends KICK.core.ProjectAsset
*/
- material.Material = function (engine,config) {
+ material.Material = function (engine, config) {
var _name = "Material",
_shader = null,
_uniforms = [],
thisObj = this,
gl = engine.gl,
_renderOrder = 0,
- /*inheritDefaultUniformsFromShader = function(){
- var shaderDefaultUniforms = _shader.defaultUniforms;
- var dirty = false;
- for (var name in shaderDefaultUniforms){
- if (!_uniforms[name]){
- var defaultValue = shaderDefaultUniforms[name];
- _uniforms[name] = {
- value: defaultValue.value,
- type: defaultValue.type
- };
- dirty = true;
- }
- }
- if (dirty){
- verifyUniforms();
- }
- },*/
/**
* Called when a shader is set or changed.
* Add location and type information to each uniform.
@@ -1121,8 +1104,10 @@ KICK.namespace = function (ns_string) {
*/
decorateUniforms = function () {
var i, uniform,
- foundUniformNames = {};
- for (i =_uniforms.length - 1; i >= 0; i--) {
+ foundUniformNames = {},
+ name,
+ element;
+ for (i = _uniforms.length - 1; i >= 0; i--) {
uniform = _shader.lookupUniform[_uniforms[i].name];
if (uniform) {
_uniforms[i].type = uniform.type;
@@ -1133,86 +1118,50 @@ KICK.namespace = function (ns_string) {
}
}
// add items not found
- for (i=0;i<_shader.materialUniforms.length;i++) {
+ for (i = 0; i < _shader.materialUniforms.length; i++) {
uniform = _shader.materialUniforms[i];
- var name = uniform.name;
+ name = uniform.name;
if (!foundUniformNames.hasOwnProperty(name)) {
// add default name
- var element = thisObj.setUniform(name, _shader.defaultUniforms[name]);
+ element = thisObj.setUniform(name, _shader.defaultUniforms[name]);
element.location = uniform.location;
element.type = uniform.type;
}
}
- }//,
- /*
- * The method replaces any invalid uniform (Array or numbers) with a wrapped one (Float32Array or Int32Array)
- * @method verifyUniforms
- * @private
- */
- /*verifyUniforms = function(){
- var uniformName,
- type,
- uniformValue,
- c = KICK.core.Constants;
+ };
- for (uniformName in _uniforms){
- uniformValue = _uniforms[uniformName].value;
- type = _uniforms[uniformName].type;
- if (type === c.GL_SAMPLER_2D || type ===c.GL_SAMPLER_CUBE ){
- if (uniformValue && typeof uniformValue.ref === 'number'){
- _uniforms[uniformName].value = engine.project.load(uniformValue.ref);
- }
- if (c._ASSERT){
- if (!(_uniforms[uniformName].value instanceof KICK.texture.Texture)){
- KICK.core.Util.fail("Uniform value should be a texture object but was "+uniformValue);
- }
- }
- }
- if (Array.isArray(uniformValue) || typeof uniformValue === 'number'){
- var array = uniformValue;
- if (typeof array === 'number'){
- array = [array];
- }
- if (type === c.GL_INT || type===c.GL_INT_VEC2 || type===c.GL_INT_VEC3 || type===c.GL_INT_VEC4){
- _uniforms[uniformName].value = new Int32Array(array);
- } else {
- _uniforms[uniformName].value = new Float32Array(array);
- }
- }
- }
- }*/;
- Object.defineProperties(this,{
+ Object.defineProperties(this, {
/**
* @property engine
* @type KICK.core.Engine
*/
- engine:{
- value:engine
+ engine: {
+ value: engine
+ },
+ /**
+ * @property name
+ * @type String
+ */
+ name: {
+ get: function () { return _name; },
+ set: function (newValue) { _name = newValue; }
},
- /**
- * @property name
- * @type String
- */
- name:{
- get:function(){return _name;},
- set:function(newValue){_name = newValue;}
- },
/**
* @property shader
* @type KICK.material.Shader
*/
- shader:{
- get:function(){
+ shader: {
+ get: function () {
return _shader;
},
- set:function(newValue){
- if (!newValue instanceof KICK.material.Shader){
+ set: function (newValue) {
+ if (!newValue instanceof KICK.material.Shader) {
fail("KICK.material.Shader expected");
}
- if (_shader !==newValue){
+ if (_shader !== newValue) {
_shader = newValue;
- if (_shader){
+ if (_shader) {
thisObj.init();
_renderOrder = _shader.renderOrder;
decorateUniforms();
@@ -1225,45 +1174,15 @@ KICK.namespace = function (ns_string) {
* @property uniforms
* @type Object
*/
- uniforms:{
- value:null
+ uniforms: {
+ value: null
},
- /*
- * Object with of uniforms.
- * The object has a number of named properties one for each uniform. The uniform object contains value and type.
- * The value is always an array<br>
- * Note when updating the a uniform through a reference to uniforms, it is important to call the material.shader.markUniformUpdated().
- * When the material.uniform is set to something the markUniformUpdated function is implicit called.
- * @property uniforms
- * @type Object
- */
- /*uniforms:{
- get:function(){
- return _uniforms;
- },
- set:function(newValue){
- if (newValue != _uniforms){
- newValue = newValue || {};
- for (var name in newValue){
- if (newValue.hasOwnProperty(name)){
- var excludeClasses = [KICK.texture.Texture,KICK.texture.MovieTexture,KICK.texture.RenderTexture];
- _uniforms[name] = KICK.core.Util.deepCopy(newValue[name], excludeClasses);
- }
- }
- verifyUniforms();
- }
-
- if (_shader){
- _shader.markUniformUpdated();
- }
- }
- },*/
/**
* @property renderOrder
* @type Number
*/
- renderOrder:{
- get:function(){
+ renderOrder: {
+ get: function () {
return _renderOrder;
}
}
@@ -1276,50 +1195,53 @@ KICK.namespace = function (ns_string) {
* @protected
* @return {Number}
*/
- this.bind = function(currentTexture){
- for (var i=0;i<_uniforms.length;i++){
- var value = _uniforms[i].value,
- location = _uniforms[i].location;
- switch (_uniforms[i].type){
- case c.GL_FLOAT:
- gl.uniform1fv(location,value);
- break;
- case c.GL_FLOAT_MAT2:
- gl.uniformMatrix2fv(location,false,value);
- break;
- case c.GL_FLOAT_MAT3:
- gl.uniformMatrix3fv(location,false,value);
- break;
- case c.GL_FLOAT_MAT4:
- gl.uniformMatrix4fv(location,false,value);
- break;
- case c.GL_FLOAT_VEC2:
- gl.uniform2fv(location,value);
- break;
- case c.GL_FLOAT_VEC3:
- gl.uniform3fv(location,value);
- break;
- case c.GL_FLOAT_VEC4:
- gl.uniform4fv(location,value);
- break;
- case c.GL_INT:
- gl.uniform1iv(location,value);
- break;
- case c.GL_INT_VEC2:
- gl.uniform2iv(location,value);
- break;
- case c.GL_INT_VEC3:
- gl.uniform3iv(location,value);
- break;
- case c.GL_INT_VEC4:
- gl.uniform4iv(location,value);
- break;
- case c.GL_SAMPLER_CUBE:
- case c.GL_SAMPLER_2D:
- value.bind(currentTexture);
- gl.uniform1i(location,currentTexture);
- currentTexture ++;
- break;
+ this.bind = function (currentTexture) {
+ var i,
+ value,
+ location;
+ for (i = 0; i < _uniforms.length; i++) {
+ value = _uniforms[i].value;
+ location = _uniforms[i].location;
+ switch (_uniforms[i].type) {
+ case c.GL_FLOAT:
+ gl.uniform1fv(location, value);
+ break;
+ case c.GL_FLOAT_MAT2:
+ gl.uniformMatrix2fv(location, false, value);
+ break;
+ case c.GL_FLOAT_MAT3:
+ gl.uniformMatrix3fv(location, false, value);
+ break;
+ case c.GL_FLOAT_MAT4:
+ gl.uniformMatrix4fv(location, false, value);
+ break;
+ case c.GL_FLOAT_VEC2:
+ gl.uniform2fv(location, value);
+ break;
+ case c.GL_FLOAT_VEC3:
+ gl.uniform3fv(location, value);
+ break;
+ case c.GL_FLOAT_VEC4:
+ gl.uniform4fv(location, value);
+ break;
+ case c.GL_INT:
+ gl.uniform1iv(location, value);
+ break;
+ case c.GL_INT_VEC2:
+ gl.uniform2iv(location, value);
+ break;
+ case c.GL_INT_VEC3:
+ gl.uniform3iv(location, value);
+ break;
+ case c.GL_INT_VEC4:
+ gl.uniform4iv(location, value);
+ break;
+ case c.GL_SAMPLER_CUBE:
+ case c.GL_SAMPLER_2D:
+ value.bind(currentTexture);
+ gl.uniform1i(location, currentTexture);
+ currentTexture++;
+ break;
}
}
return currentTexture;
@@ -1333,9 +1255,10 @@ KICK.namespace = function (ns_string) {
* @return {KICK.material.MaterialUniform}
*/
this.setUniform = function (name, value) {
- var foundElement;
- for (var i = 0; i < _uniforms.length && !foundElement; i++) {
- if (_uniforms[i].name===name){
+ var foundElement,
+ i;
+ for (i = 0; i < _uniforms.length && !foundElement; i++) {
+ if (_uniforms[i].name === name) {
foundElement = _uniforms[i];
foundElement.value = value;
}
@@ -1345,14 +1268,14 @@ KICK.namespace = function (ns_string) {
_shader.markUniformUpdated();
} else if (!foundElement) {
foundElement = new material.MaterialUniform({
- name:name,
- value:value
+ name: name,
+ value: value
});
_uniforms.push(foundElement);
}
- if (ASSERT){
- if (_shader){
- if (typeof (value) === "undefined" ){
+ if (ASSERT) {
+ if (_shader) {
+ if (typeof (value) === "undefined") {
fail("Type of value is undefined");
}
}
@@ -1366,7 +1289,8 @@ KICK.namespace = function (ns_string) {
* @return {Float32Array|Int32Array|KICK.texture.Texture}
*/
this.getUniform = function (name) {
- for (var i = 0; i < _uniforms.length; i++) {
+ var i;
+ for (i = 0; i < _uniforms.length; i++) {
if (_uniforms[i].name === name) {
return _uniforms[i].value;
}
@@ -1377,7 +1301,7 @@ KICK.namespace = function (ns_string) {
/**
* @method destroy
*/
- this.destroy = function(){
+ this.destroy = function () {
engine.project.removeResourceDescriptor(thisObj.uid);
};
@@ -1387,9 +1311,9 @@ KICK.namespace = function (ns_string) {
* If shader is invalid, the error shader is used
* @method init
*/
- this.init = function(){
- if (!_shader){
- KICK.core.Util.fail("Cannot initiate shader in material "+_name);
+ this.init = function () {
+ if (!_shader) {
+ KICK.core.Util.fail("Cannot initiate shader in material " + _name);
thisObj._shader = engine.project.load(engine.project.ENGINE_SHADER___ERROR);
}
};
@@ -1399,34 +1323,35 @@ KICK.namespace = function (ns_string) {
* @method toJSON
* @return {string}
*/
- this.toJSON = function(){
- var serializedUniforms = {};
- for (var i=0;i<_uniforms.length;i++){
+ this.toJSON = function () {
+ var i,
+ serializedUniforms = {};
+ for (i = 0; i < _uniforms.length; i++) {
serializedUniforms[_uniforms[i].name] = _uniforms[i].toJSON();
}
return {
uid: thisObj.uid,
- name:_name,
+ name: _name,
shader: KICK.core.Util.getJSONReference(engine,_shader),
uniformData: serializedUniforms // uniformData only used during serialization
};
};
- (function init(){
+ (function init() {
var uniformData = config.uniformData,
name,
value;
- if (uniformData){
+ if (uniformData) {
delete config.uniformData;
}
- if (config.uniforms){
+ if (config.uniforms) {
console.log("Warn - Material.uniforms is deprecated");
delete config.uniforms;
}
- applyConfig(thisObj,config);
- if (uniformData){
- for (name in uniformData){
- if (uniformData.hasOwnProperty(name)){
+ applyConfig(thisObj, config);
+ if (uniformData) {
+ for (name in uniformData) {
+ if (uniformData.hasOwnProperty(name)) {
value = uniformData[name];
value = convertUniformValue(_shader.lookupUniform[name].type, value, engine);
thisObj.setUniform(name, value);
@@ -1434,16 +1359,16 @@ KICK.namespace = function (ns_string) {
}
}
engine.project.registerObject(thisObj, "KICK.material.Material");
- })();
+ }());
};
/**
* Material material uniform object
- * todo make class protected
* @class MaterialUniform
* @namespace KICK.material
* @constructor
* @param {Object} configuration
+ * @protected
*/
material.MaterialUniform = function (configuration) {
var value,
@@ -1477,11 +1402,11 @@ KICK.namespace = function (ns_string) {
* @property value
* @type Float32Array|Int32Array|KICK.texture.Texture
*/
- value:{
- get:function(){
+ value: {
+ get: function () {
return value;
},
- set:function(newValue){
+ set: function (newValue) {
value = newValue;
}
}
@@ -1493,22 +1418,22 @@ KICK.namespace = function (ns_string) {
* @method toJSON
* @return {string}
*/
- this.toJSON = function(){
+ this.toJSON = function () {
var value = thisObj.value;
if (value instanceof Float32Array || value instanceof Int32Array) {
value = core.Util.typedArrayToArray(value);
} else {
- if (KICK.core.Constants._ASSERT){
- if (!value instanceof KICK.texture.Texture){
+ if (KICK.core.Constants._ASSERT) {
+ if (!value instanceof KICK.texture.Texture) {
KICK.core.Util.fail("Unknown uniform value type. Expected Texture");
}
}
- value = KICK.core.Util.getJSONReference(value.engine,value);
+ value = KICK.core.Util.getJSONReference(value.engine, value);
}
return {
name: thisObj.name,
value: value
};
- }
+ };
};
-})();
+}());
Please sign in to comment.
Something went wrong with that request. Please try again.