1 if(!dojo._hasResource["dojox.gfx3d.lighting"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
2 dojo._hasResource["dojox.gfx3d.lighting"] = true;
3 dojo.provide("dojox.gfx3d.lighting");
4 dojo.require("dojox.gfx._base");
7 var lite = dojox.gfx3d.lighting;
9 dojo.mixin(dojox.gfx3d.lighting, {
12 return {r: 0, g: 0, b: 0, a: 1};
15 return {r: 1, g: 1, b: 1, a: 1};
17 toStdColor: function(c){
18 c = dojox.gfx.normalizeColor(c);
19 return {r: c.r / 255, g: c.g / 255, b: c.b / 255, a: c.a};
21 fromStdColor: function(c){
22 return new dojo.Color([Math.round(255 * c.r), Math.round(255 * c.g), Math.round(255 * c.b), c.a]);
24 scaleColor: function(s, c){
25 return {r: s * c.r, g: s * c.g, b: s * c.b, a: s * c.a};
27 addColor: function(a, b){
28 return {r: a.r + b.r, g: a.g + b.g, b: a.b + b.b, a: a.a + b.a};
30 multiplyColor: function(a, b){
31 return {r: a.r * b.r, g: a.g * b.g, b: a.b * b.b, a: a.a * b.a};
33 saturateColor: function(c){
35 r: c.r < 0 ? 0 : c.r > 1 ? 1 : c.r,
36 g: c.g < 0 ? 0 : c.g > 1 ? 1 : c.g,
37 b: c.b < 0 ? 0 : c.b > 1 ? 1 : c.b,
38 a: c.a < 0 ? 0 : c.a > 1 ? 1 : c.a
41 mixColor: function(c1, c2, s){
42 return lite.addColor(lite.scaleColor(s, c1), lite.scaleColor(1 - s, c2));
44 diff2Color: function(c1, c2){
49 return r * r + g * g + b * b + a * a;
51 length2Color: function(c){
52 return c.r * c.r + c.g * c.g + c.b * c.b + c.a * c.a;
56 //TODO: move vector utilities from this file to vector.js
58 return a.x * b.x + a.y * b.y + a.z * b.z;
60 scale: function(s, v){
61 return {x: s * v.x, y: s * v.y, z: s * v.z};
64 return {x: a.x + b.x, y: a.y + b.y, z: a.z + b.z};
66 saturate: function(v){
67 return Math.min(Math.max(v, 0), 1);
70 return Math.sqrt(dojox.gfx3d.lighting.dot(v, v));
72 normalize: function(v){
73 return lite.scale(1 / lite.length(v), v);
75 faceforward: function(n, i){
76 var p = dojox.gfx3d.lighting;
77 var s = p.dot(i, n) < 0 ? 1 : -1;
80 reflect: function(i, n){
81 var p = dojox.gfx3d.lighting;
82 return p.add(i, p.scale(-2 * p.dot(i, n), n));
86 diffuse: function(normal, lights){
88 for(var i = 0; i < lights.length; ++i){
90 d = lite.dot(lite.normalize(l.direction), normal);
91 c = lite.addColor(c, lite.scaleColor(d, l.color));
93 return lite.saturateColor(c);
95 specular: function(normal, v, roughness, lights){
97 for(var i = 0; i < lights.length; ++i){
99 h = lite.normalize(lite.add(lite.normalize(l.direction), v)),
100 s = Math.pow(Math.max(0, lite.dot(normal, h)), 1 / roughness);
101 c = lite.addColor(c, lite.scaleColor(s, l.color));
103 return lite.saturateColor(c);
105 phong: function(normal, v, size, lights){
106 normal = lite.normalize(normal);
107 var c = lite.black();
108 for(var i = 0; i < lights.length; ++i){
110 r = lite.reflect(lite.scale(-1, lite.normalize(v)), normal),
111 s = Math.pow(Math.max(0, lite.dot(r, lite.normalize(l.direction))), size);
112 c = lite.addColor(c, lite.scaleColor(s, l.color));
114 return lite.saturateColor(c);
118 // this lighting model is derived from RenderMan Interface Specification Version 3.2
120 dojo.declare("dojox.gfx3d.lighting.Model", null, {
121 constructor: function(incident, lights, ambient, specular){
122 this.incident = lite.normalize(incident);
124 for(var i = 0; i < lights.length; ++i){
126 this.lights.push({direction: lite.normalize(l.direction), color: lite.toStdColor(l.color)});
128 this.ambient = lite.toStdColor(ambient.color ? ambient.color : "white");
129 this.ambient = lite.scaleColor(ambient.intensity, this.ambient);
130 this.ambient = lite.scaleColor(this.ambient.a, this.ambient);
132 this.specular = lite.toStdColor(specular ? specular : "white");
133 this.specular = lite.scaleColor(this.specular.a, this.specular);
135 this.npr_cool = {r: 0, g: 0, b: 0.4, a: 1};
136 this.npr_warm = {r: 0.4, g: 0.4, b: 0.2, a: 1};
137 this.npr_alpha = 0.2;
139 this.npr_scale = 0.6;
141 constant: function(normal, finish, pigment){
142 pigment = lite.toStdColor(pigment);
143 var alpha = pigment.a, color = lite.scaleColor(alpha, pigment);
145 return lite.fromStdColor(lite.saturateColor(color));
147 matte: function(normal, finish, pigment){
148 if(typeof finish == "string"){ finish = lite.finish[finish]; }
149 pigment = lite.toStdColor(pigment);
150 normal = lite.faceforward(lite.normalize(normal), this.incident);
151 var ambient = lite.scaleColor(finish.Ka, this.ambient),
152 shadow = lite.saturate(-4 * lite.dot(normal, this.incident)),
153 diffuse = lite.scaleColor(shadow * finish.Kd, lite.diffuse(normal, this.lights)),
154 color = lite.scaleColor(pigment.a, lite.multiplyColor(pigment, lite.addColor(ambient, diffuse)));
156 return lite.fromStdColor(lite.saturateColor(color));
158 metal: function(normal, finish, pigment){
159 if(typeof finish == "string"){ finish = lite.finish[finish]; }
160 pigment = lite.toStdColor(pigment);
161 normal = lite.faceforward(lite.normalize(normal), this.incident);
162 var v = lite.scale(-1, this.incident), specular, color,
163 ambient = lite.scaleColor(finish.Ka, this.ambient),
164 shadow = lite.saturate(-4 * lite.dot(normal, this.incident));
165 if("phong" in finish){
166 specular = lite.scaleColor(shadow * finish.Ks * finish.phong, lite.phong(normal, v, finish.phong_size, this.lights));
168 specular = lite.scaleColor(shadow * finish.Ks, lite.specular(normal, v, finish.roughness, this.lights));
170 color = lite.scaleColor(pigment.a, lite.addColor(lite.multiplyColor(pigment, ambient), lite.multiplyColor(this.specular, specular)));
172 return lite.fromStdColor(lite.saturateColor(color));
174 plastic: function(normal, finish, pigment){
175 if(typeof finish == "string"){ finish = lite.finish[finish]; }
176 pigment = lite.toStdColor(pigment);
177 normal = lite.faceforward(lite.normalize(normal), this.incident);
178 var v = lite.scale(-1, this.incident), specular, color,
179 ambient = lite.scaleColor(finish.Ka, this.ambient),
180 shadow = lite.saturate(-4 * lite.dot(normal, this.incident)),
181 diffuse = lite.scaleColor(shadow * finish.Kd, lite.diffuse(normal, this.lights));
182 if("phong" in finish){
183 specular = lite.scaleColor(shadow * finish.Ks * finish.phong, lite.phong(normal, v, finish.phong_size, this.lights));
185 specular = lite.scaleColor(shadow * finish.Ks, lite.specular(normal, v, finish.roughness, this.lights));
187 color = lite.scaleColor(pigment.a, lite.addColor(lite.multiplyColor(pigment, lite.addColor(ambient, diffuse)), lite.multiplyColor(this.specular, specular)));
189 return lite.fromStdColor(lite.saturateColor(color));
191 npr: function(normal, finish, pigment){
192 if(typeof finish == "string"){ finish = lite.finish[finish]; }
193 pigment = lite.toStdColor(pigment);
194 normal = lite.faceforward(lite.normalize(normal), this.incident);
195 var ambient = lite.scaleColor(finish.Ka, this.ambient),
196 shadow = lite.saturate(-4 * lite.dot(normal, this.incident)),
197 diffuse = lite.scaleColor(shadow * finish.Kd, lite.diffuse(normal, this.lights)),
198 color = lite.scaleColor(pigment.a, lite.multiplyColor(pigment, lite.addColor(ambient, diffuse))),
199 cool = lite.addColor(this.npr_cool, lite.scaleColor(this.npr_alpha, color)),
200 warm = lite.addColor(this.npr_warm, lite.scaleColor(this.npr_beta, color)),
201 d = (1 + lite.dot(this.incident, normal)) / 2,
202 color = lite.scaleColor(this.npr_scale, lite.addColor(color, lite.mixColor(cool, warm, d)));
204 return lite.fromStdColor(lite.saturateColor(color));
209 // POV-Ray basic finishes
211 dojox.gfx3d.lighting.finish = {
215 defaults: {Ka: 0.1, Kd: 0.6, Ks: 0.0, roughness: 0.05},
217 dull: {Ka: 0.1, Kd: 0.6, Ks: 0.5, roughness: 0.15},
218 shiny: {Ka: 0.1, Kd: 0.6, Ks: 1.0, roughness: 0.001},
219 glossy: {Ka: 0.1, Kd: 0.6, Ks: 1.0, roughness: 0.0001},
221 phong_dull: {Ka: 0.1, Kd: 0.6, Ks: 0.5, phong: 0.5, phong_size: 1},
222 phong_shiny: {Ka: 0.1, Kd: 0.6, Ks: 1.0, phong: 1.0, phong_size: 200},
223 phong_glossy: {Ka: 0.1, Kd: 0.6, Ks: 1.0, phong: 1.0, phong_size: 300},
225 luminous: {Ka: 1.0, Kd: 0.0, Ks: 0.0, roughness: 0.05},
229 // very soft and dull
230 metalA: {Ka: 0.35, Kd: 0.3, Ks: 0.8, roughness: 1/20},
231 // fairly soft and dull
232 metalB: {Ka: 0.30, Kd: 0.4, Ks: 0.7, roughness: 1/60},
233 // medium reflectivity, holds color well
234 metalC: {Ka: 0.25, Kd: 0.5, Ks: 0.8, roughness: 1/80},
235 // highly hard and polished, high reflectivity
236 metalD: {Ka: 0.15, Kd: 0.6, Ks: 0.8, roughness: 1/100},
237 // very highly polished and reflective
238 metalE: {Ka: 0.10, Kd: 0.7, Ks: 0.8, roughness: 1/120}