223 lines
5.8 KiB
JavaScript
223 lines
5.8 KiB
JavaScript
|
/**
|
||
|
* 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;
|
||
|
|
||
|
/**
|
||
|
* Creates an instance of our texture object
|
||
|
* @returns {*}
|
||
|
*/
|
||
|
GameLib.D3.Material.Shader.prototype.createInstance = function() {
|
||
|
|
||
|
var fragmentShader = '';
|
||
|
var vertexShader = '';
|
||
|
|
||
|
if (this.fragmentShader && this.fragmentShader.instance) {
|
||
|
fragmentShader = this.fragmentShader.instance;
|
||
|
}
|
||
|
|
||
|
if (this.vertexShader && this.vertexShader.instance) {
|
||
|
vertexShader = this.vertexShader.instance;
|
||
|
}
|
||
|
|
||
|
this.instance = new THREE.ShaderMaterial(
|
||
|
{
|
||
|
clipping : this.clipping,
|
||
|
defaultAttributeValues : this.defaultAttributeValues,
|
||
|
extensions : this.extensions,
|
||
|
fog : this.fog,
|
||
|
fragmentShader : fragmentShader,
|
||
|
linewidth : this.linewidth,
|
||
|
morphTargets : this.morphTargets,
|
||
|
morphNormals : this.morphNormals,
|
||
|
skinning : this.skinning,
|
||
|
uniforms : this.uniforms,
|
||
|
vertexColors : this.vertexColors,
|
||
|
vertexShader : vertexShader,
|
||
|
wireframe : this.wireframe,
|
||
|
wireframeLinewidth : this.wireframeLinewidth
|
||
|
}
|
||
|
);
|
||
|
|
||
|
if (GameLib.Utils.Defined(this.index0AttributeName)) {
|
||
|
this.instance.index0AttributeName = this.index0AttributeName;
|
||
|
}
|
||
|
|
||
|
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) {
|
||
|
console.warn('shader material not ready');
|
||
|
}
|
||
|
|
||
|
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') {
|
||
|
this.instance.fragmentShader = this.fragmentShader.instance;
|
||
|
this.instance.needsUpdate = true;
|
||
|
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') {
|
||
|
this.instance.vertexShader = this.vertexShader.instance;
|
||
|
this.instance.needsUpdate = true;
|
||
|
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;
|
||
|
};
|