surface
flame_base(float    Kfb = 1,
                    freq = 4,
                    radius = 0.4,
                    radius_mask = 0.3,
                    offset_radius = 0.01,
                    offset_radius_a = 0.0,
                    s1 = 0.5,
                    t1 = 0.5,
                    lump = 0.4,
                    outsideblur = 0.03,
                    insideblur = 0.1,
                    outsideblur_f = 0.05,
                    outsideblur_m = 0.05,
                    insideblur_m = 0.2,
                    mask_offset = 0.3,
                    a1 = 0.9,
                    a2 = 0.3,
                    b1 = 0.3,
                    b2 = 0.1;
            color    top = color(1.0,1.0,0.8),
                    bottom = color(1.0,0.6,0.0),
                    blueflame = color(0,0,1);
            string    spacename = "shader")
{
color    surfcolor = blueflame;
  
point     spaceP = transform(spacename, P);
color     transparency = float noise(spaceP * freq);
  
//blue
//point     objP = transform("shader", P);
//float    dist = distance(objP, point(0,0,0));
float    dist = sqrt((s-s1)*(s-s1) + (t-t1)*(t-t1));
float    noiseResult = (noise(P*lump) - 0.5) * 0.2;
float    R = radius + noiseResult;    
float    outsideclip = 1 - smoothstep(R, R + outsideblur, dist);
float    insideclip = smoothstep(R - insideblur, R, dist);
float    clip = outsideclip * insideclip;    
color    transparency_b = transparency * clip;
transparency_b = transparency_b * smoothstep (a2, a1, t);    /*top and bottom */    
transparency_b = transparency_b * ((smoothstep(0.5 + b2, 0.5+b1, s))+ 
                                (1 - smoothstep (0.5-b1, 0.5-b2, s)));    /*right and left*/
  
//flame
color    flamecolor = color(1,1,1);
float    R_flame = R - offset_radius;    
float    outsideclip_f = 1 - smoothstep(R_flame - outsideblur_f, R_flame, dist);
  
//arc
color    arccolor = mix(top, bottom, t);
  
float    R_arc = R - offset_radius *1.3;    
float    outsideclip_arc = 1 - smoothstep(R_arc - outsideblur_f, R_arc, dist);
  
//mask
float    R_mask = radius_mask + noiseResult;    
//float    dist_m = distance(objP, point(0,0,mask_offset));
float    dist_m = sqrt((s-s1)*(s-s1) + (t-(t1 + mask_offset))*(t-(t1 + mask_offset)));
float    insideclip_m = smoothstep(R_mask, R_mask + insideblur_m, dist_m);
  
flamecolor = mix(flamecolor, arccolor, 1-insideclip_m);
  
color    blueResult = transparency_b * surfcolor,
        arcResult = outsideclip_arc * arccolor * insideclip_m,
        flameResult = outsideclip_f * flamecolor * insideclip_m;
  
  
  
/* STEP 1 - set the apparent surface opacity */
Oi = Os * clamp((transparency_b + (outsideclip_arc * insideclip_m) + (outsideclip_f * insideclip_m)), color(0, 0, 0), color(1, 1, 1));
  
/* STEP 2 - calculate the apparent surface color */
Ci = Oi * Cs * Kfb * (blueResult + arcResult + flameResult);
//Ci = Oi * Cs * Kfb * (blueResult);
}