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.js387
1 files changed, 96 insertions, 291 deletions
diff --git a/js/lib/rdge/materials/water-material.js b/js/lib/rdge/materials/water-material.js
index 8dbb3233..3e63ddb6 100644
--- a/js/lib/rdge/materials/water-material.js
+++ b/js/lib/rdge/materials/water-material.js
@@ -11,81 +11,49 @@ var Texture = require("js/lib/rdge/texture").Texture;
11// Class GLMaterial 11// Class GLMaterial
12// RDGE representation of a material. 12// RDGE representation of a material.
13/////////////////////////////////////////////////////////////////////// 13///////////////////////////////////////////////////////////////////////
14var WaterMaterial = function WaterMaterial() { 14var WaterMaterial = function WaterMaterial()
15 /////////////////////////////////////////////////////////////////////// 15{
16 // Instance variables 16 ///////////////////////////////////////////////////////////////////////
17 /////////////////////////////////////////////////////////////////////// 17 // Instance variables
18 this._name = "WaterMaterial"; 18 ///////////////////////////////////////////////////////////////////////
19 this._shaderName = "water"; 19 this._name = "WaterMaterial";
20 20 this._shaderName = "water";
21 this._texMap = 'assets/images/rocky-normal.jpg'; 21
22 //this._texMap = 'assets/images/powderblue.png'; 22 this._defaultTexMap = 'assets/images/rocky-normal.jpg';
23 23
24 this._time = 0.0; 24 this._time = 0.0;
25 this._dTime = 0.01; 25 this._dTime = 0.01;
26 this._emboss = 0.3; 26
27 this._delta = 20.0; 27 this.isAnimated = function() { return true; };
28 this._intensity = 3.0; 28 this.getShaderDef = function() { return waterMaterialDef; };
29 29
30 this._speed = 0.2; 30 ///////////////////////////////////////////////////////////////////////
31 31 // Properties
32 /////////////////////////////////////////////////////////////////////// 32 ///////////////////////////////////////////////////////////////////////
33 // Properties 33 // all defined in parent PulseMaterial.js
34 /////////////////////////////////////////////////////////////////////// 34 // load the local default value
35 // all defined in parent PulseMaterial.js 35 this._propNames = ["u_tex0", "u_emboss", "u_delta", "u_intensity", "u_speed"];
36 // load the local default value
37 //this._propValues = [];
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"]; 36 this._propLabels = ["Texture map", "Emboss", "Delta", "Intensity", "Speed"];
41 this._propTypes = ["file", "float", "float", "float", "float"]; 37 this._propTypes = ["file", "float", "float", "float", "float"];
42 38
43 this._propValues = []; 39 var u_tex_index = 0,
44 this._propValues[ this._propNames[0] ] = this._texMap.slice(0); 40 u_emboss_index = 1,
45 this._propValues[ this._propNames[1] ] = this._emboss; 41 u_delta_index = 2,
46 this._propValues[ this._propNames[2] ] = this._delta; 42 u_intensity_index = 3,
47 this._propValues[ this._propNames[3] ] = this._intensity; 43 u_speed_index = 4;
48 this._propValues[ this._propNames[4] ] = this._speed;
49
50 ///////////////////////////////////////////////////////////////////////
51 // Methods
52 ///////////////////////////////////////////////////////////////////////
53
54 this.setProperty = function( prop, value ) {
55 // make sure we have legitimate imput
56 var ok = this.validateProperty( prop, value );
57 if (!ok) {
58 console.log( "invalid property in Water Material:" + prop + " : " + value );
59 }
60
61 switch (prop)
62 {
63 case "texmap":
64 this.setTextureMap(value);
65 break;
66
67 case "emboss":
68 this.setEmboss( value );
69 break;
70
71 case "delta":
72 this.setDelta( value );
73 break;
74
75 case "intensity":
76 this.setIntensity( value );
77 break;
78 44
79 case "speed": 45 this._propValues = [];
80 this.setSpeed( value ); 46 this._propValues[ this._propNames[u_tex_index ] ] = this._defaultTexMap.slice(0);
81 break; 47 this._propValues[ this._propNames[u_emboss_index ] ] = 0.3;
48 this._propValues[ this._propNames[u_delta_index ] ] = 20.0;
49 this._propValues[ this._propNames[u_intensity_index ] ] = 3.0;
50 this._propValues[ this._propNames[u_speed_index ] ] = 0.2;
82 51
83 case "color": 52 ///////////////////////////////////////////////////////////////////////
84 break; 53 // Methods
85 } 54 ///////////////////////////////////////////////////////////////////////
86 };
87 55
88 this.init = function (world) { 56 this.init = function (world) {
89 // save the world 57 // save the world
90 if (world) this.setWorld(world); 58 if (world) this.setWorld(world);
91 59
@@ -99,119 +67,14 @@ var WaterMaterial = function WaterMaterial() {
99 this._materialNode.setShader(this._shader); 67 this._materialNode.setShader(this._shader);
100 68
101 this._time = 0; 69 this._time = 0;
102 if (this._shader && this._shader['default']) { 70 if (this._shader && this._shader['default'])
103 this._shader['default'].u_time.set([this._time]); 71 this._shader['default'].u_time.set([this._time]);
104 72
105 this._shader['default'].u_emboss.set( [this._emboss] );
106 this._shader['default'].u_delta.set( [this._delta] );
107 this._shader['default'].u_intensity.set( [this._intensity] );
108 this._shader['default'].u_speed.set( [this._speed] );
109 }
110
111 var texMapName = this._propValues[this._propNames[0]];
112 this._glTex = new Texture( world, texMapName );
113
114 // set the shader values in the shader 73 // set the shader values in the shader
115 this.updateTexture(); 74 this.setShaderValues();
116 this.setResolution([world.getViewportWidth(), world.getViewportHeight()]); 75 this.setResolution([world.getViewportWidth(), world.getViewportHeight()]);
117 this.update(0); 76 this.update(0);
118 }; 77 };
119
120 this.setTextureMap = function( texMapName )
121 {
122 this._texMap = texMapName.slice();
123 this._propValues[ this._propNames[0] ] = this._texMap.slice(0);
124
125 this._glTex = new Texture( this.getWorld(), texMapName );
126 this.updateTexture();
127 }
128
129 this.updateTexture = function() {
130
131 var texMapName = this._propValues[this._propNames[0]];
132 this._glTex = new Texture( this.getWorld(), texMapName );
133
134 var material = this._materialNode;
135 if (material) {
136 var technique = material.shaderProgram['default'];
137 var renderer = RDGE.globals.engine.getContext().renderer;
138 if (renderer && technique) {
139 var wrap = 'REPEAT', mips = true;
140 var tex;
141 if (this._glTex)
142 {
143 if (this._glTex.isAnimated())
144 this._glTex.render();
145 tex = this._glTex.getTexture();
146 }
147
148 if (tex) {
149 technique.u_tex0.set( tex );
150 }
151 }
152 }
153 };
154
155 this.setEmboss = function( value )
156 {
157 this._emboss = value;
158 this._propValues[ "emboss" ] = value;
159
160 var material = this._materialNode;
161 if (material) {
162 var technique = material.shaderProgram['default'];
163 var renderer = RDGE.globals.engine.getContext().renderer;
164 if (renderer && technique) {
165 technique.u_emboss.set( [value] );
166 }
167 }
168 }
169
170 this.setIntensity = function( value )
171 {
172 this._intensity = value;
173 this._propValues[ "intensity" ] = value;
174
175 var material = this._materialNode;
176 if (material) {
177 var technique = material.shaderProgram['default'];
178 var renderer = RDGE.globals.engine.getContext().renderer;
179 if (renderer && technique) {
180 technique.u_intensity.set( [value] );
181 console.log( "setting intensity to: " + value );
182 }
183 }
184 }
185
186 this.setDelta = function( value )
187 {
188 this._delta = value;
189 this._propValues[ "delta" ] = value;
190
191 var material = this._materialNode;
192 if (material) {
193 var technique = material.shaderProgram['default'];
194 var renderer = RDGE.globals.engine.getContext().renderer;
195 if (renderer && technique) {
196 technique.u_delta.set( [value] );
197 }
198 }
199 }
200
201 this.setSpeed =