Revision 353f7580
Added by Leszek Koltunski over 5 years ago
src/main/java/org/distorted/library/effect/VertexEffectDistort.java | ||
---|---|---|
48 | 48 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
49 | 49 |
// PUBLIC API |
50 | 50 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
51 |
// Point (Px,Py) gets moved by vector (Wx,Wy,Wz) where Wx/Wy = Vx/Vy i.e. Wx=aVx and Wy=aVy where |
|
51 |
// Def: P-current vertex being distorted, S - center of the effect, X- point where half-line SP meets |
|
52 |
// the region circle (if P is inside the circle); (Vx,Vy,Vz) - force vector. |
|
53 |
// |
|
54 |
// horizontal part |
|
55 |
// Point (Px,Py) gets moved by vector (Wx,Wy) where Wx/Wy = Vx/Vy i.e. Wx=aVx and Wy=aVy where |
|
52 | 56 |
// a=Py/Sy (N --> when (Px,Py) is above (Sx,Sy)) or a=Px/Sx (W) or a=(w-Px)/(w-Sx) (E) or a=(h-Py)/(h-Sy) (S) |
53 | 57 |
// It remains to be computed which of the N,W,E or S case we have: answer: a = min[ Px/Sx , Py/Sy , (w-Px)/(w-Sx) , (h-Py)/(h-Sy) ] |
54 | 58 |
// Computations above are valid for screen (0,0)x(w,h) but here we have (-w/2,-h/2)x(w/2,h/2) |
55 | 59 |
// |
56 |
// the vertical part
|
|
57 |
// Let |(v.x,v.y),(ux,uy)| = |PS|, ux-v.x=dx,uy-v.y=dy, f(x) (0<=x<=|SX|) be the shape of the side of the bubble.
|
|
58 |
// H(v.x,v.y) = |PS|>|SX| ? 0 : f(|PX|)
|
|
59 |
// N(v.x,v.y) = |PS|>|SX| ? (0,0,1) : ( -(dx/|PS|)sin(beta), -(dy/|PS|)sin(beta), cos(beta) ) where tan(beta) is f'(|PX|)
|
|
60 |
// ( i.e. normalize( dx, dy, -|PS|/f'(|PX|)) |
|
60 |
// vertical part |
|
61 |
// Let vector PS = (dx,dy), f(x) (0<=x<=|SX|) be the shape of the side of the bubble.
|
|
62 |
// H(Px,Py) = |PS|>|SX| ? 0 : f(|PX|)
|
|
63 |
// N(Px,Py) = |PS|>|SX| ? (0,0,1) : ( -(dx/|PS|)sin(beta), -(dy/|PS|)sin(beta), cos(beta) ) where tan(beta) is f'(|PX|)
|
|
64 |
// ( i.e. normalize( dx, dy, -|PS|/f'(|PX|) )
|
|
61 | 65 |
// |
62 |
// Now we also have to take into account the effect horizontal move by V=(u_dVx[i],u_dVy[i]) will have on the normal vector.
|
|
66 |
// Now we also have to take into account the effect horizontal move by V=(Vx,Vy) will have on the normal vector.
|
|
63 | 67 |
// Solution: |
64 | 68 |
// 1. Decompose the V into two subcomponents, one parallel to SX and another perpendicular. |
65 | 69 |
// 2. Convince yourself (draw!) that the perpendicular component has no effect on normals. |
... | ... | |
67 | 71 |
// can be negative depending on the direction) |
68 | 72 |
// 4. that in turn leaves the x and y parts of the normal unchanged and multiplies the z component by a! |
69 | 73 |
// |
70 |
// |Vpar| = (u_dVx[i]*dx - u_dVy[i]*dy) / sqrt(ps_sq) = (Vx*dx-Vy*dy)/ sqrt(ps_sq) (-Vy because y is inverted)
|
|
74 |
// |Vpar| = (Vx*dx-Vy*dy) / sqrt(ps_sq) (-Vy because y is inverted)
|
|
71 | 75 |
// a = (|SX| - |Vpar|)/|SX| = 1 - |Vpar|/((sqrt(ps_sq)/(1-d)) = 1 - (1-d)*|Vpar|/sqrt(ps_sq) = 1-(1-d)*(Vx*dx-Vy*dy)/ps_sq |
72 | 76 |
// |
73 | 77 |
// Side of the bubble |
74 | 78 |
// |
75 | 79 |
// choose from one of the three bubble shapes: the cone, the thin bubble and the thick bubble |
76 | 80 |
// Case 1: |
77 |
// f(t) = t, i.e. f(x) = uz * x/|SX| (a cone)
|
|
78 |
// -|PS|/f'(|PX|) = -|PS|*|SX|/uz but since ps_sq=|PS|^2 and d=|PX|/|SX| then |PS|*|SX| = ps_sq/(1-d)
|
|
79 |
// so finally -|PS|/f'(|PX|) = -ps_sq/(uz*(1-d))
|
|
81 |
// f(t) = t, i.e. f(x) = Vz * x/|SX| (a cone)
|
|
82 |
// -|PS|/f'(|PX|) = -|PS|*|SX|/Vz but since ps_sq=|PS|^2 and d=|PX|/|SX| then |PS|*|SX| = ps_sq/(1-d)
|
|
83 |
// so finally -|PS|/f'(|PX|) = -ps_sq/(Vz*(1-d))
|
|
80 | 84 |
// |
81 | 85 |
// Case 2: |
82 | 86 |
// f(t) = 3t^2 - 2t^3 --> f(0)=0, f'(0)=0, f'(1)=0, f(1)=1 (the bell curve) |
83 |
// here we have t = x/|SX| which makes f'(|PX|) = 6*uz*|PS|*|PX|/|SX|^3.
|
|
84 |
// so -|PS|/f'(|PX|) = (-|SX|^3)/(6uz|PX|) = (-|SX|^2) / (6*uz*d) but
|
|
87 |
// here we have t = x/|SX| which makes f'(|PX|) = 6*Vz*|PS|*|PX|/|SX|^3.
|
|
88 |
// so -|PS|/f'(|PX|) = (-|SX|^3)/(6Vz|PX|) = (-|SX|^2) / (6*Vz*d) but
|
|
85 | 89 |
// d = |PX|/|SX| and ps_sq = |PS|^2 so |SX|^2 = ps_sq/(1-d)^2 |
86 |
// so finally -|PS|/f'(|PX|) = -ps_sq/ (6uz*d*(1-d)^2)
|
|
90 |
// so finally -|PS|/f'(|PX|) = -ps_sq/ (6Vz*d*(1-d)^2)
|
|
87 | 91 |
// |
88 | 92 |
// Case 3: |
89 | 93 |
// f(t) = 3t^4-8t^3+6t^2 would be better as this satisfies f(0)=0, f'(0)=0, f'(1)=0, f(1)=1, |
90 | 94 |
// f(0.5)=0.7 and f'(t)= t(t-1)^2 >=0 for t>=0 so this produces a fuller, thicker bubble! |
91 |
// then -|PS|/f'(|PX|) = (-|PS|*|SX)) / (12uz*d*(d-1)^2) but |PS|*|SX| = ps_sq/(1-d) (see above!)
|
|
92 |
// so finally -|PS|/f'(|PX|) = -ps_sq/ (12uz*d*(1-d)^3)
|
|
95 |
// then -|PS|/f'(|PX|) = (-|PS|*|SX)) / (12Vz*d*(d-1)^2) but |PS|*|SX| = ps_sq/(1-d) (see above!)
|
|
96 |
// so finally -|PS|/f'(|PX|) = -ps_sq/ (12Vz*d*(1-d)^3)
|
|
93 | 97 |
// |
94 | 98 |
// Now, new requirement: we have to be able to add up normal vectors, i.e. distort already distorted surfaces. |
95 | 99 |
// If a surface is given by z = f(x,y), then the normal vector at (x0,y0) is given by (-df/dx (x0,y0), -df/dy (x0,y0), 1 ). |
... | ... | |
97 | 101 |
// then the normal to g = f1+f2 is simply given by (f1x+f2x,f1y+f2y,1), i.e. if the third components are equal, then we |
98 | 102 |
// can simply add up the first and second components. |
99 | 103 |
// |
100 |
// Thus we actually want to compute N(v.x,v.y) = a*(-(dx/|PS|)*f'(|PX|), -(dy/|PS|)*f'(|PX|), 1) and keep adding
|
|
104 |
// Thus we actually want to compute N(Px,Py) = a*(-(dx/|PS|)*f'(|PX|), -(dy/|PS|)*f'(|PX|), 1) and keep adding
|
|
101 | 105 |
// the first two components. (a is the horizontal part) |
106 |
// |
|
107 |
// 2019-01-09 fixes for stuff discovered with the Earth testing app |
|
108 |
// Now, the above stuff works only if the normal vector is close to (0,0,1). To make it work for any situation, |
|
109 |
// rotate the normal, force and ps vectors along the axis (n.y,-n.x,0) and angle A between the normal and (0,0,1) |
|
110 |
// then modify the rotated normal (so (0,0,1)) and rotate the modified normal back. |
|
111 |
// |
|
112 |
// if we have a vector (vx,vy,vz) that w want to rotate with a rotation that turns the normal into (0,0,1), then |
|
113 |
// a) axis of rotation = (n.x,n.y,n.z) x (0,0,1) = (n.y,-n.x,0) |
|
114 |
// b) angle of rotation A: cosA = (n.x,n.y,n.z)*(0,0,1) = n.z (and sinA = + sqrt(1-n.z^2)) |
|
115 |
// |
|
116 |
// so normalized axisNor = (n.x/ sinA, -n.y/sinA, 0) and now from Rodrigues rotation formula |
|
117 |
// Vrot = v*cosA + (axisNor x v)*sinA + axisNor*(axis*v)*(1-cosA) |
|
118 |
// |
|
119 |
// which makes Vrot = (a+n.y*c , b-n.y*c , v*n) where |
|
120 |
// a = vx*nz-vz*nx , b = vy*nz-vz*ny , c = (vx*ny-vy*nx)/(1+nz) (unless n=(0,0,-1)) |
|
102 | 121 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
103 | 122 |
/** |
104 | 123 |
* Have to call this before the shaders get compiled (i.e before Distorted.onCreate()) for the Effect to work. |
... | ... | |
107 | 126 |
{ |
108 | 127 |
addEffect(EffectName.DISTORT, |
109 | 128 |
|
110 |
"vec3 center = vUniforms[effect+1].yzw; \n" |
|
111 |
+ "vec3 ps = center-v; \n" |
|
112 |
+ "vec3 force = vUniforms[effect].xyz; \n" |
|
113 |
+ "float d = degree(vUniforms[effect+2],center,ps); \n" |
|
114 |
+ "float denom = dot(ps+(1.0-d)*force,ps); \n" |
|
115 |
+ "float one_over_denom = 1.0/(denom-0.001*(sign(denom)-1.0)); \n" // = denom==0 ? 1000:1/denom; |
|
129 |
"vec3 center = vUniforms[effect+1].yzw; \n" |
|
130 |
+ "vec3 ps = center-v; \n" |
|
131 |
+ "vec3 force = vUniforms[effect].xyz; \n" |
|
132 |
+ "vec4 region= vUniforms[effect+2]; \n" |
|
133 |
+ "float d = degree(region,center,ps); \n" |
|
134 |
|
|
135 |
+ "if( d>0.0 ) \n" |
|
136 |
+ " { \n" |
|
137 |
+ " v += d*force; \n" |
|
116 | 138 |
|
117 |
//v.z += force.z*d; // cone
|
|
118 |
//b = -(force.z*(1.0-d))*one_over_denom; //
|
|
139 |
+ " float tp = 1.0+n.z; \n"
|
|
140 |
+ " float tr = 1.0 / (tp - (1.0 - sign(tp))); \n"
|
|
119 | 141 |
|
120 |
//v.z += force.z*d*d*(3.0-2.0*d); // thin bubble |
|
121 |
//b = -(6.0*force.z*d*(1.0-d)*(1.0-d))*one_over_denom; // |
|
142 |
+ " float ap = ps.x*n.z - ps.z*n.x; \n" // likewise rotate the ps vector |
|
143 |
+ " float bp = ps.y*n.z - ps.z*n.y; \n" // |
|
144 |
+ " float cp =(ps.x*n.y - ps.y*n.x)*tr; \n" // |
|
145 |
+ " vec3 psRot = vec3( ap+n.y*cp , bp-n.x*cp , dot(ps,n) ); \n" // |
|
122 | 146 |
|
123 |
+ "v.z += force.z*d*d*(3.0*d*d -8.0*d +6.0); \n" // thick bubble |
|
124 |
+ "float b = -(12.0*force.z*d*(1.0-d)*(1.0-d)*(1.0-d))*one_over_denom; \n" // |
|
147 |
+ " psRot = normalize(psRot); \n" |
|
148 |
+ " vec3 N = vec3( -dot(force,n)*psRot.xy, region.w ); \n" // modified rotated normal |
|
149 |
// dot(force,n) is rotated force.z |
|
150 |
+ " float an = N.x*n.z + N.z*n.x; \n" // now create the normal vector |
|
151 |
+ " float bn = N.y*n.z + N.z*n.y; \n" // back from our modified normal |
|
152 |
+ " float cn =(N.x*n.y - N.y*n.x)*tr; \n" // rotated back |
|
153 |
+ " n = vec3( an+n.y*cn , bn-n.x*cn , -N.x*n.x-N.y*n.y+N.z*n.z);\n" // notice 4 signs change! |
|
125 | 154 |
|
126 |
+ "v.xy += d*force.xy; \n"
|
|
127 |
+ "n.xy += n.z*b*ps.xy;"
|
|
155 |
+ " n = normalize(n); \n"
|
|
156 |
+ " } \n"
|
|
128 | 157 |
); |
129 | 158 |
} |
130 | 159 |
|
Also available in: Unified diff
Make Distort truly 3D.