Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Creates a background texture
- // Color: White 0xFFFFFF
- private function createTexture():void
- {
- var bitmapData:BitmapData = new BitmapData(512, 512, false, 0xFFFFFF);
- createdPictureTexture = new Bitmap(bitmapData);
- loadPicture();
- }
- // Loads the photo
- private function loadPicture():void
- {
- var pictureLoader:Loader = new Loader();
- var pictureURLRequest:URLRequest = new URLRequest( PATH TO THE PHOTO );
- pictureLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, pictureComplete);
- pictureLoader.load(pictureURLRequest);
- }
- /**
- * Gets called when the photo has finished loading
- *
- * Determines what to do with it depending on the models material type (shader)
- **/
- private function pictureComplete(e:Event):void
- {
- picture = e.target.content as Bitmap;
- if(model.MaterialType == "Gouraud" || model.MaterialType == "FlatShade")
- {
- replaceWhiteBackgroundColor();
- }
- else if(model.MaterialType == "Phong")
- {
- mergeTextureWithPicture();
- }
- else
- {
- mergeTextureWithPicture();
- }
- }
- /**
- * Simple method that replaces white pixels with transparent pixels.
- * You might not need to call it. Depends on the results you are getting without it.
- *
- * Calls mergeTextureWithPicture() at the end
- **/
- private function replaceWhiteBackgroundColor():void
- {
- var sourceRect:Rectangle = new Rectangle(0, 0, picture.bitmapData.width, picture.bitmapData.height);
- var destPoint:Point = new Point();
- var operation:String = ">=";
- // The value against which each pixel is compared
- var threshold:uint = 0x00e0e1df;
- //var threshold:uint = 0x007b7e79;
- // The color that the pixels are set to that pass the threshold test
- var color:uint = 0xFFe8e8e8;
- // The exact opposite of color
- var mask:uint = 0x00ffffff;
- var copySource:Boolean = true;
- picture.bitmapData.threshold(picture.bitmapData,
- sourceRect,
- destPoint,
- operation,
- threshold,
- color,
- mask,
- copySource);
- mergeTextureWithPicture();
- }
- /**
- * 1. The merged bitmap data only contains the createdPictureTexture from above
- *
- * 2. Measure dimensions
- *
- * 3. Calculate the values neccessary for the matrix
- *
- * 4. Define the position matrix
- *
- * 5. Define the scale matrix (The picture might need to be scaled)
- *
- * 6. Concatenate the scale matrix with the position matrix
- *
- * 7. Draw the results of that concatenation "onto" mergedBitmapData
- *
- * Merged BitmapData has now the photo on a plain background which can be wrapped around the cup
- *
- **/
- private function mergeTextureWithPicture():void
- {
- var mergedBitmapData : BitmapData = new BitmapData(createdPictureTexture.width, createdPictureTexture.height, false, 0xff0000);
- mergedBitmapData.draw(createdPictureTexture);
- var pictureSize:Point = measurePictureSize();
- var mywidth:Number = createdPictureTexture.width - pictureSize.x;
- var onePercent:Number = mywidth / 100;
- var myheight:Number = createdPictureTexture.height - pictureSize.y;
- var onePercenty:Number = myheight / 100;
- var ix2:Number = onePercent * model.PicturePositionX;
- var ij2:Number = onePercenty * model.PicturePositionY;
- var positionMatrix:Matrix = new Matrix(1, 0, 0, 1, ix2, ij2);
- var scaleMatrix:Matrix = new Matrix();
- scaleMatrix.scale(model.PictureScaleX, model.PictureScaleY);
- scaleMatrix.concat(positionMatrix);
- mergedBitmapData.draw(picture, scaleMatrix);
- mergedTexture = new Bitmap(mergedBitmapData);
- dispatchEvent(new TextureCreatingEvent("complete"));
- }
- /**
- * Measures the size of the loaded image
- **/
- private function measurePictureSize():Point
- {
- var pictureSize:Point = new Point(0, 0);
- var bitmapData:BitmapData = new BitmapData(picture.width, picture.height);
- bitmapData.draw(picture);
- var bitmap:Bitmap = new Bitmap(bitmapData);
- bitmap.scaleX = model.PictureScaleX;
- bitmap.scaleY = model.PictureScaleY;
- pictureSize.x = bitmap.width;
- pictureSize.y = bitmap.height;
- return pictureSize;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement