diff options
Diffstat (limited to 'js/lib/rdge/materials/pulse-material.js')
-rw-r--r-- | js/lib/rdge/materials/pulse-material.js | 320 |
1 files changed, 166 insertions, 154 deletions
diff --git a/js/lib/rdge/materials/pulse-material.js b/js/lib/rdge/materials/pulse-material.js index 4d5a158d..c065b4a1 100644 --- a/js/lib/rdge/materials/pulse-material.js +++ b/js/lib/rdge/materials/pulse-material.js | |||
@@ -37,176 +37,188 @@ var Texture = require("js/lib/rdge/texture").Texture; | |||
37 | /////////////////////////////////////////////////////////////////////// | 37 | /////////////////////////////////////////////////////////////////////// |
38 | var PulseMaterial = function PulseMaterial() | 38 | var PulseMaterial = function PulseMaterial() |
39 | { | 39 | { |
40 | var MaterialLibrary = require("js/models/materials-model").MaterialsModel; | 40 | var MaterialLibrary = require("js/models/materials-model").MaterialsModel; |
41 | 41 | ||
42 | // initialize the inherited members | 42 | // initialize the inherited members |
43 | this.inheritedFrom = Material; | 43 | this.inheritedFrom = Material; |
44 | this.inheritedFrom(); | 44 | this.inheritedFrom(); |
45 | 45 | ||
46 | /////////////////////////////////////////////////////////////////////// | 46 | /////////////////////////////////////////////////////////////////////// |
47 | // Instance variables | 47 | // Instance variables |
48 | /////////////////////////////////////////////////////////////////////// | 48 | /////////////////////////////////////////////////////////////////////// |
49 | this._name = "Pulse"; | 49 | this._name = "Pulse"; |
50 | this._shaderName = "pulse"; | 50 | this._shaderName = "pulse"; |
51 | 51 | ||
52 | this._texMap = 'assets/images/cubelight.png'; | 52 | this._texMap = 'assets/images/cubelight.png'; |
53 | 53 | ||
54 | this._time = 0.0; | 54 | this._time = 0.0; |
55 | this._dTime = 0.01; | 55 | this._dTime = 0.01; |
56 | 56 | ||
57 | this._glTextures = []; | 57 | this._glTextures = []; |
58 | 58 | ||
59 | /////////////////////////////////////////////////////////////////////// | 59 | /////////////////////////////////////////////////////////////////////// |
60 | // Property Accessors | 60 | // Property Accessors |
61 | /////////////////////////////////////////////////////////////////////// | 61 | /////////////////////////////////////////////////////////////////////// |
62 | this.isAnimated = function() { return true; }; | 62 | this.isAnimated = function() { return true; }; |
63 | this.getShaderDef = function() { return pulseMaterialDef; } | 63 | this.getShaderDef = function() { return pulseMaterialDef; } |
64 | 64 | ||
65 | /////////////////////////////////////////////////////////////////////// | 65 | /////////////////////////////////////////////////////////////////////// |
66 | // Material Property Accessors | 66 | // Material Property Accessors |
67 | /////////////////////////////////////////////////////////////////////// | 67 | /////////////////////////////////////////////////////////////////////// |
68 | 68 | ||
69 | var u_tex0_index = 0, | 69 | var u_tex0_index = 0, |
70 | u_xScale_index = 1, | 70 | u_xScale_index = 1, |
71 | u_yScale_index = 2, | 71 | u_yScale_index = 2, |
72 | u_speed_index = 3; | 72 | u_speed_index = 3; |
73 | 73 | ||
74 | this._propNames = ["u_tex0", "u_xscale", "u_yscale", "u_speed" ]; | 74 | this._propNames = ["u_tex0", "u_xscale", "u_yscale", "u_speed" ]; |
75 | this._propLabels = ["Texture map", "X Range", "Y Range", "Speed" ]; | 75 | this._propLabels = ["Texture map", "X Range", "Y Range", "Speed" ]; |
76 | this._propTypes = ["file", "float", "float", "float"]; | 76 | this._propTypes = ["file", "float", "float", "float"]; |
77 | this._propValues = []; | 77 | this._propValues = []; |
78 | 78 | ||
79 | this._propValues[ this._propNames[ u_tex0_index] ] = this._texMap.slice(0); | 79 | this._propValues[ this._propNames[ u_tex0_index] ] = this._texMap.slice(0); |
80 | this._propValues[ this._propNames[u_xScale_index] ] = 0.5; | 80 | this._propValues[ this._propNames[u_xScale_index] ] = 0.5; |
81 | this._propValues[ this._propNames[u_yScale_index] ] = 0.4; | 81 | this._propValues[ this._propNames[u_yScale_index] ] = 0.4; |
82 | this._propValues[ this._propNames[ u_speed_index] ] = 1.0; | 82 | this._propValues[ this._propNames[ u_speed_index] ] = 1.0; |
83 | /////////////////////////////////////////////////////////////////////// | 83 | /////////////////////////////////////////////////////////////////////// |
84 | 84 | ||
85 | 85 | ||
86 | /////////////////////////////////////////////////////////////////////// | 86 | /////////////////////////////////////////////////////////////////////// |
87 | // Methods | 87 | // Methods |
88 | /////////////////////////////////////////////////////////////////////// | 88 | /////////////////////////////////////////////////////////////////////// |
89 | // duplicate method required | 89 | // duplicate method required |
90 | 90 | ||
91 | this.init = function( world ) | 91 | this.init = function( world ) |
92 | { | ||
93 | // save the world | ||
94 | if (world) this.setWorld( world ); | ||
95 | |||
96 | // this variable declared above is inherited set to a smaller delta. | ||
97 | // the pulse material runs a little faster | ||
98 | this._dTime = 0.01; | ||
99 | |||
100 | // set up the shader | ||
101 | this._shader = new RDGE.jshader(); | ||
102 | this._shader.def = pulseMaterialDef; | ||
103 | this._shader.init(); | ||
104 | |||
105 | // set up the material node | ||
106 | this._materialNode = RDGE.createMaterialNode("pulseMaterial" + "_" + world.generateUniqueNodeID()); | ||
107 | this._materialNode.setShader(this._shader); | ||
108 | |||
109 | this._time = 0; | ||
110 | if (this._shader && this._shader['default']) { | ||
111 | this._shader['default'].u_time.set( [this._time] ); | ||
112 | } | ||
113 | |||
114 | // set the shader values in the shader | ||
115 | this.setShaderValues(); | ||
116 | this.setResolution( [world.getViewportWidth(),world.getViewportHeight()] ); | ||
117 | this.update( 0 ); | ||
118 | }; | ||
119 | |||
120 | this.resetToDefault = function() | ||
92 | { | 121 | { |
93 | // save the world | 122 | this._propValues[ this._propNames[ u_tex0_index] ] = this._texMap.slice(0); |
94 | if (world) this.setWorld( world ); | 123 | this._propValues[ this._propNames[u_xScale_index] ] = 0.5; |
95 | 124 | this._propValues[ this._propNames[u_yScale_index] ] = 0.4; | |
96 | // this variable declared above is inherited set to a smaller delta. | 125 | this._propValues[ this._propNames[ u_speed_index] ] = 1.0; |
97 | // the pulse material runs a little faster | 126 | |
98 | this._dTime = 0.01; | 127 | var nProps = this._propNames.length; |
99 | 128 | for (var i=0; i<nProps; i++) | |
100 | // set up the shader | 129 | this.setProperty( this._propNames[i], this._propValues[this._propNames[i]] ); |
101 | this._shader = new RDGE.jshader(); | ||
102 | this._shader.def = pulseMaterialDef; | ||
103 | this._shader.init(); | ||
104 | |||
105 | // set up the material node | ||
106 | this._materialNode = RDGE.createMaterialNode("pulseMaterial" + "_" + world.generateUniqueNodeID()); | ||
107 | this._materialNode.setShader(this._shader); | ||
108 | |||
109 | this._time = 0; | ||
110 | if (this._shader && this._shader['default']) { | ||
111 | this._shader['default'].u_time.set( [this._time] ); | ||
112 | } | ||
113 | |||
114 | // set the shader values in the shader | ||
115 | this.setShaderValues(); | ||
116 | this.setResolution( [world.getViewportWidth(),world.getViewportHeight()] ); | ||
117 | this.update( 0 ); | ||
118 | }; | 130 | }; |
119 | 131 | ||
120 | this.update = function( time ) | 132 | this.update = function( time ) |
121 | { | 133 | { |
122 | var material = this._materialNode; | 134 | var material = this._materialNode; |
123 | if (material) | 135 | if (material) |
124 | { | 136 | { |
125 | var technique = material.shaderProgram['default']; | 137 | var technique = material.shaderProgram['default']; |
126 | var renderer = RDGE.globals.engine.getContext().renderer; | 138 | var renderer = RDGE.globals.engine.getContext().renderer; |
127 | if (renderer && technique) | 139 | if (renderer && technique) |
128 | { | 140 | { |
129 | var glTex = this._glTextures["u_tex0"]; | 141 | var glTex = this._glTextures["u_tex0"]; |
130 | if (glTex) | 142 | if (glTex) |
131 | { | 143 | { |
132 | //this.updateTexture(); | 144 | //this.updateTexture(); |
133 | if (glTex.isAnimated()) | 145 | if (glTex.isAnimated()) |
134 | glTex.render(); | 146 | glTex.render(); |
135 | tex = glTex.getTexture(); | 147 | tex = glTex.getTexture(); |
136 | if (tex) | 148 | if (tex) |
137 | technique.u_tex0.set( tex ); | 149 | technique.u_tex0.set( tex ); |
138 | } | 150 | } |
139 | 151 | ||
140 | if (this._shader && this._shader['default']) { | 152 | if (this._shader && this._shader['default']) { |
141 | this._shader['default'].u_time.set( [this._time] ); | 153 | this._shader['default'].u_time.set( [this._time] ); |
142 | } | 154 | } |
143 | this._time += this._dTime; | 155 | this._time += this._dTime; |
144 | } | 156 | } |
145 | } | 157 | } |
146 | }; | 158 | }; |
147 | 159 | ||
148 | this.setResolution = function( res ) { | 160 | this.setResolution = function( res ) { |
149 | var material = this._materialNode; | 161 | var material = this._materialNode; |
150 | if (material) { | 162 | if (material) { |
151 | var technique = material.shaderProgram['default']; | 163 | var technique = material.shaderProgram['default']; |
152 | var renderer = RDGE.globals.engine.getContext().renderer; | 164 | var renderer = RDGE.globals.engine.getContext().renderer; |
153 | if (renderer && technique) { | 165 | if (renderer && technique) { |
154 | technique.u_resolution.set( res ); | 166 | technique.u_resolution.set( res ); |
155 | } | 167 | } |
156 | } | 168 | } |
157 | }; | 169 | }; |
158 | 170 | ||
159 | }; | 171 | }; |
160 | 172 | ||
161 | /////////////////////////////////////////////////////////////////////////////////////// | 173 | /////////////////////////////////////////////////////////////////////////////////////// |
162 | // RDGE shader | 174 | // RDGE shader |
163 | 175 | ||