Project

General

Profile

« Previous | Next » 

Revision 5e58293a

Added by Leszek Koltunski over 7 years ago

Effect-independent normals.

View differences:

src/main/res/raw/main_vertex_shader.glsl
214 214
//        along the force line is.
215 215
//        0<=C<1 looks completely ridiculous and C<0 destroys the system.
216 216

  
217
void deform(in int effect, inout vec3 v, inout vec3 n)
217
void deform(in int effect, inout vec3 v)
218 218
  {
219 219
  const vec2 ONE = vec2(1.0,1.0);
220 220

  
......
247 247

  
248 248
  v.x -= (mvXvert+mvXhorz);
249 249
  v.y -= (mvYvert+mvYhorz);
250

  
251 250
  v.z += force.z*d*d*(3.0*d*d -8.0*d +6.0);                          // thick bubble
252
  float b = -(12.0*force.z*d*(1.0-d)*(1.0-d)*(1.0-d))*one_over_denom;//
253

  
254
  n.xy += n.z*b*ps;
255 251
  }
256 252

  
257 253
//////////////////////////////////////////////////////////////////////////////////////////////
......
309 305
// Thus we actually want to compute N(v.x,v.y) = a*(-(dx/|PS|)*f'(|PX|), -(dy/|PS|)*f'(|PX|), 1) and keep adding
310 306
// the first two components. (a is the horizontal part)
311 307
        
312
void distort(in int effect, inout vec3 v, inout vec3 n)
308
void distort(in int effect, inout vec3 v)
313 309
  {
314 310
  vec2 center = vUniforms[effect+1].yz;
315 311
  vec2 ps = center-v.xy;
316 312
  vec3 force = vUniforms[effect].xyz;
317 313
  float d = degree(vUniforms[effect+2],center,ps);
318
  float denom = dot(ps+(1.0-d)*force.xy,ps);
319
  float one_over_denom = 1.0/(denom-0.001*(sign(denom)-1.0));          // = denom==0 ? 1000:1/denom;
320 314

  
321
  //v.z += force.z*d;                                                  // cone
322
  //b = -(force.z*(1.0-d))*one_over_denom;                             //
323
        
324
  //v.z += force.z*d*d*(3.0-2.0*d);                                    // thin bubble
325
  //b = -(6.0*force.z*d*(1.0-d)*(1.0-d))*one_over_denom;               //
326
        
327
  v.z += force.z*d*d*(3.0*d*d -8.0*d +6.0);                            // thick bubble
328
  float b = -(12.0*force.z*d*(1.0-d)*(1.0-d)*(1.0-d))*one_over_denom;  //
329
                
315
  //v.z += force.z*d;                       // cone
316
  //v.z += force.z*d*d*(3.0-2.0*d);         // thin bubble
317
  v.z += force.z*d*d*(3.0*d*d -8.0*d +6.0); // thick bubble
318

  
330 319
  v.xy += d*force.xy;
331
  n.xy += n.z*b*ps;
332 320
  }
333 321
 
334 322
//////////////////////////////////////////////////////////////////////////////////////////////
......
460 448
//
461 449
// Generally speaking I'd keep to amplitude < length, as the opposite case has some other problems as well.
462 450

  
463
void wave(in int effect, inout vec3 v, inout vec3 n)
451
void wave(in int effect, inout vec3 v)
464 452
  {
465 453
  vec2 center     = vUniforms[effect+1].yz;
466 454
  float amplitude = vUniforms[effect  ].x;
......
485 473
    vec3 dir= vec3(sinB*cosA,cosB*cosA,sinA);
486 474

  
487 475
    v += sin(angle)*deg*dir;
488

  
489
    if( n.z != 0.0 )
490
      {
491
      float sqrtX = sqrt(dir.y*dir.y + dir.z*dir.z);
492
      float sqrtY = sqrt(dir.x*dir.x + dir.z*dir.z);
493

  
494
      float sinX = ( sqrtY==0.0 ? 0.0 : dir.z / sqrtY);
495
      float cosX = ( sqrtY==0.0 ? 1.0 : dir.x / sqrtY);
496
      float sinY = ( sqrtX==0.0 ? 0.0 : dir.z / sqrtX);
497
      float cosY = ( sqrtX==0.0 ? 1.0 : dir.y / sqrtX);
498

  
499
      float abs_z = dir.z <0.0 ? -(sinX*sinY) : (sinX*sinY);
500

  
501
      float tmp = 1.578*cos(angle)*deg/length;
502

  
503
      float fx =-cosB*tmp;
504
      float fy = sinB*tmp;
505

  
506
      vec3 sx = vec3 (1.0+cosX*fx,cosY*sinX*fx,abs_z*fx);
507
      vec3 sy = vec3 (cosX*sinY*fy,1.0+cosY*fy,abs_z*fy);
508

  
509
      vec3 normal = cross(sx,sy);
510

  
511
      if( normal.z<=0.0 )                   // Why this bizarre shit rather than the straightforward
512
        {                                   //
513
        normal.x= 0.0;                      // if( normal.z>0.0 )
514
        normal.y= 0.0;                      //   {
515
        normal.z= 1.0;                      //   n.x = (n.x*normal.z + n.z*normal.x);
516
        }                                   //   n.y = (n.y*normal.z + n.z*normal.y);
517
                                            //   n.z = (n.z*normal.z);
518
                                            //   }
519
      n.x = (n.x*normal.z + n.z*normal.x);  //
520
      n.y = (n.y*normal.z + n.z*normal.y);  // ? Because if we do the above, my shitty Nexus4 crashes
521
      n.z = (n.z*normal.z);                 // during shader compilation!
522
      }
523 476
    }
524 477
  }
525 478

  
......
533 486
  vec3 n = a_Normal;
534 487

  
535 488
#if NUM_VERTEX>0
489

  
490
  vec3 b1;
491
  float len = n.x*n.x+n.y*n.y;
492
  float l = u_objD.x / 2.0;
493

  
494
  if( len>0.0 )
495
    {
496
    len = sqrt(len);
497
    b1 = vec3( n.y/len,-n.x/len,0.0);
498
    }
499
  else
500
    {
501
    b1 = vec3(1.0,0.0,0.0);
502
    }
503

  
504
  vec3 b2 = cross(n,b1);
505
  vec3 v1 = v+b1;
506
  vec3 v2 = v+b2;
507

  
536 508
  int j=0;
537 509

  
538 510
  for(int i=0; i<vNumEffects; i++)
539 511
    {
540
         if( vType[i]==DISTORT) distort(j,v,n);
541
    else if( vType[i]==DEFORM ) deform (j,v,n);
542
    else if( vType[i]==SINK   ) sink   (j,v);
543
    else if( vType[i]==PINCH  ) pinch  (j,v);
544
    else if( vType[i]==SWIRL  ) swirl  (j,v);
545
    else if( vType[i]==WAVE   ) wave   (j,v,n);
512
    if( vType[i]==DISTORT)
513
      {
514
      distort(j,v);
515
      distort(j,v1);
516
      distort(j,v2);
517
      }
518
    else if( vType[i]==DEFORM )
519
      {
520
      deform (j,v);
521
      deform (j,v1);
522
      deform (j,v2);
523
      }
524
    else if( vType[i]==SINK   )
525
      {
526
      sink   (j,v);
527
      sink   (j,v1);
528
      sink   (j,v2);
529
      }
530
    else if( vType[i]==PINCH  )
531
      {
532
      pinch  (j,v);
533
      pinch  (j,v1);
534
      pinch  (j,v2);
535
      }
536
    else if( vType[i]==SWIRL  )
537
      {
538
      swirl  (j,v);
539
      swirl  (j,v1);
540
      swirl  (j,v2);
541
      }
542
    else if( vType[i]==WAVE   )
543
      {
544
      wave   (j,v);
545
      wave   (j,v1);
546
      wave   (j,v2);
547
      }
546 548

  
547 549
    j+=3;
548 550
    }
549
 
551

  
552
  n = cross(v1-v,v2-v);
553

  
550 554
  restrictZ(v.z);
551 555
#endif
552 556
   

Also available in: Unified diff