root / trunk / web / dojo / dojox / gfx3d / lighting.js @ 10
History | View | Annotate | Download (6.51 KB)
1 |
/*
|
---|---|
2 |
Copyright (c) 2004-2010, 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 |
|
7 |
|
8 |
if(!dojo._hasResource["dojox.gfx3d.lighting"]){ |
9 |
dojo._hasResource["dojox.gfx3d.lighting"]=true; |
10 |
dojo.provide("dojox.gfx3d.lighting");
|
11 |
dojo.require("dojox.gfx._base");
|
12 |
(function(){
|
13 |
var _1=dojox.gfx3d.lighting;
|
14 |
dojo.mixin(dojox.gfx3d.lighting,{black:function(){ |
15 |
return {r:0,g:0,b:0,a:1}; |
16 |
},white:function(){ |
17 |
return {r:1,g:1,b:1,a:1}; |
18 |
},toStdColor:function(c){ |
19 |
c=dojox.gfx.normalizeColor(c); |
20 |
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]); |
23 |
},scaleColor:function(s,c){ |
24 |
return {r:s*c.r,g:s*c.g,b:s*c.b,a:s*c.a}; |
25 |
},addColor:function(a,b){ |
26 |
return {r:a.r+b.r,g:a.g+b.g,b:a.b+b.b,a:a.a+b.a}; |
27 |
},multiplyColor: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}; |
29 |
},saturateColor:function(c){ |
30 |
return {r:c.r<0?0:c.r>1?1:c.r,g:c.g<0?0:c.g>1?1:c.g,b:c.b<0?0:c.b>1?1:c.b,a:c.a<0?0:c.a>1?1:c.a}; |
31 |
},mixColor:function(c1,c2,s){ |
32 |
return _1.addColor(_1.scaleColor(s,c1),_1.scaleColor(1-s,c2)); |
33 |
},diff2Color:function(c1,c2){ |
34 |
var r=c1.r-c2.r;
|
35 |
var g=c1.g-c2.g;
|
36 |
var b=c1.b-c2.b;
|
37 |
var a=c1.a-c2.a;
|
38 |
return r*r+g*g+b*b+a*a;
|
39 |
},length2Color:function(c){ |
40 |
return c.r*c.r+c.g*c.g+c.b*c.b+c.a*c.a;
|
41 |
},dot:function(a,b){ |
42 |
return a.x*b.x+a.y*b.y+a.z*b.z;
|
43 |
},scale:function(s,v){ |
44 |
return {x:s*v.x,y:s*v.y,z:s*v.z}; |
45 |
},add:function(a,b){ |
46 |
return {x:a.x+b.x,y:a.y+b.y,z:a.z+b.z}; |
47 |
},saturate:function(v){ |
48 |
return Math.min(Math.max(v,0),1); |
49 |
},length:function(v){ |
50 |
return Math.sqrt(dojox.gfx3d.lighting.dot(v,v));
|
51 |
},normalize:function(v){ |
52 |
return _1.scale(1/_1.length(v),v); |
53 |
},faceforward:function(n,i){ |
54 |
var p=dojox.gfx3d.lighting;
|
55 |
var s=p.dot(i,n)<0?1:-1; |
56 |
return p.scale(s,n);
|
57 |
},reflect:function(i,n){ |
58 |
var p=dojox.gfx3d.lighting;
|
59 |
return p.add(i,p.scale(-2*p.dot(i,n),n)); |
60 |
},diffuse:function(_2,_3){ |
61 |
var c=_1.black();
|
62 |
for(var i=0;i<_3.length;++i){ |
63 |
var l=_3[i],d=_1.dot(_1.normalize(l.direction),_2);
|
64 |
c=_1.addColor(c,_1.scaleColor(d,l.color)); |
65 |
} |
66 |
return _1.saturateColor(c);
|
67 |
},specular:function(_4,v,_5,_6){ |
68 |
var c=_1.black();
|
69 |
for(var i=0;i<_6.length;++i){ |
70 |
var l=_6[i],h=_1.normalize(_1.add(_1.normalize(l.direction),v)),s=Math.pow(Math.max(0,_1.dot(_4,h)),1/_5); |
71 |
c=_1.addColor(c,_1.scaleColor(s,l.color)); |
72 |
} |
73 |
return _1.saturateColor(c);
|
74 |
},phong:function(_7,v,_8,_9){ |
75 |
_7=_1.normalize(_7); |
76 |
var c=_1.black();
|
77 |
for(var i=0;i<_9.length;++i){ |
78 |
var l=_9[i],r=_1.reflect(_1.scale(-1,_1.normalize(v)),_7),s=Math.pow(Math.max(0,_1.dot(r,_1.normalize(l.direction))),_8); |
79 |
c=_1.addColor(c,_1.scaleColor(s,l.color)); |
80 |
} |
81 |
return _1.saturateColor(c);
|
82 |
}}); |
83 |
dojo.declare("dojox.gfx3d.lighting.Model",null,{constructor:function(_a,_b,_c,_d){ |
84 |
this.incident=_1.normalize(_a);
|
85 |
this.lights=[];
|
86 |
for(var i=0;i<_b.length;++i){ |
87 |
var l=_b[i];
|
88 |
this.lights.push({direction:_1.normalize(l.direction),color:_1.toStdColor(l.color)}); |
89 |
} |
90 |
this.ambient=_1.toStdColor(_c.color?_c.color:"white"); |
91 |
this.ambient=_1.scaleColor(_c.intensity,this.ambient); |
92 |
this.ambient=_1.scaleColor(this.ambient.a,this.ambient); |
93 |
this.ambient.a=1; |
94 |
this.specular=_1.toStdColor(_d?_d:"white"); |
95 |
this.specular=_1.scaleColor(this.specular.a,this.specular); |
96 |
this.specular.a=1; |
97 |
this.npr_cool={r:0,g:0,b:0.4,a:1}; |
98 |
this.npr_warm={r:0.4,g:0.4,b:0.2,a:1}; |
99 |
this.npr_alpha=0.2; |
100 |
this.npr_beta=0.6; |
101 |
this.npr_scale=0.6; |
102 |
},constant:function(_e,_f,_10){ |
103 |
_10=_1.toStdColor(_10); |
104 |
var _11=_10.a,_12=_1.scaleColor(_11,_10);
|
105 |
_12.a=_11; |
106 |
return _1.fromStdColor(_1.saturateColor(_12));
|
107 |
},matte:function(_13,_14,_15){ |
108 |
if(typeof _14=="string"){ |
109 |
_14=_1.finish[_14]; |
110 |
} |
111 |
_15=_1.toStdColor(_15); |
112 |
_13=_1.faceforward(_1.normalize(_13),this.incident);
|
113 |
var _16=_1.scaleColor(_14.Ka,this.ambient),_17=_1.saturate(-4*_1.dot(_13,this.incident)),_18=_1.scaleColor(_17*_14.Kd,_1.diffuse(_13,this.lights)),_19=_1.scaleColor(_15.a,_1.multiplyColor(_15,_1.addColor(_16,_18))); |
114 |
_19.a=_15.a; |
115 |
return _1.fromStdColor(_1.saturateColor(_19));
|
116 |
},metal:function(_1a,_1b,_1c){ |
117 |
if(typeof _1b=="string"){ |
118 |
_1b=_1.finish[_1b]; |
119 |
} |
120 |
_1c=_1.toStdColor(_1c); |
121 |
_1a=_1.faceforward(_1.normalize(_1a),this.incident);
|
122 |
var v=_1.scale(-1,this.incident),_1d,_1e,_1f=_1.scaleColor(_1b.Ka,this.ambient),_20=_1.saturate(-4*_1.dot(_1a,this.incident)); |
123 |
if("phong" in _1b){ |
124 |
_1d=_1.scaleColor(_20*_1b.Ks*_1b.phong,_1.phong(_1a,v,_1b.phong_size,this.lights));
|
125 |
}else{
|
126 |
_1d=_1.scaleColor(_20*_1b.Ks,_1.specular(_1a,v,_1b.roughness,this.lights));
|
127 |
} |
128 |
_1e=_1.scaleColor(_1c.a,_1.addColor(_1.multiplyColor(_1c,_1f),_1.multiplyColor(this.specular,_1d)));
|
129 |
_1e.a=_1c.a; |
130 |
return _1.fromStdColor(_1.saturateColor(_1e));
|
131 |
},plastic:function(_21,_22,_23){ |
132 |
if(typeof _22=="string"){ |
133 |
_22=_1.finish[_22]; |
134 |
} |
135 |
_23=_1.toStdColor(_23); |
136 |
_21=_1.faceforward(_1.normalize(_21),this.incident);
|
137 |
var v=_1.scale(-1,this.incident),_24,_25,_26=_1.scaleColor(_22.Ka,this.ambient),_27=_1.saturate(-4*_1.dot(_21,this.incident)),_28=_1.scaleColor(_27*_22.Kd,_1.diffuse(_21,this.lights)); |
138 |
if("phong" in _22){ |
139 |
_24=_1.scaleColor(_27*_22.Ks*_22.phong,_1.phong(_21,v,_22.phong_size,this.lights));
|
140 |
}else{
|
141 |
_24=_1.scaleColor(_27*_22.Ks,_1.specular(_21,v,_22.roughness,this.lights));
|
142 |
} |
143 |
_25=_1.scaleColor(_23.a,_1.addColor(_1.multiplyColor(_23,_1.addColor(_26,_28)),_1.multiplyColor(this.specular,_24)));
|
144 |
_25.a=_23.a; |
145 |
return _1.fromStdColor(_1.saturateColor(_25));
|
146 |
},npr:function(_29,_2a,_2b){ |
147 |
if(typeof _2a=="string"){ |
148 |
_2a=_1.finish[_2a]; |
149 |
} |
150 |
_2b=_1.toStdColor(_2b); |
151 |
_29=_1.faceforward(_1.normalize(_29),this.incident);
|
152 |
var _2c=_1.scaleColor(_2a.Ka,this.ambient),_2d=_1.saturate(-4*_1.dot(_29,this.incident)),_2e=_1.scaleColor(_2d*_2a.Kd,_1.diffuse(_29,this.lights)),_2f=_1.scaleColor(_2b.a,_1.multiplyColor(_2b,_1.addColor(_2c,_2e))),_30=_1.addColor(this.npr_cool,_1.scaleColor(this.npr_alpha,_2f)),_31=_1.addColor(this.npr_warm,_1.scaleColor(this.npr_beta,_2f)),d=(1+_1.dot(this.incident,_29))/2,_2f=_1.scaleColor(this.npr_scale,_1.addColor(_2f,_1.mixColor(_30,_31,d))); |
153 |
_2f.a=_2b.a; |
154 |
return _1.fromStdColor(_1.saturateColor(_2f));
|
155 |
}}); |
156 |
})(); |
157 |
dojox.gfx3d.lighting.finish={defaults:{Ka:0.1,Kd:0.6,Ks:0,roughness:0.05},dull:{Ka:0.1,Kd:0.6,Ks:0.5,roughness:0.15},shiny:{Ka:0.1,Kd:0.6,Ks:1,roughness:0.001},glossy:{Ka:0.1,Kd:0.6,Ks:1,roughness:0.0001},phong_dull:{Ka:0.1,Kd:0.6,Ks:0.5,phong:0.5,phong_size:1},phong_shiny:{Ka:0.1,Kd:0.6,Ks:1,phong:1,phong_size:200},phong_glossy:{Ka:0.1,Kd:0.6,Ks:1,phong:1,phong_size:300},luminous:{Ka:1,Kd:0,Ks:0,roughness:0.05},metalA:{Ka:0.35,Kd:0.3,Ks:0.8,roughness:1/20},metalB:{Ka:0.3,Kd:0.4,Ks:0.7,roughness:1/60},metalC:{Ka:0.25,Kd:0.5,Ks:0.8,roughness:1/80},metalD:{Ka:0.15,Kd:0.6,Ks:0.8,roughness:1/100},metalE:{Ka:0.1,Kd:0.7,Ks:0.8,roughness:1/120}}; |
158 |
} |