aboutsummaryrefslogtreecommitdiff
path: root/js/lib/rdge/materials/flag-material.js
diff options
context:
space:
mode:
Diffstat (limited to 'js/lib/rdge/materials/flag-material.js')
-rw-r--r--js/lib/rdge/materials/flag-material.js172
1 files changed, 92 insertions, 80 deletions
diff --git a/js/lib/rdge/materials/flag-material.js b/js/lib/rdge/materials/flag-material.js
index b87013ba..7ad8b6e9 100644
--- a/js/lib/rdge/materials/flag-material.js
+++ b/js/lib/rdge/materials/flag-material.js
@@ -37,19 +37,19 @@ var FlagMaterial = function FlagMaterial() {
37 /////////////////////////////////////////////////////////////////////// 37 ///////////////////////////////////////////////////////////////////////
38 // Instance variables 38 // Instance variables
39 /////////////////////////////////////////////////////////////////////// 39 ///////////////////////////////////////////////////////////////////////
40 this._name = "Flag"; 40 this._name = "Flag";
41 this._shaderName = "flag"; 41 this._shaderName = "flag";
42 42
43 this._texMap = 'assets/images/us_flag.png'; 43 this._texMap = 'assets/images/us_flag.png';
44 44
45 this._time = 0.0; 45 this._time = 0.0;
46 this._dTime = 0.1; 46 this._dTime = 0.1;
47 47
48 this._speed = 1.0; 48 this._speed = 1.0;
49 this._waveWidth = 1.0; 49 this._waveWidth = 1.0;
50 this._waveHeight = 1.0; 50 this._waveHeight = 1.0;
51 51
52 this._hasVertexDeformation = true; 52 this._hasVertexDeformation = true;
53 53
54 // array textures indexed by shader uniform name 54 // array textures indexed by shader uniform name
55 this._glTextures = []; 55 this._glTextures = [];
@@ -57,96 +57,108 @@ var FlagMaterial = function FlagMaterial() {
57 /////////////////////////////////////////////////////////////////////// 57 ///////////////////////////////////////////////////////////////////////
58 // Properties 58 // Properties
59 /////////////////////////////////////////////////////////////////////// 59 ///////////////////////////////////////////////////////////////////////
60 // all defined in parent PulseMaterial.js 60 // all defined in parent PulseMaterial.js
61 // load the local default value 61 // load the local default value
62 this._propNames = ["u_tex0", "u_waveWidth", "u_waveHeight", "u_speed" ]; 62 this._propNames = ["u_tex0", "u_waveWidth", "u_waveHeight", "u_speed" ];
63 this._propLabels = ["Texture map", "Wave Width", "Wave Height", "Speed" ]; 63 this._propLabels = ["Texture map", "Wave Width", "Wave Height", "Speed" ];
64 this._propTypes = ["file", "float", "float", "float" ]; 64 this._propTypes = ["file", "float", "float", "float" ];
65 this._propValues = []; 65 this._propValues = [];
66 66
67 this._propValues[ this._propNames[0] ] = this._texMap.slice(0); 67 this._propValues[ this._propNames[0] ] = this._texMap.slice(0);
68 this._propValues[ this._propNames[1] ] = this._waveWidth; 68 this._propValues[ this._propNames[1] ] = this._waveWidth;
69 this._propValues[ this._propNames[2] ] = this._waveHeight; 69 this._propValues[ this._propNames[2] ] = this._waveHeight;
70 this._propValues[ this._propNames[3] ] = this._speed; 70 this._propValues[ this._propNames[3] ] = this._speed;
71 71
72 72
73 // a material can be animated or not. default is not. 73 // a material can be animated or not. default is not.
74 // Any material needing continuous rendering should override this method 74 // Any material needing continuous rendering should override this method
75 this.isAnimated = function() { return true; }; 75 this.isAnimated = function() { return true; };
76 this.getShaderDef = function() { return flagMaterialDef; } 76 this.getShaderDef = function() { return flagMaterialDef; }
77 77
78 /////////////////////////////////////////////////////////////////////// 78 ///////////////////////////////////////////////////////////////////////
79 // Methods 79 // Methods
80 /////////////////////////////////////////////////////////////////////// 80 ///////////////////////////////////////////////////////////////////////
81 // duplcate method requirde 81 // duplcate method requirde
82 82
83 this.init = function( world ) 83 this.init = function( world )
84 {
85 // save the world
86 if (world) this.setWorld( world );
87
88 // set up the shader
89 this._shader = new RDGE.jshader();
90 this._shader.def = flagMaterialDef;
91 this._shader.init();
92
93 // set up the material node
94 this._materialNode = RDGE.createMaterialNode("flagMaterial" + "_" + world.generateUniqueNodeID());
95 this._materialNode.setShader(this._shader);
96
97 this._time = 0;
98 if (this._shader && this._shader['default'])
99 this._shader['default'].u_time.set( [this._time] );
100
101 // set the shader values in the shader
102 this.setShaderValues();
103 this.update( 0 );
104 }
105
106 this.resetToDefault = function()
84 { 107 {
85 // save the world 108 this._propValues[ this._propNames[0] ] = this._texMap.slice(0);
86 if (world) this.setWorld( world ); 109 this._propValues[ this._propNames[1] ] = this._waveWidth;
87 110 this._propValues[ this._propNames[2] ] = this._waveHeight;
88 // set up the shader 111 this._propValues[ this._propNames[3] ] = this._speed;
89 this._shader = new RDGE.jshader(); 112
90 this._shader.def = flagMaterialDef; 113 var nProps = this._propNames.length;
91 this._shader.init(); 114 for (var i=0; i<nProps; i++)
92 115 this.setProperty( this._propNames[i], this._propValues[this._propNames[i]] );
93 // set up the material node 116};
94 this._materialNode = RDGE.createMaterialNode("flagMaterial" + "_" + world.generateUniqueNodeID());
95 this._materialNode.setShader(this._shader);
96
97 this._time = 0;
98 if (this._shader && this._shader['default'])
99 this._shader['default'].u_time.set( [this._time] );
100
101 // set the shader values in the shader
102 this.setShaderValues();
103 this.update( 0 );
104 }
105}; 117};
106 118
107/////////////////////////////////////////////////////////////////////////////////////// 119///////////////////////////////////////////////////////////////////////////////////////
108// RDGE shader 120// RDGE shader
109 121
110// shader spec (can also be loaded from a .JSON file, or constructed at runtime) 122// shader spec (can also be loaded from a .JSON file, or constructed at runtime)
111var flagMaterialDef = 123var flagMaterialDef =
112{'shaders': 124{'shaders':
113 { 125 {
114 'defaultVShader':"assets/shaders/Flag.vert.glsl", 126 'defaultVShader':"assets/shaders/Flag.vert.glsl",
115 'defaultFShader':"assets/shaders/Flag.frag.glsl" 127 'defaultFShader':"assets/shaders/Flag.frag.glsl"
116 }, 128 },
117 'techniques': 129 'techniques':
118 { 130 {
119 'default': 131 'default':
120 [ 132 [
121 { 133 {
122 'vshader' : 'defaultVShader', 134 'vshader' : 'defaultVShader',
123 'fshader' : 'defaultFShader', 135 'fshader' : 'defaultFShader',
124 // attributes 136 // attributes
125 'attributes' : 137 'attributes' :
126 { 138 {
127 'vert' : { 'type' : 'vec3' }, 139 'vert' : { 'type' : 'vec3' },
128 'normal' : { 'type' : 'vec3' }, 140 'normal' : { 'type' : 'vec3' },
129 'texcoord' : { 'type' : 'vec2' } 141 'texcoord' : { 'type' : 'vec2' }
130 }, 142 },
131 // parameters 143 // parameters
132 'params' : 144 'params' :
133 { 145 {
134 'u_tex0': { 'type' : 'tex2d' }, 146 'u_tex0': { 'type' : 'tex2d' },
135 'u_time' : { 'type' : 'float' }, 147 'u_time' : { 'type' : 'float' },
136 'u_speed' : { 'type' : 'float' }, 148 'u_speed' : { 'type' : 'float' },
137 'u_waveWidth' : { 'type' : 'float' }, 149 'u_waveWidth' : { 'type' : 'float' },
138 'u_waveHeight' : { 'type' : 'float' } 150 'u_waveHeight' : { 'type' : 'float' }
139 }, 151 },
140 152
141 // render states 153 // render states
142 'states' : 154 'states' :
143 { 155 {
144 'depthEnable' : true, 156 'depthEnable' : true,
145 'offset':[1.0, 0.1] 157 'offset':[1.0, 0.1]
146 } 158 }
147 } 159 }
148 ] 160 ]
149 } 161 }
150}; 162};
151 163
152FlagMaterial.prototype = new PulseMaterial(); 164FlagMaterial.prototype = new PulseMaterial();