shader materials, shaders and instanced geometries

beta.r3js.org
-=yb4f310 2018-02-12 14:42:46 +01:00
parent 6cc5fa642e
commit e06f7094ce
24 changed files with 1095 additions and 122 deletions

View File

@ -391,7 +391,14 @@ GameLib.Component.BOX3 = 0x9a;
GameLib.Component.DRAW_RANGE = 0x9b;
GameLib.Component.GROUP = 0x9c;
GameLib.Component.MAX_COMPONENTS = 0x9d;
GameLib.Component.MATERIAL_SHADER = 0x9d;
GameLib.Component.SHADER = 0x9e;
GameLib.Component.SHADER_VERTEX = 0x9f;
GameLib.Component.SHADER_FRAGMENT = 0xa0;
GameLib.Component.GEOMETRY_BUFFER_INSTANCED = 0xa1;
GameLib.Component.MAX_COMPONENTS = 0xa2;
GameLib.Component.GRAPHICS_RUNTIME = 0x1;
GameLib.Component.PHYSICS_RUNTIME = 0x2;
@ -1348,6 +1355,36 @@ GameLib.Component.GetComponentInfo = function(number) {
constructor : GameLib.Group,
apiConstructor : GameLib.API.Group
};
case 0x9d : return {
name : 'GameLib.D3.Material.Shader',
runtime : GameLib.Component.GRAPHICS_RUNTIME,
constructor : GameLib.D3.Material.Shader,
apiConstructor : GameLib.D3.API.Material.Shader
};
case 0x9e : return {
name : 'GameLib.D3.Shader',
runtime : GameLib.Component.GRAPHICS_RUNTIME,
constructor : GameLib.D3.Shader,
apiConstructor : GameLib.D3.API.Shader
};
case 0x9f : return {
name : 'GameLib.D3.Shader.Vertex',
runtime : GameLib.Component.GRAPHICS_RUNTIME,
constructor : GameLib.D3.Shader.Vertex,
apiConstructor : GameLib.D3.API.Shader.Vertex
};
case 0xa0 : return {
name : 'GameLib.D3.Shader.Fragment',
runtime : GameLib.Component.GRAPHICS_RUNTIME,
constructor : GameLib.D3.Shader.Fragment,
apiConstructor : GameLib.D3.API.Shader.Fragment
};
case 0xa1 : return {
name : 'GameLib.D3.Geometry.Buffer.Instanced',
runtime : GameLib.Component.GRAPHICS_RUNTIME,
constructor : GameLib.D3.Geometry.Buffer.Instanced,
apiConstructor : GameLib.D3.API.Geometry.Buffer.Instanced
};
break;
}

View File

@ -20,7 +20,9 @@ GameLib.API.Image = function(
extension,
path,
contentType,
size
size,
width,
height
) {
if (GameLib.Utils.UndefinedOrNull(id)) {
id = GameLib.Utils.RandomId();
@ -75,6 +77,16 @@ GameLib.API.Image = function(
}
this.size = size;
if (GameLib.Utils.UndefinedOrNull(width)) {
width = 0;
}
this.width = width;
if (GameLib.Utils.UndefinedOrNull(height)) {
height = 0;
}
this.height = height;
GameLib.API.Component.call(
this,
GameLib.Component.IMAGE,

View File

@ -46,26 +46,32 @@ GameLib.CustomCode.prototype.createInstance = function() {
/**
* Updates the instance with the current state
*/
GameLib.CustomCode.prototype.updateInstance = function() {
GameLib.CustomCode.prototype.updateInstance = function(property) {
try {
this.instance = new Function('data', this.code).bind(this);
this.publish(
GameLib.Event.COMPILE_SUCCESS,
{
component : this
}
)
} catch (error) {
this.instance = new Function('data', "console.log('compilation update failed for : " + this.name + "');").bind(this);
this.publish(
GameLib.Event.COMPILE_FAILED,
{
component : this
}
)
}
if (property === 'code') {
try {
this.instance = new Function('data', this.code).bind(this);
this.publish(
GameLib.Event.COMPILE_SUCCESS,
{
component: this
}
)
} catch (error) {
this.instance = new Function('data', "console.log('compilation update failed for : " + this.name + "');").bind(this);
this.publish(
GameLib.Event.COMPILE_FAILED,
{
component: this
}
)
}
return;
}
GameLib.Component.prototype.updateInstance.call(this, property);
};
/**
@ -84,19 +90,6 @@ GameLib.CustomCode.prototype.toApiObject = function() {
};
/**
* Converts from an Object CustomCode to a GameLib.CustomCode
* @param objectCustomCode Object
* @returns {GameLib.CustomCode}
* @constructor
*/
GameLib.CustomCode.FromObject = function(objectCustomCode) {
var apiCustomCode = GameLib.API.CustomCode.FromObject(objectCustomCode);
return new GameLib.CustomCode(
apiCustomCode
);
};
GameLib.CustomCode.prototype.launchEditor = function(){
GameLib.Event.Emit(
@ -124,7 +117,7 @@ GameLib.CustomCode.prototype.launchEditor = function(){
this.code = this.editor.getValue();
this.updateInstance();
this.updateInstance('code');
}.bind(this))
};

View File

@ -167,6 +167,9 @@ GameLib.D3.API.Geometry = function(
case GameLib.D3.API.Geometry.GEOMETRY_TYPE_BUFFER_TUBE :
name = 'Geometry Buffer Tube';
break;
case GameLib.D3.API.Geometry.GEOMETRY_TYPE_BUFFER_INSTANCED :
name = 'Geometry Buffer Instanced';
break;
default :
console.warn('no nice name for geometry');
name = 'Geometry';
@ -348,6 +351,9 @@ GameLib.D3.API.Geometry.GetComponentType = function(geometryType) {
case GameLib.D3.API.Geometry.GEOMETRY_TYPE_BUFFER_TUBE :
componentType = GameLib.Component.GEOMETRY_BUFFER_TUBE;
break;
case GameLib.D3.API.Geometry.GEOMETRY_TYPE_BUFFER_INSTANCED :
componentType = GameLib.Component.GEOMETRY_BUFFER_INSTANCED;
break;
default:
throw new Error('unhandled geometry type: ' + geometryType);
}
@ -404,3 +410,4 @@ GameLib.D3.API.Geometry.GEOMETRY_TYPE_BUFFER_TEXT = 0x29;
GameLib.D3.API.Geometry.GEOMETRY_TYPE_BUFFER_TORUS = 0x2a;
GameLib.D3.API.Geometry.GEOMETRY_TYPE_BUFFER_TORUS_KNOT = 0x2b;
GameLib.D3.API.Geometry.GEOMETRY_TYPE_BUFFER_TUBE = 0x2c;
GameLib.D3.API.Geometry.GEOMETRY_TYPE_BUFFER_INSTANCED = 0x2d;

View File

@ -0,0 +1,39 @@
/**
* GameLib.D3.API.Geometry.Buffer.Instanced
* @param apiGeometry
* @param maxInstancedCount
* @constructor
*/
GameLib.D3.API.Geometry.Buffer.Instanced = function(
apiGeometry,
maxInstancedCount
) {
if (GameLib.Utils.UndefinedOrNull(apiGeometry)) {
apiGeometry = {
geometryType: GameLib.D3.API.Geometry.GEOMETRY_TYPE_BUFFER_INSTANCED
};
}
if (GameLib.Utils.UndefinedOrNull(apiGeometry.geometryType)) {
apiGeometry.geometryType = GameLib.D3.API.Geometry.GEOMETRY_TYPE_BUFFER_INSTANCED;
}
if (GameLib.Utils.UndefinedOrNull(maxInstancedCount)) {
maxInstancedCount = null;
}
this.maxInstancedCount = maxInstancedCount;
GameLib.D3.API.Geometry.Buffer.call(
this,
apiGeometry,
apiGeometry.attributes,
apiGeometry.drawRange,
apiGeometry.groups,
apiGeometry.index,
apiGeometry.morphAttributes
);
};
GameLib.D3.API.Geometry.Buffer.Instanced.prototype = Object.create(GameLib.D3.API.Geometry.Buffer.prototype);
GameLib.D3.API.Geometry.Buffer.Instanced.prototype.constructor = GameLib.D3.API.Geometry.Buffer.Instanced;

View File

@ -103,6 +103,9 @@ GameLib.D3.API.Material = function(
case GameLib.D3.API.Material.MATERIAL_TYPE_PHONG :
name = 'Material Phong';
break;
case GameLib.D3.API.Material.MATERIAL_TYPE_SHADER :
name = 'Material Shader';
break;
default :
console.warn('no nice name for material');
name = 'Material';
@ -214,7 +217,10 @@ GameLib.D3.API.Material = function(
if (GameLib.Utils.UndefinedOrNull(lights)) {
if (this.materialType === GameLib.D3.API.Material.MATERIAL_TYPE_BASIC) {
if (
this.materialType === GameLib.D3.API.Material.MATERIAL_TYPE_BASIC ||
this.materialType === GameLib.D3.API.Material.MATERIAL_TYPE_SHADER
) {
lights = false;
} else {
lights = true;
@ -313,6 +319,9 @@ GameLib.D3.API.Material.GetComponentType = function(materialType) {
case GameLib.D3.API.Material.MATERIAL_TYPE_PHONG :
componentType = GameLib.Component.MATERIAL_PHONG;
break;
case GameLib.D3.API.Material.MATERIAL_TYPE_SHADER :
componentType = GameLib.Component.MATERIAL_SHADER;
break;
default :
throw new Error('unhandled material type: ' + materialType);
}

View File

@ -0,0 +1,193 @@
/**
* GameLib.D3.API.Material.Shader
* @param apiMaterial
* @param clipping
* @param defaultAttributeValues
* @param extensions
* @param fog
* @param fragmentShader
* @param index0AttributeName
* @param linewidth
* @param morphTargets
* @param morphNormals
* @param program
* @param skinning
* @param uniforms
* @param vertexShader
* @param wireframe
* @param wireframeLinewidth
* @constructor
*/
GameLib.D3.API.Material.Shader = function(
apiMaterial,
clipping,
defaultAttributeValues,
extensions,
fog,
fragmentShader,
index0AttributeName,
linewidth,
morphTargets,
morphNormals,
program,
skinning,
uniforms,
vertexColors,
vertexShader,
wireframe,
wireframeLinewidth
) {
if (GameLib.Utils.UndefinedOrNull(apiMaterial)) {
apiMaterial = {
materialType: GameLib.D3.API.Material.MATERIAL_TYPE_SHADER
};
}
if (GameLib.Utils.UndefinedOrNull(apiMaterial.materialType)) {
apiMaterial.materialType = GameLib.D3.API.Material.MATERIAL_TYPE_SHADER;
}
if (GameLib.Utils.UndefinedOrNull(clipping)) {
clipping = false;
}
this.clipping = clipping;
if (GameLib.Utils.UndefinedOrNull(defaultAttributeValues)) {
defaultAttributeValues = {
'color' : [1, 1, 1],
'uv' : [0, 0],
'uv2' : [0, 0]
};
}
this.defaultAttributeValues = defaultAttributeValues;
if (GameLib.Utils.UndefinedOrNull(extensions)) {
extensions = {
derivatives : false,
fragDepth : false,
drawBuffers : false,
shaderTextureLOD: false
};
}
this.extensions = extensions;
if (GameLib.Utils.UndefinedOrNull(fragmentShader)) {
fragmentShader = null;
}
this.fragmentShader = fragmentShader;
if (GameLib.Utils.UndefinedOrNull(index0AttributeName)) {
index0AttributeName = undefined;
}
this.index0AttributeName = index0AttributeName;
if (GameLib.Utils.UndefinedOrNull(linewidth)) {
linewidth = 1;
}
this.linewidth = linewidth;
if (GameLib.Utils.UndefinedOrNull(morphNormals)) {
morphNormals = false;
}
this.morphNormals = morphNormals;
if (GameLib.Utils.UndefinedOrNull(morphTargets)) {
morphTargets = false;
}
this.morphTargets = morphTargets;
if (GameLib.Utils.UndefinedOrNull(program)) {
program = null;
}
this.program = program;
if (GameLib.Utils.UndefinedOrNull(skinning)) {
skinning = false;
}
this.skinning = skinning;
if (GameLib.Utils.UndefinedOrNull(vertexShader)) {
vertexShader = null;
}
this.vertexShader = vertexShader;
if (GameLib.Utils.UndefinedOrNull(wireframe)) {
wireframe = false;
}
this.wireframe = wireframe;
if (GameLib.Utils.UndefinedOrNull(wireframeLinewidth)) {
wireframeLinewidth = 1;
}
this.wireframeLinewidth = wireframeLinewidth;
GameLib.D3.API.Material.call(
this,
apiMaterial.id,
apiMaterial.name,
apiMaterial.materialType,
apiMaterial.parentEntity,
apiMaterial.parentMeshes,
apiMaterial.alphaTest,
apiMaterial.blendDst,
apiMaterial.blendDstAlpha,
apiMaterial.blendEquation,
apiMaterial.blendEquationAlpha,
apiMaterial.blending,
apiMaterial.blendSrc,
apiMaterial.blendSrcAlpha,
apiMaterial.clipIntersection,
apiMaterial.clippingPlanes,
apiMaterial.clipShadows,
apiMaterial.colorWrite,
apiMaterial.customDepthMaterial,
apiMaterial.customDistanceMaterial,
apiMaterial.defines,
apiMaterial.depthFunc,
apiMaterial.depthTest,
apiMaterial.depthWrite,
apiMaterial.fog,
apiMaterial.lights,
apiMaterial.opacity,
apiMaterial.overdraw,
apiMaterial.polygonOffset,
apiMaterial.polygonOffsetFactor,
apiMaterial.polygonOffsetUnits,
apiMaterial.precision,
apiMaterial.premultipliedAlpha,
apiMaterial.dithering,
apiMaterial.flatShading,
apiMaterial.side,
apiMaterial.transparent,
apiMaterial.vertexColors,
apiMaterial.visible
);
/**
* We override vertex colors, fog and uniforms
*/
if (GameLib.Utils.UndefinedOrNull(fog)) {
fog = true;
}
this.fog = fog;
if (GameLib.Utils.UndefinedOrNull(vertexColors)) {
vertexColors = true;
}
this.vertexColors = vertexColors;
if (GameLib.Utils.UndefinedOrNull(uniforms)) {
uniforms = {};
/**
* object of the form:
* {
* "uniform1" : {value : 1.0}
* };
*/
}
this.uniforms = uniforms;
};
GameLib.D3.API.Material.Shader.prototype = Object.create(GameLib.D3.API.Material.prototype);
GameLib.D3.API.Material.Shader.prototype.constructor = GameLib.D3.API.Material.Shader;

View File

@ -0,0 +1,87 @@
/**
* GameLib.D3.API.Shader
* @param id
* @param name
* @param shaderType
* @param parentEntity
* @param code
* @constructor
*/
GameLib.D3.API.Shader = function(
id,
name,
shaderType,
parentEntity,
code
) {
if (GameLib.Utils.UndefinedOrNull(id)) {
id = GameLib.Utils.RandomId();
}
this.id = id;
if (GameLib.Utils.UndefinedOrNull(shaderType)) {
shaderType = GameLib.D3.API.Shader.SHADER_TYPE_NONE;
}
this.shaderType = shaderType;
if (GameLib.Utils.UndefinedOrNull(name)) {
switch (this.shaderType) {
case GameLib.D3.API.Shader.SHADER_TYPE_NONE :
name = 'Shader';
break;
case GameLib.D3.API.Shader.SHADER_TYPE_VERTEX :
name = 'Shader Vertex';
break;
case GameLib.D3.API.Shader.SHADER_TYPE_FRAGMENT :
name = 'Shader Fragment';
break;
default :
console.warn('no nice Shader name');
name = 'Shader';
}
name += ' (' + this.id + ')';
}
this.name = name;
if (GameLib.Utils.UndefinedOrNull(code)) {
code = '';
}
this.code = code;
GameLib.API.Component.call(
this,
GameLib.D3.API.Shader.GetComponentType(this.shaderType),
parentEntity
);
};
GameLib.D3.API.Shader.prototype = Object.create(GameLib.API.Component.prototype);
GameLib.D3.API.Shader.prototype.constructor = GameLib.D3.API.Shader;
GameLib.D3.API.Shader.GetComponentType = function(shaderType) {
var componentType = null;
switch (shaderType) {
case GameLib.D3.API.Shader.SHADER_TYPE_NONE :
componentType = GameLib.Component.SHADER;
break;
case GameLib.D3.API.Shader.SHADER_TYPE_VERTEX :
componentType = GameLib.Component.SHADER_VERTEX;
break;
case GameLib.D3.API.Shader.SHADER_TYPE_FRAGMENT :
componentType = GameLib.Component.SHADER_FRAGMENT;
break;
default:
throw new Error('unsupported Shader type: ' + shaderType);
}
return componentType;
};
GameLib.D3.API.Shader.SHADER_TYPE_NONE = 0x0;
GameLib.D3.API.Shader.SHADER_TYPE_VERTEX = 0x1;
GameLib.D3.API.Shader.SHADER_TYPE_FRAGMENT = 0x2;

View File

@ -0,0 +1,31 @@
/**
* GameLib.D3.API.Shader.Fragment
* @param apiShader
* @constructor
*/
GameLib.D3.API.Shader.Fragment = function(
apiShader
) {
if (GameLib.Utils.UndefinedOrNull(apiShader)) {
apiShader = {
shaderType: GameLib.D3.API.Shader.SHADER_TYPE_FRAGMENT
};
}
if (GameLib.Utils.UndefinedOrNull(apiShader.materialType)) {
apiShader.shaderType = GameLib.D3.API.Shader.SHADER_TYPE_FRAGMENT;
}
GameLib.D3.API.Shader.call(
this,
apiShader.id,
apiShader.name,
apiShader.shaderType,
apiShader.parentEntity,
apiShader.code
);
};
GameLib.D3.API.Shader.Fragment.prototype = Object.create(GameLib.D3.API.Shader.prototype);
GameLib.D3.API.Shader.Fragment.prototype.constructor = GameLib.D3.API.Shader.Fragment;

View File

@ -0,0 +1,31 @@
/**
* GameLib.D3.API.Shader.Vertex
* @param apiShader
* @constructor
*/
GameLib.D3.API.Shader.Vertex = function(
apiShader
) {
if (GameLib.Utils.UndefinedOrNull(apiShader)) {
apiShader = {
shaderType: GameLib.D3.API.Shader.SHADER_TYPE_VERTEX
};
}
if (GameLib.Utils.UndefinedOrNull(apiShader.materialType)) {
apiShader.shaderType = GameLib.D3.API.Shader.SHADER_TYPE_VERTEX;
}
GameLib.D3.API.Shader.call(
this,
apiShader.id,
apiShader.name,
apiShader.shaderType,
apiShader.parentEntity,
apiShader.code
);
};
GameLib.D3.API.Shader.Vertex.prototype = Object.create(GameLib.D3.API.Shader.prototype);
GameLib.D3.API.Shader.Vertex.prototype.constructor = GameLib.D3.API.Shader.Vertex;

View File

@ -52,14 +52,33 @@ GameLib.D3.API.Shadow = function(
this.name = name;
if (GameLib.Utils.UndefinedOrNull(camera)) {
camera = new GameLib.D3.API.Camera.Perspective(
{
aspect : 1
},
0.5,
500,
90
);
var cameraName = 'Camera ' + this.name;
if (this.shadowType === GameLib.D3.API.Shadow.SHADOW_TYPE_DIRECTIONAL) {
camera = new GameLib.D3.API.Camera.Orthographic(
{
name : cameraName
},
10,
0.5,
500,
-5,
5,
5,
-5
)
} else {
camera = new GameLib.D3.API.Camera.Perspective(
{
name: cameraName,
aspect: 1
},
0.5,
500,
90
);
}
}
this.camera = camera;

View File

@ -17,19 +17,6 @@ GameLib.D3.API.Shadow.Directional = function(
apiDirectionalShadow.shadowType = GameLib.D3.API.Shadow.SHADOW_TYPE_DIRECTIONAL;
}
if (GameLib.Utils.UndefinedOrNull(apiDirectionalShadow.camera)) {
apiDirectionalShadow.camera = new GameLib.D3.API.Camera.Orthographic(
null,
10,
0.5,
500,
-5,
5,
5,
-5
)
}
GameLib.D3.API.Shadow.call(
this,
apiDirectionalShadow.id,

View File

@ -0,0 +1,82 @@
/**
* GameLib.D3.Geometry.Buffer.Instanced
* @param graphics GameLib.GraphicsRuntime
* @param apiGeometryBufferInstanced
* @constructor
*/
GameLib.D3.Geometry.Buffer.Instanced = function(
graphics,
apiGeometryBufferInstanced
) {
this.graphics = graphics;
this.graphics.isNotThreeThrow();
if (GameLib.Utils.UndefinedOrNull(apiGeometryBufferInstanced)) {
apiGeometryBufferInstanced = {
geometryType : GameLib.D3.API.Geometry.GEOMETRY_TYPE_BUFFER_INSTANCED
};
}
GameLib.D3.API.Geometry.Buffer.Instanced.call(
this,
apiGeometryBufferInstanced,
apiGeometryBufferInstanced.maxInstancedCount
);
GameLib.D3.Geometry.Buffer.call(
this,
this.graphics,
apiGeometryBufferInstanced
);
};
GameLib.D3.Geometry.Buffer.Instanced.prototype = Object.create(GameLib.D3.Geometry.Buffer.prototype);
GameLib.D3.Geometry.Buffer.Instanced.prototype.constructor = GameLib.D3.Geometry.Buffer.Instanced;
/**
* Creates a light instance
* @returns {*}
*/
GameLib.D3.Geometry.Buffer.Instanced.prototype.createInstance = function() {
this.instance = new THREE.InstancedBufferGeometry();
if (GameLib.Utils.Defined(this.maxInstancedCount)) {
this.instance.maxInstancedCount = this.maxInstancedCount;
}
GameLib.D3.Geometry.Buffer.prototype.createInstance.call(this);
};
/**
* Updates the instance with the current state
*/
GameLib.D3.Geometry.Buffer.Instanced.prototype.updateInstance = function(property) {
if (
property === 'maxInstancedCount'
) {
this.instance.maxInstancedCount = this.maxInstancedCount;
return;
}
GameLib.D3.Geometry.Buffer.prototype.updateInstance.call(this, property);
};
/**
* Converts a GameLib.D3.Geometry.Buffer.Instanced to a GameLib.D3.API.Geometry.Buffer.Instanced
* @returns {GameLib.D3.API.Geometry.Buffer}
*/
GameLib.D3.Geometry.Buffer.Instanced.prototype.toApiObject = function() {
var apiBufferGeometry = GameLib.D3.Geometry.Buffer.prototype.toApiObject.call(this);
var apiGeometryBufferInstanced = new GameLib.D3.API.Geometry.Buffer.Instanced(
apiBufferGeometry,
this.maxInstancedCount
);
return apiGeometryBufferInstanced;
};

View File

@ -81,12 +81,7 @@ GameLib.D3.Light.Directional.prototype.createInstance = function() {
/**
* This component is created during runtime
*/
this.shadow = new GameLib.D3.Shadow.Directional(
this.graphics,
{
parentEntity : this.parentEntity
}
);
this.shadow = new GameLib.D3.Shadow.Directional(this.graphics);
this.shadow.instance = this.instance.shadow;

View File

@ -99,6 +99,10 @@ GameLib.D3.Material = function(
linkedObjects.normalMap = GameLib.D3.Texture;
linkedObjects.specularMap = GameLib.D3.Texture;
break;
case GameLib.D3.API.Material.MATERIAL_TYPE_SHADER :
linkedObjects.vertexShader = GameLib.D3.Shader.Vertex;
linkedObjects.fragmentShader = GameLib.D3.Shader.Fragment;
break;
default :
throw new Error('unhandled material type: ' + this.materialType);
@ -174,11 +178,6 @@ GameLib.D3.Material.prototype.updateInstance = function(property) {
return;
}
if (property === 'parentEntity') {
console.warn('todo: parent entity update');
return;
}
if (property === 'parentMeshes') {
console.warn('parent meshes are read-only');
return;

View File

@ -0,0 +1,222 @@
/**
* 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;
};

View File

@ -294,11 +294,12 @@ GameLib.D3.Mesh.prototype.updateInstance = function(property) {
this.instance.material = materialInstances;
}
console.warn('materials not ready for this mesh ' + this.name);
return;
} else {
console.warn('materials not ready for this mesh ' + this.name);
if (GameLib.Utils.UndefinedOrNull(this.instance)) {
/**
* Do nothing

View File

@ -186,56 +186,7 @@ GameLib.D3.Mesh.Plane.prototype.toApiObject = function() {
};
GameLib.D3.Mesh.Plane.prototype.getHeightData = function() {
var i;
var img = this.materials.reduce(
function(result, material) {
if (
material.bumpMap &&
material.bumpMap.image &&
material.bumpMap.image.instance
) {
result = material.bumpMap.image.instance;
}
return result;
},
null
);
if (!img) {
throw new Error('bumpmap image not found');
}
var canvas = document.createElement( 'canvas' );
canvas.width = this.widthSegments + 1;//img.width;
canvas.height = this.heightSegments + 1;//img.height;
var context = canvas.getContext( '2d' );
var size = (this.widthSegments + 1) * (this.heightSegments + 1);
var data = new Float32Array( size );
context.drawImage(img,0,0, canvas.width, canvas.height);
for (i = 0; i < size; i ++ ) {
data[i] = 0
}
var imgd = context.getImageData(0, 0, (this.widthSegments + 1), (this.heightSegments + 1));
var pix = imgd.data;
var j=0;
for (i = 0; i<pix.length; i +=4) {
var all = pix[i]+pix[i+1]+pix[i+2];
data[j++] = all/(12*this.heightMapScale);
}
// data = GameLib.Utils.InterpolateArray(data, (this.widthSegments + 1) * (this.heightSegments + 1));
return data;
};
GameLib.D3.Mesh.Plane.prototype.generateDotMap = function() {

View File

@ -0,0 +1,79 @@
/**
* GameLib.D3.Shader
* @param graphics GameLib.GraphicsRuntime
* @param apiShader GameLib.D3.API.Shader
* @constructor
*/
GameLib.D3.Shader = function(
graphics,
apiShader
) {
this.graphics = graphics;
this.graphics.isNotThreeThrow();
if (GameLib.Utils.UndefinedOrNull(apiShader)) {
apiShader = {};
}
GameLib.D3.API.Shader.call(
this,
apiShader.id,
apiShader.name,
apiShader.shaderType,
apiShader.parentEntity,
apiShader.code
);
GameLib.Component.call(this);
};
GameLib.D3.Shader.prototype = Object.create(GameLib.CustomCode.prototype);
GameLib.D3.Shader.prototype.constructor = GameLib.D3.Shader;
/**
* Creates a light instance
* @returns {*}
*/
GameLib.D3.Shader.prototype.createInstance = function() {
this.instance = this.code;
/**
* We don't actually call the CustomCode createInstance - since these aren't javascript functions, they are
* GLSL code
*/
GameLib.Component.prototype.createInstance.call(this);
};
/**
* Updates the instance with the current state
*/
GameLib.D3.Shader.prototype.updateInstance = function(property) {
if (GameLib.Utils.UndefinedOrNull(property)) {
console.warn('no property for Shader: ' + this.name);
}
if (property === 'code') {
this.instance = this.code;
return;
}
GameLib.Component.prototype.updateInstance.call(this, property);
};
/**
* Converts a GameLib.D3.Shader to a GameLib.D3.API.Shader
* @returns {GameLib.D3.API.Shader}
*/
GameLib.D3.Shader.prototype.toApiObject = function() {
return new GameLib.D3.API.Shader(
this.id,
this.name,
this.shaderType,
GameLib.Utils.IdOrNull(this.parentEntity),
this.code
);
};

View File

@ -0,0 +1,59 @@
/**
* GameLib.D3.Shader.Fragment
* @param graphics GameLib.GraphicsRuntime
* @param apiFragmentShader
* @constructor
*/
GameLib.D3.Shader.Fragment = function(
graphics,
apiFragmentShader
) {
this.graphics = graphics;
this.graphics.isNotThreeThrow();
if (GameLib.Utils.UndefinedOrNull(apiFragmentShader)) {
apiFragmentShader = {
shaderType : GameLib.D3.API.Shader.SHADER_TYPE_FRAGMENT
};
}
GameLib.D3.API.Shader.Fragment.call(
this,
apiFragmentShader
);
GameLib.D3.Shader.call(
this,
this.graphics,
apiFragmentShader
);
};
GameLib.D3.Shader.Fragment.prototype = Object.create(GameLib.D3.Shader.prototype);
GameLib.D3.Shader.Fragment.prototype.constructor = GameLib.D3.Shader.Fragment;
/**
* Creates a shader instance
* @returns {*}
*/
GameLib.D3.Shader.Fragment.prototype.createInstance = function() {
GameLib.D3.Shader.prototype.createInstance.call(this);
};
/**
* Updates the instance with the current state
*/
GameLib.D3.Shader.Fragment.prototype.updateInstance = function(property) {
GameLib.D3.Shader.prototype.updateInstance.call(this, property);
};
/**
* Converts a GameLib.D3.Shader to a GameLib.D3.API.Shader
* @returns {GameLib.D3.API.Shader}
*/
GameLib.D3.Shader.Fragment.prototype.toApiObject = function() {
var apiShader = GameLib.D3.Shader.prototype.toApiObject.call(this);
return apiShader;
};

View File

@ -0,0 +1,59 @@
/**
* GameLib.D3.Shader.Vertex
* @param graphics GameLib.GraphicsRuntime
* @param apiVertexShader
* @constructor
*/
GameLib.D3.Shader.Vertex = function(
graphics,
apiVertexShader
) {
this.graphics = graphics;
this.graphics.isNotThreeThrow();
if (GameLib.Utils.UndefinedOrNull(apiVertexShader)) {
apiVertexShader = {
shaderType : GameLib.D3.API.Shader.SHADER_TYPE_VERTEX
};
}
GameLib.D3.API.Shader.Vertex.call(
this,
apiVertexShader
);
GameLib.D3.Shader.call(
this,
this.graphics,
apiVertexShader
);
};
GameLib.D3.Shader.Vertex.prototype = Object.create(GameLib.D3.Shader.prototype);
GameLib.D3.Shader.Vertex.prototype.constructor = GameLib.D3.Shader.Vertex;
/**
* Creates a shader instance
* @returns {*}
*/
GameLib.D3.Shader.Vertex.prototype.createInstance = function() {
GameLib.D3.Shader.prototype.createInstance.call(this);
};
/**
* Updates the instance with the current state
*/
GameLib.D3.Shader.Vertex.prototype.updateInstance = function(property) {
GameLib.D3.Shader.prototype.updateInstance.call(this, property);
};
/**
* Converts a GameLib.D3.Shader to a GameLib.D3.API.Shader
* @returns {GameLib.D3.API.Shader}
*/
GameLib.D3.Shader.Vertex.prototype.toApiObject = function() {
var apiShader = GameLib.D3.Shader.prototype.toApiObject.call(this);
return apiShader;
};

View File

@ -43,7 +43,12 @@ GameLib.D3.Shadow = function(
this
);
GameLib.Component.call(this);
GameLib.Component.call(
this,
{
camera : GameLib.D3.Camera
}
);
};
GameLib.D3.Shadow.prototype = Object.create(GameLib.Component.prototype);

View File

@ -21,7 +21,9 @@ GameLib.Image = function(
apiImage.extension,
apiImage.path,
apiImage.contentType,
apiImage.size
apiImage.size,
apiImage.width,
apiImage.height
);
GameLib.Component.call(
@ -48,6 +50,10 @@ GameLib.Image.prototype.createInstance = function() {
},
function(imageInstance) {
this.instance = imageInstance;
this.width = this.instance.width;
this.height = this.instance.height;
GameLib.Component.prototype.createInstance.call(this);
}.bind(this),
function(error) {
@ -74,6 +80,22 @@ GameLib.Image.prototype.updateInstance = function(property) {
property === 'path'
) {
this.createInstance();
return;
}
if (GameLib.Utils.UndefinedOrNull(this.instance)) {
console.warn('image not ready yet');
return;
}
if (
property === 'width' ||
property === 'height'
) {
console.warn('width and height is read only');
this.width = this.instance.width;
this.height = this.instance.height;
return;
}
GameLib.Component.prototype.updateInstance.call(this, property);
@ -87,6 +109,8 @@ GameLib.Image.prototype.updateFromRawObject = function(rawObject) {
this.path = rawObject.path;
this.contentType = rawObject.contentType;
this.size = rawObject.size;
this.width = rawObject.width;
this.height = rawObject.height;
};
/**
@ -104,7 +128,9 @@ GameLib.Image.prototype.toApiObject = function() {
this.extension,
this.path,
this.contentType,
this.size
this.size,
this.width,
this.height
);
return apiImage;
@ -118,5 +144,54 @@ GameLib.Image.prototype.updateFromInstance = function() {
this.extension = this.instance.extension || 'no extension';
this.path = this.instance.path || 'no path';
this.contentType = this.instance.contentType || 'no content type';
this.size = this.instance.size || 'no size';
this.size = this.instance.size || 0;
this.width = this.instance.width || 0;
this.height = this.instance.height || 0;
};
/**
* Returns an array of Height Data for this image
* @returns {Float32Array | null}
*/
GameLib.Image.prototype.getHeightData = function() {
if (GameLib.Utils.UndefinedOrNull(this.instance)) {
console.warn('this image is not ready to have its height data processed');
return null;
}
var canvas = document.createElement( 'canvas' );
canvas.width = this.width;
canvas.height = this.height;
var context = canvas.getContext( '2d' );
context.drawImage(this.instance, 0, 0, canvas.width, canvas.height);
var imageData = context.getImageData(0, 0, this.width, this.height);
var pixels = imageData.data;
var data = new Float32Array( this.width * this.height );
var height, i, j = 0;
for (i = 0; i < pixels.length; i += 4) {
/**
* We ignore the alpha channel for now
*/
height = (pixels[i] + pixels[i+1] + pixels[i+2]);
/**
* Clamp values to zero or a number between 0 and 1
*/
if (height > 3) {
height = height / 768;
} else {
height = 0;
}
data[j++] = height;
}
return data;
};

View File

@ -1191,7 +1191,8 @@ GameLib.System.GUI.prototype.buildControl = function(folder, componentTemplate,
{
'standard': GameLib.D3.API.Material.MATERIAL_TYPE_STANDARD,
'basic': GameLib.D3.API.Material.MATERIAL_TYPE_BASIC,
'phong': GameLib.D3.API.Material.MATERIAL_TYPE_PHONG
'phong': GameLib.D3.API.Material.MATERIAL_TYPE_PHONG,
'shader': GameLib.D3.API.Material.MATERIAL_TYPE_SHADER
// 'points': GameLib.D3.API.Material.MATERIAL_TYPE_POINTS,
// 'toon': GameLib.D3.API.Material.MATERIAL_TYPE_TOON,
// 'line basic' : GameLib.D3.API.Material.MATERIAL_TYPE_LINE_BASIC