public static exSprite NewSlicedSprite(GameObject _go, exTextureInfo _info, int _left, int _right, int _top, int _bottom, int _width, int _height, Color _color, bool _borderOnly) { exSprite sprite = _go.GetComponent <exSprite>(); if (sprite == null) { sprite = _go.AddComponent <exSprite>(); } if (sprite.shader == null) { sprite.shader = Shader.Find("ex2D/Alpha Blended"); } sprite.spriteType = exSpriteType.Sliced; sprite.textureInfo = _info; sprite.borderOnly = _borderOnly; sprite.customBorderSize = true; sprite.leftBorderSize = _left; sprite.rightBorderSize = _right; sprite.topBorderSize = _top; sprite.bottomBorderSize = _bottom; sprite.customSize = true; sprite.width = _width; sprite.height = _height; sprite.color = _color; return(sprite); }
/////////////////////////////////////////////////////////////////////////////// // texture /////////////////////////////////////////////////////////////////////////////// // ------------------------------------------------------------------ // Desc: // ------------------------------------------------------------------ public static float CalculateTextureInfoScale(Rect _rect, exTextureInfo _textureInfo) { float scale = 1.0f; if (_textureInfo == null) { return(scale); } float width = _textureInfo.width; float height = _textureInfo.height; // confirm the scale, width and height if (width > _rect.width && height > _rect.height) { scale = Mathf.Min(_rect.width / width, _rect.height / height); } else if (width > _rect.width) { scale = _rect.width / width; } else if (height > _rect.height) { scale = _rect.height / height; } return(scale); }
// ------------------------------------------------------------------ // Desc: // ------------------------------------------------------------------ void OnInspectorUpdate() { if (curEdit == null) { return; } for (int i = curEdit.textureInfos.Count - 1; i >= 0; --i) { exTextureInfo textureInfo = curEdit.textureInfos[i]; if (textureInfo == null) { curEdit.textureInfos.RemoveAt(i); } } for (int i = curEdit.bitmapFonts.Count - 1; i >= 0; --i) { exBitmapFont bitmapFont = curEdit.bitmapFonts[i]; if (bitmapFont == null) { curEdit.bitmapFonts.RemoveAt(i); } } for (int i = selectedObjects.Count - 1; i >= 0; --i) { Object obj = selectedObjects[i]; if (obj == null) { selectedObjects.RemoveAt(i); } } Repaint(); }
// ------------------------------------------------------------------ // Desc: // ------------------------------------------------------------------ public override string GetInfoString() { exTextureInfo textureInfo = base.target as exTextureInfo; string text = textureInfo.width.ToString() + "x" + textureInfo.height.ToString(); return(text); }
public static void AddDiceData(exTextureInfo _textureInfo, List <int> _diceData, exTextureInfo.Dice _dice) { _diceData.Add(_dice.offset_x); _diceData.Add(_dice.offset_y); _diceData.Add(_dice.rotated ? -_dice.width : _dice.width); _diceData.Add(_dice.height); _diceData.Add(_dice.x); _diceData.Add(_dice.y); //Debug.Log("rect " + _rect + " " + _x + " " + _y); /* 屏蔽数据压缩 * if ( _dice.width <= 0 || _dice.height <= 0 ) { * _diceData.Add( DiceEnumerator.EMPTY ); * } * else { * if ( _dice.width == _textureInfo.diceUnitWidth && _dice.height == _textureInfo.diceUnitHeight ) { * _diceData.Add( _dice.rotated ? DiceEnumerator.MAX_ROTATED : DiceEnumerator.MAX ); * // TODO: use y instead of this flag * } * else { * _diceData.Add( _dice.offset_x ); * _diceData.Add( _dice.offset_y ); * _diceData.Add( _dice.rotated ? - _dice.width : _dice.width ); * _diceData.Add( _dice.height ); * } * _diceData.Add( _dice.x ); * _diceData.Add( _dice.y ); * } */ }
// ------------------------------------------------------------------ // Desc: // ------------------------------------------------------------------ void UpdateSelection() { if (curEdit != null) { bool needRepaint = false; selectedObjects.Clear(); foreach (Object obj in Selection.objects) { exTextureInfo textureInfo = obj as exTextureInfo; if (textureInfo != null && curEdit.textureInfos.IndexOf(textureInfo) != -1) { selectedObjects.Add(textureInfo); needRepaint = true; } exBitmapFont bitmapFont = obj as exBitmapFont; if (bitmapFont != null && curEdit.bitmapFonts.IndexOf(bitmapFont) != -1) { selectedObjects.Add(bitmapFont); needRepaint = true; } } if (needRepaint) { Repaint(); } } }
static void Create_TextureInfo_FromSelected() { if (Selection.objects.Length == 0) { return; } List <Object> nextSelections = new List <Object>(); foreach (Object obj in Selection.objects) { Texture2D rawTexture = obj as Texture2D; // check if this is a font info if (rawTexture == null) { Debug.LogError("You can only create texture-info from selected texture"); return; } string rawTexturePath = AssetDatabase.GetAssetPath(rawTexture); string dirPath = Path.GetDirectoryName(rawTexturePath); exEditorUtility.ImportTextureForAtlas(rawTexture); exTextureInfo textureInfo = exGenericAssetUtility <exTextureInfo> .LoadExistsOrCreate(dirPath, rawTexture.name); textureInfo.rawTextureGUID = exEditorUtility.AssetToGUID(rawTexture); textureInfo.rawAtlasGUID = exEditorUtility.AssetToGUID(rawTexture); textureInfo.texture = rawTexture; textureInfo.rotated = false; textureInfo.trim = true; textureInfo.trimThreshold = 1; Rect trimRect = exTextureUtility.GetTrimTextureRect(rawTexture, 1, new Rect(0, 0, rawTexture.width, rawTexture.height)); if (trimRect.width <= 0 || trimRect.height <= 0) { textureInfo.trim = false; trimRect = new Rect(0, 0, rawTexture.width, rawTexture.height); } textureInfo.trim_x = (int)trimRect.x; textureInfo.trim_y = (int)trimRect.y; textureInfo.width = (int)trimRect.width; textureInfo.height = (int)trimRect.height; textureInfo.x = (int)trimRect.x; textureInfo.y = (int)trimRect.y; textureInfo.rawWidth = rawTexture.width; textureInfo.rawHeight = rawTexture.height; EditorUtility.SetDirty(textureInfo); AssetDatabase.ImportAsset(AssetDatabase.GetAssetPath(textureInfo), ImportAssetOptions.ForceSynchronousImport); AssetDatabase.Refresh(); nextSelections.Add(textureInfo); } Selection.objects = nextSelections.ToArray(); Selection.activeObject = nextSelections[0]; }
/////////////////////////////////////////////////////////////////////////////// // Internal Functions /////////////////////////////////////////////////////////////////////////////// // ------------------------------------------------------------------ // Desc: // ------------------------------------------------------------------ void Init() { bool initialized = (nameToState != null); if (initialized == false) { sprite_ = GetComponent <exSprite>(); defaultTextureInfo = sprite_.textureInfo; nameToState = new Dictionary <string, exSpriteAnimationState>(); for (int i = 0; i < animations.Count; ++i) { exSpriteAnimationClip clip = animations[i]; if (clip != null) { exSpriteAnimationState state = new exSpriteAnimationState(clip); nameToState[state.name] = state; if (ReferenceEquals(defaultAnimation, clip)) { curAnimation = state; lastFrameIndex = -1; } } } exDebug.Assert(defaultAnimation == null || defaultAnimation == nameToState[defaultAnimation.name].clip); } }
// ------------------------------------------------------------------ // Desc: // ------------------------------------------------------------------ void ProcessEvents() { int controlID = GUIUtility.GetControlID(exAtlasEditorHash, FocusType.Passive); Event e = Event.current; switch (e.GetTypeForControl(controlID)) { case EventType.ScrollWheel: if (e.control) { curEdit.scale += -e.delta.y * 0.1f; Repaint(); e.Use(); } break; case EventType.KeyDown: if (e.keyCode == KeyCode.Backspace || e.keyCode == KeyCode.Delete) { if (selectedObjects.Count > 0) { AssetDatabase.StartAssetEditing(); foreach (Object obj in selectedObjects) { exTextureInfo textureInfo = obj as exTextureInfo; if (textureInfo) { int i = curEdit.textureInfos.IndexOf(textureInfo); if (i != -1) { curEdit.textureInfos.RemoveAt(i); curEdit.needRebuild = true; AssetDatabase.DeleteAsset(AssetDatabase.GetAssetPath(textureInfo)); } } exBitmapFont bitmapFont = obj as exBitmapFont; if (bitmapFont) { int i = curEdit.bitmapFonts.IndexOf(bitmapFont); if (i != -1) { curEdit.bitmapFonts.RemoveAt(i); curEdit.needRebuild = true; AssetDatabase.DeleteAsset(AssetDatabase.GetAssetPath(bitmapFont)); } } } AssetDatabase.ImportAsset(AssetDatabase.GetAssetPath(curEdit)); AssetDatabase.StopAssetEditing(); selectedObjects.Clear(); Repaint(); e.Use(); } } break; } }
// ------------------------------------------------------------------ // Desc: // ------------------------------------------------------------------ public static void GUI_DrawRawTextureInfo(Rect _rect, exTextureInfo _textureInfo, Color _color, float _scale = 1.0f, bool _useTextureOffset = false) { // check texture info if (_textureInfo == null) { return; } // check raw/atlas texture Texture2D texture = exEditorUtility.LoadAssetFromGUID <Texture2D>(_textureInfo.rawTextureGUID); if (texture == null) { return; } // calculate uv Rect uv = new Rect((float)_textureInfo.trim_x / (float)texture.width, (float)_textureInfo.trim_y / (float)texture.height, (float)_textureInfo.width / (float)texture.width, (float)_textureInfo.height / (float)texture.height); float width = _textureInfo.width * _scale; float height = _textureInfo.height * _scale; float offsetX = 0.0f; float offsetY = 0.0f; if (_useTextureOffset) { offsetX = (_textureInfo.rawWidth - _textureInfo.width) * 0.5f - _textureInfo.trim_x; offsetX *= _scale; offsetY = (_textureInfo.rawHeight - _textureInfo.height) * 0.5f - _textureInfo.trim_y; offsetY *= _scale; } // Rect pos = new Rect(_rect.center.x - width * 0.5f - offsetX, _rect.center.y - height * 0.5f + offsetY, width, height); // draw the texture Color old = GUI.color; GUI.color = _color; GUI.DrawTextureWithTexCoords(pos, texture, uv); GUI.color = old; // DEBUG { // exEditorUtility.GUI_DrawRectBorder ( _rect, Color.white ); // } DEBUG end }
/////////////////////////////////////////////////////////////////////////////// // functions /////////////////////////////////////////////////////////////////////////////// // ------------------------------------------------------------------ // Desc: // ------------------------------------------------------------------ void UpdateEditObject() { exTextureInfo info = Selection.activeObject as exTextureInfo; if (info != null && info != curEdit) { Edit(info); } }
// ------------------------------------------------------------------ /// \param _obj /// Check if the object is valid atlas and open it in atlas editor. // ------------------------------------------------------------------ public void Edit( exTextureInfo _info ) { if ( _info == null ) return; curEdit = _info; Reset (); Repaint (); }
static void Postfix(string Path, ref exTextureInfo __result) { //This postfix assumes that it is always running on the UI (Unity) thread. if (__result == null && Path.EndsWith(Constants.FlippedTileSuffix)) { if (TextureMaker.MakeFlippedTexture(Path, out exTextureInfo result)) { __result = result; } } }
// ------------------------------------------------------------------ /// \param _obj /// Check if the object is valid atlas and open it in atlas editor. // ------------------------------------------------------------------ public void Edit(exTextureInfo _info) { if (_info == null) { return; } curEdit = _info; Reset(); Repaint(); }
/////////////////////////////////////////////////////////////////////////////// // static /////////////////////////////////////////////////////////////////////////////// // ------------------------------------------------------------------ // Desc: // ------------------------------------------------------------------ public exTextureInfo GetTextureInfoByName(string _name) { for (int i = 0; i < textureInfos.Count; ++i) { exTextureInfo textureInfo = textureInfos[i]; if (textureInfo.name == _name) { return(textureInfo); } } return(null); }
// ------------------------------------------------------------------ // Desc: // ------------------------------------------------------------------ Rect MapTextureInfo(Rect _atlasRect, exTextureInfo _textureInfo) { Rect rect = new Rect(_textureInfo.x * curEdit.scale, _textureInfo.y * curEdit.scale, _textureInfo.rotatedWidth * curEdit.scale, _textureInfo.rotatedHeight * curEdit.scale); rect.x = _atlasRect.x + rect.x; rect.y = _atlasRect.y + _atlasRect.height - rect.y - rect.height; rect = exGeometryUtility.Rect_FloorToInt(rect); return(rect); }
public DiceEnumerator(List <int> _diceData, exTextureInfo _textureInfo) { diceData = _diceData; diceUnitWidth = _diceData[0]; diceUnitHeight = _diceData[1]; dataIndex = -1; #if UNITY_EDITOR textureInfo = _textureInfo; int rowCount; exSpriteUtility.GetDicingCount(textureInfo, out columnCount, out rowCount); diceIndex = -1; #endif Reset(); }
public static void GetTilingCount(exISprite _sprite, out int _colCount, out int _rowCount) { exTextureInfo ti = _sprite.textureInfo; if (ti != null && ti.width + _sprite.tiledSpacing.x != 0 && ti.height + _sprite.tiledSpacing.y != 0) { _colCount = Mathf.Max(Mathf.CeilToInt(_sprite.width / (ti.width + _sprite.tiledSpacing.x)), 1); _rowCount = Mathf.Max(Mathf.CeilToInt(_sprite.height / (ti.height + _sprite.tiledSpacing.y)), 1); } else { _colCount = 1; _rowCount = 1; } }
public static void SetTextureInfo(exSpriteBase _sprite, ref exTextureInfo _ti, exTextureInfo _newTi, bool _useTextureOffset, exSpriteType _spriteType) { exTextureInfo old = _ti; _ti = _newTi; if (_newTi != null) { if (_newTi.texture == null) { Debug.LogWarning("invalid textureInfo"); } if (_spriteType == exSpriteType.Tiled) { if (old == null || ReferenceEquals(old, _newTi) || _newTi.width != old.width || _newTi.height != old.height) { (_sprite as exISprite).UpdateBufferSize(); _sprite.updateFlags |= exUpdateFlags.Vertex; // tile数量可能不变,但是间距可能会改变 } } else if (_spriteType == exSpriteType.Diced) { //if (_newTi.isDiced == false) { // Debug.LogWarning ("The texture info is not diced!"); //} (_sprite as exISprite).UpdateBufferSize(); _sprite.updateFlags |= exUpdateFlags.Vertex; } else { if (_sprite.customSize == false && (old == null || (_newTi.width != old.width || _newTi.height != old.height))) { _sprite.updateFlags |= exUpdateFlags.Vertex; } } if (_useTextureOffset) { _sprite.updateFlags |= exUpdateFlags.Vertex; } _sprite.updateFlags |= exUpdateFlags.UV; // 换了texture,UV也会重算,不换texture就更要改UV,否则没有换textureInfo的必要了。 if (old == null || ReferenceEquals(old.texture, _newTi.texture) == false) { // texture changed _sprite.updateFlags |= (exUpdateFlags.Vertex | exUpdateFlags.UV); (_sprite as exISprite).UpdateMaterial(); } } }
public static void GetDicingCount(exTextureInfo _ti, out int _colCount, out int _rowCount) { _colCount = 1; _rowCount = 1; if (_ti != null) { if (_ti.diceUnitWidth > 0 && _ti.width > 0) { _colCount = Mathf.CeilToInt((float)_ti.width / _ti.diceUnitWidth); } if (_ti.diceUnitHeight > 0 && _ti.height > 0) { _rowCount = Mathf.CeilToInt((float)_ti.height / _ti.diceUnitHeight); } } }
public static exTextureInfo Create(Texture2D _texture) { exTextureInfo newTextureInfo = ScriptableObject.CreateInstance <exTextureInfo>(); Rect rect = new Rect(0, 0, _texture.width, _texture.height); newTextureInfo.texture = _texture; newTextureInfo.rotated = false; newTextureInfo.trim = false; newTextureInfo.trim_x = (int)rect.x; newTextureInfo.trim_y = (int)rect.y; newTextureInfo.width = (int)rect.width; newTextureInfo.height = (int)rect.height; newTextureInfo.x = (int)rect.x; newTextureInfo.y = (int)rect.y; newTextureInfo.rawWidth = _texture.width; newTextureInfo.rawHeight = _texture.height; return(newTextureInfo); }
public static exSprite NewSimpleSprite(GameObject _go, exTextureInfo _info, int _width, int _height, Color _color) { exSprite sprite = _go.GetComponent <exSprite>(); if (sprite == null) { sprite = _go.AddComponent <exSprite>(); } if (sprite.shader == null) { sprite.shader = Shader.Find("ex2D/Alpha Blended"); } sprite.spriteType = exSpriteType.Simple; sprite.textureInfo = _info; sprite.customSize = true; sprite.width = _width; sprite.height = _height; sprite.color = _color; return(sprite); }
// ------------------------------------------------------------------ // Change vertex buffer from simple to tiled // ------------------------------------------------------------------ public static void TiledUpdateVertexBuffer(exSpriteBase _sprite, exTextureInfo _textureInfo, bool _useTextureOffset, Vector2 _tiledSpacing, Space _space, exList<Vector3> _vertices, int _startIndex) { /* tile index: 8 9 10 11 4 5 6 7 0 1 2 3 */ //if (_vertices.Count == 0) { // return; //} int oriW = _textureInfo.width; int oriH = _textureInfo.height; int oriRawW = _textureInfo.rawWidth; int oriRawH = _textureInfo.rawHeight; try { // use entire sprite size _textureInfo.width = Mathf.Max((int)Mathf.Abs(_sprite.width), 1); _textureInfo.height = Mathf.Max((int)Mathf.Abs(_sprite.height), 1); _textureInfo.rawWidth = Mathf.Max(_textureInfo.width + oriRawW - oriW, 1); _textureInfo.rawHeight = Mathf.Max(_textureInfo.height + oriRawH - oriH, 1); // get entire sprite SimpleUpdateVertexBuffer(_sprite, _textureInfo, _useTextureOffset, _space, _vertices, _startIndex); } finally { // restore _textureInfo.width = oriW; _textureInfo.height = oriH; _textureInfo.rawWidth = oriRawW; _textureInfo.rawHeight = oriRawH; } Vector3 v0 = _vertices.buffer [_startIndex + 0]; Vector3 v1 = _vertices.buffer [_startIndex + 1]; Vector3 v2 = _vertices.buffer [_startIndex + 2]; int colCount, rowCount; exSpriteUtility.GetTilingCount ((exISprite)_sprite, out colCount, out rowCount); Vector2 lastTileRawSize = new Vector2(_sprite.width % (_textureInfo.width + _tiledSpacing.x), _sprite.height % (_textureInfo.height + _tiledSpacing.y)); Vector3 horizontalTileDis, verticalTileDis; if (lastTileRawSize.x > 0.0f) { float perc = lastTileRawSize.x / (_textureInfo.width + _tiledSpacing.x); horizontalTileDis = (v2 - v1) / (colCount - 1 + perc); } else { horizontalTileDis = (v2 - v1) / colCount; } if (lastTileRawSize.y > 0.0f) { float perc = lastTileRawSize.y / (_textureInfo.height + _tiledSpacing.y); verticalTileDis = (v1 - v0) / (rowCount - 1 + perc); } else { verticalTileDis = (v1 - v0) / rowCount; } Vector2 lastTilePercent = new Vector2(lastTileRawSize.x / _textureInfo.width, lastTileRawSize.y / _textureInfo.height); Vector3 trimedTileBottomToTop = verticalTileDis / (_textureInfo.height + _tiledSpacing.y) * _textureInfo.height; Vector3 trimedTileLeftToRight = horizontalTileDis / (_textureInfo.width + _tiledSpacing.x) * _textureInfo.width; int i = _startIndex; Vector3 rowBottomLeft = v0; for (int r = 0; r < rowCount; ++r) { Vector3 bottomLeft = rowBottomLeft; Vector3 topLeft; if (r < rowCount - 1 || lastTilePercent.y >= 1.0f || lastTilePercent.y == 0.0f) { topLeft = bottomLeft + trimedTileBottomToTop; } else { topLeft = v1; // clip last row } for (int c = 0; c < colCount; ++c) { _vertices.buffer[i++] = bottomLeft; _vertices.buffer[i++] = topLeft; _vertices.buffer[i++] = topLeft + trimedTileLeftToRight; _vertices.buffer[i++] = bottomLeft + trimedTileLeftToRight; // next column bottomLeft += horizontalTileDis; topLeft += horizontalTileDis; } // clip last column if (0.0f < lastTilePercent.x && lastTilePercent.x < 1.0f) { Vector3 clipped = trimedTileLeftToRight * (1.0f - lastTilePercent.x); _vertices.buffer[i - 2] -= clipped; _vertices.buffer[i - 1] -= clipped; } // next row rowBottomLeft += verticalTileDis; } }
// ------------------------------------------------------------------ // Desc: // ------------------------------------------------------------------ public static void GUI_DrawTextureInfo(Rect _rect, exTextureInfo _textureInfo, Color _color, bool _useTextureOffset = false) { if (_textureInfo == null) { return; } if (_textureInfo.texture == null) { return; } float s0 = (float)_textureInfo.x / (float)_textureInfo.texture.width; float s1 = (float)(_textureInfo.x + _textureInfo.rotatedWidth) / (float)_textureInfo.texture.width; float t0 = (float)_textureInfo.y / (float)_textureInfo.texture.height; float t1 = (float)(_textureInfo.y + _textureInfo.rotatedHeight) / (float)_textureInfo.texture.height; materialAlphaBlended.mainTexture = _textureInfo.texture; materialAlphaBlended.SetPass(0); GL.Begin(GL.QUADS); GL.Color(_color); if (_textureInfo.isDiced) { Vector2 texelSize = _textureInfo.texture.texelSize; float x_scale = _rect.width / _textureInfo.width; float y_scale = _rect.height / _textureInfo.height; foreach (exTextureInfo.Dice dice in _textureInfo.dices) { if (dice.sizeType != exTextureInfo.DiceType.Empty) { s0 = dice.x * texelSize.x; t0 = dice.y * texelSize.y; s1 = (dice.x + dice.rotatedWidth) * texelSize.x; t1 = (dice.y + dice.rotatedHeight) * texelSize.y; float start_x = dice.trim_x * x_scale; float start_y = dice.trim_y * y_scale; float end_x = (dice.trim_x + dice.width) * x_scale; float end_y = (dice.trim_y + dice.height) * y_scale; if (dice.rotated == false) { GL.TexCoord2(s0, t0); GL.Vertex3(_rect.x + start_x, _rect.yMax - start_y, 0.0f); GL.TexCoord2(s0, t1); GL.Vertex3(_rect.x + start_x, _rect.yMax - end_y, 0.0f); GL.TexCoord2(s1, t1); GL.Vertex3(_rect.x + end_x, _rect.yMax - end_y, 0.0f); GL.TexCoord2(s1, t0); GL.Vertex3(_rect.x + end_x, _rect.yMax - start_y, 0.0f); } else { GL.TexCoord2(s1, t0); GL.Vertex3(_rect.x + start_x, _rect.yMax - start_y, 0.0f); GL.TexCoord2(s0, t0); GL.Vertex3(_rect.x + start_x, _rect.yMax - end_y, 0.0f); GL.TexCoord2(s0, t1); GL.Vertex3(_rect.x + end_x, _rect.yMax - end_y, 0.0f); GL.TexCoord2(s1, t1); GL.Vertex3(_rect.x + end_x, _rect.yMax - start_y, 0.0f); } } } } else { if (_textureInfo.rotated == false) { GL.TexCoord2(s0, t0); GL.Vertex3(_rect.x, _rect.yMax, 0.0f); GL.TexCoord2(s0, t1); GL.Vertex3(_rect.x, _rect.y, 0.0f); GL.TexCoord2(s1, t1); GL.Vertex3(_rect.xMax, _rect.y, 0.0f); GL.TexCoord2(s1, t0); GL.Vertex3(_rect.xMax, _rect.yMax, 0.0f); } else { GL.TexCoord2(s1, t0); GL.Vertex3(_rect.x, _rect.yMax, 0.0f); GL.TexCoord2(s0, t0); GL.Vertex3(_rect.x, _rect.y, 0.0f); GL.TexCoord2(s0, t1); GL.Vertex3(_rect.xMax, _rect.y, 0.0f); GL.TexCoord2(s1, t1); GL.Vertex3(_rect.xMax, _rect.yMax, 0.0f); } } GL.End(); }
// ------------------------------------------------------------------ // Desc: // ------------------------------------------------------------------ public static void SimpleUpdateBuffers(exSpriteBase _sprite, exTextureInfo _textureInfo, bool _useTextureOffset, Space _space, exList<Vector3> _vertices, exList<Vector2> _uvs, exList<int> _indices, int _vbIndex, int _ibIndex) { if (/*transparent_ == false && */(_sprite.updateFlags & exUpdateFlags.Vertex) != 0) { SpriteBuilder.SimpleUpdateVertexBuffer(_sprite, _textureInfo, _useTextureOffset, _space, _vertices, _vbIndex); } if (/*transparent_ == false && */(_sprite.updateFlags & exUpdateFlags.Index) != 0 && _indices != null) { _indices.buffer[_ibIndex] = _vbIndex; _indices.buffer[_ibIndex + 1] = _vbIndex + 1; _indices.buffer[_ibIndex + 2] = _vbIndex + 2; _indices.buffer[_ibIndex + 3] = _vbIndex + 2; _indices.buffer[_ibIndex + 4] = _vbIndex + 3; _indices.buffer[_ibIndex + 5] = _vbIndex; } if (/*transparent_ == false && */(_sprite.updateFlags & exUpdateFlags.UV) != 0) { Vector2 texelSize; if (_textureInfo.texture != null) { texelSize = _textureInfo.texture.texelSize; } else { texelSize = new Vector2(1.0f / _textureInfo.rawWidth, 1.0f / _textureInfo.rawHeight); } Vector2 start = new Vector2((float)_textureInfo.x * texelSize.x, (float)_textureInfo.y * texelSize.y); Vector2 end = new Vector2((float)(_textureInfo.x + _textureInfo.rotatedWidth) * texelSize.x, (float)(_textureInfo.y + _textureInfo.rotatedHeight) * texelSize.y); if ( _textureInfo.rotated ) { _uvs.buffer[_vbIndex + 0] = new Vector2(end.x, start.y); _uvs.buffer[_vbIndex + 1] = start; _uvs.buffer[_vbIndex + 2] = new Vector2(start.x, end.y); _uvs.buffer[_vbIndex + 3] = end; } else { _uvs.buffer[_vbIndex + 0] = start; _uvs.buffer[_vbIndex + 1] = new Vector2(start.x, end.y); _uvs.buffer[_vbIndex + 2] = end; _uvs.buffer[_vbIndex + 3] = new Vector2(end.x, start.y); } } }
public static bool MakeFlippedTexture(string flippedPath, out exTextureInfo generatedTextureInfo) { //this method assumes that: // • You are calling it on the UI (Unity) thread // • You are providing a path that ends with the qudux flipped suffix (i.e. _qudux_flipped.png) // • SpriteManager.InfoMap is already initialized (generally because you should call // SpriteManager.GetTextureInfo before calling this function) string sourcePath = flippedPath.Substring(0, flippedPath.LastIndexOf(Constants.FlippedTileSuffix)); generatedTextureInfo = null; try { exTextureInfo info = GetTextureInfoFromBaseTilePath(sourcePath, out _); Texture2D texture2D = new Texture2D(info.width, info.height, TextureFormat.ARGB32, mipChain: false); texture2D.filterMode = UnityEngine.FilterMode.Point; Color[] pixels = info.texture.GetPixels(info.x, info.y, info.width, info.height, 0); //flip the tile pixels horizontally for (int row = 0; row < info.height; ++row) { Array.Reverse(pixels, row * info.width, info.width); } texture2D.SetPixels(pixels); texture2D.Apply(); // store texture in current game session's SpriteManager InfoMap string f = flippedPath.ToLower(); if (f.Contains("textures")) { f = "assets_content_" + f.Substring(f.IndexOf("textures")); } f = f.Replace('/', '_').Replace('\\', '_'); exTextureInfo flippedTextureInfo = new exTextureInfo { texture = texture2D, width = texture2D.width, height = texture2D.height, x = 0, y = 0, ShaderMode = 0 }; if (SpriteManagerInfoMap == null) { Log("QudUX: (Error) Couldn't MakeFlippedTexture because SpriteManager.InfoMap is not yet initialized."); return(false); } if (SpriteManagerInfoMap.ContainsKey(f)) { SpriteManagerInfoMap[f] = flippedTextureInfo; } else { SpriteManagerInfoMap.Add(f, flippedTextureInfo); } string key = f.ToLower().Replace(".png", ".bmp"); if (SpriteManagerInfoMap.ContainsKey(key)) { SpriteManagerInfoMap[key] = flippedTextureInfo; } else { SpriteManagerInfoMap.Add(key, flippedTextureInfo); } key = f.ToLower().Replace(".png", ""); if (SpriteManagerInfoMap.ContainsKey(key)) { SpriteManagerInfoMap[key] = flippedTextureInfo; } else { SpriteManagerInfoMap.Add(key, flippedTextureInfo); } generatedTextureInfo = flippedTextureInfo; return(true); } catch (Exception ex) { Log($"QudUX: (Error) Failed to generate flipped tile.\nException details: \n" + ex.ToString()); return(false); } }
// ------------------------------------------------------------------ // Change vertex buffer from simple to sliced // ------------------------------------------------------------------ public static void SimpleVertexBufferToSliced(exSpriteBase _sprite, exTextureInfo textureInfo_, exList<Vector3> _vertices, int _startIndex) { /* vertex index: 12 13 14 15 8 9 10 11 4 5 6 7 0 1 2 3 */ // get border size float leftBorderSize, rightBorderSize, topBorderSize, bottomBorderSize; exISprite iSprite = _sprite as exISprite; if (iSprite.customBorderSize) { leftBorderSize = iSprite.leftBorderSize; rightBorderSize = iSprite.rightBorderSize; topBorderSize = iSprite.topBorderSize; bottomBorderSize = iSprite.bottomBorderSize; } else { leftBorderSize = (float)textureInfo_.borderLeft; rightBorderSize = (float)textureInfo_.borderRight; topBorderSize = (float)textureInfo_.borderTop; bottomBorderSize = (float)textureInfo_.borderBottom; } // left right columns Vector3 v0 = _vertices.buffer[_startIndex + 0]; Vector3 v12 = _vertices.buffer[_startIndex + 1]; Vector3 v15 = _vertices.buffer[_startIndex + 2]; Vector3 v3 = _vertices.buffer[_startIndex + 3]; //_vertices.buffer[_startIndex + 0] = v0; //_vertices.buffer[_startIndex + 3] = v3; _vertices.buffer[_startIndex + 12] = v12; _vertices.buffer[_startIndex + 15] = v15; float height = _sprite.height; float yStep1 = bottomBorderSize / height; // position step, not uv step float yStep2 = (height - topBorderSize) / height; _vertices.buffer[_startIndex + 4] = v0 + (v12 - v0) * yStep1; _vertices.buffer[_startIndex + 7] = v3 + (v15 - v3) * yStep1; _vertices.buffer[_startIndex + 8] = v0 + (v12 - v0) * yStep2; _vertices.buffer[_startIndex + 11] = v3 + (v15 - v3) * yStep2; // mid columns float width = _sprite.width; float xStep1 = leftBorderSize / width; float xStep2 = (width - rightBorderSize) / width; for (int i = 0; i <= 12; i += 4) { Vector3 left = _vertices.buffer[_startIndex + i]; Vector3 right = _vertices.buffer[_startIndex + i + 3]; _vertices.buffer[_startIndex + i + 1] = left + (right - left) * xStep1; _vertices.buffer[_startIndex + i + 2] = left + (right - left) * xStep2; } }
// ------------------------------------------------------------------ /// update the default texture info if we dynamically change it in the game // ------------------------------------------------------------------ public void UpdateDefaultSprite (exTextureInfo _textureInfo) { defaultTextureInfo = _textureInfo; }
// ------------------------------------------------------------------ // Desc: // ------------------------------------------------------------------ public void AddFrame(exTextureInfo _info, int _frames = 1) { InsertFrameInfo(frameInfos.Count, new FrameInfo(_info, _frames)); }
// ------------------------------------------------------------------ // Desc: // ------------------------------------------------------------------ public static void SlicedUpdateBuffers(exSpriteBase _sprite, exTextureInfo _textureInfo, bool _useTextureOffset, Space _space, exList<Vector3> _vertices, exList<Vector2> _uvs, exList<int> _indices, int _vbIndex, int _ibIndex) { SpriteBuilder.SimpleUpdateBuffers(_sprite, _textureInfo, _useTextureOffset, _space, _vertices, _uvs, _indices, _vbIndex, _ibIndex); if ((_sprite.updateFlags & exUpdateFlags.Vertex) != 0) { SpriteBuilder.SimpleVertexBufferToSliced(_sprite, _textureInfo, _vertices, _vbIndex); } if ((_sprite.updateFlags & exUpdateFlags.Index) != 0 && _indices != null) { bool borderOnly = (_sprite as exISprite).borderOnly; int centerIndexIfBorderOnly = borderOnly ? 5 : int.MinValue; for (int i = 0; i <= 10; ++i) { if (i != 3 && i != 7 && i != centerIndexIfBorderOnly) { // 0 1 2 4 5 6 8 9 10 int blVertexIndex = _vbIndex + i; // bottom left vertex index _indices.buffer[_ibIndex++] = blVertexIndex; _indices.buffer[_ibIndex++] = blVertexIndex + 4; _indices.buffer[_ibIndex++] = blVertexIndex + 5; _indices.buffer[_ibIndex++] = blVertexIndex + 5; _indices.buffer[_ibIndex++] = blVertexIndex + 1; _indices.buffer[_ibIndex++] = blVertexIndex; } } } if ((_sprite.updateFlags & exUpdateFlags.UV) != 0) { float xStep1, xStep2, yStep1, yStep2; if (_textureInfo.rotated == false) { yStep1 = (float)_textureInfo.borderBottom / _textureInfo.height; // uv step, not position step yStep2 = (float)(_textureInfo.height - _textureInfo.borderTop) / _textureInfo.height; xStep1 = (float)_textureInfo.borderLeft / _textureInfo.width; xStep2 = (float)(_textureInfo.width - _textureInfo.borderRight) / _textureInfo.width; } else { xStep1 = (float)_textureInfo.borderBottom / _textureInfo.height; // uv step, not position step xStep2 = (float)(_textureInfo.height - _textureInfo.borderTop) / _textureInfo.height; yStep1 = (float)_textureInfo.borderLeft / _textureInfo.width; yStep2 = (float)(_textureInfo.width - _textureInfo.borderRight) / _textureInfo.width; } Vector2 uv0, uv15; uv0 = _uvs.buffer[_vbIndex + 0]; uv15 = _uvs.buffer[_vbIndex + 2]; Vector2 uv5 = new Vector2(uv0.x + (uv15.x - uv0.x) * xStep1, uv0.y + (uv15.y - uv0.y) * yStep1); Vector2 uv10 = new Vector2(uv0.x + (uv15.x - uv0.x) * xStep2, uv0.y + (uv15.y - uv0.y) * yStep2); if (_textureInfo.rotated == false) { //_uvs.buffer[vertexBufferIndex + 0] = uv0; _uvs.buffer[_vbIndex + 1] = new Vector2(uv5.x, uv0.y); _uvs.buffer[_vbIndex + 2] = new Vector2(uv10.x, uv0.y); _uvs.buffer[_vbIndex + 3] = new Vector2(uv15.x, uv0.y); _uvs.buffer[_vbIndex + 4] = new Vector2(uv0.x, uv5.y); _uvs.buffer[_vbIndex + 5] = uv5; _uvs.buffer[_vbIndex + 6] = new Vector2(uv10.x, uv5.y); _uvs.buffer[_vbIndex + 7] = new Vector2(uv15.x, uv5.y); _uvs.buffer[_vbIndex + 8] = new Vector2(uv0.x, uv10.y); _uvs.buffer[_vbIndex + 9] = new Vector2(uv5.x, uv10.y); _uvs.buffer[_vbIndex + 10] = uv10; _uvs.buffer[_vbIndex + 11] = new Vector2(uv15.x, uv10.y); _uvs.buffer[_vbIndex + 12] = new Vector2(uv0.x, uv15.y); _uvs.buffer[_vbIndex + 13] = new Vector2(uv5.x, uv15.y); _uvs.buffer[_vbIndex + 14] = new Vector2(uv10.x, uv15.y); _uvs.buffer[_vbIndex + 15] = uv15; } else { //_uvs.buffer[vertexBufferIndex + 0] = uv0; _uvs.buffer[_vbIndex + 1] = new Vector2(uv0.x, uv5.y); _uvs.buffer[_vbIndex + 2] = new Vector2(uv0.x, uv10.y); _uvs.buffer[_vbIndex + 3] = new Vector2(uv0.x, uv15.y); _uvs.buffer[_vbIndex + 4] = new Vector2(uv5.x, uv0.y); _uvs.buffer[_vbIndex + 5] = uv5; _uvs.buffer[_vbIndex + 6] = new Vector2(uv5.x, uv10.y); _uvs.buffer[_vbIndex + 7] = new Vector2(uv5.x, uv15.y); _uvs.buffer[_vbIndex + 8] = new Vector2(uv10.x, uv0.y); _uvs.buffer[_vbIndex + 9] = new Vector2(uv10.x, uv5.y); _uvs.buffer[_vbIndex + 10] = uv10; _uvs.buffer[_vbIndex + 11] = new Vector2(uv10.x, uv15.y); _uvs.buffer[_vbIndex + 12] = new Vector2(uv15.x, uv0.y); _uvs.buffer[_vbIndex + 13] = new Vector2(uv15.x, uv5.y); _uvs.buffer[_vbIndex + 14] = new Vector2(uv15.x, uv10.y); _uvs.buffer[_vbIndex + 15] = uv15; } } }
// ------------------------------------------------------------------ // Desc: // ------------------------------------------------------------------ public static void SimpleUpdateVertexBuffer(exSpriteBase _sprite, exTextureInfo _textureInfo, bool _useTextureOffset, Space _space, exList<Vector3> _vertices, int _startIndex) { Vector2 anchorOffset; float halfHeight = _textureInfo.height * 0.5f; float halfWidth = _textureInfo.width * 0.5f; if (_useTextureOffset) { switch (_sprite.anchor) { case Anchor.TopLeft: anchorOffset.x = halfWidth + _textureInfo.trim_x; anchorOffset.y = -halfHeight + _textureInfo.trim_y - (_textureInfo.rawHeight - _textureInfo.height); break; case Anchor.TopCenter: anchorOffset.x = _textureInfo.trim_x - (_textureInfo.rawWidth - _textureInfo.width) * 0.5f; anchorOffset.y = -halfHeight + _textureInfo.trim_y - (_textureInfo.rawHeight - _textureInfo.height); break; case Anchor.TopRight: anchorOffset.x = -halfWidth + _textureInfo.trim_x - (_textureInfo.rawWidth - _textureInfo.width); anchorOffset.y = -halfHeight + _textureInfo.trim_y - (_textureInfo.rawHeight - _textureInfo.height); break; // case Anchor.MidLeft: anchorOffset.x = halfWidth + _textureInfo.trim_x; anchorOffset.y = _textureInfo.trim_y - (_textureInfo.rawHeight - _textureInfo.height) * 0.5f; break; case Anchor.MidCenter: anchorOffset.x = _textureInfo.trim_x - (_textureInfo.rawWidth - _textureInfo.width) * 0.5f; anchorOffset.y = _textureInfo.trim_y - (_textureInfo.rawHeight - _textureInfo.height) * 0.5f; break; case Anchor.MidRight: anchorOffset.x = -halfWidth + _textureInfo.trim_x - (_textureInfo.rawWidth - _textureInfo.width); anchorOffset.y = _textureInfo.trim_y - (_textureInfo.rawHeight - _textureInfo.height) * 0.5f; break; // case Anchor.BotLeft: anchorOffset.x = halfWidth + _textureInfo.trim_x; anchorOffset.y = halfHeight + _textureInfo.trim_y; break; case Anchor.BotCenter: anchorOffset.x = _textureInfo.trim_x - (_textureInfo.rawWidth - _textureInfo.width) * 0.5f; anchorOffset.y = halfHeight + _textureInfo.trim_y; break; case Anchor.BotRight: anchorOffset.x = -halfWidth + _textureInfo.trim_x - (_textureInfo.rawWidth - _textureInfo.width); anchorOffset.y = halfHeight + _textureInfo.trim_y; break; // default: anchorOffset.x = _textureInfo.trim_x - (_textureInfo.rawWidth - _textureInfo.width) * 0.5f; anchorOffset.y = _textureInfo.trim_y - (_textureInfo.rawHeight - _textureInfo.height) * 0.5f; break; } } else { switch ( _sprite.anchor ) { case Anchor.TopLeft : anchorOffset.x = halfWidth; anchorOffset.y = -halfHeight; break; case Anchor.TopCenter : anchorOffset.x = 0.0f; anchorOffset.y = -halfHeight; break; case Anchor.TopRight : anchorOffset.x = -halfWidth; anchorOffset.y = -halfHeight; break; case Anchor.MidLeft : anchorOffset.x = halfWidth; anchorOffset.y = 0.0f; break; case Anchor.MidCenter : anchorOffset.x = 0.0f; anchorOffset.y = 0.0f; break; case Anchor.MidRight : anchorOffset.x = -halfWidth; anchorOffset.y = 0.0f; break; case Anchor.BotLeft : anchorOffset.x = halfWidth; anchorOffset.y = halfHeight; break; case Anchor.BotCenter : anchorOffset.x = 0.0f; anchorOffset.y = halfHeight; break; case Anchor.BotRight : anchorOffset.x = -halfWidth; anchorOffset.y = halfHeight; break; default : anchorOffset.x = 0.0f; anchorOffset.y = 0.0f; break; } } //v1 v2 //v0 v3 Vector3 v0 = new Vector3 (-halfWidth + anchorOffset.x, -halfHeight + anchorOffset.y, 0.0f); Vector3 v1 = new Vector3 (-halfWidth + anchorOffset.x, halfHeight + anchorOffset.y, 0.0f); Vector3 v2 = new Vector3 (halfWidth + anchorOffset.x, halfHeight + anchorOffset.y, 0.0f); Vector3 v3 = new Vector3 (halfWidth + anchorOffset.x, -halfHeight + anchorOffset.y, 0.0f); if (_sprite.customSize) { Vector2 customSizeScale = new Vector2 (_sprite.width / _textureInfo.width, _sprite.height / _textureInfo.height); v0.x *= customSizeScale.x; v0.y *= customSizeScale.y; v1.x *= customSizeScale.x; v1.y *= customSizeScale.y; v2.x *= customSizeScale.x; v2.y *= customSizeScale.y; v3.x *= customSizeScale.x; v3.y *= customSizeScale.y; } Vector3 offset = _sprite.offset; v0 += offset; v1 += offset; v2 += offset; v3 += offset; Vector2 shear = _sprite.shear; if (shear.x != 0) { float offsetX = _sprite.GetScaleY(_space) * shear.x; float topOffset = offsetX * (halfHeight + anchorOffset.y); float botOffset = offsetX * (-halfHeight + anchorOffset.y); v0.x += botOffset; v1.x += topOffset; v2.x += topOffset; v3.x += botOffset; } if (shear.y != 0) { float offsetY = _sprite.GetScaleX(_space) * shear.y; float leftOffset = offsetY * (-halfWidth + anchorOffset.x); float rightOffset = offsetY * (halfWidth + anchorOffset.x); v0.y += leftOffset; v1.y += leftOffset; v2.y += rightOffset; v3.y += rightOffset; } if (_space == Space.World) { exDebug.Assert((_sprite as exLayeredSprite) != null); v0 = _sprite.cachedWorldMatrix.MultiplyPoint3x4(v0); v1 = _sprite.cachedWorldMatrix.MultiplyPoint3x4(v1); v2 = _sprite.cachedWorldMatrix.MultiplyPoint3x4(v2); v3 = _sprite.cachedWorldMatrix.MultiplyPoint3x4(v3); // 将z都设为0,使mesh所有mesh的厚度都为0,这样在mesh进行深度排序时会方便一些。但是不能用于3D Sprite v0.z = 0; v1.z = 0; v2.z = 0; v3.z = 0; } _vertices.buffer[_startIndex + 0] = v0; _vertices.buffer[_startIndex + 1] = v1; _vertices.buffer[_startIndex + 2] = v2; _vertices.buffer[_startIndex + 3] = v3; // TODO: pixel-perfect }
// ------------------------------------------------------------------ // Desc: // ------------------------------------------------------------------ public override void OnInspectorGUI() { // get old trim value serializedObject.Update(); SerializedProperty propTrim = serializedObject.FindProperty("trim"); bool oldTrim = propTrim.boolValue; DrawDefaultInspector(); // process trim property if (propTrim.boolValue != oldTrim) { foreach (Object obj in serializedObject.targetObjects) { exTextureInfo textureInfo = obj as exTextureInfo; if (textureInfo == null) { continue; } Texture2D rawTexture = exEditorUtility.LoadAssetFromGUID <Texture2D> (textureInfo.rawTextureGUID); if (rawTexture == null) { continue; } if (propTrim.boolValue) { Rect trimRect = exTextureUtility.GetTrimTextureRect(rawTexture, textureInfo.trimThreshold, new Rect(0, 0, rawTexture.width, rawTexture.height)); if (trimRect.width <= 0 || trimRect.height <= 0) { Debug.LogWarning("Can't not trim texture " + obj.name + ", empty pixel in it"); trimRect = new Rect(0, 0, rawTexture.width, rawTexture.height); textureInfo.trim = false; } textureInfo.trim_x = (int)trimRect.x; textureInfo.trim_y = (int)trimRect.y; textureInfo.width = (int)trimRect.width; textureInfo.height = (int)trimRect.height; } else { textureInfo.trim_x = 0; textureInfo.trim_y = 0; textureInfo.width = rawTexture.width; textureInfo.height = rawTexture.height; } EditorUtility.SetDirty(textureInfo); } } // if (serializedObject.isEditingMultipleObjects == false) { GUILayout.BeginHorizontal(); GUILayout.FlexibleSpace(); // Select if (GUILayout.Button("Select...", GUILayout.Width(60), GUILayout.Height(20))) { exTextureInfo textureInfo = target as exTextureInfo; Texture texture = exEditorUtility.LoadAssetFromGUID <Texture>(textureInfo.rawTextureGUID); EditorGUIUtility.PingObject(texture); } // Atlas if (GUILayout.Button("Atlas...", GUILayout.Width(60), GUILayout.Height(20))) { exAtlasEditor editor = EditorWindow.GetWindow <exAtlasEditor>(); exTextureInfo textureInfo = target as exTextureInfo; exAtlas atlas = exEditorUtility.LoadAssetFromGUID <exAtlas>(textureInfo.rawAtlasGUID); editor.Edit(atlas); } // Edit if (GUILayout.Button("Edit...", GUILayout.Width(60), GUILayout.Height(20))) { exTextureInfoEditor editor = EditorWindow.GetWindow <exTextureInfoEditor>(); exTextureInfo textureInfo = target as exTextureInfo; editor.Edit(textureInfo); } GUILayout.Space(5); GUILayout.EndHorizontal(); } }
// ------------------------------------------------------------------ // Desc: // ------------------------------------------------------------------ public static void SimpleVertexBufferToDiced(exSpriteBase _sprite, exTextureInfo _textureInfo, exList<Vector3> _vertices, int _startIndex) { /* dice index: 8 9 10 11 4 5 6 7 0 1 2 3 */ if (_textureInfo.isDiced == false) { return; } Vector3 v0 = _vertices.buffer [_startIndex + 0]; Vector3 v1 = _vertices.buffer [_startIndex + 1]; Vector3 v2 = _vertices.buffer [_startIndex + 2]; int colCount, rowCount; exSpriteUtility.GetDicingCount (_textureInfo, out colCount, out rowCount); Vector2 lastTileRawSize = new Vector2(); if (_textureInfo.diceUnitWidth > 0) { lastTileRawSize.x = _textureInfo.width % _textureInfo.diceUnitWidth; } if (_textureInfo.diceUnitHeight > 0) { lastTileRawSize.y = _textureInfo.height % _textureInfo.diceUnitHeight; } Vector3 diceLeftToRight, diceBottomToTop; if (lastTileRawSize.x > 0.0f) { float perc = lastTileRawSize.x / _textureInfo.diceUnitWidth; diceLeftToRight = (v2 - v1) / (colCount - 1 + perc); } else { diceLeftToRight = (v2 - v1) / colCount; } if (lastTileRawSize.y > 0.0f) { float perc = lastTileRawSize.y / _textureInfo.diceUnitHeight; diceBottomToTop = (v1 - v0) / (rowCount - 1 + perc); } else { diceBottomToTop = (v1 - v0) / rowCount; } Vector3 l2rStepPerTile = diceLeftToRight / _textureInfo.diceUnitWidth; Vector3 b2tStepPerTile = diceBottomToTop / _textureInfo.diceUnitHeight; int i = _startIndex; Vector3 rowBottomLeft = v0; DiceEnumerator diceEnumerator = _textureInfo.dices; for (int r = 0; r < rowCount; ++r) { Vector3 bottomLeft = rowBottomLeft; Vector3 topLeft = bottomLeft + diceBottomToTop; for (int c = 0; c < colCount; ++c) { bool hasNext = diceEnumerator.MoveNext (); if (hasNext == false) { // 后面都被Trim掉了 return; } exTextureInfo.Dice dice = diceEnumerator.Current; if (dice.sizeType == exTextureInfo.DiceType.Max) { _vertices.buffer[i++] = bottomLeft; _vertices.buffer[i++] = topLeft; _vertices.buffer[i++] = topLeft + diceLeftToRight; _vertices.buffer[i++] = bottomLeft + diceLeftToRight; } else if (dice.sizeType == exTextureInfo.DiceType.Trimmed) { Vector3 offsetX = l2rStepPerTile * dice.offset_x; Vector3 offsetY = b2tStepPerTile * dice.offset_y; Vector3 offsetEndX = l2rStepPerTile * (dice.offset_x + dice.width); Vector3 offsetEndY = b2tStepPerTile * (dice.offset_y + dice.height); _vertices.buffer[i++] = bottomLeft + offsetX + offsetY; _vertices.buffer[i++] = bottomLeft + offsetX + offsetEndY; _vertices.buffer[i++] = bottomLeft + offsetEndX + offsetEndY; _vertices.buffer[i++] = bottomLeft + offsetEndX + offsetY; } bottomLeft += diceLeftToRight; // next column topLeft += diceLeftToRight; // next column } // next row rowBottomLeft += diceBottomToTop; } exDebug.Assert(diceEnumerator.MoveNext() == false, string.Format("row: {0} col: {1} ", rowCount, colCount)); }
// ------------------------------------------------------------------ // Desc: // ------------------------------------------------------------------ Rect MapTextureInfo( Rect _atlasRect, exTextureInfo _textureInfo ) { Rect rect = new Rect ( _textureInfo.x * curEdit.scale, _textureInfo.y * curEdit.scale, _textureInfo.rotatedWidth * curEdit.scale, _textureInfo.rotatedHeight * curEdit.scale ); rect.x = _atlasRect.x + rect.x; rect.y = _atlasRect.y + _atlasRect.height - rect.y - rect.height; rect = exGeometryUtility.Rect_FloorToInt(rect); return rect; }
// ------------------------------------------------------------------ // Desc: // ------------------------------------------------------------------ protected override void DoInspectorGUI() { base.DoInspectorGUI(); // textureInfo EditorGUILayout.BeginHorizontal(); EditorGUILayout.BeginVertical(); GUILayout.Space(3); EditorGUI.BeginChangeCheck(); EditorGUILayout.PropertyField(textureInfoProp, new GUIContent("Texture Info")); if (EditorGUI.EndChangeCheck()) { foreach (Object obj in serializedObject.targetObjects) { ex3DSprite sp = obj as ex3DSprite; if (sp) { sp.textureInfo = textureInfoProp.objectReferenceValue as exTextureInfo; if (sp.textureInfo != null) { if (sp.textureInfo.hasBorder) { sp.spriteType = exSpriteType.Sliced; sp.customSize = true; } else if (sp.textureInfo.isDiced) { sp.spriteType = exSpriteType.Diced; } else if (sp.spriteType == exSpriteType.Sliced) { sp.spriteType = exSpriteType.Simple; sp.customSize = false; } else if (sp.spriteType == exSpriteType.Diced) { sp.spriteType = exSpriteType.Simple; } } EditorUtility.SetDirty(sp); } } } EditorGUILayout.EndVertical(); if (GUILayout.Button("Refresh", GUILayout.Width(57), GUILayout.Height(16))) { foreach (Object obj in serializedObject.targetObjects) { ex3DSprite sp = obj as ex3DSprite; if (sp) { sp.textureInfo = sp.textureInfo; EditorUtility.SetDirty(sp); } } } EditorGUILayout.EndHorizontal(); EditorGUILayout.Space(); // draw preview rect if (serializedObject.isEditingMultipleObjects == false) { float preview_width = 100.0f; float preview_height = 100.0f; Rect lastRect = GUILayoutUtility.GetLastRect(); float indent_space = 20.0f; Rect previewRect = new Rect(indent_space, lastRect.yMax, preview_width, preview_height); // preview if (Event.current.type == EventType.Repaint) { exTextureInfo textureInfo = textureInfoProp.objectReferenceValue as exTextureInfo; // draw Checker Texture2D checker = exEditorUtility.textureCheckerboard; GUI.DrawTextureWithTexCoords(previewRect, checker, new Rect(0.0f, 0.0f, 3.0f, 3.0f)); // draw TextureInfo if (textureInfo != null) { float scale = exEditorUtility.CalculateTextureInfoScale(previewRect, textureInfo); Rect pos = new Rect(previewRect.center.x - textureInfo.width * 0.5f * scale + 2.0f, previewRect.center.y - textureInfo.height * 0.5f * scale + 2.0f, textureInfo.width * scale - 4.0f, textureInfo.height * scale - 4.0f); exEditorUtility.GUI_DrawTextureInfo(pos, textureInfo, Color.white); } // draw border exEditorUtility.GL_DrawRectLine(new Vector3 [] { new Vector3(indent_space, lastRect.yMax, 0.0f), new Vector3(indent_space + preview_width, lastRect.yMax, 0.0f), new Vector3(indent_space + preview_width, lastRect.yMax + preview_height, 0.0f), new Vector3(indent_space, lastRect.yMax + preview_height, 0.0f), }, new Color(0.8f, 0.8f, 0.8f, 1.0f)); } GUILayoutUtility.GetRect(preview_width, preview_height); // edit button Rect editBtnPos = new Rect(previewRect.xMax - 50 - 2, previewRect.yMax - 20 - 2, 50, 20); if (GUI.Button(editBtnPos, "Edit...")) { EditorWindow.GetWindow <exTextureInfoEditor>().Edit(textureInfoProp.objectReferenceValue as exTextureInfo); } } // useTextureOffset EditorGUI.BeginChangeCheck(); EditorGUILayout.PropertyField(useTextureOffsetProp, new GUIContent("Use Texture Offset")); if (EditorGUI.EndChangeCheck()) { foreach (Object obj in serializedObject.targetObjects) { ex3DSprite sp = obj as ex3DSprite; if (sp) { sp.useTextureOffset = useTextureOffsetProp.boolValue; EditorUtility.SetDirty(sp); } } } // type EditorGUI.BeginChangeCheck(); EditorGUILayout.PropertyField(spriteTypeProp, new GUIContent("Sprite Type")); if (EditorGUI.EndChangeCheck()) { foreach (Object obj in serializedObject.targetObjects) { ex3DSprite sp = obj as ex3DSprite; if (sp) { sp.spriteType = (exSpriteType)spriteTypeProp.intValue; EditorUtility.SetDirty(sp); } } } if (spriteTypeProp.enumValueIndex == (int)exSpriteType.Tiled) { customSizeProp.boolValue = true; ++EditorGUI.indentLevel; // tiled spacing EditorGUI.BeginChangeCheck(); EditorGUILayout.PropertyField(tiledSpacingProp, new GUIContent("Tiled Spacing"), true); if (EditorGUI.EndChangeCheck()) { foreach (Object obj in serializedObject.targetObjects) { ex3DSprite sp = obj as ex3DSprite; if (sp) { sp.tiledSpacing = tiledSpacingProp.vector2Value; if (sp.textureInfo != null) { sp.tiledSpacing = new Vector2(Mathf.Max(-sp.textureInfo.width + 1, sp.tiledSpacing.x), Mathf.Max(-sp.textureInfo.height + 1, sp.tiledSpacing.y)); } EditorUtility.SetDirty(sp); } } } EditorGUILayout.BeginHorizontal(); GUILayout.FlexibleSpace(); if (GUILayout.Button("Use Raw Size", GUILayout.Width(88), GUILayout.Height(16))) { foreach (Object obj in serializedObject.targetObjects) { ex3DSprite sp = obj as ex3DSprite; if (sp && sp.textureInfo != null) { exTextureInfo ti = sp.textureInfo; sp.tiledSpacing = new Vector2(ti.rawWidth - ti.width, ti.rawHeight - ti.height) / 2; EditorUtility.SetDirty(sp); } } } EditorGUILayout.EndHorizontal(); --EditorGUI.indentLevel; } else if (spriteTypeProp.enumValueIndex == (int)exSpriteType.Sliced) { ++EditorGUI.indentLevel; // border only EditorGUI.BeginChangeCheck(); EditorGUILayout.PropertyField(borderOnlyProp, new GUIContent("Border Only"), true); if (EditorGUI.EndChangeCheck()) { foreach (Object obj in serializedObject.targetObjects) { ex3DSprite sp = obj as ex3DSprite; if (sp) { sp.borderOnly = borderOnlyProp.boolValue; EditorUtility.SetDirty(sp); } } } // custom border size EditorGUILayout.BeginHorizontal(); EditorGUI.BeginChangeCheck(); EditorGUILayout.PropertyField(customBorderSizeProp, new GUIContent("Custom Border Size"), true); if (EditorGUI.EndChangeCheck()) { foreach (Object obj in serializedObject.targetObjects) { ex3DSprite sp = obj as ex3DSprite; if (sp) { sp.customBorderSize = customBorderSizeProp.boolValue; EditorUtility.SetDirty(sp); } } } if (customBorderSizeProp.boolValue) { // reset border size if (GUILayout.Button("Reset", GUILayout.Width(57), GUILayout.Height(16))) { foreach (Object obj in serializedObject.targetObjects) { ex3DSprite sp = obj as ex3DSprite; if (sp) { sp.leftBorderSize = sp.textureInfo.borderLeft; sp.rightBorderSize = sp.textureInfo.borderRight; sp.topBorderSize = sp.textureInfo.borderTop; sp.bottomBorderSize = sp.textureInfo.borderBottom; EditorUtility.SetDirty(sp); } } } EditorGUILayout.EndHorizontal(); ++EditorGUI.indentLevel; // left right top bottom EditorGUI.BeginChangeCheck(); EditorGUILayout.PropertyField(leftProp, new GUIContent("Left"), true); EditorGUILayout.PropertyField(rightProp, new GUIContent("Right"), true); EditorGUILayout.PropertyField(topProp, new GUIContent("Top"), true); EditorGUILayout.PropertyField(bottomProp, new GUIContent("Bottom"), true); if (EditorGUI.EndChangeCheck()) { foreach (Object obj in serializedObject.targetObjects) { ex3DSprite sp = obj as ex3DSprite; if (sp) { sp.leftBorderSize = leftProp.floatValue; sp.rightBorderSize = rightProp.floatValue; sp.topBorderSize = topProp.floatValue; sp.bottomBorderSize = bottomProp.floatValue; EditorUtility.SetDirty(sp); } } } --EditorGUI.indentLevel; } else { EditorGUILayout.EndHorizontal(); } --EditorGUI.indentLevel; } EditorGUILayout.Space(); }
// ------------------------------------------------------------------ // Desc: // ------------------------------------------------------------------ public static void DicedUpdateBuffers(exSpriteBase _sprite, exTextureInfo _textureInfo, bool _useTextureOffset, Space _space, exList<Vector3> _vertices, exList<Vector2> _uvs, exList<int> _indices, int _vbIndex, int _ibIndex) { if (_textureInfo.isDiced == false) { SimpleUpdateBuffers(_sprite, _textureInfo, _useTextureOffset, _space, _vertices, _uvs, _indices, _vbIndex, _ibIndex); return; } //if (_vertices.Count == 0) { // return; //} if ((_sprite.updateFlags & exUpdateFlags.Vertex) != 0) { // get entire sprite SimpleUpdateVertexBuffer(_sprite, _textureInfo, _useTextureOffset, _space, _vertices, _vbIndex); SimpleVertexBufferToDiced(_sprite, _textureInfo, _vertices, _vbIndex); } if ((_sprite.updateFlags & exUpdateFlags.Index) != 0 && _indices != null) { /* dice index: 8 9 10 11 4 5 6 7 0 1 2 3 */ int i = _ibIndex; for (int v = _vbIndex; v < _vertices.Count; v += exMesh.QUAD_VERTEX_COUNT) { _indices.buffer[i++] = v; _indices.buffer[i++] = v + 1; _indices.buffer[i++] = v + 2; _indices.buffer[i++] = v + 2; _indices.buffer[i++] = v + 3; _indices.buffer[i++] = v; } } if ((_sprite.updateFlags & exUpdateFlags.UV) != 0) { Vector2 texelSize; if (_textureInfo.texture != null) { texelSize = _textureInfo.texture.texelSize; } else { texelSize = new Vector2(1.0f / _textureInfo.rawWidth, 1.0f / _textureInfo.rawHeight); } foreach (exTextureInfo.Dice dice in _textureInfo.dices) { if (dice.sizeType != exTextureInfo.DiceType.Empty) { Vector2 start = new Vector2(dice.x * texelSize.x, dice.y * texelSize.y); Vector2 end = new Vector2((dice.x + dice.rotatedWidth) * texelSize.x, (dice.y + dice.rotatedHeight) * texelSize.y); if ( dice.rotated ) { _uvs.buffer[_vbIndex++] = new Vector2(end.x, start.y); _uvs.buffer[_vbIndex++] = start; _uvs.buffer[_vbIndex++] = new Vector2(start.x, end.y); _uvs.buffer[_vbIndex++] = end; } else { _uvs.buffer[_vbIndex++] = start; _uvs.buffer[_vbIndex++] = new Vector2(start.x, end.y); _uvs.buffer[_vbIndex++] = end; _uvs.buffer[_vbIndex++] = new Vector2(end.x, start.y); } } } } }
// ------------------------------------------------------------------ // Desc: // ------------------------------------------------------------------ public static void GL_DrawTextureInfo(exTextureInfo _textureInfo, Vector2 _pos, Color _color) { if (_textureInfo == null) { return; } if (_textureInfo.texture == null) { return; } Vector2 halfSize = new Vector2(_textureInfo.width * 0.5f, _textureInfo.height * 0.5f); float s0 = (float)_textureInfo.x / (float)_textureInfo.texture.width; float s1 = (float)(_textureInfo.x + _textureInfo.rotatedWidth) / (float)_textureInfo.texture.width; float t0 = (float)_textureInfo.y / (float)_textureInfo.texture.height; float t1 = (float)(_textureInfo.y + _textureInfo.rotatedHeight) / (float)_textureInfo.texture.height; materialAlphaBlended.mainTexture = _textureInfo.texture; materialAlphaBlended.SetPass(0); GL.Begin(GL.QUADS); GL.Color(_color); if (_textureInfo.isDiced) { Vector2 texelSize = _textureInfo.texture.texelSize; foreach (exTextureInfo.Dice dice in _textureInfo.dices) { if (dice.sizeType != exTextureInfo.DiceType.Empty) { s0 = dice.x * texelSize.x; t0 = dice.y * texelSize.y; s1 = (dice.x + dice.rotatedWidth) * texelSize.x; t1 = (dice.y + dice.rotatedHeight) * texelSize.y; if (dice.rotated == false) { GL.TexCoord2(s0, t0); GL.Vertex3(-halfSize.x + _pos.x + dice.trim_x, -halfSize.y + _pos.y + dice.trim_y, 0.0f); GL.TexCoord2(s0, t1); GL.Vertex3(-halfSize.x + _pos.x + dice.trim_x, -halfSize.y + _pos.y + dice.trim_y + dice.height, 0.0f); GL.TexCoord2(s1, t1); GL.Vertex3(-halfSize.x + _pos.x + dice.trim_x + dice.width, -halfSize.y + _pos.y + dice.trim_y + dice.height, 0.0f); GL.TexCoord2(s1, t0); GL.Vertex3(-halfSize.x + _pos.x + dice.trim_x + dice.width, -halfSize.y + _pos.y + dice.trim_y, 0.0f); } else { GL.TexCoord2(s1, t0); GL.Vertex3(-halfSize.x + _pos.x + dice.trim_x, -halfSize.y + _pos.y + dice.trim_y, 0.0f); GL.TexCoord2(s0, t0); GL.Vertex3(-halfSize.x + _pos.x + dice.trim_x, -halfSize.y + _pos.y + dice.trim_y + dice.height, 0.0f); GL.TexCoord2(s0, t1); GL.Vertex3(-halfSize.x + _pos.x + dice.trim_x + dice.width, -halfSize.y + _pos.y + dice.trim_y + dice.height, 0.0f); GL.TexCoord2(s1, t1); GL.Vertex3(-halfSize.x + _pos.x + dice.trim_x + dice.width, -halfSize.y + _pos.y + dice.trim_y, 0.0f); } } } } else { if (_textureInfo.rotated == false) { GL.TexCoord2(s0, t0); GL.Vertex3(-halfSize.x + _pos.x, -halfSize.y + _pos.y, 0.0f); GL.TexCoord2(s0, t1); GL.Vertex3(-halfSize.x + _pos.x, halfSize.y + _pos.y, 0.0f); GL.TexCoord2(s1, t1); GL.Vertex3(halfSize.x + _pos.x, halfSize.y + _pos.y, 0.0f); GL.TexCoord2(s1, t0); GL.Vertex3(halfSize.x + _pos.x, -halfSize.y + _pos.y, 0.0f); } else { GL.TexCoord2(s1, t0); GL.Vertex3(-halfSize.x + _pos.x, -halfSize.y + _pos.y, 0.0f); GL.TexCoord2(s0, t0); GL.Vertex3(-halfSize.x + _pos.x, halfSize.y + _pos.y, 0.0f); GL.TexCoord2(s0, t1); GL.Vertex3(halfSize.x + _pos.x, halfSize.y + _pos.y, 0.0f); GL.TexCoord2(s1, t1); GL.Vertex3(halfSize.x + _pos.x, -halfSize.y + _pos.y, 0.0f); } } GL.End(); }
public static exSprite NewSimpleSprite( GameObject _go, exTextureInfo _info, int _width, int _height, Color _color ) { exSprite sprite = _go.GetComponent<exSprite>(); if ( sprite == null ) { sprite = _go.AddComponent<exSprite>(); } if ( sprite.shader == null ) sprite.shader = Shader.Find("ex2D/Alpha Blended"); sprite.spriteType = exSpriteType.Simple; sprite.textureInfo = _info; sprite.customSize = true; sprite.width = _width; sprite.height = _height; sprite.color = _color; return sprite; }
public static exSprite NewSlicedSprite( GameObject _go, exTextureInfo _info, int _left, int _right, int _top, int _bottom, int _width, int _height, Color _color, bool _borderOnly ) { exSprite sprite = _go.GetComponent<exSprite>(); if ( sprite == null ) { sprite = _go.AddComponent<exSprite>(); } if ( sprite.shader == null ) sprite.shader = Shader.Find("ex2D/Alpha Blended"); sprite.spriteType = exSpriteType.Sliced; sprite.textureInfo = _info; sprite.borderOnly = _borderOnly; sprite.customBorderSize = true; sprite.leftBorderSize = _left; sprite.rightBorderSize = _right; sprite.topBorderSize = _top; sprite.bottomBorderSize = _bottom; sprite.customSize = true; sprite.width = _width; sprite.height = _height; sprite.color = _color; return sprite; }
public static void SetTextureInfo(exSpriteBase _sprite, ref exTextureInfo _ti, exTextureInfo _newTi, bool _useTextureOffset, exSpriteType _spriteType) { exTextureInfo old = _ti; _ti = _newTi; if (_newTi != null) { if (_newTi.texture == null) { Debug.LogWarning("invalid textureInfo"); } if (_spriteType == exSpriteType.Tiled) { if (old == null || ReferenceEquals(old, _newTi) || _newTi.width != old.width || _newTi.height != old.height) { (_sprite as exISprite).UpdateBufferSize (); _sprite.updateFlags |= exUpdateFlags.Vertex; // tile数量可能不变,但是间距可能会改变 } } else if (_spriteType == exSpriteType.Diced) { //if (_newTi.isDiced == false) { // Debug.LogWarning ("The texture info is not diced!"); //} (_sprite as exISprite).UpdateBufferSize (); _sprite.updateFlags |= exUpdateFlags.Vertex; } else { if (_sprite.customSize == false && (old == null || (_newTi.width != old.width || _newTi.height != old.height))) { _sprite.updateFlags |= exUpdateFlags.Vertex; } } if (_useTextureOffset) { _sprite.updateFlags |= exUpdateFlags.Vertex; } _sprite.updateFlags |= exUpdateFlags.UV; // 换了texture,UV也会重算,不换texture就更要改UV,否则没有换textureInfo的必要了。 if (old == null || ReferenceEquals(old.texture, _newTi.texture) == false) { // texture changed _sprite.updateFlags |= (exUpdateFlags.Vertex | exUpdateFlags.UV); (_sprite as exISprite).UpdateMaterial(); } } }
// ------------------------------------------------------------------ // Desc: // ------------------------------------------------------------------ public static bool Exists( exAtlas _atlas, exTextureInfo _textureInfo ) { return _atlas.textureInfos.IndexOf(_textureInfo) != -1; }
// TODO { // // ------------------------------------------------------------------ // // Desc: // // ------------------------------------------------------------------ // public override void OnPreviewSettings () { // } // } TODO end // ------------------------------------------------------------------ // Desc: // ------------------------------------------------------------------ public override void OnPreviewGUI(Rect _rect, GUIStyle _background) { if (Event.current.type != EventType.Repaint) { return; } exTextureInfo textureInfo = base.target as exTextureInfo; if (textureInfo == null || textureInfo.texture == null) { return; } _background.Draw(_rect, false, false, false, false); float ratio = Mathf.Min(Mathf.Min(_rect.width / (float)textureInfo.width, _rect.height / (float)textureInfo.height), 1.0f); float w = (float)textureInfo.width * ratio; float h = (float)textureInfo.height * ratio; Rect rect = new Rect(_rect.x + (_rect.width - w) * 0.5f, _rect.y + (_rect.height - h) * 0.5f, w, h); rect = exGeometryUtility.Rect_FloorToInt(rect); // EditorGUI.DrawPreviewTexture(rect, textureInfo.texture); exEditorUtility.GUI_DrawTextureInfo(rect, textureInfo, Color.white); // if ( textureInfo.rotated ) { // float xStart = (float)textureInfo.x/(float)textureInfo.texture.width; // float xEnd = xStart + (float)textureInfo.rotatedWidth/(float)textureInfo.texture.width; // float yStart = (float)textureInfo.y/(float)textureInfo.texture.height; // float yEnd = yStart + (float)textureInfo.rotatedHeight/(float)textureInfo.texture.height; // quadMaterial.mainTexture = textureInfo.texture; // quadMaterial.SetPass(0); // quadMesh.hideFlags = HideFlags.DontSave; // quadMesh.vertices = new Vector3[] { // new Vector3 ( rect.x, rect.y, 0.0f ), // new Vector3 ( rect.x, rect.y + rect.height, 0.0f ), // new Vector3 ( rect.x + rect.width, rect.y + rect.height, 0.0f ), // new Vector3 ( rect.x + rect.width, rect.y, 0.0f ), // }; // quadMesh.uv = new Vector2[] { // new Vector2 ( xStart, yStart ), // new Vector2 ( xEnd, yStart ), // new Vector2 ( xEnd, yEnd ), // new Vector2 ( xStart, yEnd ), // }; // quadMesh.colors32 = new Color32[] { // new Color32 ( 255, 255, 255, 255 ), // new Color32 ( 255, 255, 255, 255 ), // new Color32 ( 255, 255, 255, 255 ), // new Color32 ( 255, 255, 255, 255 ), // }; // quadMesh.triangles = new int[] { // 0, 1, 2, // 0, 2, 3 // }; // Graphics.DrawMeshNow ( quadMesh, Vector3.zero, Quaternion.identity ); // } // else { // float uv_s = (float)textureInfo.x / (float)textureInfo.texture.width; // float uv_t = (float)textureInfo.y / (float)textureInfo.texture.height; // float uv_w = (float)textureInfo.rotatedWidth / (float)textureInfo.texture.width; // float uv_h = (float)textureInfo.rotatedHeight / (float)textureInfo.texture.height; // GUI.DrawTextureWithTexCoords( rect, // textureInfo.texture, // new Rect( uv_s, uv_t, uv_w, uv_h ) ); // } }
// ------------------------------------------------------------------ // Desc: // ------------------------------------------------------------------ public void AddFrameAt(int _idx, exTextureInfo _info, int _frames = 1) { InsertFrameInfo(_idx, new FrameInfo(_info, _frames)); }
// ------------------------------------------------------------------ // Desc: // ------------------------------------------------------------------ public static void TiledUpdateBuffers(exSpriteBase _sprite, exTextureInfo _textureInfo, bool _useTextureOffset, Vector2 _tiledSpacing, Space _space, exList<Vector3> _vertices, exList<Vector2> _uvs, exList<int> _indices, int _vbIndex, int _ibIndex) { if (_vertices.Count == 0) { return; } SpriteBuilder.SimpleUpdateBuffers(_sprite, _textureInfo, _useTextureOffset, _space, _vertices, _uvs, _indices, _vbIndex, _ibIndex); if ((_sprite.updateFlags & exUpdateFlags.Vertex) != 0) { TiledUpdateVertexBuffer(_sprite, _textureInfo, _useTextureOffset, _tiledSpacing, _space, _vertices, _vbIndex); } int colCount, rowCount; exSpriteUtility.GetTilingCount ((exISprite)_sprite, out colCount, out rowCount); if ((_sprite.updateFlags & exUpdateFlags.Index) != 0 && _indices != null) { /* tile index: 8 9 10 11 4 5 6 7 0 1 2 3 */ int v = _vbIndex; int i = _ibIndex; int quadCount = colCount * rowCount; for (int q = 0; q < quadCount; ++q) { _indices.buffer[i++] = v; _indices.buffer[i++] = v + 1; _indices.buffer[i++] = v + 2; _indices.buffer[i++] = v + 2; _indices.buffer[i++] = v + 3; _indices.buffer[i++] = v; v += exMesh.QUAD_VERTEX_COUNT; } } if ((_sprite.updateFlags & exUpdateFlags.UV) != 0) { Vector2 uv0 = _uvs.buffer[_vbIndex + 0]; Vector2 uv2 = _uvs.buffer[_vbIndex + 2]; Vector2 uv3 = _uvs.buffer[_vbIndex + 3]; Vector2 lastTileRawSize = new Vector2(_sprite.width % (_textureInfo.width + _tiledSpacing.x), _sprite.height % (_textureInfo.height + _tiledSpacing.y)); Vector2 clippedUv2 = uv2; if (0.0f < lastTileRawSize.y && lastTileRawSize.y < _textureInfo.height) { // clipped last row float stepY = lastTileRawSize.y / _textureInfo.height; if (_textureInfo.rotated == false) clippedUv2.y = Mathf.Lerp(uv0.y, uv2.y, stepY); else clippedUv2.x = Mathf.Lerp(uv0.x, uv2.x, stepY); } if (0.0f < lastTileRawSize.x && lastTileRawSize.x < _textureInfo.width) { // clipped last column float stepX = lastTileRawSize.x / _textureInfo.width; if (_textureInfo.rotated == false) clippedUv2.x = Mathf.Lerp(uv0.x, uv2.x, stepX); else clippedUv2.y = Mathf.Lerp(uv0.y, uv2.y, stepX); } int i = _vbIndex; if (_textureInfo.rotated == false) { for (int r = 0; r < rowCount; ++r) { float rowTopUv = (r < rowCount - 1) ? uv2.y : clippedUv2.y; for (int c = 0; c < colCount; ++c) { _uvs.buffer[i++] = uv0; _uvs.buffer[i++] = new Vector2(uv0.x, rowTopUv); _uvs.buffer[i++] = new Vector2(uv2.x, rowTopUv); _uvs.buffer[i++] = uv3; } // clip last column _uvs.buffer[i - 2].x = clippedUv2.x; _uvs.buffer[i - 1].x = clippedUv2.x; } } else { for (int r = 0; r < rowCount; ++r) { float rowTopUv = (r < rowCount - 1) ? uv2.x : clippedUv2.x; for (int c = 0; c < colCount; ++c) { _uvs.buffer[i++] = uv0; _uvs.buffer[i++] = new Vector2(rowTopUv, uv0.y); _uvs.buffer[i++] = new Vector2(rowTopUv, uv2.y); _uvs.buffer[i++] = uv3; } // clip last column _uvs.buffer[i - 2].y = clippedUv2.y; _uvs.buffer[i - 1].y = clippedUv2.y; } } } }
/////////////////////////////////////////////////////////////////////////////// // Internal Functions /////////////////////////////////////////////////////////////////////////////// // ------------------------------------------------------------------ // Desc: // ------------------------------------------------------------------ void Init () { bool initialized = (nameToState != null); if (initialized == false) { sprite_ = GetComponent<exSprite>(); defaultTextureInfo = sprite_.textureInfo; nameToState = new Dictionary<string, exSpriteAnimationState>(); for (int i = 0; i < animations.Count; ++i) { exSpriteAnimationClip clip = animations[i]; if (clip != null) { exSpriteAnimationState state = new exSpriteAnimationState(clip); nameToState[state.name] = state; if (ReferenceEquals(defaultAnimation, clip)) { curAnimation = state; lastFrameIndex = -1; } } } exDebug.Assert(defaultAnimation == null || defaultAnimation == nameToState[defaultAnimation.name].clip); } }
public int frames = 1; ///< frame count public FrameInfo(exTextureInfo _textureInfo, int _frames) { textureInfo = _textureInfo; frames = _frames; }