aboutsummaryrefslogtreecommitdiff
path: root/js/lib/rdge/materials/water-material.js
diff options
context:
space:
mode:
Diffstat (limited to 'js/lib/rdge/materials/water-material.js')
-rw-r--r--js/lib/rdge/materials/water-material.js182
1 files changed, 158 insertions, 24 deletions
diff --git a/js/lib/rdge/materials/water-material.js b/js/lib/rdge/materials/water-material.js
index 37836636..1f0aa46e 100644
--- a/js/lib/rdge/materials/water-material.js
+++ b/js/lib/rdge/materials/water-material.js
@@ -5,6 +5,7 @@ No rights, expressed or implied, whatsoever to this software are provided by Mot
5</copyright> */ 5</copyright> */
6 6
7var PulseMaterial = require("js/lib/rdge/materials/pulse-material").PulseMaterial; 7var PulseMaterial = require("js/lib/rdge/materials/pulse-material").PulseMaterial;
8var Texture = require("js/lib/rdge/texture").Texture;
8 9
9/////////////////////////////////////////////////////////////////////// 10///////////////////////////////////////////////////////////////////////
10// Class GLMaterial 11// Class GLMaterial
@@ -22,26 +23,43 @@ var WaterMaterial = function WaterMaterial() {
22 23
23 this._time = 0.0; 24 this._time = 0.0;
24 this._dTime = 0.01; 25 this._dTime = 0.01;
26 this._emboss = 0.3;
27 this._delta = 20.0;
28 this._intensity = 3.0;
29
30 this._speed = 0.2;
25 31
26 /////////////////////////////////////////////////////////////////////// 32 ///////////////////////////////////////////////////////////////////////
27 // Properties 33 // Properties
28 /////////////////////////////////////////////////////////////////////// 34 ///////////////////////////////////////////////////////////////////////
29 // all defined in parent PulseMaterial.js 35 // all defined in parent PulseMaterial.js
30 // load the local default value 36 // load the local default value
31 this._propValues = []; 37 //this._propValues = [];
32 this._propValues[this._propNames[0]] = this._texMap.slice(0); 38 //this._propValues[this._propNames[0]] = this._texMap.slice(0);
39 this._propNames = ["texmap", "emboss", "delta", "intensity", "speed"];
40 this._propLabels = ["Texture map", "Emboss", "Delta", "Intensity", "Speed"];
41 this._propTypes = ["file", "float", "float", "float", "float"];
42
43 this._propValues = [];
44 this._propValues[ this._propNames[0] ] = this._texMap.slice(0);
45 this._propValues[ this._propNames[1] ] = this._emboss;
46 this._propValues[ this._propNames[2] ] = this._delta;
47 this._propValues[ this._propNames[3] ] = this._intensity;
48 this._propValues[ this._propNames[4] ] = this._speed;
33 49
34 /////////////////////////////////////////////////////////////////////// 50 ///////////////////////////////////////////////////////////////////////
35 // Methods 51 // Methods
36 /////////////////////////////////////////////////////////////////////// 52 ///////////////////////////////////////////////////////////////////////
37 // duplcate method requirde 53 // duplcate method requirde
38 this.dup = function (world) { 54 this.dup = function (world) {
39 // allocate a new uber material 55 // get the current values;
40 var newMat = new WaterMaterial();
41
42 // copy over the current values;
43 var propNames = [], propValues = [], propTypes = [], propLabels = []; 56 var propNames = [], propValues = [], propTypes = [], propLabels = [];
44 this.getAllProperties(propNames, propValues, propTypes, propLabels); 57 this.getAllProperties(propNames, propValues, propTypes, propLabels);
58
59 // allocate a new material
60 var newMat = new WaterMaterial();
61
62 // copy over the current values;
45 var n = propNames.length; 63 var n = propNames.length;
46 for (var i = 0; i < n; i++) 64 for (var i = 0; i < n; i++)
47 newMat.setProperty(propNames[i], propValues[i]); 65 newMat.setProperty(propNames[i], propValues[i]);
@@ -49,29 +67,141 @@ var WaterMaterial = function WaterMaterial() {
49 return newMat; 67 return newMat;
50 }; 68 };
51 69
70 this.setProperty = function( prop, value ) {
71 // make sure we have legitimate imput
72 var ok = this.validateProperty( prop, value );
73 if (!ok) {
74 console.log( "invalid property in Water Material:" + prop + " : " + value );
75 }
76
77 switch (prop)
78 {
79 case "texmap":
80 this.setTextureMap(value);
81 break;
82
83 case "emboss":
84 this.setEmboss( value );
85 break;
86
87 case "delta":
88 this.setDelta( value );
89 break;
90
91 case "intensity":
92 this.setIntensity( value );
93 break;
94
95 case "speed":
96 this.setSpeed( value );
97 break;
98
99 case "color":
100 break;
101 }
102 };
103
52 this.init = function (world) { 104 this.init = function (world) {
53 // save the world 105 // save the world
54 if (world) this.setWorld(world); 106 if (world) this.setWorld(world);
107
108 // set up the shader
109 this._shader = new RDGE.jshader();
110 this._shader.def = waterMaterialDef;
111 this._shader.init();
112
113 // set up the material node
114 this._materialNode = RDGE.createMaterialNode("waterMaterial" + "_" + world.generateUniqueNodeID());
115 this._materialNode.setShader(this._shader);
116
117 this._time = 0;
118 if (this._shader && this._shader['default']) {
119 this._shader['default'].u_time.set([this._time]);
120
121 this._shader['default'].u_emboss.set( [this._emboss] );
122 this._shader['default'].u_delta.set( [this._delta] );
123 this._shader['default'].u_intensity.set( [this._intensity] );
124 this._shader['default'].u_speed.set( [this._speed] );
125 }
126
127 var texMapName = this._propValues[this._propNames[0]];
128 this._glTex = new Texture( world, texMapName );
129
130 // set the shader values in the shader
131 this.updateTexture();
132 this.setResolution([world.getViewportWidth(), world.getViewportHeight()]);
133 this.update(0);
134 };
135
136 this.setTextureMap = function( texMapName )
137 {
138 this._texMap = texMapName.slice();
139 this._propValues[ this._propNames[0] ] = this._texMap.slice(0);
55 140
56 // set up the shader 141 this._glTex = new Texture( this.getWorld(), texMapName );
57 this._shader = new RDGE.jshader(); 142 this.updateTexture();
58 this._shader.def = waterMaterialDef; 143 }
59 this._shader.init();
60 144
61 // set up the material node 145 this.setEmboss = function( value )
62 this._materialNode = RDGE.createMaterialNode("waterMaterial" + "_" + world.generateUniqueNodeID()); 146 {
63 this._materialNode.setShader(this._shader); 147 this._emboss = value;
148 this._propValues[ "emboss" ] = value;
149
150 var material = this._materialNode;
151 if (material) {
152 var technique = material.shaderProgram['default'];
153 var renderer = RDGE.globals.engine.getContext().renderer;
154 if (renderer && technique) {
155 technique.u_emboss.set( [value] );
156 }
157 }
158 }
64 159
65 this._time = 0; 160 this.setIntensity = function( value )
66 if (this._shader && this._shader['default']) { 161 {
67 this._shader['default'].u_time.set([this._time]); 162 this._intensity = value;
68 } 163 this._propValues[ "intensity" ] = value;
164
165 var material = this._materialNode;
166 if (material) {
167 var technique = material.shaderProgram['default'];
168 var renderer = RDGE.globals.engine.getContext().renderer;
169 if (renderer && technique) {
170 technique.u_intensity.set( [value] );
171 console.log( "setting intensity to: " + value );
172 }
173 }
174 }
69 175
70 // set the shader values in the shader 176 this.setDelta = function( value )
71 this.updateTexture(); 177 {
72 this.setResolution([world.getViewportWidth(), world.getViewportHeight()]); 178 this._delta = value;
73 this.update(0); 179 this._propValues[ "delta" ] = value;
74 }; 180
181 var material = this._materialNode;
182 if (material) {
183 var technique = material.shaderProgram['default'];
184 var renderer = RDGE.globals.engine.getContext().renderer;
185 if (renderer && technique) {
186 technique.u_delta.set( [value] );
187 }
188 }
189 }
190
191 this.setSpeed = function( value )
192 {
193 this._speed = value;
194 this._propValues[ "speed" ] = value;
195
196 var material = this._materialNode;
197 if (material) {
198 var technique = material.shaderProgram['default'];
199 var renderer = RDGE.globals.engine.getContext().renderer;
200 if (renderer && technique) {
201 techniq