diff options
author | Valerio Virgillito | 2012-04-04 15:53:29 -0700 |
---|---|---|
committer | Valerio Virgillito | 2012-04-04 15:53:29 -0700 |
commit | e721a7c1009f298a1bd8fea583da14535e039880 (patch) | |
tree | 9415f5ba25ad643df678e561719d2708fd3ead5a /js/helper-classes/RDGE/src/core/script/ubershader.js | |
parent | 8482e23cd9b8c4700b5130f2588e5eb24d376536 (diff) | |
parent | 7249d6377c8897b2f9d8115be99717be971a0981 (diff) | |
download | ninja-e721a7c1009f298a1bd8fea583da14535e039880.tar.gz |
Merge pull request #156 from mayhewinator/WorkingBranch
Namespace and code cleanup for RDGE and related files
Diffstat (limited to 'js/helper-classes/RDGE/src/core/script/ubershader.js')
-rwxr-xr-x | js/helper-classes/RDGE/src/core/script/ubershader.js | 159 |
1 files changed, 0 insertions, 159 deletions
diff --git a/js/helper-classes/RDGE/src/core/script/ubershader.js b/js/helper-classes/RDGE/src/core/script/ubershader.js deleted file mode 100755 index 5d13d5a8..00000000 --- a/js/helper-classes/RDGE/src/core/script/ubershader.js +++ /dev/null | |||
@@ -1,159 +0,0 @@ | |||
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 | /* UBERSHADER */ | ||
8 | /* | ||
9 | The ubershader function takes a JSON definition object and creates a jshader | ||
10 | that supports all or a subset of ubershader features. The ubershader currently | ||
11 | supports up to four per-pixel lights, optional diffuse, normal, specular, and | ||
12 | environment map textures. Lights can be directional, point, or spot lights. The | ||
13 | resulting shader is very much optimized based on the given use case, so if the | ||
14 | configuration changes, such as by adding a light, removing a texture, or even | ||
15 | changing the type of a light) then the shader needs to be rebuilt and recompiled | ||
16 | to reflect those changes. | ||
17 | */ | ||
18 | ubershader = function(def) { | ||
19 | var r = new XMLHttpRequest(); | ||
20 | r.open('GET', "assets/shaders/ub_vshader.glsl", false); | ||
21 | r.send(null); | ||
22 | if (r.status == 200) { | ||
23 | vshader = r.responseText; | ||
24 | } | ||
25 | r.open('GET', "assets/shaders/ub_fshader.glsl", false); | ||
26 | r.send(null); | ||
27 | if (r.status == 200) { | ||
28 | fshader = r.responseText; | ||
29 | } | ||
30 | var preproc = ""; | ||
31 | // var paramBlock = {}; | ||
32 | |||
33 | // paramBlock['u_uvMatrix'] = { 'type': 'mat4' }; | ||
34 | |||
35 | if (typeof def.material != 'undefined') { | ||
36 | preproc += '#define MATERIAL\n'; | ||
37 | } | ||
38 | if (typeof def.lighting != 'undefined') { | ||
39 | preproc += '#define LIGHTING\n'; | ||
40 | preproc += '#define SPECULAR\n'; | ||
41 | for (i = 0; i < 4; ++i) { | ||
42 | var light = def.lighting['light' + i]; | ||
43 | var t; | ||
44 | if (typeof light != 'undefined') { | ||
45 | switch (light.type) { | ||
46 | case 'directional': t = 0; break; | ||
47 | case 'point': t = 1; break; | ||
48 | case 'spot': t = 2; break; | ||
49 | } | ||
50 | preproc += '#define LIGHT_' + i + ' ' + t + '\n'; | ||
51 | preproc += '#define LIGHT_' + i + '_SPECULAR\n'; | ||
52 | } | ||
53 | } | ||
54 | } | ||
55 | if (typeof def.diffuseMap != 'undefined') { | ||
56 | preproc += '#define DIFFUSE_MAP\n'; | ||
57 | } | ||
58 | if (typeof def.normalMap != 'undefined') { | ||
59 | preproc += '#define NORMAL_MAP\n'; | ||
60 | } | ||
61 | if (typeof def.specularMap != 'undefined') { | ||
62 | preproc += '#define SPECULAR_MAP\n'; | ||
63 | } | ||
64 | if (typeof def.environmentMap != 'undefined') { | ||
65 | preproc += '#define ENVIRONMENT_MAP\n'; | ||
66 | } | ||
67 | |||
68 | // prefix preprocessor settings | ||
69 | vshader = preproc + vshader; | ||
70 | fshader = preproc + fshader; | ||
71 | |||
72 | // build output jshader | ||
73 | uberJShader = new jshader(); | ||
74 | uberJShader.def = { | ||
75 | 'shaders': { | ||
76 | 'defaultVShader': vshader, | ||
77 | 'defaultFShader': fshader | ||
78 | }, | ||
79 | 'techniques': { | ||
80 | 'defaultTechnique': [{ | ||
81 | 'vshader': 'defaultVShader', | ||
82 | 'fshader': 'defaultFShader', | ||
83 | 'attributes': { | ||
84 | 'a_pos': { 'type': 'vec3' }, | ||
85 | 'a_normal': { 'type': 'vec3' }, | ||
86 | 'a_texcoord': { 'type': 'vec2' } | ||
87 | }, | ||
88 | 'params': { | ||
89 | specularColor : 'u_specularColor', | ||
90 | }, | ||
91 | 'states': { | ||
92 | 'depthEnable': true, | ||
93 | 'blendEnable': false, | ||
94 | 'culling': true, | ||
95 | 'cullFace': "FRONT" | ||
96 | } | ||
97 | }] | ||
98 | } | ||
99 | } | ||
100 | // initialize the jshader | ||
101 | uberJShader.init(); | ||
102 | |||
103 | // initialize shader parameters | ||
104 | var technique = uberJShader.defaultTechnique; | ||
105 | if (typeof def.material != 'undefined') { | ||
106 | technique.u_ambientColor.set(def.material.ambientColor); | ||
107 | technique.u_diffuseColor.set(def.material.diffuseColor); | ||
108 | if (technique.u_specularColor) | ||
109 | technique.u_specularColor.set(def.material.specularColor); | ||
110 | if (technique.u_specularPower) | ||
111 | technique.u_specularPower.set([def.material.specularPower]); | ||
112 | } | ||
113 | if (typeof def.lighting != 'undefined') { | ||
114 | for (i = 0; i < 4; ++i) { | ||
115 | var light = def.lighting["light" + i]; | ||
116 | if (typeof light != "undefined") { | ||
117 | if (light.type == 'directional') { | ||
118 | paramBlock['u_light' + i + 'Dir'] = { 'type': 'vec3' }; | ||
119 | technique['u_light' + i + 'Dir'].set(light['direction'] || [0, 0, 1]); | ||
120 | } | ||
121 | else if (light.type == 'spot') { | ||
122 | paramBlock['u_light' + i + 'Spot'] = { 'type': 'vec2' }; | ||
123 | technique['u_light' + i + 'Position'].set(light['position'] || [0, 0, 0]); | ||
124 | var deg2Rad = Math.PI / 180; | ||
125 | technique['u_light' + i + 'Spot'].set([Math.cos((light['spotInnerCutoff'] || 45.0) * deg2Rad), | ||
126 | Math.cos((light['spotOuterCutoff'] || 90.0) * deg2Rad)]); | ||
127 | technique['u_light' + i + 'Atten'].set(light['attenuation'] || [1, 0, 0]); | ||
128 | } else { | ||
129 | technique['u_light' + i + 'Position'].set(light['position'] || [0, 0, 0]); | ||
130 | technique['u_light' + i + 'Atten'].set(light['attenuation'] || [1, 0, 0]); | ||
131 | } | ||
132 | technique['u_light' + i + 'Color'].set(light['diffuseColor'] || [1, 1, 1, 1]); | ||
133 | technique['u_light' + i + 'Specular'].set(light['specularColor'] || [1, 1, 1, 1]); | ||
134 | } | ||
135 | } | ||
136 | } | ||
137 | |||
138 | if (technique.u_uvMatrix) | ||
139 | technique.u_uvMatrix.set(def.uvTransform || mat4.identity()); | ||
140 | |||
141 | renderer = g_Engine.getContext().renderer; | ||
142 | if (technique.s_diffuseMap && typeof def.diffuseMap != 'undefined') { | ||
143 | technique.s_diffuseMap.set(renderer.getTextureByName(def.diffuseMap.texture, def.diffuseMap.wrap, def.diffuseMap.mips)); | ||
144 | } | ||
145 | if (technique.s_normalMap && typeof def.normalMap != 'undefined') { | ||
146 | technique.s_normalMap.set(renderer.getTextureByName(def.normalMap.texture, def.normalMap.wrap, def.normalMap.mips)); | ||
147 | } | ||
148 | if (technique.s_specMap && typeof def.specularMap != 'undefined') { | ||
149 | technique.s_specMap.set(renderer.getTextureByName(def.specularMap.texture, def.specularMap.wrap)); | ||
150 | } | ||
151 | if (technique.s_envMap && typeof def.environmentMap != 'undefined') { | ||
152 | technique.s_envMap.set(renderer.getTextureByName(def.environmentMap.texture, def.environmentMap.wrap)); | ||
153 | } | ||
154 | if (technique.u_envReflection) { | ||
155 | technique.u_envReflection.set([def.environmentMap.envReflection || 1.0]); | ||
156 | } | ||
157 | return uberJShader; | ||
158 | } | ||
159 | |||