//---------------------------------------------------------------------------------------------------------------------------------------------------------- // Extract and mask image-piece to be used as puzzle-piece texture Texture2D ExtractFromImage(Texture2D _image, PuzzleElement _puzzlElement, int _x, int _y, int _elementBaseSize, Vector2 _elementSizeRatio) { // Get proper piece of image Color[] pixels = _image.GetPixels( (int)((_x * _elementBaseSize - _puzzlElement.pixelOffset.x) * _elementSizeRatio.x), (int)(_image.height - (_y + 1) * _elementBaseSize * _elementSizeRatio.y - _puzzlElement.pixelOffset.height * _elementSizeRatio.y), (int)(_puzzlElement.maskWidth * _elementSizeRatio.x), (int)(_puzzlElement.maskHeight * _elementSizeRatio.y) ); Texture2D result = new Texture2D( (int)(_puzzlElement.maskWidth * _elementSizeRatio.x), (int)(_puzzlElement.maskHeight * _elementSizeRatio.y) ); // Apply mask result.wrapMode = TextureWrapMode.Clamp; _puzzlElement.ApplyMask(pixels, ref result); return(result); }
//---------------------------------------------------------------------------------------------------------------------------------------------------------- // Generates example puzzle-piece/ to be shown as preview in Editor window Texture2D GenerateExamplePiece(Texture2D _subElement, int _elementBaseSize) { PuzzleElement puzzlePiece; // Calculate randomly which sub-element variants should be used int top = Random.Range(-1, 1) * 2 + 1; int left = Random.Range(-1, 1) * 2 + 1; int bottom = Random.Range(-1, 1) * 2 + 1; int right = Random.Range(-1, 1) * 2 + 1; // Prepare sub-element variants Color[] subElementPixels = _subElement.GetPixels(); Color[] topPixels = subElementPixels; Color[] leftPixels = TextureUtility.Rotate90(subElementPixels, _subElement.width, _subElement.height, false); // Prepare element mask puzzlePiece = new PuzzleElement ( top, left, bottom, right, _elementBaseSize, _subElement, topPixels, leftPixels ); // Create resultTexture puzzlePiece.texture = new Texture2D(puzzlePiece.maskWidth, puzzlePiece.maskHeight); Color[] piecePreviewColor = new Color [puzzlePiece.maskWidth * puzzlePiece.maskHeight]; for (int i = 0; i < piecePreviewColor.Length; i++) { piecePreviewColor[i] = Color.black; } puzzlePiece.ApplyMask(piecePreviewColor, ref puzzlePiece.texture); return(puzzlePiece.texture); }