diff --git a/src/game-lib-a-component-a.js b/src/game-lib-a-component-a.js index 709f0cb..e3312c0 100644 --- a/src/game-lib-a-component-a.js +++ b/src/game-lib-a-component-a.js @@ -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; } diff --git a/src/game-lib-api-image.js b/src/game-lib-api-image.js index e540914..6568e2d 100644 --- a/src/game-lib-api-image.js +++ b/src/game-lib-api-image.js @@ -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, diff --git a/src/game-lib-custom-code.js b/src/game-lib-custom-code.js index c06e3bd..2668193 100644 --- a/src/game-lib-custom-code.js +++ b/src/game-lib-custom-code.js @@ -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)) }; diff --git a/src/game-lib-d3-api-geometry-a.js b/src/game-lib-d3-api-geometry-a.js index ce53047..de7c9ff 100644 --- a/src/game-lib-d3-api-geometry-a.js +++ b/src/game-lib-d3-api-geometry-a.js @@ -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; diff --git a/src/game-lib-d3-api-geometry-buffer-instanced.js b/src/game-lib-d3-api-geometry-buffer-instanced.js new file mode 100644 index 0000000..b70a081 --- /dev/null +++ b/src/game-lib-d3-api-geometry-buffer-instanced.js @@ -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; diff --git a/src/game-lib-d3-api-material-a.js b/src/game-lib-d3-api-material-a.js index 955ce93..d6feb5b 100644 --- a/src/game-lib-d3-api-material-a.js +++ b/src/game-lib-d3-api-material-a.js @@ -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); } diff --git a/src/game-lib-d3-api-material-shader.js b/src/game-lib-d3-api-material-shader.js new file mode 100644 index 0000000..2051503 --- /dev/null +++ b/src/game-lib-d3-api-material-shader.js @@ -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; diff --git a/src/game-lib-d3-api-shader-a.js b/src/game-lib-d3-api-shader-a.js new file mode 100644 index 0000000..4b03e89 --- /dev/null +++ b/src/game-lib-d3-api-shader-a.js @@ -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; diff --git a/src/game-lib-d3-api-shader-fragment.js b/src/game-lib-d3-api-shader-fragment.js new file mode 100644 index 0000000..be3503d --- /dev/null +++ b/src/game-lib-d3-api-shader-fragment.js @@ -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; diff --git a/src/game-lib-d3-api-shader-vertex.js b/src/game-lib-d3-api-shader-vertex.js new file mode 100644 index 0000000..a6e796c --- /dev/null +++ b/src/game-lib-d3-api-shader-vertex.js @@ -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; diff --git a/src/game-lib-d3-api-shadow-a.js b/src/game-lib-d3-api-shadow-a.js index 30d4c15..1fb5dec 100644 --- a/src/game-lib-d3-api-shadow-a.js +++ b/src/game-lib-d3-api-shadow-a.js @@ -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; diff --git a/src/game-lib-d3-api-shadow-directional.js b/src/game-lib-d3-api-shadow-directional.js index 1c0a396..efce9f8 100644 --- a/src/game-lib-d3-api-shadow-directional.js +++ b/src/game-lib-d3-api-shadow-directional.js @@ -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, diff --git a/src/game-lib-d3-geometry-buffer-instanced.js b/src/game-lib-d3-geometry-buffer-instanced.js new file mode 100644 index 0000000..5db91f7 --- /dev/null +++ b/src/game-lib-d3-geometry-buffer-instanced.js @@ -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; +}; diff --git a/src/game-lib-d3-light-directional.js b/src/game-lib-d3-light-directional.js index bcff948..3c2b6ea 100644 --- a/src/game-lib-d3-light-directional.js +++ b/src/game-lib-d3-light-directional.js @@ -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; diff --git a/src/game-lib-d3-material-a.js b/src/game-lib-d3-material-a.js index 7c38380..aafb814 100644 --- a/src/game-lib-d3-material-a.js +++ b/src/game-lib-d3-material-a.js @@ -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; diff --git a/src/game-lib-d3-material-shader.js b/src/game-lib-d3-material-shader.js new file mode 100644 index 0000000..438a4fd --- /dev/null +++ b/src/game-lib-d3-material-shader.js @@ -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; +}; diff --git a/src/game-lib-d3-mesh-0.js b/src/game-lib-d3-mesh-0.js index a0ca038..fa30126 100644 --- a/src/game-lib-d3-mesh-0.js +++ b/src/game-lib-d3-mesh-0.js @@ -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 diff --git a/src/game-lib-d3-mesh-plane.js b/src/game-lib-d3-mesh-plane.js index 9d2407d..59cab09 100644 --- a/src/game-lib-d3-mesh-plane.js +++ b/src/game-lib-d3-mesh-plane.js @@ -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 3) { + height = height / 768; + } else { + height = 0; + } + + data[j++] = height; + } + + return data; +}; \ No newline at end of file diff --git a/src/game-lib-system-gui.js b/src/game-lib-system-gui.js index dd2e224..1bd609f 100644 --- a/src/game-lib-system-gui.js +++ b/src/game-lib-system-gui.js @@ -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