diff options
Diffstat (limited to 'js/lib/rdge/materials/water-material.js')
-rw-r--r-- | js/lib/rdge/materials/water-material.js | 313 |
1 files changed, 133 insertions, 180 deletions
diff --git a/js/lib/rdge/materials/water-material.js b/js/lib/rdge/materials/water-material.js index 37836636..982b581f 100644 --- a/js/lib/rdge/materials/water-material.js +++ b/js/lib/rdge/materials/water-material.js | |||
@@ -5,73 +5,79 @@ No rights, expressed or implied, whatsoever to this software are provided by Mot | |||
5 | </copyright> */ | 5 | </copyright> */ |
6 | 6 | ||
7 | var PulseMaterial = require("js/lib/rdge/materials/pulse-material").PulseMaterial; | 7 | var PulseMaterial = require("js/lib/rdge/materials/pulse-material").PulseMaterial; |
8 | var Texture = require("js/lib/rdge/texture").Texture; | ||
8 | 9 | ||
9 | /////////////////////////////////////////////////////////////////////// | 10 | /////////////////////////////////////////////////////////////////////// |
10 | // Class GLMaterial | 11 | // Class GLMaterial |
11 | // RDGE representation of a material. | 12 | // RDGE representation of a material. |
12 | /////////////////////////////////////////////////////////////////////// | 13 | /////////////////////////////////////////////////////////////////////// |
13 | var WaterMaterial = function WaterMaterial() { | 14 | var WaterMaterial = function WaterMaterial() |
14 | /////////////////////////////////////////////////////////////////////// | 15 | { |
15 | // Instance variables | 16 | /////////////////////////////////////////////////////////////////////// |
16 | /////////////////////////////////////////////////////////////////////// | 17 | // Instance variables |
17 | this._name = "WaterMaterial"; | 18 | /////////////////////////////////////////////////////////////////////// |
18 | this._shaderName = "water"; | 19 | this._name = "Water"; |
19 | 20 | this._shaderName = "water"; | |
20 | this._texMap = 'assets/images/rocky-normal.jpg'; | 21 | |
21 | //this._texMap = 'assets/images/powderblue.png'; | 22 | this._defaultTexMap = 'assets/images/rocky-normal.jpg'; |
22 | 23 | ||
23 | this._time = 0.0; | 24 | this._time = 0.0; |
24 | this._dTime = 0.01; | 25 | this._dTime = 0.01; |
25 | 26 | ||
26 | /////////////////////////////////////////////////////////////////////// | 27 | // array textures indexed by shader uniform name |
27 | // Properties | 28 | this._glTextures = []; |
28 | /////////////////////////////////////////////////////////////////////// | 29 | |
29 | // all defined in parent PulseMaterial.js | 30 | this.isAnimated = function() { return true; }; |
30 | // load the local default value | 31 | this.getShaderDef = function() { return waterMaterialDef; }; |
31 | this._propValues = []; | 32 | |
32 | this._propValues[this._propNames[0]] = this._texMap.slice(0); | 33 | /////////////////////////////////////////////////////////////////////// |
33 | 34 | // Properties | |
34 | /////////////////////////////////////////////////////////////////////// | 35 | /////////////////////////////////////////////////////////////////////// |
35 | // Methods | 36 | // all defined in parent PulseMaterial.js |
36 | /////////////////////////////////////////////////////////////////////// | 37 | // load the local default value |
37 | // duplcate method requirde | 38 | this._propNames = ["u_tex0", "u_emboss", "u_delta", "u_intensity", "u_speed"]; |
38 | this.dup = function (world) { | 39 | this._propLabels = ["Texture map", "Emboss", "Delta", "Intensity", "Speed"]; |
39 | // allocate a new uber material | 40 | this._propTypes = ["file", "float", "float", "float", "float"]; |
40 | var newMat = new WaterMaterial(); | 41 | |
41 | 42 | var u_tex_index = 0, | |
42 | // copy over the current values; | 43 | u_emboss_index = 1, |
43 | var propNames = [], propValues = [], propTypes = [], propLabels = []; | 44 | u_delta_index = 2, |
44 | this.getAllProperties(propNames, propValues, propTypes, propLabels); | 45 | u_intensity_index = 3, |
45 | var n = propNames.length; | 46 | u_speed_index = 4; |
46 | for (var i = 0; i < n; i++) | 47 | |
47 | newMat.setProperty(propNames[i], propValues[i]); | 48 | this._propValues = []; |
48 | 49 | this._propValues[ this._propNames[u_tex_index ] ] = this._defaultTexMap.slice(0); | |
49 | return newMat; | 50 | this._propValues[ this._propNames[u_emboss_index ] ] = 0.3; |
50 | }; | 51 | this._propValues[ this._propNames[u_delta_index ] ] = 20.0; |
51 | 52 | this._propValues[ this._propNames[u_intensity_index ] ] = 3.0; | |
52 | this.init = function (world) { | 53 | this._propValues[ this._propNames[u_speed_index ] ] = 0.2; |
53 | // save the world | 54 | |
54 | if (world) this.setWorld(world); | 55 | /////////////////////////////////////////////////////////////////////// |
55 | 56 | // Methods | |
56 | // set up the shader | 57 | /////////////////////////////////////////////////////////////////////// |
57 | this._shader = new RDGE.jshader(); | 58 | |
58 | this._shader.def = waterMaterialDef; | 59 | this.init = function (world) { |
59 | this._shader.init(); | 60 | // save the world |
60 | 61 | if (world) this.setWorld(world); | |
61 | // set up the material node | 62 | |
62 | this._materialNode = RDGE.createMaterialNode("waterMaterial" + "_" + world.generateUniqueNodeID()); | 63 | // set up the shader |
63 | this._materialNode.setShader(this._shader); | 64 | this._shader = new RDGE.jshader(); |
64 | 65 | this._shader.def = waterMaterialDef; | |
65 | this._time = 0; | 66 | this._shader.init(); |
66 | if (this._shader && this._shader['default']) { | 67 | |
67 | this._shader['default'].u_time.set([this._time]); | 68 | // set up the material node |
68 | } | 69 | this._materialNode = RDGE.createMaterialNode("waterMaterial" + "_" + world.generateUniqueNodeID()); |
69 | 70 | this._materialNode.setShader(this._shader); | |
70 | // set the shader values in the shader | 71 | |
71 | this.updateTexture(); | 72 | this._time = 0; |
72 | this.setResolution([world.getViewportWidth(), world.getViewportHeight()]); | 73 | if (this._shader && this._shader['default']) |
73 | this.update(0); | 74 | this._shader['default'].u_time.set([this._time]); |
74 | }; | 75 | |
76 | // set the shader values in the shader | ||
77 | this.setShaderValues(); | ||
78 | this.setResolution([world.getViewportWidth(), world.getViewportHeight()]); | ||
79 | this.update(0); | ||
80 | }; | ||
75 | }; | 81 | }; |
76 | 82 | ||
77 | /////////////////////////////////////////////////////////////////////////////////////// | 83 | /////////////////////////////////////////////////////////////////////////////////////// |
@@ -81,148 +87,95 @@ var WaterMaterial = function WaterMaterial() { | |||
81 | var waterMaterialDef = | 87 | var waterMaterialDef = |
82 | { 'shaders': | 88 | { 'shaders': |
83 | { | 89 | { |
84 | 'defaultVShader': "assets/shaders/Basic.vert.glsl", | 90 | 'defaultVShader': "assets/shaders/Basic.vert.glsl", |
85 | 'defaultFShader': "assets/shaders/Water2.frag.glsl" | 91 | 'defaultFShader': "assets/shaders/Water2.frag.glsl" |
86 | }, | 92 | }, |
87 | 'techniques': | 93 | 'techniques': |
88 | { | 94 | { |
89 | 'default': | 95 | 'default': |
90 | [ | 96 | [ |
91 | { | 97 | { |
92 | 'vshader': 'defaultVShader', | 98 | 'vshader': 'defaultVShader', |
93 | 'fshader': 'defaultFShader', | 99 | 'fshader': 'defaultFShader', |
94 | // attributes | 100 | // attributes |
95 | 'attributes': | 101 | 'attributes': |
96 | { | 102 | { |
97 | 'vert': { 'type': 'vec3' }, | 103 | 'vert': { 'type': 'vec3' }, |
98 | 'normal': { 'type': 'vec3' }, | 104 | 'normal': { 'type': 'vec3' }, |
99 | 'texcoord': { 'type': 'vec2' } | 105 | 'texcoord': { 'type': 'vec2' } |
100 | }, | 106 | }, |
101 | // parameters | 107 | // parameters |
102 | 'params': | 108 | 'params': |
103 | { | 109 | { |
104 | 'u_tex0': { 'type': 'tex2d' }, | 110 | 'u_tex0': { 'type': 'tex2d' }, |
105 | 'u_time': { 'type': 'float' }, | 111 | 'u_time': { 'type': 'float' }, |
106 | 'u_resolution': { 'type': 'vec2' } | 112 | 'u_emboss': { 'type': 'float' }, |
113 | 'u_delta': { 'type': 'float' }, | ||
114 | 'u_speed': { 'type': 'float' }, | ||
115 | 'u_intensity': { 'type': 'float' }, | ||
116 | 'u_resolution': { 'type': 'vec2' } | ||
107 | }, | 117 | }, |
108 | 118 | ||
109 | // render states | 119 | // render states |
110 | 'states': | 120 | 'states': |
111 | { | 121 | { |
112 | 'depthEnable': true, | 122 | 'depthEnable': true, |
113 | 'offset': [1.0, 0.1] | 123 | 'offset': [1.0, 0.1] |
114 | } | 124 | } |
115 | } | 125 | } |
116 | ] | 126 | ] |
117 | } | 127 | } |
118 | }; | 128 | }; |
119 | 129 | ||
120 | var ParisMaterial = function ParisMaterial() { | 130 | var ParisMaterial = function ParisMaterial() |
121 | // initialize the inherited members | 131 | { |
122 | this.inheritedFrom = WaterMaterial; | 132 | // initialize the inherited members |
123 | this.inheritedFrom(); | 133 | this.inheritedFrom = WaterMaterial; |
124 | 134 | this.inheritedFrom(); | |
125 | this._name = "ParisMaterial"; | ||
126 | this._shaderName = "paris"; | ||
127 | |||
128 | this._texMap = 'assets/images/paris.png'; | ||
129 | this._propValues[this._propNames[0]] = this._texMap.slice(0); | ||
130 | |||
131 | this._diffuseColor = [0.5, 0.5, 0.5, 0.5]; | ||
132 | this._propValues[this._propNames[1]] = this._diffuseColor.slice(); | ||
133 | |||
134 | // duplcate method requirde | ||
135 | this.dup = function (world) { | ||
136 | // allocate a new uber material | ||
137 | var newMat = new ParisMaterial(); | ||
138 | |||
139 | // copy over the current values; | ||
140 | var propNames = [], propValues = [], propTypes = [], propLabels = []; | ||
141 | this.getAllProperties(propNames, propValues, propTypes, propLabels); | ||
142 | var n = propNames.length; | ||
143 | for (var i = 0; i < n; i++) | ||
144 | newMat.setProperty(propNames[i], propValues[i]); | ||
145 | |||
146 | return newMat; | ||
147 | }; | ||
148 | |||
149 | this.init = function (world) { | ||
150 | // save the world | ||