lighting.js 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /*
  2. Copyright (c) 2004-2012, The Dojo Foundation All Rights Reserved.
  3. Available via Academic Free License >= 2.1 OR the modified BSD license.
  4. see: http://dojotoolkit.org/license for details
  5. */
  6. if(!dojo._hasResource["dojox.gfx3d.lighting"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
  7. dojo._hasResource["dojox.gfx3d.lighting"] = true;
  8. dojo.provide("dojox.gfx3d.lighting");
  9. dojo.require("dojox.gfx._base");
  10. (function(){
  11. var lite = dojox.gfx3d.lighting;
  12. dojo.mixin(dojox.gfx3d.lighting, {
  13. // color utilities
  14. black: function(){
  15. return {r: 0, g: 0, b: 0, a: 1};
  16. },
  17. white: function(){
  18. return {r: 1, g: 1, b: 1, a: 1};
  19. },
  20. toStdColor: function(c){
  21. c = dojox.gfx.normalizeColor(c);
  22. return {r: c.r / 255, g: c.g / 255, b: c.b / 255, a: c.a};
  23. },
  24. fromStdColor: function(c){
  25. return new dojo.Color([Math.round(255 * c.r), Math.round(255 * c.g), Math.round(255 * c.b), c.a]);
  26. },
  27. scaleColor: function(s, c){
  28. return {r: s * c.r, g: s * c.g, b: s * c.b, a: s * c.a};
  29. },
  30. addColor: 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};
  32. },
  33. multiplyColor: function(a, b){
  34. return {r: a.r * b.r, g: a.g * b.g, b: a.b * b.b, a: a.a * b.a};
  35. },
  36. saturateColor: function(c){
  37. return {
  38. r: c.r < 0 ? 0 : c.r > 1 ? 1 : c.r,
  39. g: c.g < 0 ? 0 : c.g > 1 ? 1 : c.g,
  40. b: c.b < 0 ? 0 : c.b > 1 ? 1 : c.b,
  41. a: c.a < 0 ? 0 : c.a > 1 ? 1 : c.a
  42. };
  43. },
  44. mixColor: function(c1, c2, s){
  45. return lite.addColor(lite.scaleColor(s, c1), lite.scaleColor(1 - s, c2));
  46. },
  47. diff2Color: function(c1, c2){
  48. var r = c1.r - c2.r;
  49. var g = c1.g - c2.g;
  50. var b = c1.b - c2.b;
  51. var a = c1.a - c2.a;
  52. return r * r + g * g + b * b + a * a;
  53. },
  54. length2Color: function(c){
  55. return c.r * c.r + c.g * c.g + c.b * c.b + c.a * c.a;
  56. },
  57. // vector utilities
  58. //TODO: move vector utilities from this file to vector.js
  59. dot: function(a, b){
  60. return a.x * b.x + a.y * b.y + a.z * b.z;
  61. },
  62. scale: function(s, v){
  63. return {x: s * v.x, y: s * v.y, z: s * v.z};
  64. },
  65. add: function(a, b){
  66. return {x: a.x + b.x, y: a.y + b.y, z: a.z + b.z};
  67. },
  68. saturate: function(v){
  69. return Math.min(Math.max(v, 0), 1);
  70. },
  71. length: function(v){
  72. return Math.sqrt(dojox.gfx3d.lighting.dot(v, v));
  73. },
  74. normalize: function(v){
  75. return lite.scale(1 / lite.length(v), v);
  76. },
  77. faceforward: function(n, i){
  78. var p = dojox.gfx3d.lighting;
  79. var s = p.dot(i, n) < 0 ? 1 : -1;
  80. return p.scale(s, n);
  81. },
  82. reflect: function(i, n){
  83. var p = dojox.gfx3d.lighting;
  84. return p.add(i, p.scale(-2 * p.dot(i, n), n));
  85. },
  86. // lighting utilities
  87. diffuse: function(normal, lights){
  88. var c = lite.black();
  89. for(var i = 0; i < lights.length; ++i){
  90. var l = lights[i],
  91. d = lite.dot(lite.normalize(l.direction), normal);
  92. c = lite.addColor(c, lite.scaleColor(d, l.color));
  93. }
  94. return lite.saturateColor(c);
  95. },
  96. specular: function(normal, v, roughness, lights){
  97. var c = lite.black();
  98. for(var i = 0; i < lights.length; ++i){
  99. var l = lights[i],
  100. h = lite.normalize(lite.add(lite.normalize(l.direction), v)),
  101. s = Math.pow(Math.max(0, lite.dot(normal, h)), 1 / roughness);
  102. c = lite.addColor(c, lite.scaleColor(s, l.color));
  103. }
  104. return lite.saturateColor(c);
  105. },
  106. phong: function(normal, v, size, lights){
  107. normal = lite.normalize(normal);
  108. var c = lite.black();
  109. for(var i = 0; i < lights.length; ++i){
  110. var l = lights[i],
  111. r = lite.reflect(lite.scale(-1, lite.normalize(v)), normal),
  112. s = Math.pow(Math.max(0, lite.dot(r, lite.normalize(l.direction))), size);
  113. c = lite.addColor(c, lite.scaleColor(s, l.color));
  114. }
  115. return lite.saturateColor(c);
  116. }
  117. });
  118. // this lighting model is derived from RenderMan Interface Specification Version 3.2
  119. dojo.declare("dojox.gfx3d.lighting.Model", null, {
  120. constructor: function(incident, lights, ambient, specular){
  121. this.incident = lite.normalize(incident);
  122. this.lights = [];
  123. for(var i = 0; i < lights.length; ++i){
  124. var l = lights[i];
  125. this.lights.push({direction: lite.normalize(l.direction), color: lite.toStdColor(l.color)});
  126. }
  127. this.ambient = lite.toStdColor(ambient.color ? ambient.color : "white");
  128. this.ambient = lite.scaleColor(ambient.intensity, this.ambient);
  129. this.ambient = lite.scaleColor(this.ambient.a, this.ambient);
  130. this.ambient.a = 1;
  131. this.specular = lite.toStdColor(specular ? specular : "white");
  132. this.specular = lite.scaleColor(this.specular.a, this.specular);
  133. this.specular.a = 1;
  134. this.npr_cool = {r: 0, g: 0, b: 0.4, a: 1};
  135. this.npr_warm = {r: 0.4, g: 0.4, b: 0.2, a: 1};
  136. this.npr_alpha = 0.2;
  137. this.npr_beta = 0.6;
  138. this.npr_scale = 0.6;
  139. },
  140. constant: function(normal, finish, pigment){
  141. pigment = lite.toStdColor(pigment);
  142. var alpha = pigment.a, color = lite.scaleColor(alpha, pigment);
  143. color.a = alpha;
  144. return lite.fromStdColor(lite.saturateColor(color));
  145. },
  146. matte: function(normal, finish, pigment){
  147. if(typeof finish == "string"){ finish = lite.finish[finish]; }
  148. pigment = lite.toStdColor(pigment);
  149. normal = lite.faceforward(lite.normalize(normal), this.incident);
  150. var ambient = lite.scaleColor(finish.Ka, this.ambient),
  151. shadow = lite.saturate(-4 * lite.dot(normal, this.incident)),
  152. diffuse = lite.scaleColor(shadow * finish.Kd, lite.diffuse(normal, this.lights)),
  153. color = lite.scaleColor(pigment.a, lite.multiplyColor(pigment, lite.addColor(ambient, diffuse)));
  154. color.a = pigment.a;
  155. return lite.fromStdColor(lite.saturateColor(color));
  156. },
  157. metal: function(normal, finish, pigment){
  158. if(typeof finish == "string"){ finish = lite.finish[finish]; }
  159. pigment = lite.toStdColor(pigment);
  160. normal = lite.faceforward(lite.normalize(normal), this.incident);
  161. var v = lite.scale(-1, this.incident), specular, color,
  162. ambient = lite.scaleColor(finish.Ka, this.ambient),
  163. shadow = lite.saturate(-4 * lite.dot(normal, this.incident));
  164. if("phong" in finish){
  165. specular = lite.scaleColor(shadow * finish.Ks * finish.phong, lite.phong(normal, v, finish.phong_size, this.lights));
  166. }else{
  167. specular = lite.scaleColor(shadow * finish.Ks, lite.specular(normal, v, finish.roughness, this.lights));
  168. }
  169. color = lite.scaleColor(pigment.a, lite.addColor(lite.multiplyColor(pigment, ambient), lite.multiplyColor(this.specular, specular)));
  170. color.a = pigment.a;
  171. return lite.fromStdColor(lite.saturateColor(color));
  172. },
  173. plastic: function(normal, finish, pigment){
  174. if(typeof finish == "string"){ finish = lite.finish[finish]; }
  175. pigment = lite.toStdColor(pigment);
  176. normal = lite.faceforward(lite.normalize(normal), this.incident);
  177. var v = lite.scale(-1, this.incident), specular, color,
  178. ambient = lite.scaleColor(finish.Ka, this.ambient),
  179. shadow = lite.saturate(-4 * lite.dot(normal, this.incident)),
  180. diffuse = lite.scaleColor(shadow * finish.Kd, lite.diffuse(normal, this.lights));
  181. if("phong" in finish){
  182. specular = lite.scaleColor(shadow * finish.Ks * finish.phong, lite.phong(normal, v, finish.phong_size, this.lights));
  183. }else{
  184. specular = lite.scaleColor(shadow * finish.Ks, lite.specular(normal, v, finish.roughness, this.lights));
  185. }
  186. color = lite.scaleColor(pigment.a, lite.addColor(lite.multiplyColor(pigment, lite.addColor(ambient, diffuse)), lite.multiplyColor(this.specular, specular)));
  187. color.a = pigment.a;
  188. return lite.fromStdColor(lite.saturateColor(color));
  189. },
  190. npr: function(normal, finish, pigment){
  191. if(typeof finish == "string"){ finish = lite.finish[finish]; }
  192. pigment = lite.toStdColor(pigment);
  193. normal = lite.faceforward(lite.normalize(normal), this.incident);
  194. var ambient = lite.scaleColor(finish.Ka, this.ambient),
  195. shadow = lite.saturate(-4 * lite.dot(normal, this.incident)),
  196. diffuse = lite.scaleColor(shadow * finish.Kd, lite.diffuse(normal, this.lights)),
  197. color = lite.scaleColor(pigment.a, lite.multiplyColor(pigment, lite.addColor(ambient, diffuse))),
  198. cool = lite.addColor(this.npr_cool, lite.scaleColor(this.npr_alpha, color)),
  199. warm = lite.addColor(this.npr_warm, lite.scaleColor(this.npr_beta, color)),
  200. d = (1 + lite.dot(this.incident, normal)) / 2,
  201. color = lite.scaleColor(this.npr_scale, lite.addColor(color, lite.mixColor(cool, warm, d)));
  202. color.a = pigment.a;
  203. return lite.fromStdColor(lite.saturateColor(color));
  204. }
  205. });
  206. })();
  207. // POV-Ray basic finishes
  208. dojox.gfx3d.lighting.finish = {
  209. // Default
  210. defaults: {Ka: 0.1, Kd: 0.6, Ks: 0.0, roughness: 0.05},
  211. dull: {Ka: 0.1, Kd: 0.6, Ks: 0.5, roughness: 0.15},
  212. shiny: {Ka: 0.1, Kd: 0.6, Ks: 1.0, roughness: 0.001},
  213. glossy: {Ka: 0.1, Kd: 0.6, Ks: 1.0, roughness: 0.0001},
  214. phong_dull: {Ka: 0.1, Kd: 0.6, Ks: 0.5, phong: 0.5, phong_size: 1},
  215. phong_shiny: {Ka: 0.1, Kd: 0.6, Ks: 1.0, phong: 1.0, phong_size: 200},
  216. phong_glossy: {Ka: 0.1, Kd: 0.6, Ks: 1.0, phong: 1.0, phong_size: 300},
  217. luminous: {Ka: 1.0, Kd: 0.0, Ks: 0.0, roughness: 0.05},
  218. // Metals
  219. // very soft and dull
  220. metalA: {Ka: 0.35, Kd: 0.3, Ks: 0.8, roughness: 1/20},
  221. // fairly soft and dull
  222. metalB: {Ka: 0.30, Kd: 0.4, Ks: 0.7, roughness: 1/60},
  223. // medium reflectivity, holds color well
  224. metalC: {Ka: 0.25, Kd: 0.5, Ks: 0.8, roughness: 1/80},
  225. // highly hard and polished, high reflectivity
  226. metalD: {Ka: 0.15, Kd: 0.6, Ks: 0.8, roughness: 1/100},
  227. // very highly polished and reflective
  228. metalE: {Ka: 0.10, Kd: 0.7, Ks: 0.8, roughness: 1/120}
  229. };
  230. }