aboutsummaryrefslogtreecommitdiff
path: root/js/lib/rdge/materials/bump-metal-material.js
diff options
context:
space:
mode:
Diffstat (limited to 'js/lib/rdge/materials/bump-metal-material.js')
-rwxr-xr-xjs/lib/rdge/materials/bump-metal-material.js199
1 files changed, 106 insertions, 93 deletions
diff --git a/js/lib/rdge/materials/bump-metal-material.js b/js/lib/rdge/materials/bump-metal-material.js
index 3db3df1d..02540033 100755
--- a/js/lib/rdge/materials/bump-metal-material.js
+++ b/js/lib/rdge/materials/bump-metal-material.js
@@ -1,24 +1,25 @@
1/* <copyright> 1/* <copyright>
2Copyright (c) 2012, Motorola Mobility, Inc 2Copyright (c) 2012, Motorola Mobility LLC.
3All Rights Reserved. 3All Rights Reserved.
4BSD License.
5 4
6Redistribution and use in source and binary forms, with or without 5Redistribution and use in source and binary forms, with or without
7modification, are permitted provided that the following conditions are met: 6modification, are permitted provided that the following conditions are met:
8 7
9 - Redistributions of source code must retain the above copyright notice, 8* Redistributions of source code must retain the above copyright notice,
10 this list of conditions and the following disclaimer. 9 this list of conditions and the following disclaimer.
11 - Redistributions in binary form must reproduce the above copyright 10
12 notice, this list of conditions and the following disclaimer in the 11* Redistributions in binary form must reproduce the above copyright notice,
13 documentation and/or other materials provided with the distribution. 12 this list of conditions and the following disclaimer in the documentation
14 - Neither the name of Motorola Mobility nor the names of its contributors 13 and/or other materials provided with the distribution.
15 may be used to endorse or promote products derived from this software 14
16 without specific prior written permission. 15* Neither the name of Motorola Mobility LLC nor the names of its
16 contributors may be used to endorse or promote products derived from this
17 software without specific prior written permission.
17 18
18THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 19THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 22ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 23LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 24CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 25SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
@@ -40,92 +41,104 @@ var BumpMetalMaterial = function BumpMetalMaterial() {
40 /////////////////////////////////////////////////////////////////////// 41 ///////////////////////////////////////////////////////////////////////
41 // Instance variables 42 // Instance variables
42 /////////////////////////////////////////////////////////////////////// 43 ///////////////////////////////////////////////////////////////////////
43 this._name = "Bump Metal"; 44 this._name = "Bump Metal";
44 this._shaderName = "bumpMetal"; 45 this._shaderName = "bumpMetal";
45 46
46 47
48 this._defaultLightDiff = [0.3, 0.3, 0.3, 1.0];
47 this._defaultDiffuseTexture = "assets/images/metal.png"; 49 this._defaultDiffuseTexture = "assets/images/metal.png";
48 this._defaultSpecularTexture = "assets/images/silver.png"; 50 this._defaultSpecularTexture = "assets/images/silver.png";
49 this._defaultNormalTexture = "assets/images/normalMap.png"; 51 this._defaultNormalTexture = "assets/images/normalMap.png";
50 52
51 // array textures indexed by shader uniform name 53 // array textures indexed by shader uniform name
52 this._glTextures = []; 54 this._glTextures = [];
53 55
54 this._speed = 1.0; 56 this._speed = 1.0;
55 57
56 /////////////////////////////////////////////////////////////////////// 58 ///////////////////////////////////////////////////////////////////////
57 // Property Accessors 59 // Property Accessors
58 /////////////////////////////////////////////////////////////////////// 60 ///////////////////////////////////////////////////////////////////////
59 this.isAnimated = function() { return true; }; 61 this.isAnimated = function() { return true; };
60 this.getShaderDef = function() { return bumpMetalMaterialDef; }; 62 this.getShaderDef = function() { return bumpMetalMaterialDef; };
61 63
62 /////////////////////////////////////////////////////////////////////// 64 ///////////////////////////////////////////////////////////////////////
63 // Material Property Accessors 65 // Material Property Accessors
64 /////////////////////////////////////////////////////////////////////// 66 ///////////////////////////////////////////////////////////////////////
65 this._propNames = ["u_light0Diff", "u_colMap", "u_normalMap", "u_glowMap" ]; 67 this._propNames = ["u_light0Diff", "u_colMap", "u_normalMap", "u_glowMap" ];
66 this._propLabels = ["Diffuse Color", "Diffuse Map", "Bump Map", "Specular Map" ]; 68 this._propLabels = ["Diffuse Color", "Diffuse Map", "Bump Map", "Specular Map" ];
67 this._propTypes = ["color", "file", "file", "file" ]; 69 this._propTypes = ["color", "file", "file", "file" ];
68 this._propValues = []; 70 this._propValues = [];
69 71
70 this._propValues[ this._propNames[0] ] = [0.3, 0.3, 0.3, 1.0]; 72 this._propValues[ this._propNames[0] ] = this._defaultLightDiff;
71 this._propValues[ this._propNames[1] ] = this._defaultDiffuseTexture.slice(0); 73 this._propValues[ this._propNames[1] ] = this._defaultDiffuseTexture.slice(0);
72 this._propValues[ this._propNames[2] ] = this._defaultNormalTexture.slice(0); 74 this._propValues[ this._propNames[2] ] = this._defaultNormalTexture.slice(0);
73 this._propValues[ this._propNames[3] ] = this._defaultSpecularTexture.slice(0); 75 this._propValues[ this._propNames[3] ] = this._defaultSpecularTexture.slice(0);
74 76
75 77
76 /////////////////////////////////////////////////////////////////////// 78 ///////////////////////////////////////////////////////////////////////
77 // Methods 79 // Methods
78 /////////////////////////////////////////////////////////////////////// 80 ///////////////////////////////////////////////////////////////////////
79 81
80 this.init = function( world ) 82 this.init = function( world )
81 { 83 {
82 // save the world 84 // save the world
83 if (world) { 85 if (world) {
84 this.setWorld( world ); 86 this.setWorld( world );
85 } 87 }
86 88
87 // set up the shader 89 // set up the shader
88 this._shader = new RDGE.jshader(); 90 this._shader = new RDGE.jshader();
89 this._shader.def = bumpMetalMaterialDef; 91 this._shader.def = bumpMetalMaterialDef;
90 this._shader.init(); 92 this._shader.init();
93
94 // set up the material node
95 this._materialNode = RDGE.createMaterialNode( this.getShaderName() + "_" + world.generateUniqueNodeID() );
96 this._materialNode.setShader(this._shader);
91 97
92 // set up the material node 98 this.setShaderValues();
93 this._materialNode = RDGE.createMaterialNode( this.getShaderName() + "_" + world.generateUniqueNodeID() ); 99 this.update(0);
94 this._materialNode.setShader(this._shader); 100 };
95 101
96 this.setShaderValues(); 102 this.resetToDefault = function()
97 this.update(0); 103 {
98 }; 104 this._propValues[ this._propNames[0] ] = this._defaultLightDiff;
105 this._propValues[ this._propNames[1] ] = this._defaultDiffuseTexture.slice(0);
106 this._propValues[ this._propNames[2] ] = this._defaultNormalTexture.slice(0);
107 this._propValues[ this._propNames[3] ] = this._defaultSpecularTexture.slice(0);
108 for (var i=0; i<4; i++)
109 this.setProperty( this._propNames[i], this._propValues[this._propNames[i]] );
110};
99}; 111};
100 112
113
101/////////////////////////////////////////////////////////////////////////////////////// 114///////////////////////////////////////////////////////////////////////////////////////
102// RDGE shader 115// RDGE shader
103 116
104// shader spec (can also be loaded from a .JSON file, or constructed at runtime) 117// shader spec (can also be loaded from a .JSON file, or constructed at runtime)
105var bumpMetalMaterialDef = bumpMetalShaderDef = 118var bumpMetalMaterialDef = bumpMetalShaderDef =
106{ 119{
107 'shaders': 120 'shaders':
108 { 121 {
109 // this shader is being referenced by file 122 // this shader is being referenced by file
110 'defaultVShader':"assets/shaders/test_vshader.glsl", 123 'defaultVShader':"assets/shaders/test_vshader.glsl",
111 'defaultFShader':"assets/shaders/test_fshader.glsl", 124 'defaultFShader':"assets/shaders/test_fshader.glsl",
112 125
113 // this shader is inline 126 // this shader is inline
114 'dirLightVShader': "\ 127 'dirLightVShader': "\
115 uniform mat4 u_mvMatrix;\ 128 uniform mat4 u_mvMatrix;\
116 uniform mat4 u_normalMatrix;\ 129 uniform mat4 u_normalMatrix;\
117 uniform mat4 u_projMatrix;\ 130 uniform mat4 u_projMatrix;\
118 uniform mat4 u_worldMatrix;\ 131 uniform mat4 u_worldMatrix;\
119 attribute vec3 a_pos;\ 132 attribute vec3 a_pos;\
120 attribute vec3 a_nrm;\ 133 attribute vec3 a_nrm;\
121 varying vec3 vNormal;\ 134 varying vec3 vNormal;\
122 varying vec3 vPos;\ 135 varying vec3 vPos;\
123 void main() {\ 136 void main() {\
124 vNormal.xyz = (u_normalMatrix*vec4(a_nrm, 0.0)).xyz;\ 137 vNormal.xyz = (u_normalMatrix*vec4(a_nrm, 0.0)).xyz;\
125 gl_Position = u_projMatrix * u_mvMatrix * vec4(a_pos,1.0);\ 138 gl_Position = u_projMatrix * u_mvMatrix * vec4(a_pos,1.0);\
126 vPos = (u_worldMatrix * vec4(a_pos,1.0)).xyz;\ 139 vPos = (u_worldMatrix * vec4(a_pos,1.0)).xyz;\
127 }", 140 }",
128 'dirLightFShader': "\ 141 'dirLightFShader': "\
129 precision highp float;\ 142 precision highp float;\
130 uniform vec4 u_light1Diff;\ 143 uniform vec4 u_light1Diff;\
131 uniform vec3 u_light1Pos;\ 144 uniform vec3 u_light1Pos;\
@@ -142,37 +155,37 @@ var bumpMetalMaterialDef = bumpMetalShaderDef =
142 float alpha2 = max(0.0, 1.0 - ( (light2.x*light2.x)/range + (light2.y*light2.y)/range + (light2.z*light2.z)/range));\ 155 float alpha2 = max(0.0, 1.0 - ( (light2.x*light2.x)/range + (light2.y*light2.y)/range + (light2.z*light2.z)/range));\
143 gl_FragColor = vec4((u_light2Diff*alpha2 + u_light1Diff*alpha1).rgb, 1.0);\ 156 gl_FragColor = vec4((u_light2Diff*alpha2 + u_light1Diff*alpha1).rgb, 1.0);\
144 }" 157 }"
145 }, 158 },
146 'techniques': 159 'techniques':
147 {