diff options
Diffstat (limited to 'js/lib/rdge/materials/flag-material.js')
-rw-r--r-- | js/lib/rdge/materials/flag-material.js | 195 |
1 files changed, 104 insertions, 91 deletions
diff --git a/js/lib/rdge/materials/flag-material.js b/js/lib/rdge/materials/flag-material.js index b87013ba..4b80f126 100644 --- a/js/lib/rdge/materials/flag-material.js +++ b/js/lib/rdge/materials/flag-material.js | |||
@@ -1,24 +1,25 @@ | |||
1 | /* <copyright> | 1 | /* <copyright> |
2 | Copyright (c) 2012, Motorola Mobility, Inc | 2 | Copyright (c) 2012, Motorola Mobility LLC. |
3 | All Rights Reserved. | 3 | All Rights Reserved. |
4 | BSD License. | ||
5 | 4 | ||
6 | Redistribution and use in source and binary forms, with or without | 5 | Redistribution and use in source and binary forms, with or without |
7 | modification, are permitted provided that the following conditions are met: | 6 | modification, are permitted provided that the following conditions are met: |
8 | 7 | ||
9 | - Redistributions of source code must retain the above copyright notice, | 8 | * Redistributions of source code must retain the above copyright notice, |
10 | this list of conditions and the following disclaimer. | 9 | this list of conditions and the following disclaimer. |
11 | - Redistributions in binary form must reproduce the above copyright | 10 | |
12 | notice, this list of conditions and the following disclaimer in the | 11 | * Redistributions in binary form must reproduce the above copyright notice, |
13 | documentation and/or other materials provided with the distribution. | 12 | this list of conditions and the following disclaimer in the documentation |
14 | - Neither the name of Motorola Mobility nor the names of its contributors | 13 | and/or other materials provided with the distribution. |
15 | may be used to endorse or promote products derived from this software | 14 | |
16 | without specific prior written permission. | 15 | * Neither the name of Motorola Mobility LLC nor the names of its |
16 | contributors may be used to endorse or promote products derived from this | ||
17 | software without specific prior written permission. | ||
17 | 18 | ||
18 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | 19 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
19 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | 20 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
20 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | 21 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
21 | ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE | 22 | ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
22 | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | 23 | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
23 | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | 24 | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
24 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | 25 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
@@ -37,19 +38,19 @@ var FlagMaterial = function FlagMaterial() { | |||
37 | /////////////////////////////////////////////////////////////////////// | 38 | /////////////////////////////////////////////////////////////////////// |
38 | // Instance variables | 39 | // Instance variables |
39 | /////////////////////////////////////////////////////////////////////// | 40 | /////////////////////////////////////////////////////////////////////// |
40 | this._name = "Flag"; | 41 | this._name = "Flag"; |
41 | this._shaderName = "flag"; | 42 | this._shaderName = "flag"; |
42 | 43 | ||
43 | this._texMap = 'assets/images/us_flag.png'; | 44 | this._texMap = 'assets/images/us_flag.png'; |
44 | 45 | ||
45 | this._time = 0.0; | 46 | this._time = 0.0; |
46 | this._dTime = 0.1; | 47 | this._dTime = 0.1; |
47 | 48 | ||
48 | this._speed = 1.0; | 49 | this._speed = 1.0; |
49 | this._waveWidth = 1.0; | 50 | this._waveWidth = 1.0; |
50 | this._waveHeight = 1.0; | 51 | this._waveHeight = 1.0; |
51 | 52 | ||
52 | this._hasVertexDeformation = true; | 53 | this._hasVertexDeformation = true; |
53 | 54 | ||
54 | // array textures indexed by shader uniform name | 55 | // array textures indexed by shader uniform name |
55 | this._glTextures = []; | 56 | this._glTextures = []; |
@@ -57,96 +58,108 @@ var FlagMaterial = function FlagMaterial() { | |||
57 | /////////////////////////////////////////////////////////////////////// | 58 | /////////////////////////////////////////////////////////////////////// |
58 | // Properties | 59 | // Properties |
59 | /////////////////////////////////////////////////////////////////////// | 60 | /////////////////////////////////////////////////////////////////////// |
60 | // all defined in parent PulseMaterial.js | 61 | // all defined in parent PulseMaterial.js |
61 | // load the local default value | 62 | // load the local default value |
62 | this._propNames = ["u_tex0", "u_waveWidth", "u_waveHeight", "u_speed" ]; | 63 | this._propNames = ["u_tex0", "u_waveWidth", "u_waveHeight", "u_speed" ]; |
63 | this._propLabels = ["Texture map", "Wave Width", "Wave Height", "Speed" ]; | 64 | this._propLabels = ["Texture map", "Wave Width", "Wave Height", "Speed" ]; |
64 | this._propTypes = ["file", "float", "float", "float" ]; | 65 | this._propTypes = ["file", "float", "float", "float" ]; |
65 | this._propValues = []; | 66 | this._propValues = []; |
66 | 67 | ||
67 | this._propValues[ this._propNames[0] ] = this._texMap.slice(0); | 68 | this._propValues[ this._propNames[0] ] = this._texMap.slice(0); |
68 | this._propValues[ this._propNames[1] ] = this._waveWidth; | 69 | this._propValues[ this._propNames[1] ] = this._waveWidth; |
69 | this._propValues[ this._propNames[2] ] = this._waveHeight; | 70 | this._propValues[ this._propNames[2] ] = this._waveHeight; |
70 | this._propValues[ this._propNames[3] ] = this._speed; | 71 | this._propValues[ this._propNames[3] ] = this._speed; |
71 | 72 | ||
72 | 73 | ||
73 | // a material can be animated or not. default is not. | 74 | // a material can be animated or not. default is not. |
74 | // Any material needing continuous rendering should override this method | 75 | // Any material needing continuous rendering should override this method |
75 | this.isAnimated = function() { return true; }; | 76 | this.isAnimated = function() { return true; }; |
76 | this.getShaderDef = function() { return flagMaterialDef; } | 77 | this.getShaderDef = function() { return flagMaterialDef; } |
77 | 78 | ||
78 | /////////////////////////////////////////////////////////////////////// | 79 | /////////////////////////////////////////////////////////////////////// |
79 | // Methods | 80 | // Methods |
80 | /////////////////////////////////////////////////////////////////////// | 81 | /////////////////////////////////////////////////////////////////////// |
81 | // duplcate method requirde | 82 | // duplcate method requirde |
83 | |||
84 | this.init = function( world ) | ||
85 | { | ||
86 | // save the world | ||
87 | if (world) this.setWorld( world ); | ||
88 | |||
89 | // set up the shader | ||
90 | this._shader = new RDGE.jshader(); | ||
91 | this._shader.def = flagMaterialDef; | ||
92 | this._shader.init(); | ||
93 | |||
94 | // set up the material node | ||
95 | this._materialNode = RDGE.createMaterialNode("flagMaterial" + "_" + world.generateUniqueNodeID()); | ||
96 | this._materialNode.setShader(this._shader); | ||
82 | 97 | ||
83 | this.init = function( world ) | 98 | this._time = 0; |
99 | if (this._shader && this._shader['default']) | ||
100 | this._shader['default'].u_time.set( [this._time] ); | ||
101 | |||
102 | // set the shader values in the shader | ||
103 | this.setShaderValues(); | ||
104 | this.update( 0 ); | ||
105 | } | ||
106 | |||
107 | this.resetToDefault = function() | ||
84 | { | 108 | { |
85 | // save the world | 109 | this._propValues[ this._propNames[0] ] = this._texMap.slice(0); |
86 | if (world) this.setWorld( world ); | 110 | this._propValues[ this._propNames[1] ] = this._waveWidth; |
87 | 111 | this._propValues[ this._propNames[2] ] = this._waveHeight; | |
88 | // set up the shader | 112 | this._propValues[ this._propNames[3] ] = this._speed; |
89 | this._shader = new RDGE.jshader(); | 113 | |
90 | this._shader.def = flagMaterialDef; | 114 | var nProps = this._propNames.length; |
91 | this._shader.init(); | 115 | for (var i=0; i<nProps; i++) |
92 | 116 | this.setProperty( this._propNames[i], this._propValues[this._propNames[i]] ); | |
93 | // set up the material node | 117 | }; |
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 | }; | 118 | }; |
106 | 119 | ||
107 | /////////////////////////////////////////////////////////////////////////////////////// | 120 | /////////////////////////////////////////////////////////////////////////////////////// |
108 | // RDGE shader | 121 | // RDGE shader |
109 | 122 | ||
110 | // shader spec (can also be loaded from a .JSON file, or constructed at runtime) | 123 | // shader spec (can also be loaded from a .JSON file, or constructed at runtime) |
111 | var flagMaterialDef = | 124 | var flagMaterialDef = |
112 | {'shaders': | 125 | {'shaders': |
113 | { | 126 | { |
114 | 'defaultVShader':"assets/shaders/Flag.vert.glsl", | 127 | 'defaultVShader':"assets/shaders/Flag.vert.glsl", |
115 | 'defaultFShader':"assets/shaders/Flag.frag.glsl" | 128 | 'defaultFShader':"assets/shaders/Flag.frag.glsl" |
116 | }, | 129 | }, |
117 | 'techniques': | 130 | 'techniques': |
118 | { | 131 | { |
119 | 'default': | 132 | 'default': |
120 | [ | 133 | [ |
121 | { | 134 | { |
122 | 'vshader' : 'defaultVShader', | 135 | 'vshader' : 'defaultVShader', |
123 | 'fshader' : 'defaultFShader', | 136 | 'fshader' : 'defaultFShader', |
124 | // attributes | 137 | // attributes |
125 | 'attributes' : | 138 | 'attributes' : |
126 | { | 139 | { |
127 | 'vert' : { 'type' : 'vec3' }, | 140 | 'vert' : { 'type' : 'vec3' }, |
128 | 'normal' : { 'type' : 'vec3' }, | 141 | 'normal' : { 'type' : 'vec3' }, |
129 | 'texcoord' : { 'type' : 'vec2' } | 142 | 'texcoord' : { 'type' : 'vec2' } |
130 | }, | 143 | }, |
131 | // parameters | 144 | // parameters |
132 | 'params' : | 145 | 'params' : |
133 | { | 146 | { |
134 | 'u_tex0': { 'type' : 'tex2d' }, | 147 | 'u_tex0': { 'type' : 'tex2d' }, |
135 | 'u_time' : { 'type' : 'float' }, | 148 | 'u_time' : { 'type' : 'float' }, |
136 | 'u_speed' : { 'type' : 'float' }, | 149 | 'u_speed' : { 'type' : 'float' }, |
137 | 'u_waveWidth' : { 'type' : 'float' }, | 150 |