1 | define(["dojo/_base/lang","./matrix","./vector"], |
---|
2 | function(lang,m,v){ |
---|
3 | |
---|
4 | var gfx3d = lang.getObject("dojox.gfx3d",true); |
---|
5 | |
---|
6 | var dist = function(a, b){ return Math.sqrt(Math.pow(b.x - a.x, 2) + Math.pow(b.y - a.y, 2)); }; |
---|
7 | var N = 32; |
---|
8 | |
---|
9 | gfx3d.gradient = function(model, material, center, radius, from, to, matrix){ |
---|
10 | // summary: calculate a cylindrical gradient |
---|
11 | // model: dojox.gfx3d.lighting.Model: color model |
---|
12 | // material: Object: defines visual properties |
---|
13 | // center: Object: center of the cylinder's bottom |
---|
14 | // radius: Number: radius of the cylinder |
---|
15 | // from: Number: from position in radians |
---|
16 | // to: Number: from position in radians |
---|
17 | // matrix: dojox.gfx3d.Matrix3D: the cumulative transformation matrix |
---|
18 | // tolerance: Number: tolerable difference in colors between gradient steps |
---|
19 | |
---|
20 | var mx = m.normalize(matrix), |
---|
21 | f = m.multiplyPoint(mx, radius * Math.cos(from) + center.x, radius * Math.sin(from) + center.y, center.z), |
---|
22 | t = m.multiplyPoint(mx, radius * Math.cos(to) + center.x, radius * Math.sin(to) + center.y, center.z), |
---|
23 | c = m.multiplyPoint(mx, center.x, center.y, center.z), step = (to - from) / N, r = dist(f, t) / 2, |
---|
24 | mod = model[material.type], fin = material.finish, pmt = material.color, |
---|
25 | colors = [{offset: 0, color: mod.call(model, v.substract(f, c), fin, pmt)}]; |
---|
26 | |
---|
27 | for(var a = from + step; a < to; a += step){ |
---|
28 | var p = m.multiplyPoint(mx, radius * Math.cos(a) + center.x, radius * Math.sin(a) + center.y, center.z), |
---|
29 | df = dist(f, p), dt = dist(t, p); |
---|
30 | colors.push({offset: df / (df + dt), color: mod.call(model, v.substract(p, c), fin, pmt)}); |
---|
31 | } |
---|
32 | colors.push({offset: 1, color: mod.call(model, v.substract(t, c), fin, pmt)}); |
---|
33 | |
---|
34 | return {type: "linear", x1: 0, y1: -r, x2: 0, y2: r, colors: colors}; |
---|
35 | }; |
---|
36 | |
---|
37 | return gfx3d.gradient; |
---|
38 | }); |
---|