diff options
author | Nivesh Rajbhandari | 2012-03-12 16:25:28 -0700 |
---|---|---|
committer | Nivesh Rajbhandari | 2012-03-12 16:25:28 -0700 |
commit | 5fb358d9e80a364d5f300d60b7a9fe0f773741e7 (patch) | |
tree | 2c4d5292b19c91697dffb542f369ec9dcb340f77 /js/lib/rdge/materials/uber-material.js | |
parent | 5f9b67be3b8bdb391d021103c6bcee6be2f6cdf4 (diff) | |
parent | 69d90467865a1384725b2301901be2180c5a841f (diff) | |
download | ninja-5fb358d9e80a364d5f300d60b7a9fe0f773741e7.tar.gz |
Merge branch 'refs/heads/ninja-internal' into ToolFixes
Diffstat (limited to 'js/lib/rdge/materials/uber-material.js')
-rwxr-xr-x | js/lib/rdge/materials/uber-material.js | 614 |
1 files changed, 614 insertions, 0 deletions
diff --git a/js/lib/rdge/materials/uber-material.js b/js/lib/rdge/materials/uber-material.js new file mode 100755 index 00000000..6bc35d51 --- /dev/null +++ b/js/lib/rdge/materials/uber-material.js | |||
@@ -0,0 +1,614 @@ | |||
1 | /* <copyright> | ||
2 | This file contains proprietary software owned by Motorola Mobility, Inc.<br/> | ||
3 | No rights, expressed or implied, whatsoever to this software are provided by Motorola Mobility, Inc. hereunder.<br/> | ||
4 | (c) Copyright 2011 Motorola Mobility, Inc. All Rights Reserved. | ||
5 | </copyright> */ | ||
6 | |||
7 | var Material = require("js/lib/rdge/materials/material").Material; | ||
8 | |||
9 | var UberMaterial = function UberMaterial() { | ||
10 | /////////////////////////////////////////////////////////////////////// | ||
11 | // Instance variables | ||
12 | /////////////////////////////////////////////////////////////////////// | ||
13 | this._name = "UberMaterial"; | ||
14 | this._shaderName = "uber"; | ||
15 | this.getShaderName = function() { return this._shaderName; }; | ||
16 | |||
17 | // set some default values | ||
18 | this._ambientColor = [ 0.0, 0.0, 0.0, 1.0 ]; | ||
19 | this._diffuseColor = [ 1.0, 1.0, 1.0, 1.0 ]; | ||
20 | this._specularColor = [ 1.0, 1.0, 1.0, 1.0 ]; | ||
21 | this._specularPower = 32.0; | ||
22 | this._environmentAmount = 0.2; // 0 .. 1 | ||
23 | |||
24 | // set the default maps | ||
25 | this._diffuseMapOb = { 'texture' : 'assets/images/rocky-diffuse.jpg', 'wrap' : 'REPEAT' }; | ||
26 | this._normalMapOb = { 'texture' : 'assets/images/rocky-normal.jpg', 'wrap' : 'REPEAT' }; | ||
27 | this._specularMapOb = { 'texture' : 'assets/images/rocky-spec.jpg', 'wrap' : 'REPEAT' }; | ||
28 | this._environmentMapOb = { 'texture' : 'assets/images/silver.png', 'wrap' : 'CLAMP', 'envReflection' : this._environmentAmount }; | ||
29 | |||
30 | this._useDiffuseMap = true; | ||
31 | this._useNormalMap = true; | ||
32 | this._useSpecularMap = true; | ||
33 | this._useEnvironmentMap = true; | ||
34 | this._useLights = [true, true, true, true]; | ||
35 | |||
36 | /////////////////////////////////////////////////////////////////////// | ||
37 | // Material Property Accessors | ||
38 | /////////////////////////////////////////////////////////////////////// | ||
39 | this._propNames = ["ambientColor", "diffuseColor", "specularColor", "specularPower" , "diffuseMap", "normalMap", "specularMap", "environmentMap", "environmentAmount" ]; | ||
40 | this._propLabels = ["Ambient Color", "Diffuse Color", "Specular Color", "Specular Power", "Texture Map", "Bump Map", "Specular Map", "Environment Map", "Environment Map Amount" ]; | ||
41 | this._propTypes = ["color", "color", "color", "float", "file", "file", "file", "file", "float" ]; | ||
42 | this._propValues = []; | ||
43 | |||
44 | this._propValues[ this._propNames[0] ] = this._ambientColor.slice(0); | ||
45 | this._propValues[ this._propNames[1] ] = this._diffuseColor.slice(0); | ||
46 | this._propValues[ this._propNames[2] ] = this._specularColor.slice(0); | ||
47 | this._propValues[ this._propNames[3] ] = this._specularPower; | ||
48 | this._propValues[ this._propNames[4] ] = this._diffuseMapOb['texture']; | ||
49 | this._propValues[ this._propNames[5] ] = this._normalMapOb['texture']; | ||
50 | this._propValues[ this._propNames[6] ] = this._specularMapOb['texture']; | ||
51 | this._propValues[ this._propNames[7] ] = this._environmentMapOb['texture']; | ||
52 | this._propValues[ this._propNames[8] ] = this._environmentMapOb['envReflection']; | ||
53 | |||
54 | this.setProperty = function( prop, value ) { | ||
55 | if (prop == "color") prop = "ambientColor"; | ||
56 | var valid = this.validateProperty( prop, value ); | ||
57 | if (valid) { | ||
58 | this._propValues[prop] = value; | ||
59 | |||
60 | switch (prop) | ||
61 | { | ||
62 | case "diffuseMap": | ||
63 | this.updateDiffuseMap(); | ||
64 | break; | ||
65 | case "normalMap": | ||
66 | this.updateNormalMap(); | ||
67 | break; | ||
68 | case "specularMap": | ||
69 | this.updateSpecularMap(); | ||
70 | break; | ||
71 | case "environmentMap": | ||
72 | this.updateEnvironmentMap(); | ||
73 | break; | ||
74 | case "environmentAmount": | ||
75 | this.updateEnvironmentAmount( value ); | ||
76 | break; | ||
77 | case "specularPower": | ||
78 | this.updateSpecularPower( value ); | ||
79 | break; | ||
80 | case "ambientColor": | ||
81 | this.updateAmbientColor( value ); | ||
82 | break; | ||
83 | case "diffuseColor": | ||
84 | this.updateDiffuseColor( value ); | ||
85 | break; | ||
86 | case "specularColor": | ||
87 | this.updateSpecularColor( value ); | ||
88 | break; | ||
89 | } | ||
90 | } | ||
91 | }; | ||
92 | /////////////////////////////////////////////////////////////////////// | ||
93 | |||
94 | // define the 4 lights | ||
95 | this._lights = [ | ||
96 | |||
97 | { | ||
98 | 'type' : 'point', // can be 'directional', 'point' or 'spot' | ||
99 | 'spotInnerCutoff' : 14.0, // fragments in the inner cutoff 'cone' are full intensity. | ||
100 | 'spotOuterCutoff' : 15.0, // fragments outside the outer cutoff 'cone' are unlit. | ||
101 | 'position' : [ 8.0, 2.0, 8.0 ], // light position; ignored for directional lights | ||
102 | 'direction' : [ -1.0, -1.0, -1.0 ], // light direction; ignored for point lights | ||
103 | 'attenuation' : [ 1.0, 0.025, 0.00125 ], // light attenuation; constant, linear, quadratic | ||
104 | 'diffuseColor' : [ 1.0, 0.5, 0.5, 1.0 ], // diffuse light color | ||
105 | 'specularColor' : [ 1.0, 1.0, 1.0, 1.0 ] // specular light color | ||
106 | }, | ||
107 | { | ||
108 | 'type' : 'point', | ||
109 | 'spotInnerCutoff' : 9.0, | ||
110 | 'spotOuterCutoff' : 20.0, | ||
111 | 'position' : [ -8.0, 2.0, 8.0 ], | ||
112 | 'direction' : [ 1.0, -1.0, -1.0 ], | ||
113 | 'attenuation' : [ 1.0, 0.025, 0.00125 ], | ||
114 | 'diffuseColor' : [ 0.5, 1.0, 0.5, 1.0 ], | ||
115 | 'specularColor' : [ 1.0, 1.0, 1.0, 1.0 ] | ||
116 | }, | ||
117 | { | ||
118 | 'type' : 'point', | ||
119 | 'spotInnerCutoff' : 9.0, | ||
120 | 'spotOuterCutoff' : 20.0, | ||
121 | 'position' : [ -8.0, 2.0, -8.0 ], | ||
122 | 'direction' : [ 1.0, -1.0, 1.0 ], | ||
123 | 'attenuation' : [ 1.0, 0.25, 0.0125 ], | ||
124 | 'diffuseColor' : [ 0.5, 0.5, 1.0, 1.0 ], | ||
125 | 'specularColor' : [ 1.0, 1.0, 1.0, 1.0 ] | ||
126 | }, | ||
127 | { | ||
128 | 'type' : 'point', | ||
129 | 'spotInnerCutoff' : 9.0, | ||
130 | 'spotOuterCutoff' : 20.0, | ||
131 | 'position' : [ 8.0, 4.0, -8.0 ], | ||
132 | 'direction' : [ -1.0, -1.0, 1.0 ], | ||
133 | 'attenuation' : [ 1.0, 0.25, 0.0125 ], | ||
134 | 'diffuseColor' : [ 1.0, 1.0, 0.5, 1.0 ], | ||
135 | 'specularColor' : [ 1.0, 1.0, 1.0, 1.0 ] | ||
136 | } | ||
137 | ]; | ||
138 | |||
139 | this._ubershaderCaps = | ||
140 | { | ||
141 | // ubershader material properties. | ||
142 | 'material' : { | ||
143 | 'ambientColor' : this._ambientColor, // material ambient color | ||
144 | 'diffuseColor' : this._diffuseColor, // material diffuse color | ||
145 | 'specularColor' : this._specularColor, // material specular color | ||
146 | 'specularPower' : this._specularPower // material specular power (shininess) | ||
147 | }, | ||
148 | |||
149 | // ubershader supports up to four lights. | ||
150 | 'lighting' : { | ||
151 | 'light0' : this._lights[0], | ||
152 | 'light1' : this._lights[1], | ||
153 | 'light2' : this._lights[2], | ||
154 | 'light3' : this._lights[3] | ||
155 | }, | ||
156 | |||
157 | // uvTransform can be used to scale or offset the texture coordinates. | ||
158 | 'uvTransform' : [ 2.0, 0, 0, 0, 0, 2.0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 1], | ||
159 | |||
160 | // optional diffuse map | ||
161 | 'diffuseMap' : this._diffuseMapOb, | ||
162 | |||
163 | // optional normal map | ||
164 | 'normalMap' : this._normalMapOb, | ||
165 | |||
166 | // optional specular map | ||
167 | 'specularMap' : this._specularMapOb, | ||
168 | |||
169 | // optional environment map | ||
170 | 'environmentMap' : this._environmentMapOb | ||
171 | }; | ||
172 | |||
173 | this.updateAmbientColor = function() { | ||
174 | this._ambientColor = this._propValues['ambientColor'].slice(0); | ||
175 | var material = this._materialNode; | ||
176 | if (material) { | ||
177 | var technique = material.shaderProgram.defaultTechnique; | ||
178 | technique.u_ambientColor.set(this._ambientColor); | ||
179 | } | ||
180 | }; | ||
181 | |||
182 | this.updateDiffuseColor = function() { | ||
183 | this._diffuseColor = this._propValues['diffuseColor'].slice(0); | ||
184 | |||
185 | var material = this._materialNode; | ||
186 | if (material) { | ||
187 | var technique = material.shaderProgram.defaultTechnique; | ||
188 | technique.u_diffuseColor.set(this._diffuseColor); | ||
189 | } | ||
190 | }; | ||
191 | |||
192 | this.updateSpecularColor = function( value ) { | ||
193 | this._specularColor = this._propValues['specularColor']; | ||
194 | |||
195 | var material = this._materialNode; | ||
196 | if (material) { | ||
197 | var technique = material.shaderProgram.defaultTechnique; | ||
198 | technique.u_specularColor.set(this._specularColor); | ||
199 | } | ||
200 | }; | ||
201 | |||
202 | this.updateSpecularPower = function( value) { | ||
203 | this._specularPower = this._propValues['specularPower']; | ||
204 | |||
205 | var material = this._materialNode; | ||
206 | if (material) | ||
207 | { | ||
208 | var technique = material.shaderProgram.defaultTechnique; | ||
209 | technique.u_specularPower.set([this._sp |