devNotes 1-7-2017 Stylus with Vector Fields

RING TONE

 

 


#pragma kernel CSMain
//FOR STYLUS
// Thread group size 
#define thread_group_size_x 32
#define thread_group_size_y 1
#define thread_group_size_z 1

struct Particle 
{
    float3      position;  
    float3      velocity; 
    float       speed;
    float3      dV_nTangent;
    float       dV_nTangent_Mag;
    float3      width_nAxis;
};

int nStylus_1_On;
int nStylus_2_On;
int num_ATT_Stylus;

float targetStrength;								
float deltaTime;
float damping;
float outer_rad_mult;
float zero_mirror_radius;

float3 styl_1_0;    
float3 styl_1_1;
float3 styl_1_2;
float3 styl_1_3;

float3 styl_1_4;
float3 styl_1_5;
float3 styl_1_6;
float3 styl_1_7;

float3 styl_1_8;
float3 styl_1_9;
float3 styl_1_10;
float3 styl_1_11;

//--------------------------------

float3 styl_2_0;    
float3 styl_2_1;
float3 styl_2_2;
float3 styl_2_3;

float3 styl_2_4;
float3 styl_2_5;
float3 styl_2_6;
float3 styl_2_7;

float3 styl_2_8;
float3 styl_2_9;
float3 styl_2_10;
float3 styl_2_11;     

//-----------------------------------

float rad_1_0;
float rad_1_1;
float rad_1_2;
float rad_1_3;

float rad_1_4;
float rad_1_5;
float rad_1_6;
float rad_1_7;

float rad_1_8;
float rad_1_9;
float rad_1_10;
float rad_1_11;
  
//-----------------------------------
                           
float rad_2_0;
float rad_2_1;
float rad_2_2;
float rad_2_3;

float rad_2_4;
float rad_2_5;
float rad_2_6;
float rad_2_7;

float rad_2_8;
float rad_2_9;
float rad_2_10;
float rad_2_11;

//-----------------------------------

RWStructuredBuffer <Particle> particles;

[numthreads(thread_group_size_x, thread_group_size_y, thread_group_size_z )]

void CSMain ( uint3 Gid : SV_GroupID, uint3 DTid : SV_DispatchThreadID, uint3 GTid : SV_GroupThreadID, uint GI : SV_GroupIndex )        
{
    int index = DTid.x;
    //int index = DTid.x + DTid.y * thread_group_size_x * 32;
    bool bEdited = false;

    float3 velDeref;
    velDeref = particles[index].velocity;
    if(nStylus_1_On == 1) //FOR STYLUS 1
    {
        float3 nDir;
	    float  fDist;
        float Or;
        float linEase;
        
        nDir = normalize(styl_1_0 - particles[index].position);
	    fDist  = distance(styl_1_0, particles[index].position);
        Or = (1+outer_rad_mult)*rad_1_0;

        if(fDist < Or)
        {
            linEase = (fDist - Or)/(rad_1_0 - Or);
            velDeref += targetStrength * nDir * deltaTime * linEase;
        }
        
        nDir = normalize(styl_1_1 - particles[index].position);
	    fDist  = distance(styl_1_1, particles[index].position);

        Or = (1+outer_rad_mult)*rad_1_1;
        if(fDist < Or)
        {
            linEase = (fDist - Or)/(rad_1_1 - Or);
            velDeref += targetStrength * nDir * deltaTime * linEase;
            bEdited = true;
        }

        nDir = normalize(styl_1_2 - particles[index].position);
	    fDist  = distance(styl_1_2, particles[index].position);

        Or = (1+outer_rad_mult)*rad_1_2;
        if(fDist < Or)
        {
            linEase = (fDist - Or)/(rad_1_2 - Or);
            velDeref += targetStrength * nDir * deltaTime * linEase;
            bEdited = true;
        }

        nDir = normalize(styl_1_3 - particles[index].position);
	    fDist  = distance(styl_1_3, particles[index].position);

        Or = (1+outer_rad_mult)*rad_1_3;
        if(fDist < Or)
        {
            linEase = (fDist - Or)/(rad_1_3 - Or);
            velDeref += targetStrength * nDir * deltaTime * linEase;
            bEdited = true;
        }

        nDir = normalize(styl_1_4 - particles[index].position);
	    fDist  = distance(styl_1_4, particles[index].position);

        Or = (1+outer_rad_mult)*rad_1_4;
        if(fDist < Or)
        {
            linEase = (fDist - Or)/(rad_1_4 - Or);
            velDeref += targetStrength * nDir * deltaTime * linEase;
            bEdited = true;
        }

        nDir = normalize(styl_1_5 - particles[index].position);
	    fDist  = distance(styl_1_5, particles[index].position);

        Or = (1+outer_rad_mult)*rad_1_5;
        if(fDist < Or)
        {
            linEase = (fDist - Or)/(rad_1_5 - Or);
            velDeref += targetStrength * nDir * deltaTime * linEase;
            bEdited = true;
        }

        nDir = normalize(styl_1_6 - particles[index].position);
	    fDist  = distance(styl_1_6, particles[index].position);

        Or = (1+outer_rad_mult)*rad_1_6;
        if(fDist < Or)
        {
            linEase = (fDist - Or)/(rad_1_6 - Or);
            velDeref += targetStrength * nDir * deltaTime * linEase;
            bEdited = true;
        }     
        
        nDir = normalize(styl_1_7 - particles[index].position);
	    fDist  = distance(styl_1_7, particles[index].position);

        Or = (1+outer_rad_mult)*rad_1_7;
        if(fDist < Or)
        {
            linEase = (fDist - Or)/(rad_1_7 - Or);
            velDeref += targetStrength * nDir * deltaTime * linEase;
            bEdited = true;
        }

        nDir = normalize(styl_1_8 - particles[index].position);
	    fDist  = distance(styl_1_8, particles[index].position);

        Or = (1+outer_rad_mult)*rad_1_8;
        if(fDist < Or)
        {
            linEase = (fDist - Or)/(rad_1_8 - Or);
            velDeref += targetStrength * nDir * deltaTime * linEase;
            bEdited = true;
        }

        nDir = normalize(styl_1_9 - particles[index].position);
	    fDist  = distance(styl_1_9, particles[index].position);

        Or = (1+outer_rad_mult)*rad_1_9;
        if(fDist < Or)
        {
            linEase = (fDist - Or)/(rad_1_9 - Or);
            velDeref += targetStrength * nDir * deltaTime * linEase;
            bEdited = true;
        }

        nDir = normalize(styl_1_10 - particles[index].position);
	    fDist  = distance(styl_1_10, particles[index].position);

        Or = (1+outer_rad_mult)*rad_1_10;
        if(fDist < Or)
        {
            linEase = (fDist - Or)/(rad_1_10 - Or);
            velDeref += targetStrength * nDir * deltaTime * linEase;
            bEdited = true;
        }

        nDir = normalize(styl_1_11 - particles[index].position);
	    fDist  = distance(styl_1_11, particles[index].position);

        Or = (1+outer_rad_mult)*rad_1_11;
        if(fDist < Or)
        {
            linEase = (fDist - Or)/(rad_1_11 - Or);
            velDeref += targetStrength * nDir * deltaTime * linEase;
            bEdited = true;
        }
    }
    if(nStylus_2_On == 1) //FOR STYLUS 2
    {
        float3 nDir;
	    float  fDist;
        float  Or;
        float  linEase;
        
        
        nDir = normalize(styl_2_0 - particles[index].position);
	    fDist  = distance(styl_2_0, particles[index].position);

        Or = (1+outer_rad_mult)*rad_2_0;
        if(fDist < Or)
        {
            linEase = (fDist - Or)/(rad_2_0 - Or);
            velDeref += targetStrength * nDir * deltaTime * linEase;
            bEdited = true;
        }
        
        nDir = normalize(styl_2_1 - particles[index].position);
	    fDist  = distance(styl_2_1, particles[index].position);

        Or = (1+outer_rad_mult)*rad_2_1;
        if(fDist < Or)
        {
            linEase = (fDist - Or)/(rad_2_1 - Or);
            velDeref += targetStrength * nDir * deltaTime * linEase;
            bEdited = true;
        }

        nDir = normalize(styl_2_2 - particles[index].position);
	    fDist  = distance(styl_2_2, particles[index].position);

        Or = (1+outer_rad_mult)*rad_2_2;
        if(fDist < Or)
        {
            linEase = (fDist - Or)/(rad_2_2 - Or);
            velDeref += targetStrength * nDir * deltaTime * linEase;
            bEdited = true;
        }

        nDir = normalize(styl_2_3 - particles[index].position);
	    fDist  = distance(styl_2_3, particles[index].position);

        Or = (1+outer_rad_mult)*rad_2_3;
        if(fDist < Or)
        {
            linEase = (fDist - Or)/(rad_2_3 - Or);
            velDeref += targetStrength * nDir * deltaTime * linEase;
            bEdited = true;
        }

        nDir = normalize(styl_2_4 - particles[index].position);
	    fDist  = distance(styl_2_4, particles[index].position);

        Or = (1+outer_rad_mult)*rad_2_4;
        if(fDist < Or)
        {
            linEase = (fDist - Or)/(rad_2_4 - Or);
            velDeref += targetStrength * nDir * deltaTime * linEase;
            bEdited = true;
        }

        nDir = normalize(styl_2_5 - particles[index].position);
	    fDist  = distance(styl_2_5, particles[index].position);

        Or = (1+outer_rad_mult)*rad_2_5;
        if(fDist < Or)
        {
            linEase = (fDist - Or)/(rad_2_5 - Or);
            velDeref += targetStrength * nDir * deltaTime * linEase;
            bEdited = true;
        }

        nDir = normalize(styl_2_6 - particles[index].position);
	    fDist  = distance(styl_2_6, particles[index].position);

        Or = (1+outer_rad_mult)*rad_2_6;
        if(fDist < Or)
        {
            linEase = (fDist - Or)/(rad_2_6 - Or);
            velDeref += targetStrength * nDir * deltaTime * linEase;
            bEdited = true;
        }      
        
        nDir = normalize(styl_2_7 - particles[index].position);
	    fDist  = distance(styl_2_7, particles[index].position);

        Or = (1+outer_rad_mult)*rad_2_7;
        if(fDist < Or)
        {
            linEase = (fDist - Or)/(rad_2_7 - Or);
            velDeref += targetStrength * nDir * deltaTime * linEase;
            bEdited = true;
        }

        nDir = normalize(styl_2_8 - particles[index].position);
	    fDist  = distance(styl_2_8, particles[index].position);

        Or = (1+outer_rad_mult)*rad_2_8;
        if(fDist < Or)
        {
            linEase = (fDist - Or)/(rad_2_8 - Or);
            velDeref += targetStrength * nDir * deltaTime * linEase;
            bEdited = true;
        }

        nDir = normalize(styl_2_9 - particles[index].position);
	    fDist  = distance(styl_2_9, particles[index].position);

        Or = (1+outer_rad_mult)*rad_2_9;
        if(fDist < Or)
        {
            linEase = (fDist - Or)/(rad_2_9 - Or);
            velDeref += targetStrength * nDir * deltaTime * linEase;
            bEdited = true;
        }

        nDir = normalize(styl_2_10 - particles[index].position);
	    fDist  = distance(styl_2_10, particles[index].position);

        Or = (1+outer_rad_mult)*rad_2_10;
        if(fDist < Or)
        {
            linEase = (fDist - Or)/(rad_2_10 - Or);
            velDeref += targetStrength * nDir * deltaTime * linEase;
            bEdited = true;
        }

        nDir = normalize(styl_2_11 - particles[index].position);
	    fDist  = distance(styl_2_11, particles[index].position);

        Or = (1+outer_rad_mult)*rad_2_11;
        if(fDist < Or)
        {
            linEase = (fDist - Or)/(rad_2_11 - Or);
            velDeref += targetStrength * nDir * deltaTime * linEase;
            bEdited = true;
        }
    }

    float3 vUp;
    float3 vRight;
    float3 vFwd;

    vUp.x = 0;
    vUp.y = 1;
    vUp.z = 0;

    vRight.x = 1;
    vRight.y = 0;
    vRight.z = 0;

    vFwd.x = 0;
    vFwd.y = 0;
    vFwd.z = 1;

    //float3 dV = velDeref - particles[index].velocity;
    float3 newVelNorm = vUp;
    //float3 dvAxial = dot(dV,newVelNorm)*newVelNorm;
    //float3 dV_Tangent = dV-dvAxial;
    particles[index].dV_nTangent = vRight;
    particles[index].dV_nTangent_Mag = 1;
    particles[index].width_nAxis = vFwd;
      
    particles[index].velocity = velDeref * damping + 0.000001*vUp;
	particles[index].position += particles[index].velocity * deltaTime;

    if(bEdited)
    {
        particles[index].speed = length(particles[index].velocity) + 0.000001;
    }
}

 

CrrBTU1UEAAi71l