Advertisement
Guest User

Untitled

a guest
Aug 18th, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Creates a background texture
  2. // Color: White 0xFFFFFF
  3. private function createTexture():void
  4.         {
  5.              var bitmapData:BitmapData = new BitmapData(512, 512, false, 0xFFFFFF);
  6.             createdPictureTexture = new Bitmap(bitmapData);
  7.            
  8.             loadPicture();
  9.         }
  10.  
  11.  
  12. // Loads the photo
  13.         private function loadPicture():void
  14.         {
  15.             var pictureLoader:Loader = new Loader();
  16.             var pictureURLRequest:URLRequest = new URLRequest( PATH TO THE PHOTO );
  17.             pictureLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, pictureComplete);
  18.             pictureLoader.load(pictureURLRequest);
  19.         }
  20.  
  21.         /**
  22.          * Gets called when the photo has finished loading
  23.          *
  24.          * Determines what to do with it depending on the models material type (shader)
  25.          **/
  26.         private function pictureComplete(e:Event):void
  27.         {
  28.             picture = e.target.content as Bitmap;
  29.            
  30.             if(model.MaterialType == "Gouraud" || model.MaterialType == "FlatShade")
  31.             {
  32.                 replaceWhiteBackgroundColor(); 
  33.             }
  34.             else if(model.MaterialType == "Phong")
  35.             {
  36.                 mergeTextureWithPicture();
  37.             }
  38.             else
  39.             {
  40.                 mergeTextureWithPicture();
  41.             }
  42.            
  43.         }
  44.  
  45.  
  46. /**
  47.          * Simple method that replaces white pixels with transparent pixels.
  48.          * You might not need to call it. Depends on the results you are getting without it.
  49.          *
  50.          * Calls mergeTextureWithPicture() at the end
  51.          **/
  52.         private function replaceWhiteBackgroundColor():void
  53.         {
  54.             var sourceRect:Rectangle = new Rectangle(0, 0, picture.bitmapData.width, picture.bitmapData.height);
  55.             var destPoint:Point = new Point();
  56.             var operation:String = ">=";
  57.            
  58.             // The value against which each pixel is compared
  59.             var threshold:uint = 0x00e0e1df;
  60.             //var threshold:uint = 0x007b7e79;
  61.            
  62.             // The color that the pixels are set to that pass the threshold test
  63.             var color:uint = 0xFFe8e8e8;
  64.            
  65.             // The exact opposite of color
  66.             var mask:uint = 0x00ffffff;
  67.             var copySource:Boolean = true;
  68.  
  69.             picture.bitmapData.threshold(picture.bitmapData,
  70.                                  sourceRect,
  71.                                  destPoint,
  72.                                  operation,
  73.                                  threshold,
  74.                                  color,
  75.                                  mask,
  76.                                  copySource);
  77.                                  
  78.             mergeTextureWithPicture();
  79.            
  80.         }
  81.  
  82. /**
  83.          * 1. The merged bitmap data only contains the createdPictureTexture from above
  84.          *
  85.          * 2. Measure dimensions
  86.          *
  87.          * 3. Calculate the values neccessary for the matrix
  88.          *
  89.          * 4. Define the position matrix
  90.          *
  91.          * 5. Define the scale matrix (The picture might need to be scaled)
  92.          *
  93.          * 6. Concatenate the scale matrix with the position matrix
  94.          *
  95.          * 7. Draw the results of that concatenation "onto" mergedBitmapData
  96.          *
  97.          * Merged BitmapData has now the photo on a plain background which can be wrapped around the cup
  98.          *
  99.          **/
  100.         private function mergeTextureWithPicture():void
  101.         {
  102.             var mergedBitmapData : BitmapData = new BitmapData(createdPictureTexture.width, createdPictureTexture.height, false, 0xff0000);
  103.             mergedBitmapData.draw(createdPictureTexture);
  104.            
  105.            
  106.             var pictureSize:Point = measurePictureSize();
  107.            
  108.             var mywidth:Number = createdPictureTexture.width - pictureSize.x;
  109.             var onePercent:Number = mywidth / 100;
  110.            
  111.             var myheight:Number = createdPictureTexture.height - pictureSize.y;
  112.             var onePercenty:Number = myheight / 100;
  113.            
  114.             var ix2:Number = onePercent * model.PicturePositionX;
  115.             var ij2:Number = onePercenty * model.PicturePositionY;
  116.            
  117.             var positionMatrix:Matrix = new Matrix(1, 0, 0, 1, ix2, ij2);
  118.            
  119.             var scaleMatrix:Matrix = new Matrix();
  120.             scaleMatrix.scale(model.PictureScaleX, model.PictureScaleY);
  121.             scaleMatrix.concat(positionMatrix);
  122.            
  123.             mergedBitmapData.draw(picture, scaleMatrix);
  124.            
  125.            
  126.             mergedTexture = new Bitmap(mergedBitmapData);
  127.  
  128.            
  129.             dispatchEvent(new TextureCreatingEvent("complete"));
  130.         }
  131.  
  132.  
  133. /**
  134.          * Measures the size of the loaded image
  135.          **/
  136.         private function measurePictureSize():Point
  137.         {
  138.             var pictureSize:Point = new Point(0, 0);
  139.                        
  140.             var bitmapData:BitmapData = new BitmapData(picture.width, picture.height);
  141.             bitmapData.draw(picture);
  142.            
  143.             var bitmap:Bitmap = new Bitmap(bitmapData);
  144.            
  145.             bitmap.scaleX = model.PictureScaleX;
  146.             bitmap.scaleY = model.PictureScaleY;
  147.            
  148.             pictureSize.x = bitmap.width;
  149.             pictureSize.y = bitmap.height;
  150.            
  151.            
  152.             return pictureSize;
  153.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement