       x=xy[tubeIndex]
       y=xy[tubeIndex+1]
 
-      #vectr from the previous point to this
+      #(dx,dy,dz) is an approximate tangent
       dx = xyNext[tubeIndex] - xyPrev[tubeIndex]
       dy = xyNext[tubeIndex+1] - xyPrev[tubeIndex+1] #y-y0
       #dz = @stepZ
+      iqxyz2 = 1.0 / ( dx*dx+dy*dy+dz*dz)
+      iqxyz = 1.0 / Math.sqrt( iqxyz2 ) #normalizing K
 
       #x0 = x
       #y0 = y
+
+      #main normal is calculated by projecting the last normal 
+      # pn X tangent:
+      tan = (x_pn*dx + y_pn*dy + z_pn*dz) * iqxyz2 #tangent component, scaled by iqxyz
+      xn1 = x_pn - tan * dx
+      yn1 = y_pn - tan * dy
+      zn1 = z_pn - tan * dz
+      #now normalize it
+      inorm1 = 1.0/Math.sqrt(xn1*xn1+yn1*yn1+zn1*zn1)
+      if isFinite inorm1
+        xn1 *= inorm1
+        yn1 *= inorm1
+        zn1 *= inorm1
+      else
+        xn1=1.0
+        yn1=0.0
+        zn1=0.0
+      
 
       #compute normals
       # noraml of form

      
    r = @tubeRadius

    #x0 = xys[0][i]
    #y0 = xys[0][i]
    dz = @stepZ


    # x, y pairs of the tube cross-section
    @tubeShape = nGonVertices (options.tubeSides ? 3), (options.tubeRadius ? 0.1)

    # Normals from the previous call
    @prevNormals = new Float32Array @nCells*3
    for i in [0...@nCells*3] by 3
      @prevNormals[i] = 1
      @prevNormals[i+1] = 0
      @prevNormals[i+2] = 0
    return
    
  #to make chunk of N segments, we need n+3 states.
  # before-first, first, ... , last, after-last.
