From 335ce503996e3ccbd2909086328d0a31fbd03370 Mon Sep 17 00:00:00 2001 From: hwc487 Date: Thu, 31 May 2012 17:07:10 -0700 Subject: Added the flag shader to the runtime. --- assets/canvas-runtime.js | 82 ++++++++++++++++++++++++++++++++-- assets/descriptor.json | 2 + js/io/system/ninjalibrary.json | 2 +- js/lib/rdge/materials/flag-material.js | 31 +++++++++++++ 4 files changed, 113 insertions(+), 4 deletions(-) diff --git a/assets/canvas-runtime.js b/assets/canvas-runtime.js index f4bfa0ae..918dffdc 100644 --- a/assets/canvas-runtime.js +++ b/assets/canvas-runtime.js @@ -580,9 +580,17 @@ NinjaCvsRt.RuntimeGeomObj = Object.create(Object.prototype, { case "zinvert": case "keleidoscope": case "radialBlur": - case "pulse": mat = Object.create(NinjaCvsRt.RuntimePulseMaterial, {}); break; + case "pulse": + mat = Object.create(NinjaCvsRt.RuntimePulseMaterial, {}); + break; - case "twistVert": mat = Object.create(NinjaCvsRt.RuntimeTwistVertMaterial, {}); break; + case "twistVert": + mat = Object.create(NinjaCvsRt.RuntimeTwistVertMaterial, {}); + break; + + case "flag": + mat = Object.create(NinjaCvsRt.RuntimeFlagMaterial, {}); + break; default: console.log( "material type: " + shaderName + " is not supported" ); @@ -1381,6 +1389,7 @@ NinjaCvsRt.RuntimeFlatMaterial = Object.create(NinjaCvsRt.RuntimeMaterial, { }); + NinjaCvsRt.RuntimeTwistVertMaterial = Object.create(NinjaCvsRt.RuntimeMaterial, { _name: { value: "TwistVertMaterial", writable: true }, _shaderName: { value: "twistVert", writable: true }, @@ -1410,7 +1419,7 @@ NinjaCvsRt.RuntimeTwistVertMaterial = Object.create(NinjaCvsRt.RuntimeMaterial, init: { value: function(world) { - var material = this._materialNode; + var material = this._materialNode; if (material) { var technique = material.shaderProgram["twistMe"]; @@ -1488,6 +1497,7 @@ NinjaCvsRt.RuntimePulseMaterial = Object.create(NinjaCvsRt.RuntimeMaterial, { if (tex) technique.u_tex0.set( tex ); + this._shader["default"].u_speed.set( [1.0] ); this._shader["default"].u_time.set( [this._time] ); } } @@ -1516,6 +1526,72 @@ NinjaCvsRt.RuntimePulseMaterial = Object.create(NinjaCvsRt.RuntimeMaterial, { } }); + +NinjaCvsRt.RuntimeFlagMaterial = Object.create(NinjaCvsRt.RuntimeMaterial, { + _name: { value: "FlagMaterial", writable: true }, + _shaderName: { value: "flag", writable: true }, + + // default values + _texMap: { value: 'assets/images/cubelight.png', writable: true }, + _speed: { value: 1.0, writable: true }, + _waveWidth: { value: 1.0, writable: true }, + _waveHeight: { value: 1.0, writable: true }, + _dTime: { value: 0.1, writable: true }, + + importJSON: { + value: function(jObj) { + this._texMap = jObj.texMap; + this._waveWidth = jObj.waveWidth; + this._waveHeight = jObj.waveHeight; + this._speed = jObj.speed; + } + }, + + init: { + value: function(world) { + if (this._shader) { + var material = this._materialNode; + if (material) + { + var technique = material.shaderProgram['default']; + var renderer = RDGE.globals.engine.getContext().renderer; + if (renderer && technique) + { + + if (this._shader && this._shader['default']) { + var wrap = 'REPEAT', mips = true; + var tex = renderer.getTextureByName(this._texMap, wrap, mips ); + if (tex) technique.u_tex0.set( tex ); + + technique.u_speed.set( [this._speed] ); + technique.u_waveWidth.set( [this._waveWidth] ); + technique.u_waveHeight.set( [this._waveHeight] ); + } + } + } + } + } + }, + + update: { + value: function(time) { + var material = this._materialNode; + if (material) + { + var technique = material.shaderProgram['default']; + var renderer = RDGE.globals.engine.getContext().renderer; + if (renderer && technique) + { + if (this._shader && this._shader['default']) { + this._shader['default'].u_time.set( [this._time] ); + } + this._time += this._dTime * this._speed; + } + } + } + } +}); + NinjaCvsRt.RuntimeRadialGradientMaterial = Object.create(NinjaCvsRt.RuntimeMaterial, { _name: { value: "RadialGradientMaterial", writable: true }, _shaderName: { value: "radialGradient", writable: true }, diff --git a/assets/descriptor.json b/assets/descriptor.json index 85fbc996..2873205f 100644 --- a/assets/descriptor.json +++ b/assets/descriptor.json @@ -24,6 +24,7 @@ "images/paris.png", "images/powderblue.png", "images/raiders.png", + "images/us_flag.png", "shaders/Basic.frag.glsl", "shaders/TwistVert.vert.glsl", "shaders/Basic.vert.glsl", @@ -31,6 +32,7 @@ "shaders/Deform.frag.glsl", "shaders/Water2.frag.glsl", "shaders/Flag.vert.glsl", + "shaders/Flag.frag.glsl", "shaders/ZInvert.frag.glsl", "shaders/Fly.frag.glsl", "shaders/linearGradient.frag.glsl", diff --git a/js/io/system/ninjalibrary.json b/js/io/system/ninjalibrary.json index 9547c1e8..bee603ae 100644 --- a/js/io/system/ninjalibrary.json +++ b/js/io/system/ninjalibrary.json @@ -1,6 +1,6 @@ { "libraries": [ {"name": "Montage", "path": "/node_modules/descriptor.json", "version": "0.8.0.0"}, - {"name": "RDGE", "path": "/assets/descriptor.json", "version": "0.5.6.4"} + {"name": "RDGE", "path": "/assets/descriptor.json", "version": "0.5.6.6"} ] } \ No newline at end of file diff --git a/js/lib/rdge/materials/flag-material.js b/js/lib/rdge/materials/flag-material.js index fa844cc3..d1788fb8 100644 --- a/js/lib/rdge/materials/flag-material.js +++ b/js/lib/rdge/materials/flag-material.js @@ -160,6 +160,37 @@ var FlagMaterial = function FlagMaterial() { this.updateTexture(); this.update( 0 ); } + + this.exportJSON = function () { + var jObj = + { + 'material' : this.getShaderName(), + 'name' : this.getName(), + + 'texMap' : this._propValues[this._propNames[0]], + 'waveWidth' : this._propValues[this._propNames[1]], + 'waveHeight' : this._propValues[this._propNames[2]], + 'speed' : this._propValues[this._propNames[3]] + }; + + return jObj; + }; + + this.importJSON = function (jObj) { + if (this.getShaderName() != jObj.material) throw new Error("ill-formed material"); + this.setName(jObj.name); + + try { + + this._texMap = this._propValues[this._propNames[0]] = jObj.texMap; + this._waveWidth = this._propValues[this._propNames[1]] = jObj.waveWidth; + this._waveHeight = this._propValues[this._propNames[2]] = jObj.waveHeight; + this._speed = this._propValues[this._propNames[3]] = jObj.speed; + } + catch (e) { + throw new Error("could not import material: " + importStr); + } + }; }; /////////////////////////////////////////////////////////////////////////////////////// -- cgit v1.2.3