public void MoveToBack(SpriteMesh_Managed s) { int[] array = new int[6]; int num = this.spriteDrawOrder.IndexOf(s) * 6; if (num < 0) { return; } array[0] = this.triIndices[num]; array[1] = this.triIndices[num + 1]; array[2] = this.triIndices[num + 2]; array[3] = this.triIndices[num + 3]; array[4] = this.triIndices[num + 4]; array[5] = this.triIndices[num + 5]; for (int i = num; i > 5; i -= 6) { this.triIndices[i] = this.triIndices[i - 6]; this.triIndices[i + 1] = this.triIndices[i - 5]; this.triIndices[i + 2] = this.triIndices[i - 4]; this.triIndices[i + 3] = this.triIndices[i - 3]; this.triIndices[i + 4] = this.triIndices[i - 2]; this.triIndices[i + 5] = this.triIndices[i - 1]; this.spriteDrawOrder[i / 6] = this.spriteDrawOrder[i / 6 - 1]; } this.triIndices[0] = array[0]; this.triIndices[1] = array[1]; this.triIndices[2] = array[2]; this.triIndices[3] = array[3]; this.triIndices[4] = array[4]; this.triIndices[5] = array[5]; this.spriteDrawOrder[0] = s; this.vertCountChanged = true; }
public void MoveToFront(SpriteMesh_Managed s) { int[] array = new int[6]; int num = this.spriteDrawOrder.IndexOf(s) * 6; if (num < 0) { return; } array[0] = this.triIndices[num]; array[1] = this.triIndices[num + 1]; array[2] = this.triIndices[num + 2]; array[3] = this.triIndices[num + 3]; array[4] = this.triIndices[num + 4]; array[5] = this.triIndices[num + 5]; for (int i = num; i < this.triIndices.Length - 6; i += 6) { this.triIndices[i] = this.triIndices[i + 6]; this.triIndices[i + 1] = this.triIndices[i + 7]; this.triIndices[i + 2] = this.triIndices[i + 8]; this.triIndices[i + 3] = this.triIndices[i + 9]; this.triIndices[i + 4] = this.triIndices[i + 10]; this.triIndices[i + 5] = this.triIndices[i + 11]; this.spriteDrawOrder[i / 6] = this.spriteDrawOrder[i / 6 + 1]; } this.triIndices[this.triIndices.Length - 6] = array[0]; this.triIndices[this.triIndices.Length - 5] = array[1]; this.triIndices[this.triIndices.Length - 4] = array[2]; this.triIndices[this.triIndices.Length - 3] = array[3]; this.triIndices[this.triIndices.Length - 2] = array[4]; this.triIndices[this.triIndices.Length - 1] = array[5]; this.spriteDrawOrder[this.spriteDrawOrder.Count - 1] = s; this.vertCountChanged = true; }
public void SortDrawingOrder() { this.spriteDrawOrder.Sort(this.drawOrderComparer); if (this.winding == SpriteRoot.WINDING_ORDER.CCW) { for (int i = 0; i < this.spriteDrawOrder.Count; i++) { SpriteMesh_Managed spriteMesh_Managed = this.spriteDrawOrder[i]; this.triIndices[i * 6] = spriteMesh_Managed.mv1; this.triIndices[i * 6 + 1] = spriteMesh_Managed.mv2; this.triIndices[i * 6 + 2] = spriteMesh_Managed.mv4; this.triIndices[i * 6 + 3] = spriteMesh_Managed.mv4; this.triIndices[i * 6 + 4] = spriteMesh_Managed.mv2; this.triIndices[i * 6 + 5] = spriteMesh_Managed.mv3; } } else { for (int j = 0; j < this.spriteDrawOrder.Count; j++) { SpriteMesh_Managed spriteMesh_Managed = this.spriteDrawOrder[j]; this.triIndices[j * 6] = spriteMesh_Managed.mv1; this.triIndices[j * 6 + 1] = spriteMesh_Managed.mv4; this.triIndices[j * 6 + 2] = spriteMesh_Managed.mv2; this.triIndices[j * 6 + 3] = spriteMesh_Managed.mv4; this.triIndices[j * 6 + 4] = spriteMesh_Managed.mv3; this.triIndices[j * 6 + 5] = spriteMesh_Managed.mv2; } } this.vertCountChanged = true; }
// Removes the sprite with the specified managed mesh from the manager. public void RemoveSprite(SpriteMesh_Managed sprite) { vertices[sprite.mv1] = Vector3.zero; vertices[sprite.mv2] = Vector3.zero; vertices[sprite.mv3] = Vector3.zero; vertices[sprite.mv4] = Vector3.zero; // Remove the sprite from the billboarded list // since that list should only contain active // sprites: /* if (sprite.billboarded) activeBillboards.Remove(sprite); else */ activeBlocks.Remove(sprite); // Reset the bone: if (gameObject != null) bones[sprite.index] = transform; // Clean the sprite's settings: sprite.Clear(); sprite.sprite.spriteMesh = null; sprite.sprite = null; availableBlocks.Add(sprite); vertsChanged = true; }
/// <summary> /// Instantiates the specified prefab, which should contain /// a sprite, and immediately adds it to the manager. /// </summary> /// <param name="prefab">Prefab to be instantiated.</param> /// <param name="position">Where to place the new sprite.</param> /// <param name="rotation">Rotation of the new sprite.</param> /// <returns>Reference to the sprite contained in the prafab.</returns> public SpriteRoot CreateSprite(GameObject prefab, Vector3 position, Quaternion rotation) { GameObject go = (GameObject) Instantiate(prefab, position, rotation); SpriteMesh_Managed sm = AddSprite(go); if (sm == null) return null; return sm.sprite; }
public SpriteRoot CreateSprite(GameObject prefab, Vector3 position, Quaternion rotation) { GameObject go = (GameObject)UnityEngine.Object.Instantiate(prefab, position, rotation); SpriteMesh_Managed spriteMesh_Managed = this.AddSprite(go); if (spriteMesh_Managed == null) { return(null); } return(spriteMesh_Managed.sprite); }
protected void SetupBoneWeights(SpriteMesh_Managed s) { this.boneWeights[s.mv1].boneIndex0 = s.index; this.boneWeights[s.mv1].weight0 = 1f; this.boneWeights[s.mv2].boneIndex0 = s.index; this.boneWeights[s.mv2].weight0 = 1f; this.boneWeights[s.mv3].boneIndex0 = s.index; this.boneWeights[s.mv3].weight0 = 1f; this.boneWeights[s.mv4].boneIndex0 = s.index; this.boneWeights[s.mv4].weight0 = 1f; }
// Setups up bone weights for a sprite protected void SetupBoneWeights(SpriteMesh_Managed s) { // Each bone element corresponds to each sprite element: boneWeights[s.mv1].boneIndex0 = s.index; boneWeights[s.mv1].weight0 = 1f; boneWeights[s.mv2].boneIndex0 = s.index; boneWeights[s.mv2].weight0 = 1f; boneWeights[s.mv3].boneIndex0 = s.index; boneWeights[s.mv3].weight0 = 1f; boneWeights[s.mv4].boneIndex0 = s.index; boneWeights[s.mv4].weight0 = 1f; }
/// <summary> /// Moves the first sprite behind the second sprite by /// placing it earlier in the draw order. If the sprite /// is already behind, nothing is done. /// </summary> /// <param name="toMove">Sprite to move behind the reference sprite.</param> /// <param name="reference">Sprite behind which "toMove" will be drawn.</param> public void MoveBehind(SpriteMesh_Managed toMove, SpriteMesh_Managed reference) { int[] indices = new int[6]; int offset = spriteDrawOrder.IndexOf(toMove) * 6; int refOffset = spriteDrawOrder.IndexOf(reference) * 6; if (offset < 0) { return; } // Check to see if the sprite is already behind: if (offset < refOffset) { return; } toMove.drawLayer = reference.drawLayer - 1; // Save our indices: indices[0] = triIndices[offset]; indices[1] = triIndices[offset + 1]; indices[2] = triIndices[offset + 2]; indices[3] = triIndices[offset + 3]; indices[4] = triIndices[offset + 4]; indices[5] = triIndices[offset + 5]; // Shift all indices from here to the reference sprite up 6 slots (each sprite occupies 6 index slots): for (int i = offset; i > refOffset; i -= 6) { triIndices[i] = triIndices[i - 6]; triIndices[i + 1] = triIndices[i - 5]; triIndices[i + 2] = triIndices[i - 4]; triIndices[i + 3] = triIndices[i - 3]; triIndices[i + 4] = triIndices[i - 2]; triIndices[i + 5] = triIndices[i - 1]; spriteDrawOrder[i / 6] = spriteDrawOrder[i / 6 - 1]; } // Place our desired index value at the destination: triIndices[refOffset] = indices[0]; triIndices[refOffset + 1] = indices[1]; triIndices[refOffset + 2] = indices[2]; triIndices[refOffset + 3] = indices[3]; triIndices[refOffset + 4] = indices[4]; triIndices[refOffset + 5] = indices[5]; // Update the sprite's index offset: spriteDrawOrder[refOffset / 6] = toMove; vertCountChanged = true; }
/// <summary> /// Moves the first sprite in front of the second sprite by /// placing it later in the draw order. If the sprite is already /// in front of the reference sprite, nothing is changed. /// </summary> /// <param name="toMove">Sprite to move in front of the reference sprite.</param> /// <param name="reference">Sprite in front of which "toMove" will be drawn.</param> public void MoveInfrontOf(SpriteMesh_Managed toMove, SpriteMesh_Managed reference) { int[] indices = new int[6]; int offset = spriteDrawOrder.IndexOf(toMove) * 6; int refOffset = spriteDrawOrder.IndexOf(reference) * 6; if (offset < 0) { return; } // Check to see if the sprite is already in front: if (offset > refOffset) { return; } // Save our indices: indices[0] = triIndices[offset]; indices[1] = triIndices[offset + 1]; indices[2] = triIndices[offset + 2]; indices[3] = triIndices[offset + 3]; indices[4] = triIndices[offset + 4]; indices[5] = triIndices[offset + 5]; // Shift all indices from here to the reference sprite down 6 slots (each sprite occupies 6 index slots): for (int i = offset; i < refOffset; i += 6) { triIndices[i] = triIndices[i + 6]; triIndices[i + 1] = triIndices[i + 7]; triIndices[i + 2] = triIndices[i + 8]; triIndices[i + 3] = triIndices[i + 9]; triIndices[i + 4] = triIndices[i + 10]; triIndices[i + 5] = triIndices[i + 11]; spriteDrawOrder[i / 6] = spriteDrawOrder[i / 6 + 1]; } // Place our desired index value at the destination: triIndices[refOffset] = indices[0]; triIndices[refOffset + 1] = indices[1]; triIndices[refOffset + 2] = indices[2]; triIndices[refOffset + 3] = indices[3]; triIndices[refOffset + 4] = indices[4]; triIndices[refOffset + 5] = indices[5]; // Update the sprite's index offset: spriteDrawOrder[refOffset / 6] = toMove; vertCountChanged = true; }
/// <summary> /// Moves the specified sprite to the end of the drawing order. /// (Causes it to appear in front of other sprites.) /// </summary> /// <param name="s">Sprite to move to the end of the drawing order.</param> public void MoveToFront(SpriteMesh_Managed s) { int[] indices = new int[6]; int offset = spriteDrawOrder.IndexOf(s) * 6; if (offset < 0) { return; } s.drawLayer = spriteDrawOrder[spriteDrawOrder.Count - 1].drawLayer + 1; // Save our indices: indices[0] = triIndices[offset]; indices[1] = triIndices[offset + 1]; indices[2] = triIndices[offset + 2]; indices[3] = triIndices[offset + 3]; indices[4] = triIndices[offset + 4]; indices[5] = triIndices[offset + 5]; // Shift all indices from here forward down 6 slots (each sprite occupies 6 index slots): for (int i = offset; i < triIndices.Length - 6; i += 6) { triIndices[i] = triIndices[i + 6]; triIndices[i + 1] = triIndices[i + 7]; triIndices[i + 2] = triIndices[i + 8]; triIndices[i + 3] = triIndices[i + 9]; triIndices[i + 4] = triIndices[i + 10]; triIndices[i + 5] = triIndices[i + 11]; spriteDrawOrder[i / 6] = spriteDrawOrder[i / 6 + 1]; } // Place our desired index value at the end: triIndices[triIndices.Length - 6] = indices[0]; triIndices[triIndices.Length - 5] = indices[1]; triIndices[triIndices.Length - 4] = indices[2]; triIndices[triIndices.Length - 3] = indices[3]; triIndices[triIndices.Length - 2] = indices[4]; triIndices[triIndices.Length - 1] = indices[5]; // Update the sprite's index offset: spriteDrawOrder[spriteDrawOrder.Count - 1] = s; vertCountChanged = true; }
public void RemoveSprite(SpriteMesh_Managed sprite) { this.vertices[sprite.mv1] = Vector3.zero; this.vertices[sprite.mv2] = Vector3.zero; this.vertices[sprite.mv3] = Vector3.zero; this.vertices[sprite.mv4] = Vector3.zero; this.activeBlocks.Remove(sprite); if (base.gameObject != null) { this.bones[sprite.index] = base.transform; } sprite.Clear(); sprite.sprite.spriteMesh = null; sprite.sprite = null; this.availableBlocks.Add(sprite); this.vertsChanged = true; }
/// <summary> /// Moves the specified sprite to the start of the drawing order. /// (Causes it to appear behind other sprites.) /// </summary> /// <param name="s">Sprite to move to the start of the drawing order.</param> public void MoveToBack(SpriteMesh_Managed s) { int[] indices = new int[6]; int offset = spriteDrawOrder.IndexOf(s) * 6; if (offset < 0) { return; } s.drawLayer = spriteDrawOrder[0].drawLayer - 1; // Save our indices: indices[0] = triIndices[offset]; indices[1] = triIndices[offset + 1]; indices[2] = triIndices[offset + 2]; indices[3] = triIndices[offset + 3]; indices[4] = triIndices[offset + 4]; indices[5] = triIndices[offset + 5]; // Shift all indices from here back up 6 slots (each sprite occupies 6 index slots): for (int i = offset; i > 5; i -= 6) { triIndices[i] = triIndices[i - 6]; triIndices[i + 1] = triIndices[i - 5]; triIndices[i + 2] = triIndices[i - 4]; triIndices[i + 3] = triIndices[i - 3]; triIndices[i + 4] = triIndices[i - 2]; triIndices[i + 5] = triIndices[i - 1]; spriteDrawOrder[i / 6] = spriteDrawOrder[i / 6 - 1]; } // Place our desired index value at the beginning: triIndices[0] = indices[0]; triIndices[1] = indices[1]; triIndices[2] = indices[2]; triIndices[3] = indices[3]; triIndices[4] = indices[4]; triIndices[5] = indices[5]; // Update the sprite's index offset: spriteDrawOrder[0] = s; vertCountChanged = true; }
// Allocates initial arrays protected void InitArrays() { bones = new Transform[1]; bones[0] = transform; // Just give it something for now bindPoses = new Matrix4x4[1]; sprites = new SpriteMesh_Managed[1]; sprites[0] = new SpriteMesh_Managed(); vertices = new Vector3[4]; UVs = new Vector2[4]; colors = new Color[4]; triIndices = new int[6]; boneWeights = new BoneWeight[4]; sprites[0].index = 0; sprites[0].mv1 = 0; sprites[0].mv2 = 1; sprites[0].mv3 = 2; sprites[0].mv4 = 3; SetupBoneWeights(sprites[0]); }
public void MoveBehind(SpriteMesh_Managed toMove, SpriteMesh_Managed reference) { int[] array = new int[6]; int num = this.spriteDrawOrder.IndexOf(toMove) * 6; int num2 = this.spriteDrawOrder.IndexOf(reference) * 6; if (num < 0) { return; } if (num < num2) { return; } array[0] = this.triIndices[num]; array[1] = this.triIndices[num + 1]; array[2] = this.triIndices[num + 2]; array[3] = this.triIndices[num + 3]; array[4] = this.triIndices[num + 4]; array[5] = this.triIndices[num + 5]; for (int i = num; i > num2; i -= 6) { this.triIndices[i] = this.triIndices[i - 6]; this.triIndices[i + 1] = this.triIndices[i - 5]; this.triIndices[i + 2] = this.triIndices[i - 4]; this.triIndices[i + 3] = this.triIndices[i - 3]; this.triIndices[i + 4] = this.triIndices[i - 2]; this.triIndices[i + 5] = this.triIndices[i - 1]; this.spriteDrawOrder[i / 6] = this.spriteDrawOrder[i / 6 - 1]; } this.triIndices[num2] = array[0]; this.triIndices[num2 + 1] = array[1]; this.triIndices[num2 + 2] = array[2]; this.triIndices[num2 + 3] = array[3]; this.triIndices[num2 + 4] = array[4]; this.triIndices[num2 + 5] = array[5]; this.spriteDrawOrder[num2 / 6] = toMove; this.vertCountChanged = true; }
public SpriteMesh_Managed AddSprite(SpriteRoot sprite) { if (sprite.manager == this && sprite.AddedToManager) { return((SpriteMesh_Managed)sprite.spriteMesh); } if (!this.initialized) { if (this.spriteAddQueue == null) { this.spriteAddQueue = new List <SpriteRoot>(); } this.spriteAddQueue.Add(sprite); return(null); } if (this.availableBlocks.Empty) { this.EnlargeArrays(this.allocBlockSize); } int index = this.availableBlocks.Head.index; this.availableBlocks.Remove(this.availableBlocks.Head); SpriteMesh_Managed spriteMesh_Managed = this.sprites[index]; sprite.spriteMesh = spriteMesh_Managed; sprite.manager = this; sprite.AddedToManager = true; spriteMesh_Managed.drawLayer = sprite.drawLayer; this.bones[index] = sprite.gameObject.transform; this.bindPoses[index] = this.bones[index].worldToLocalMatrix * sprite.transform.localToWorldMatrix; this.activeBlocks.Add(spriteMesh_Managed); spriteMesh_Managed.Init(); this.SortDrawingOrder(); this.vertCountChanged = true; this.vertsChanged = true; this.uvsChanged = true; return(spriteMesh_Managed); }
/// <summary> /// Adds the specified sprite to the SpriteManager. /// </summary> /// <param name="go">Reference to the desired sprite.</param> /// <returns>A reference to the sprite's managed mesh.</returns> public SpriteMesh_Managed AddSprite(SpriteRoot sprite) { int spriteIndex; // See if the sprite is already added: if(sprite.manager == this && sprite.AddedToManager) return (SpriteMesh_Managed)sprite.spriteMesh; /* if (ensureUnique) if (AlreadyAdded(sprite)) return (SpriteMesh_Managed) sprite.spriteMesh; */ // See if we're ready to add sprites yet, // and if not, add the sprite to our deferred // add queue: if(!initialized) { if (spriteAddQueue == null) spriteAddQueue = new List<SpriteRoot>(); spriteAddQueue.Add(sprite); return null; } // Get an available sprite: if (availableBlocks.Empty) EnlargeArrays(allocBlockSize); // If we're out of available sprites, allocate some more: // Use a sprite from the list of available blocks: spriteIndex = availableBlocks.Head.index; availableBlocks.Remove(availableBlocks.Head); // Now that we're using this one, remove it from the available list // Assign the new sprite: SpriteMesh_Managed newSpritesMesh = sprites[spriteIndex]; sprite.spriteMesh = newSpritesMesh; sprite.manager = this; sprite.AddedToManager = true; newSpritesMesh.drawLayer = sprite.drawLayer; // Associate the sprite's bone: bones[spriteIndex] = sprite.gameObject.transform; // meshRenderer.bones = bones; bindPoses[spriteIndex] = bones[spriteIndex].worldToLocalMatrix * sprite.transform.localToWorldMatrix; /* // Save this to an active list now that it is in-use: if (billboarded) { newSprite.billboarded = true; activeBillboards.Add(newSprite); } else */ activeBlocks.Add(newSpritesMesh); newSpritesMesh.Init(); // Sort the draw layers: SortDrawingOrder(); // Set our flags: vertCountChanged = true; vertsChanged = true; uvsChanged = true; return newSpritesMesh; }
// Enlarges the sprite array by the specified count and also resizes // the UV and vertex arrays by the necessary corresponding amount. // Returns the index of the first newly allocated element // (ex: if the sprite array was already 10 elements long and is // enlarged by 10 elements resulting in a total length of 20, // EnlargeArrays() will return 10, indicating that element 10 is the // first of the newly allocated elements.) protected int EnlargeArrays(int count) { int firstNewElement; if (sprites == null) { InitArrays(); firstNewElement = 0; count = count - 1; // Allocate one less since InitArrays already allocated one sprite for us } else firstNewElement = sprites.Length; // Resize sprite array: SpriteMesh_Managed[] tempSprites = sprites; sprites = new SpriteMesh_Managed[sprites.Length + count]; tempSprites.CopyTo(sprites, 0); // Resize the bone array: Transform[] tempBones = bones; bones = new Transform[bones.Length + count]; tempBones.CopyTo(bones, 0); // Resize the bind poses array: Matrix4x4[] tempPoses = bindPoses; bindPoses = new Matrix4x4[bindPoses.Length + count]; tempPoses.CopyTo(bindPoses, 0); // Vertices: Vector3[] tempVerts = vertices; vertices = new Vector3[vertices.Length + count * 4]; tempVerts.CopyTo(vertices, 0); // BoneWeights: BoneWeight[] tempWeights = boneWeights; boneWeights = new BoneWeight[boneWeights.Length + count * 4]; tempWeights.CopyTo(boneWeights, 0); // UVs: Vector2[] tempUVs = UVs; UVs = new Vector2[UVs.Length + count * 4]; tempUVs.CopyTo(UVs, 0); // Colors: Color[] tempColors = colors; colors = new Color[colors.Length + count * 4]; tempColors.CopyTo(colors, 0); // Triangle indices: int[] tempTris = triIndices; triIndices = new int[triIndices.Length + count * 6]; tempTris.CopyTo(triIndices, 0); // Inform existing sprites of the new vertex and UV buffers: for (int i = 0; i < firstNewElement; ++i) { sprites[i].SetBuffers(vertices, UVs, colors); } // Setup the newly-added sprites and Add them to the list of available // sprite blocks. Also initialize the triangle indices while we're at it: for (int i = firstNewElement; i < sprites.Length; ++i) { // Create and setup sprite: sprites[i] = new SpriteMesh_Managed(); sprites[i].index = i; sprites[i].manager = this; sprites[i].SetBuffers(vertices, UVs, colors); // Setup indices of the sprite's vertices in the vertex buffer: sprites[i].mv1 = i * 4 + 0; sprites[i].mv2 = i * 4 + 1; sprites[i].mv3 = i * 4 + 2; sprites[i].mv4 = i * 4 + 3; // Setup the indices of the sprite's UV entries in the UV buffer: sprites[i].uv1 = i * 4 + 0; sprites[i].uv2 = i * 4 + 1; sprites[i].uv3 = i * 4 + 2; sprites[i].uv4 = i * 4 + 3; // Setup the indices to the color values: sprites[i].cv1 = i * 4 + 0; sprites[i].cv2 = i * 4 + 1; sprites[i].cv3 = i * 4 + 2; sprites[i].cv4 = i * 4 + 3; // Add as an available sprite: availableBlocks.Add(sprites[i]); // Init triangle indices: /* if (winding == SpriteRoot.WINDING_ORDER.CCW) { // Counter-clockwise winding triIndices[i * 6 + 0] = i * 4 + 0; // 0_ 2 0 ___ 3 triIndices[i * 6 + 1] = i * 4 + 1; // | / Verts: | /| triIndices[i * 6 + 2] = i * 4 + 3; // 1|/ 1|/__|2 triIndices[i * 6 + 3] = i * 4 + 3; // 3 triIndices[i * 6 + 4] = i * 4 + 1; // /| triIndices[i * 6 + 5] = i * 4 + 2; // 4/_|5 } else */ { // Clockwise winding triIndices[i * 6 + 0] = i * 4 + 0; // 0_ 1 0 ___ 3 triIndices[i * 6 + 1] = i * 4 + 3; // | / Verts: | /| triIndices[i * 6 + 2] = i * 4 + 1; // 2|/ 1|/__|2 triIndices[i * 6 + 3] = i * 4 + 3; // 3 triIndices[i * 6 + 4] = i * 4 + 2; // /| triIndices[i * 6 + 5] = i * 4 + 1; // 5/_|4 } // Add the index of this sprite to the draw order list spriteDrawOrder.Add(sprites[i]); // Give the bones something to point to: bones[i] = transform; // Setup a default bindpose: bindPoses[i] = bones[i].worldToLocalMatrix * transform.localToWorldMatrix; // Setup the weights: SetupBoneWeights(sprites[i]); } vertsChanged = true; uvsChanged = true; colorsChanged = true; vertCountChanged = true; return firstNewElement; }