/** * GameLib.D3.Material.Shader * @param graphics * @param apiMaterialShader * @constructor */ GameLib.D3.Material.Shader = function( graphics, apiMaterialShader ) { this.graphics = graphics; this.graphics.isNotThreeThrow(); if (GameLib.Utils.UndefinedOrNull(apiMaterialShader)) { apiMaterialShader = { materialType : GameLib.D3.API.Material.MATERIAL_TYPE_SHADER }; } GameLib.D3.API.Material.Shader.call( this, apiMaterialShader, apiMaterialShader.clipping, apiMaterialShader.defaultAttributeValues, apiMaterialShader.extensions, apiMaterialShader.fog, apiMaterialShader.fragmentShader, apiMaterialShader.index0AttributeName, apiMaterialShader.linewidth, apiMaterialShader.morphTargets, apiMaterialShader.morphNormals, apiMaterialShader.program, apiMaterialShader.skinning, apiMaterialShader.uniforms, apiMaterialShader.vertexColors, apiMaterialShader.vertexShader, apiMaterialShader.wireframe, apiMaterialShader.wireframeLinewidth ); this.vertexColors = true; GameLib.D3.Material.call( this, this.graphics, this ); }; GameLib.D3.Material.Shader.prototype = Object.create(GameLib.D3.Material.prototype); GameLib.D3.Material.Shader.prototype.constructor = GameLib.D3.Material.Shader; GameLib.D3.Material.Shader.prototype.commonInstance = function() { }; /** * Creates an instance of our texture object * @returns {*} */ GameLib.D3.Material.Shader.prototype.createInstance = function() { if (this.instance) { /** * We already have an instance from Child Class */ } else { if ( GameLib.Utils.UndefinedOrNull(this.vertexShader) || GameLib.Utils.UndefinedOrNull(this.vertexShader.instance) ) { console.warn('shader material ' + this.name + 'not ready for instance - needs a vertex shader'); return; } if ( GameLib.Utils.UndefinedOrNull(this.fragmentShader) || GameLib.Utils.UndefinedOrNull(this.fragmentShader.instance) ) { console.warn('shader material ' + this.name + 'not ready for instance - needs a fragment shader'); return; } this.instance = new THREE.ShaderMaterial( { clipping : this.clipping, defaultAttributeValues : this.defaultAttributeValues, extensions : this.extensions, fog : this.fog, fragmentShader : this.fragmentShader.instance, linewidth : this.linewidth, morphTargets : this.morphTargets, morphNormals : this.morphNormals, skinning : this.skinning, uniforms : this.uniforms, vertexColors : this.vertexColors, vertexShader : this.vertexShader.instance, wireframe : this.wireframe, wireframeLinewidth : this.wireframeLinewidth } ); } if (GameLib.Utils.Defined(this.index0AttributeName)) { this.instance.index0AttributeName = this.index0AttributeName; } console.log('shader material instance created'); GameLib.D3.Material.prototype.createInstance.call(this); }; /** * Updates the instance with the current state */ GameLib.D3.Material.Shader.prototype.updateInstance = function(property) { if (!this.instance) { if ( property === 'vertexShader' || property === 'fragmentShader' ) { this.createInstance(); } /** * If we don't return here - we risk storing this incomplete type with the entity.. */ return; } if (property === 'clipping') { this.instance.clipping = this.clipping; return; } if (property === 'defaultAttributeValues') { this.instance.defaultAttributeValues = this.defaultAttributeValues; return; } if (property === 'extensions') { this.instance.extensions = this.extensions; return; } if (property === 'fog') { this.instance.fog = this.fog; return; } if (property === 'fragmentShader') { if (this.fragmentShader && this.fragmentShader.instance) { this.instance.fragmentShader = this.fragmentShader.instance; this.instance.needsUpdate = true; } else { console.warn('fragment shader for material has been removed or not linked - using last valid fragment shader'); } return; } if (property === 'index0AttributeName') { this.instance.index0AttributeName = this.index0AttributeName; return; } if (property === 'linewidth') { this.instance.linewidth = this.linewidth; return; } if (property === 'morphNormals') { this.instance.morphNormals = this.morphNormals; return; } if (property === 'morphTargets') { this.instance.morphTargets = this.morphTargets; return; } if (property === 'program') { console.warn('program is read only'); this.program = this.instance.program; } if (property === 'skinning') { this.instance.skinning = this.skinning; return; } if (property === 'uniforms') { this.instance.uniforms = this.uniforms; return; } if (property === 'vertexShader') { if (this.vertexShader && this.vertexShader.instance) { this.instance.vertexShader = this.vertexShader.instance; this.instance.needsUpdate = true; } else { console.warn('vertex shader for material has been removed or not linked - using last valid vertex shader'); } return; } if (property === 'vertexColors') { this.instance.vertexColors = this.vertexColors; this.instance.needsUpdate = true; return; } if (property === 'wireframe') { this.instance.wireframe = this.wireframe; return; } if (property === 'wireframeLinewidth') { this.instance.wireframeLinewidth = this.wireframeLinewidth; return; } GameLib.D3.Material.prototype.updateInstance.call(this, property); }; /** * Converts a GameLib.D3.Material.Shader to a GameLib.D3.API.Material.Shader * @returns {GameLib.D3.API.Material.Shader} */ GameLib.D3.Material.Shader.prototype.toApiObject = function() { var apiMaterial = GameLib.D3.Material.prototype.toApiObject.call(this); var apiMaterialShader = new GameLib.D3.API.Material.Shader( apiMaterial, this.clipping, this.defaultAttributeValues, this.extensions, this.fog, GameLib.Utils.IdOrNull(this.fragmentShader), this.index0AttributeName, this.linewidth, this.morphTargets, this.morphNormals, null, this.skinning, this.uniforms, this.vertexColors, GameLib.Utils.IdOrNull(this.vertexShader), this.wireframe, this.wireframeLinewidth ); return apiMaterialShader; };