diff options
Diffstat (limited to 'js/helper-classes/RDGE/Materials')
17 files changed, 427 insertions, 2310 deletions
diff --git a/js/helper-classes/RDGE/Materials/BrickMaterial.js b/js/helper-classes/RDGE/Materials/BrickMaterial.js deleted file mode 100644 index aef6d1a7..00000000 --- a/js/helper-classes/RDGE/Materials/BrickMaterial.js +++ /dev/null | |||
@@ -1,231 +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 | |||
8 | /////////////////////////////////////////////////////////////////////// | ||
9 | // Class GLMaterial | ||
10 | // RDGE representation of a material. | ||
11 | /////////////////////////////////////////////////////////////////////// | ||
12 | function BrickMaterial() | ||
13 | { | ||
14 | // initialize the inherited members | ||
15 | this.inheritedFrom = GLMaterial; | ||
16 | this.inheritedFrom(); | ||
17 | |||
18 | /////////////////////////////////////////////////////////////////////// | ||
19 | // Instance variables | ||
20 | /////////////////////////////////////////////////////////////////////// | ||
21 | this._shaderName = "brick"; | ||
22 | this._name = "BrickMaterial"; | ||
23 | |||
24 | // store local values in convenient form | ||
25 | this._propNames = ["BrickColor", "MortarColor", "BrickSize", "BrickPct" ]; | ||
26 | this._propLabels = ["Brick Color", "Mortar Color", "Brick Size", "Brick Percent" ]; | ||
27 | this._propTypes = ["color", "color", "vector2d", "vector2d" ]; | ||
28 | this._propValues = []; | ||
29 | |||
30 | // set default property values | ||
31 | this._propValues[this._propNames[0]] = [0.8,0,0,1].slice(0); | ||
32 | this._propValues[this._propNames[1]] = [0.8, 0.8, 0.0, 1.0].slice(0); | ||
33 | this._propValues[this._propNames[2]] = [1, .5].slice(0); | ||
34 | this._propValues[this._propNames[3]] = [.8, .7].slice(0); | ||
35 | |||
36 | /////////////////////////////////////////////////////////////////////// | ||
37 | // Property Accessors | ||
38 | /////////////////////////////////////////////////////////////////////// | ||
39 | |||
40 | this.getBrickColor = function() { return this._propValues["BrickColor"].slice(0); } | ||
41 | this.getMortarColor = function() { return this._propValues["MortarColor"].slice(0); } | ||
42 | this.getBrickSize = function() { return this._propValues["BrickSize"].slice(0); } | ||
43 | this.getBrickPct = function() { return this._propValues["BrickPct"].slice(0); } | ||
44 | |||
45 | this.getShaderName = function() { return this._shaderName; } | ||
46 | |||
47 | /////////////////////////////////////////////////////////////////////// | ||
48 | // Methods | ||
49 | /////////////////////////////////////////////////////////////////////// | ||
50 | // duplcate method requirde | ||
51 | this.dup = function() { return new BrickMaterial(); } | ||
52 | |||
53 | this.init = function() | ||
54 | { | ||
55 | // set up the shader | ||
56 | this._shader = new jshader(); | ||
57 | this._shader.def = brickShaderDef; | ||
58 | this._shader.init(); | ||
59 | |||
60 | // set the defaults | ||
61 | this._shader.default.BrickColor.set( this.getBrickColor() ); | ||
62 | this._shader.default.MortarColor.set( this.getMortarColor() ); | ||
63 | this._shader.default.BrickSize.set( this.getBrickSize() ); | ||
64 | this._shader.default.BrickPct.set( this.getBrickPct() ); | ||
65 | |||
66 | // set up the material node | ||
67 | this._materialNode = createMaterialNode("brickMaterial"); | ||
68 | this._materialNode.setShader(this._shader); | ||
69 | } | ||
70 | |||
71 | this.setProperty = function( prop, value ) | ||
72 | { | ||
73 | // we always want to use the "color" property for something | ||
74 | if (prop == "color") prop = "BrickColor"; | ||
75 | |||
76 | // make sure we have legitimate imput | ||
77 | var ok = this.validateProperty( prop, value ); | ||
78 | if (ok) | ||
79 | { | ||
80 | this._propValues[prop] = value; | ||
81 | if (this._shader && this._shader.default) | ||
82 | this._shader.default[prop].set(value); | ||
83 | } | ||
84 | } | ||
85 | |||
86 | this.export = function() | ||
87 | { | ||
88 | // every material needs the base type and instance name | ||
89 | var exportStr = "material: " + this.getShaderName() + "\n"; | ||
90 | exportStr += "name: " + this.getName() + "\n"; | ||
91 | |||
92 | if (this._shader) | ||
93 | { | ||
94 | exportStr += "BrickColor: " + String(this._shader.default.BrickColor) + "\n"; | ||
95 | exportStr += "MortarColor: " + String(this._shader.default.MortarColor) + "\n"; | ||
96 | exportStr += "BrickSize: " + String(this._shader.default.BrickSize) + "\n"; | ||
97 | exportStr += "BrickPct: " + String(this._shader.default.BrickPct) + "\n"; | ||
98 | } | ||
99 | else | ||
100 | { | ||
101 | exportStr += "BrickColor: " + String(this.getBrickColor()) + "\n"; | ||
102 | exportStr += "MortarColor: " + String(this.getMortarColor()) + "\n"; | ||
103 | exportStr += "BrickSize: " + String(this.getBrickSize()) + "\n"; | ||
104 | exportStr += "BrickPct: " + String(this.getBrickPct()) + "\n"; | ||
105 | } | ||
106 | |||
107 | // every material needs to terminate like this | ||
108 | exportStr += "endMaterial\n"; | ||
109 | |||
110 | return exportStr; | ||
111 | } | ||
112 | |||
113 | this.import = function( importStr ) | ||
114 | { | ||
115 | var pu = new ParseUtils( importStr ); | ||
116 | var material = pu.nextValue( "material: " ); | ||
117 | if (material != this.getShaderName()) throw new Error( "ill-formed material" ); | ||
118 | this.setName( pu.nextValue( "name: ") ); | ||
119 | |||
120 | var brickColor = eval( "[" + pu.nextValue( "BrickColor: " ) + "]" ), | ||
121 | mortarColor = eval( "[" + pu.nextValue( "MortarColor: " ) + "]" ), | ||
122 | brickSize = eval( "[" + pu.nextValue( "BrickSize: " ) + "]" ), | ||
123 | brickPct = eval( "[" + pu.nextValue( "BrickPct: " ) + "]" ); | ||
124 | |||
125 | var endKey = "endMaterial\n"; | ||
126 | var index = importStr.indexOf( endKey ); | ||
127 | index += endKey.length; | ||
128 | var rtnStr = importStr.substr( index ); | ||
129 | |||
130 | return rtnStr; | ||
131 | } | ||
132 | } | ||
133 | |||
134 | /////////////////////////////////////////////////////////////////////////////////////// | ||
135 | // RDGE shader | ||
136 | var brickShaderDef = {'shaders': { | ||
137 | // Brick shader | ||
138 | 'defaultVShader':"assets/shaders/CH06-brick.vert.glsl", | ||
139 | 'defaultFShader':"assets/shaders/CH06-brick.frag.glsl", | ||
140 | |||
141 | // this shader is inline | ||
142 | 'dirLightVShader': "\ | ||
143 | uniform mat4 u_mvMatrix;\ | ||
144 | uniform mat4 u_normalMatrix;\ | ||
145 | uniform mat4 u_projMatrix;\ | ||
146 | uniform mat4 u_worldMatrix;\ | ||
147 | attribute vec3 a_pos;\ | ||
148 | attribute vec3 a_nrm;\ | ||
149 | varying vec3 vNormal;\ | ||
150 | varying vec3 vPos;\ | ||
151 | void main() {\ | ||
152 | vNormal.xyz = (u_normalMatrix*vec4(a_nrm, 0.0)).xyz;\ | ||
153 | gl_Position = u_projMatrix * u_mvMatrix * vec4(a_pos,1.0);\ | ||
154 | vPos = (u_worldMatrix * vec4(a_pos,1.0)).xyz;\ | ||
155 | }", | ||
156 | 'dirLightFShader': "\ | ||
157 | precision highp float;\ | ||
158 | uniform vec4 u_light1Diff;\ | ||
159 | uniform vec3 u_light1Pos;\ | ||
160 | uniform vec4 u_light2Diff;\ | ||
161 | uniform vec3 u_light2Pos;\ | ||
162 | varying vec3 vNormal;\ | ||
163 | varying vec3 vPos;\ | ||
164 | void main() {\ | ||
165 | vec3 light1 = vec3(u_light1Pos.x - vPos.x, u_light1Pos.y - vPos.y, u_light1Pos.z - vPos.z);\ | ||
166 | vec3 light2 = vec3(u_light2Pos.x - vPos.x, u_light2Pos.y - vPos.y, u_light2Pos.z - vPos.z);\ | ||
167 | float t = 0.75;\ | ||
168 | float range = t*t;\ | ||
169 | float alpha1 = max(0.0, 1.0 - ( (light1.x*light1.x)/range + (light1.y*light1.y)/range + (light1.z*light1.z)/range));\ | ||
170 | float alpha2 = max(0.0, 1.0 - ( (light2.x*light2.x)/range + (light2.y*light2.y)/range + (light2.z*light2.z)/range));\ | ||
171 | gl_FragColor = vec4((u_light2Diff*alpha2 + u_light1Diff*alpha1).rgb, 1.0);\ | ||
172 | }", | ||
173 | }, | ||
174 | 'techniques': { | ||
175 | 'default':[ | ||
176 | { | ||
177 | 'vshader' : 'defaultVShader', | ||
178 | 'fshader' : 'defaultFShader', | ||
179 | // attributes | ||
180 | 'attributes' : | ||
181 | { | ||