Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2018
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.     static IndexedUVSphere() {
  2.         var yLen    = 32,
  3.             xLen    = 16,
  4.             radius  = 1.0;
  5.  
  6.         var aVert   = [],
  7.             aNorm   = [],
  8.             aUV     = [],
  9.             aIndex  = [];
  10.  
  11.         //...........................................
  12.         // Create Vertex and its Attributes (UV,Norms)
  13.         var xp, yp, zp, mag,
  14.             yRad    = Math.PI,              // Look Angles
  15.             xRad    = Math.PI * 2,
  16.             yInc    = Math.PI / yLen,       // Loop Increment
  17.             xInc    = Math.PI * 2 / xLen,
  18.             xUV     = 1 / xLen,             // UV Increment
  19.             yUV     = 1 / yLen;
  20.  
  21.         for(var x=0; x <= xLen; x++){
  22.             yRad = Math.PI;
  23.             for(var y=0; y <= yLen; y++){
  24.                 //---------------------------
  25.                 //Calculate the vertex position based on the polar coord
  26.                 xp = radius * Math.sin(yRad) * Math.cos(xRad);
  27.                 yp = radius * Math.cos(yRad);                  
  28.                 zp = radius * Math.sin(yRad) * Math.sin(xRad); // Y & Z are flipped.
  29.  
  30.                 aVert.push( xp, yp, zp );
  31.  
  32.                 //Calc the normal direction.
  33.                 mag = 1 / Math.sqrt( xp*xp + yp*yp + zp*zp );
  34.                 aNorm.push( xp*mag, yp*mag, zp*mag );
  35.                 //Fungi.debugLine.addRawLine(xp, yp, zp,0, xp*mag, yp*mag, zp*mag,0);
  36.  
  37.                 //---------------------------
  38.                 //Calc the vertex's UV value
  39.                 aUV.push(
  40.                     //(x < xLen) ? x * xUV : 1,
  41.                     //(y < yLen) ? y * yUV : 1
  42.                     x * xUV,
  43.                     y * yUV
  44.                 );
  45.                 //---------------------------
  46.                 //Move onto the next Latitude position
  47.                 yRad -= yInc;
  48.             }
  49.             xRad -= xInc;   //Move onto the next Longitude
  50.         }
  51.  
  52.         //...........................................
  53.         // Triangulate all the vertices for Triangle Strip
  54.         var iLen = (xLen)*(yLen+1);
  55.         for(var i=0; i < iLen; i++){
  56.             xp = Math.floor(i / (yLen+1));  //Current longitude
  57.             yp = i % (yLen+1);              //Current latitude
  58.  
  59.             //Column index of row R and R+1
  60.             aIndex.push(xp * (yLen+1) + yp, (xp+1) * (yLen+1) + yp);
  61.  
  62.             //Create Degenerate Triangle, Last AND first index of the R+1 (next row that becomes the top row )
  63.             if(yp == yLen && i < iLen-1) aIndex.push( (xp+1) * (yLen+1) + yp, (xp+1) * (yLen+1));
  64.         }
  65.  
  66.  
  67.         let model = {
  68.             verts:      aVert,
  69.             indices:    aIndex,
  70.             uvs:        aUV,
  71.             colors:     [],
  72.             normals:    aNorm,
  73.             tangents:   [],
  74.             bitangents: [],
  75.         };
  76.  
  77.         return model;
  78.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement