Ejemplo n.º 1
0
 public void Clear()
 {
     attachments.Clear(clearArray: false);
     rawVertexCount    = -1;
     hasActiveClipping = false;
     submeshInstructions.Clear(clearArray: false);
 }
Ejemplo n.º 2
0
        public void SetWithSubset(ExposedList <SubmeshInstruction> instructions, int startSubmesh, int endSubmesh)
        {
            int num = 0;
            ExposedList <SubmeshInstruction> submeshInstructions = this.submeshInstructions;

            submeshInstructions.Clear(false);
            int newSize = endSubmesh - startSubmesh;

            submeshInstructions.Resize(newSize);
            SubmeshInstruction[] items             = submeshInstructions.Items;
            SubmeshInstruction[] instructionArray2 = instructions.Items;
            for (int i = 0; i < newSize; i++)
            {
                SubmeshInstruction instruction = instructionArray2[startSubmesh + i];
                items[i] = instruction;
                this.hasActiveClipping      |= instruction.hasClipping;
                items[i].rawFirstVertexIndex = num;
                num += instruction.rawVertexCount;
            }
            this.rawVertexCount = num;
            int startSlot = instructionArray2[startSubmesh].startSlot;
            int endSlot   = instructionArray2[endSubmesh - 1].endSlot;

            this.attachments.Clear(false);
            int num6 = endSlot - startSlot;

            this.attachments.Resize(num6);
            Attachment[] attachmentArray = this.attachments.Items;
            Slot[]       slotArray       = instructionArray2[0].skeleton.drawOrder.Items;
            for (int j = 0; j < num6; j++)
            {
                attachmentArray[j] = slotArray[startSlot + j].attachment;
            }
        }
Ejemplo n.º 3
0
        public void SetWithSubset(ExposedList <SubmeshInstruction> instructions, int startSubmesh, int endSubmesh)
        {
            int num = 0;
            ExposedList <SubmeshInstruction> exposedList = submeshInstructions;

            exposedList.Clear(clearArray: false);
            int num2 = endSubmesh - startSubmesh;

            exposedList.Resize(num2);
            SubmeshInstruction[] items  = exposedList.Items;
            SubmeshInstruction[] items2 = instructions.Items;
            for (int i = 0; i < num2; i++)
            {
                SubmeshInstruction submeshInstruction = items2[startSubmesh + i];
                items[i]                     = submeshInstruction;
                hasActiveClipping            = submeshInstruction.hasClipping;
                items[i].rawFirstVertexIndex = num;
                num += submeshInstruction.rawVertexCount;
            }
            rawVertexCount = num;
            int startSlot = items2[startSubmesh].startSlot;
            int endSlot   = items2[endSubmesh - 1].endSlot;

            attachments.Clear(clearArray: false);
            int num3 = endSlot - startSlot;

            attachments.Resize(num3);
            Attachment[] items3 = attachments.Items;
            Slot[]       items4 = items2[0].skeleton.drawOrder.Items;
            for (int j = 0; j < num3; j++)
            {
                items3[j] = items4[startSlot + j].attachment;
            }
        }
Ejemplo n.º 4
0
 public void ClipEnd()
 {
     if (clipAttachment != null)
     {
         clipAttachment   = null;
         clippingPolygons = null;
         clippedVertices.Clear();
         clippedTriangles.Clear();
         clippingPolygon.Clear();
     }
 }
Ejemplo n.º 5
0
 public void Begin()
 {
     vertexBuffer.Clear(clearArray: false);
     colorBuffer.Clear(clearArray: false);
     uvBuffer.Clear(clearArray: false);
     clipper.ClipEnd();
     meshBoundsMin.x     = float.PositiveInfinity;
     meshBoundsMin.y     = float.PositiveInfinity;
     meshBoundsMax.x     = float.NegativeInfinity;
     meshBoundsMax.y     = float.NegativeInfinity;
     meshBoundsThickness = 0f;
     submeshes.Count     = 1;
     submeshes.Items[0].Clear(clearArray: false);
     submeshIndex = 0;
 }
        public void Clear()
        {
            skeleton = null;
            canvasRenderer.Clear();

            for (int i = 0; i < canvasRenderers.Count; ++i)
            {
                canvasRenderers[i].Clear();
            }
            foreach (var mesh in meshes)
            {
                Destroy(mesh);
            }
            meshes.Clear();
        }
Ejemplo n.º 7
0
        public void Set(Vector3[] verts, Vector2[] uvs, Color32[] colors, SubmeshedMeshInstructions instructions)
        {
            mesh.vertices = verts;
            mesh.uv       = uvs;
            mesh.colors32 = colors;

            attachmentsUsed.Clear();
            attachmentsUsed.GrowIfNeeded(instructions.attachmentList.Capacity);
            attachmentsUsed.Count = instructions.attachmentList.Count;
            instructions.attachmentList.CopyTo(attachmentsUsed.Items);

            instructionsUsed.Clear();
            instructionsUsed.GrowIfNeeded(instructions.submeshInstructions.Capacity);
            instructionsUsed.Count = instructions.submeshInstructions.Count;
            instructions.submeshInstructions.CopyTo(instructionsUsed.Items);
        }
Ejemplo n.º 8
0
            public void Set(Vector3[] verts, Vector2[] uvs, Color32[] colors, ExposedList <Attachment> attachments, ExposedList <SubmeshInstruction> instructions)
            {
                mesh.vertices = verts;
                mesh.uv       = uvs;
                mesh.colors32 = colors;

                attachmentsUsed.Clear(false);
                attachmentsUsed.GrowIfNeeded(attachments.Capacity);
                attachmentsUsed.Count = attachments.Count;
                attachments.CopyTo(attachmentsUsed.Items);

                instructionsUsed.Clear(false);
                instructionsUsed.GrowIfNeeded(instructions.Capacity);
                instructionsUsed.Count = instructions.Count;
                instructions.CopyTo(instructionsUsed.Items);
            }
Ejemplo n.º 9
0
        public void Apply(Skeleton skeleton, float lastTime, float time, ExposedList <Event> firedEvents, float alpha)
        {
            float[] array = this.frames;
            if (time < array[0])
            {
                return;
            }
            int num;

            if (time >= array[array.Length - 1])
            {
                num = array.Length - 1;
            }
            else
            {
                num = Animation.binarySearch(array, time) - 1;
            }
            ExposedList <Slot> drawOrder = skeleton.drawOrder;
            ExposedList <Slot> slots     = skeleton.slots;

            int[] array2 = this.drawOrders[num];
            if (array2 == null)
            {
                drawOrder.Clear(true);
                int i     = 0;
                int count = slots.Count;
                while (i < count)
                {
                    drawOrder.Add(slots.Items[i]);
                    i++;
                }
            }
            else
            {
                Slot[] items  = drawOrder.Items;
                Slot[] items2 = slots.Items;
                int    j      = 0;
                int    num2   = array2.Length;
                while (j < num2)
                {
                    items[j] = items2[array2[j]];
                    j++;
                }
            }
        }
Ejemplo n.º 10
0
        static void FireEvents(ExposedList <Spine.Event> eventList, float weight, SkeletonAnimatorEventDelegate callback)
        {
            int eventsCount = eventList.Count;

            if (eventsCount > 0)
            {
                var eventListItems = eventList.Items;
                for (int i = 0; i < eventsCount; i++)
                {
                    if (callback != null)
                    {
                        callback(eventListItems[i], weight);
                    }
                }

                eventList.Clear(false);
            }
        }
Ejemplo n.º 11
0
        protected void DestroyMeshes()
        {
            foreach (var mesh in meshes)
            {
#if UNITY_EDITOR
                if (Application.isEditor && !Application.isPlaying)
                {
                    UnityEngine.Object.DestroyImmediate(mesh);
                }
                else
                {
                    UnityEngine.Object.Destroy(mesh);
                }
#else
                UnityEngine.Object.Destroy(mesh);
#endif
            }
            meshes.Clear();
        }
Ejemplo n.º 12
0
    void Update()
    {
        if (!valid)
        {
            return;
        }

        if (layerMixModes.Length != animator.layerCount)
        {
            System.Array.Resize <MixMode>(ref layerMixModes, animator.layerCount);
        }
        float deltaTime = Time.time - lastTime;

        skeleton.Update(Time.deltaTime);

        //apply
        int layerCount = animator.layerCount;

        for (int i = 0; i < layerCount; i++)
        {
            events.Clear();

            float layerWeight = animator.GetLayerWeight(i);
            if (i == 0)
            {
                layerWeight = 1;
            }

            var stateInfo     = animator.GetCurrentAnimatorStateInfo(i);
            var nextStateInfo = animator.GetNextAnimatorStateInfo(i);

#if UNITY_5
            var clipInfo     = animator.GetCurrentAnimatorClipInfo(i);
            var nextClipInfo = animator.GetNextAnimatorClipInfo(i);
#else
            var clipInfo     = animator.GetCurrentAnimationClipState(i);
            var nextClipInfo = animator.GetNextAnimationClipState(i);
#endif
            MixMode mode = layerMixModes[i];

            if (mode == MixMode.AlwaysMix)
            {
                //always use Mix instead of Applying the first non-zero weighted clip
                for (int c = 0; c < clipInfo.Length; c++)
                {
                    var   info   = clipInfo[c];
                    float weight = info.weight * layerWeight;
                    if (weight == 0)
                    {
                        continue;
                    }

                    float time = stateInfo.normalizedTime * info.clip.length;
                    animationTable[GetAnimationClipNameHashCode(info.clip)].Mix(skeleton, Mathf.Max(0, time - deltaTime), time, stateInfo.loop, events, weight);
                }

                if (nextStateInfo.fullPathHash != 0)
                {
                    for (int c = 0; c < nextClipInfo.Length; c++)
                    {
                        var   info   = nextClipInfo[c];
                        float weight = info.weight * layerWeight;
                        if (weight == 0)
                        {
                            continue;
                        }

                        float time = nextStateInfo.normalizedTime * info.clip.length;
                        animationTable[GetAnimationClipNameHashCode(info.clip)].Mix(skeleton, Mathf.Max(0, time - deltaTime), time, nextStateInfo.loop, events, weight);
                    }
                }
            }
            else if (mode >= MixMode.MixNext)
            {
                //apply first non-zero weighted clip
                int c = 0;

                for (; c < clipInfo.Length; c++)
                {
                    var   info   = clipInfo[c];
                    float weight = info.weight * layerWeight;
                    if (weight == 0)
                    {
                        continue;
                    }

                    float time = stateInfo.normalizedTime * info.clip.length;
                    animationTable[GetAnimationClipNameHashCode(info.clip)].Apply(skeleton, Mathf.Max(0, time - deltaTime), time, stateInfo.loop, events);
                    break;
                }

                //mix the rest
                for (; c < clipInfo.Length; c++)
                {
                    var   info   = clipInfo[c];
                    float weight = info.weight * layerWeight;
                    if (weight == 0)
                    {
                        continue;
                    }

                    float time = stateInfo.normalizedTime * info.clip.length;
                    animationTable[GetAnimationClipNameHashCode(info.clip)].Mix(skeleton, Mathf.Max(0, time - deltaTime), time, stateInfo.loop, events, weight);
                }

                c = 0;

                if (nextStateInfo.fullPathHash != 0)
                {
                    //apply next clip directly instead of mixing (ie:  no crossfade, ignores mecanim transition weights)
                    if (mode == MixMode.SpineStyle)
                    {
                        for (; c < nextClipInfo.Length; c++)
                        {
                            var   info   = nextClipInfo[c];
                            float weight = info.weight * layerWeight;
                            if (weight == 0)
                            {
                                continue;
                            }

                            float time = nextStateInfo.normalizedTime * info.clip.length;
                            animationTable[GetAnimationClipNameHashCode(info.clip)].Apply(skeleton, Mathf.Max(0, time - deltaTime), time, nextStateInfo.loop, events);
                            break;
                        }
                    }

                    //mix the rest
                    for (; c < nextClipInfo.Length; c++)
                    {
                        var   info   = nextClipInfo[c];
                        float weight = info.weight * layerWeight;
                        if (weight == 0)
                        {
                            continue;
                        }

                        float time = nextStateInfo.normalizedTime * info.clip.length;
                        animationTable[GetAnimationClipNameHashCode(info.clip)].Mix(skeleton, Mathf.Max(0, time - deltaTime), time, nextStateInfo.loop, events, weight);
                    }
                }
            }

            for (int ii = 0, nn = events.Count; ii < nn; ii++)
            {
                Spine.Event e = events.Items[ii];
                if (Event != null)
                {
                    Event(this, i, e);
                }
            }
        }

        if (_UpdateLocal != null)
        {
            _UpdateLocal(this);
        }

        skeleton.UpdateWorldTransform();

        if (_UpdateWorld != null)
        {
            _UpdateWorld(this);
            skeleton.UpdateWorldTransform();
        }

        if (_UpdateComplete != null)
        {
            _UpdateComplete(this);
        }

        lastTime = Time.time;
    }
Ejemplo n.º 13
0
 public void Dispose()
 {
     attachments.Clear(true);
 }
Ejemplo n.º 14
0
        public virtual void LateUpdate()
        {
            if (!valid || (!meshRenderer.enabled && this.generateMeshOverride == null))
            {
                return;
            }
            ExposedList <Slot> drawOrder = skeleton.drawOrder;

            Slot[] items = drawOrder.Items;
            int    count = drawOrder.Count;
            bool   flag  = renderMeshes;

            SmartMesh.Instruction    instruction = currentInstructions;
            ExposedList <Attachment> attachments = instruction.attachments;

            attachments.Clear(clearArray: false);
            attachments.GrowIfNeeded(count);
            attachments.Count = count;
            Attachment[] items2 = instruction.attachments.Items;
            ExposedList <SubmeshInstruction> submeshInstructions = instruction.submeshInstructions;

            submeshInstructions.Clear(clearArray: false);
            bool     flag2            = customSlotMaterials.Count > 0;
            int      num              = 0;
            int      num2             = 0;
            int      num3             = 0;
            int      firstVertexIndex = 0;
            int      startSlot        = 0;
            Material material         = null;

            for (int i = 0; i < count; i++)
            {
                Slot             slot             = items[i];
                Attachment       attachment       = items2[i] = slot.attachment;
                RegionAttachment regionAttachment = attachment as RegionAttachment;
                object           rendererObject;
                int num4;
                int num5;
                if (regionAttachment != null)
                {
                    rendererObject = regionAttachment.RendererObject;
                    num4           = 4;
                    num5           = 6;
                }
                else
                {
                    if (!flag)
                    {
                        continue;
                    }
                    MeshAttachment meshAttachment = attachment as MeshAttachment;
                    if (meshAttachment == null)
                    {
                        continue;
                    }
                    rendererObject = meshAttachment.RendererObject;
                    num4           = meshAttachment.worldVerticesLength >> 1;
                    num5           = meshAttachment.triangles.Length;
                }
                Material value;
                if (flag2)
                {
                    if (!customSlotMaterials.TryGetValue(slot, out value))
                    {
                        value = (Material)((AtlasRegion)rendererObject).page.rendererObject;
                    }
                }
                else
                {
                    value = (Material)((AtlasRegion)rendererObject).page.rendererObject;
                }
                bool flag3 = separatorSlots.Count > 0 && separatorSlots.Contains(slot);
                if (num > 0 && (material.GetInstanceID() != value.GetInstanceID() || flag3))
                {
                    submeshInstructions.Add(new SubmeshInstruction
                    {
                        skeleton         = skeleton,
                        material         = material,
                        startSlot        = startSlot,
                        endSlot          = i,
                        triangleCount    = num3,
                        firstVertexIndex = firstVertexIndex,
                        vertexCount      = num2,
                        forceSeparate    = flag3
                    });
                    num3             = 0;
                    num2             = 0;
                    firstVertexIndex = num;
                    startSlot        = i;
                }
                material = value;
                num3    += num5;
                num     += num4;
                num2    += num4;
            }
            if (num2 != 0)
            {
                submeshInstructions.Add(new SubmeshInstruction
                {
                    skeleton         = skeleton,
                    material         = material,
                    startSlot        = startSlot,
                    endSlot          = count,
                    triangleCount    = num3,
                    firstVertexIndex = firstVertexIndex,
                    vertexCount      = num2,
                    forceSeparate    = false
                });
            }
            instruction.vertexCount        = num;
            instruction.immutableTriangles = immutableTriangles;
            if (customMaterialOverride.Count > 0)
            {
                SubmeshInstruction[] items3 = submeshInstructions.Items;
                for (int j = 0; j < submeshInstructions.Count; j++)
                {
                    Material material2 = items3[j].material;
                    Material value2;
                    if (customMaterialOverride.TryGetValue(material2, out value2))
                    {
                        items3[j].material = value2;
                    }
                }
            }
            if (this.generateMeshOverride != null)
            {
                this.generateMeshOverride(instruction);
                if (disableRenderingOnOverride)
                {
                    return;
                }
            }
            if (ArraysMeshGenerator.EnsureSize(num, ref vertices, ref uvs, ref colors) && calculateNormals)
            {
                Vector3[] array  = normals = new Vector3[num];
                Vector3   vector = new Vector3(0f, 0f, -1f);
                for (int k = 0; k < num; k++)
                {
                    array[k] = vector;
                }
            }
            Vector3 boundsMin = default(Vector3);
            Vector3 boundsMax = default(Vector3);

            if (num <= 0)
            {
                boundsMin = new Vector3(0f, 0f, 0f);
                boundsMax = new Vector3(0f, 0f, 0f);
            }
            else
            {
                boundsMin.x = 2.14748365E+09f;
                boundsMin.y = 2.14748365E+09f;
                boundsMax.x = -2.14748365E+09f;
                boundsMax.y = -2.14748365E+09f;
                if (zSpacing > 0f)
                {
                    boundsMin.z = 0f;
                    boundsMax.z = zSpacing * (float)(count - 1);
                }
                else
                {
                    boundsMin.z = zSpacing * (float)(count - 1);
                    boundsMax.z = 0f;
                }
            }
            int vertexIndex = 0;

            ArraysMeshGenerator.FillVerts(skeleton, 0, count, zSpacing, pmaVertexColors, vertices, uvs, colors, ref vertexIndex, ref tempVertices, ref boundsMin, ref boundsMax, flag);
            SmartMesh next = doubleBufferedMesh.GetNext();
            Mesh      mesh = next.mesh;

            mesh.vertices = vertices;
            mesh.colors32 = colors;
            mesh.uv       = uvs;
            mesh.bounds   = ArraysMeshGenerator.ToBounds(boundsMin, boundsMax);
            SmartMesh.Instruction instructionUsed = next.instructionUsed;
            if (calculateNormals && instructionUsed.vertexCount < num)
            {
                mesh.normals = normals;
            }
            bool flag4  = CheckIfMustUpdateMeshStructure(instruction, instructionUsed);
            int  count2 = submeshInstructions.Count;

            if (flag4)
            {
                ExposedList <Material> exposedList = submeshMaterials;
                exposedList.Clear(clearArray: false);
                int count3 = submeshes.Count;
                if (submeshes.Capacity < count2)
                {
                    submeshes.Capacity = count2;
                }
                for (int l = count3; l < count2; l++)
                {
                    submeshes.Items[l] = new ArraysMeshGenerator.SubmeshTriangleBuffer(submeshInstructions.Items[l].triangleCount);
                }
                submeshes.Count = count2;
                bool flag5 = !instruction.immutableTriangles;
                int  m     = 0;
                int  num6  = count2 - 1;
                for (; m < count2; m++)
                {
                    SubmeshInstruction submeshInstruction = submeshInstructions.Items[m];
                    if (flag5 || m >= count3)
                    {
                        ArraysMeshGenerator.SubmeshTriangleBuffer submeshTriangleBuffer = submeshes.Items[m];
                        int triangleCount = submeshInstruction.triangleCount;
                        if (flag)
                        {
                            ArraysMeshGenerator.FillTriangles(ref submeshTriangleBuffer.triangles, skeleton, triangleCount, submeshInstruction.firstVertexIndex, submeshInstruction.startSlot, submeshInstruction.endSlot, m == num6);
                            submeshTriangleBuffer.triangleCount = triangleCount;
                        }
                        else
                        {
                            ArraysMeshGenerator.FillTrianglesQuads(ref submeshTriangleBuffer.triangles, ref submeshTriangleBuffer.triangleCount, ref submeshTriangleBuffer.firstVertex, submeshInstruction.firstVertexIndex, triangleCount, m == num6);
                        }
                    }
                    exposedList.Add(submeshInstruction.material);
                }
                mesh.subMeshCount = count2;
                for (int n = 0; n < count2; n++)
                {
                    mesh.SetTriangles(submeshes.Items[n].triangles, n);
                }
            }
            if (calculateTangents)
            {
                ArraysMeshGenerator.SolveTangents2DEnsureSize(ref tangents, ref tempTanBuffer, vertices.Length);
                for (int num7 = 0; num7 < count2; num7++)
                {
                    ArraysMeshGenerator.SubmeshTriangleBuffer submeshTriangleBuffer2 = submeshes.Items[num7];
                    ArraysMeshGenerator.SolveTangents2DTriangles(tempTanBuffer, submeshTriangleBuffer2.triangles, submeshTriangleBuffer2.triangleCount, vertices, uvs, num);
                }
                ArraysMeshGenerator.SolveTangents2DBuffer(tangents, tempTanBuffer, num);
                mesh.tangents = tangents;
            }
            Material[] array2 = sharedMaterials;
            bool       flag6  = flag4 || array2.Length != count2;

            if (!flag6)
            {
                SubmeshInstruction[] items4 = submeshInstructions.Items;
                int num8 = 0;
                for (int num9 = array2.Length; num8 < num9; num8++)
                {
                    if (array2[num8].GetInstanceID() != items4[num8].material.GetInstanceID())
                    {
                        flag6 = true;
                        break;
                    }
                }
            }
            if (flag6)
            {
                if (submeshMaterials.Count == sharedMaterials.Length)
                {
                    submeshMaterials.CopyTo(sharedMaterials);
                }
                else
                {
                    sharedMaterials = submeshMaterials.ToArray();
                }
                meshRenderer.sharedMaterials = sharedMaterials;
            }
            meshFilter.sharedMesh = mesh;
            next.instructionUsed.Set(instruction);
        }
Ejemplo n.º 15
0
        public void BuildMeshWithArrays(SkeletonRendererInstruction instruction, bool updateTriangles)
        {
            Settings settings       = this.settings;
            int      rawVertexCount = instruction.rawVertexCount;

            if (rawVertexCount > vertexBuffer.Items.Length)
            {
                Array.Resize(ref vertexBuffer.Items, rawVertexCount);
                Array.Resize(ref uvBuffer.Items, rawVertexCount);
                Array.Resize(ref colorBuffer.Items, rawVertexCount);
            }
            vertexBuffer.Count = (uvBuffer.Count = (colorBuffer.Count = rawVertexCount));
            Color32 color = default(Color32);
            int     num   = 0;

            float[] array = tempVerts;
            Vector3 v     = meshBoundsMin;
            Vector3 v2    = meshBoundsMax;

            Vector3[] items   = vertexBuffer.Items;
            Vector2[] items2  = uvBuffer.Items;
            Color32[] items3  = colorBuffer.Items;
            int       num2    = 0;
            int       i       = 0;
            Vector2   vector  = default(Vector2);
            Vector2   vector2 = default(Vector2);

            for (int count = instruction.submeshInstructions.Count; i < count; i++)
            {
                SubmeshInstruction submeshInstruction = instruction.submeshInstructions.Items[i];
                Skeleton           skeleton           = submeshInstruction.skeleton;
                Slot[]             items4             = skeleton.drawOrder.Items;
                float num3      = skeleton.a * 255f;
                float r         = skeleton.r;
                float g         = skeleton.g;
                float b         = skeleton.b;
                int   endSlot   = submeshInstruction.endSlot;
                int   startSlot = submeshInstruction.startSlot;
                num2 = endSlot;
                if (settings.tintBlack)
                {
                    int num4 = num;
                    vector.y = 1f;
                    if (uv2 == null)
                    {
                        uv2 = new ExposedList <Vector2>();
                        uv3 = new ExposedList <Vector2>();
                    }
                    if (rawVertexCount > uv2.Items.Length)
                    {
                        Array.Resize(ref uv2.Items, rawVertexCount);
                        Array.Resize(ref uv3.Items, rawVertexCount);
                    }
                    uv2.Count = (uv3.Count = rawVertexCount);
                    Vector2[] items5 = uv2.Items;
                    Vector2[] items6 = uv3.Items;
                    for (int j = startSlot; j < endSlot; j++)
                    {
                        Slot       slot       = items4[j];
                        Attachment attachment = slot.attachment;
                        vector2.x = slot.r2;
                        vector2.y = slot.g2;
                        vector.x  = slot.b2;
                        RegionAttachment regionAttachment = attachment as RegionAttachment;
                        if (regionAttachment != null)
                        {
                            items5[num4]     = vector2;
                            items5[num4 + 1] = vector2;
                            items5[num4 + 2] = vector2;
                            items5[num4 + 3] = vector2;
                            items6[num4]     = vector;
                            items6[num4 + 1] = vector;
                            items6[num4 + 2] = vector;
                            items6[num4 + 3] = vector;
                            num4            += 4;
                            continue;
                        }
                        MeshAttachment meshAttachment = attachment as MeshAttachment;
                        if (meshAttachment != null)
                        {
                            int worldVerticesLength = meshAttachment.worldVerticesLength;
                            for (int k = 0; k < worldVerticesLength; k += 2)
                            {
                                items5[num4] = vector2;
                                items6[num4] = vector;
                                num4++;
                            }
                        }
                    }
                }
                for (int l = startSlot; l < endSlot; l++)
                {
                    Slot             slot2             = items4[l];
                    Attachment       attachment2       = slot2.attachment;
                    float            z                 = (float)l * settings.zSpacing;
                    RegionAttachment regionAttachment2 = attachment2 as RegionAttachment;
                    if (regionAttachment2 != null)
                    {
                        regionAttachment2.ComputeWorldVertices(slot2.bone, array, 0);
                        float num5  = array[0];
                        float num6  = array[1];
                        float num7  = array[2];
                        float num8  = array[3];
                        float num9  = array[4];
                        float num10 = array[5];
                        float num11 = array[6];
                        float num12 = array[7];
                        items[num].x     = num5;
                        items[num].y     = num6;
                        items[num].z     = z;
                        items[num + 1].x = num11;
                        items[num + 1].y = num12;
                        items[num + 1].z = z;
                        items[num + 2].x = num7;
                        items[num + 2].y = num8;
                        items[num + 2].z = z;
                        items[num + 3].x = num9;
                        items[num + 3].y = num10;
                        items[num + 3].z = z;
                        if (settings.pmaVertexColors)
                        {
                            color.a = (byte)(num3 * slot2.a * regionAttachment2.a);
                            color.r = (byte)(r * slot2.r * regionAttachment2.r * (float)(int)color.a);
                            color.g = (byte)(g * slot2.g * regionAttachment2.g * (float)(int)color.a);
                            color.b = (byte)(b * slot2.b * regionAttachment2.b * (float)(int)color.a);
                            if (slot2.data.blendMode == BlendMode.Additive)
                            {
                                color.a = 0;
                            }
                        }
                        else
                        {
                            color.a = (byte)(num3 * slot2.a * regionAttachment2.a);
                            color.r = (byte)(r * slot2.r * regionAttachment2.r * 255f);
                            color.g = (byte)(g * slot2.g * regionAttachment2.g * 255f);
                            color.b = (byte)(b * slot2.b * regionAttachment2.b * 255f);
                        }
                        items3[num]     = color;
                        items3[num + 1] = color;
                        items3[num + 2] = color;
                        items3[num + 3] = color;
                        float[] uvs = regionAttachment2.uvs;
                        items2[num].x     = uvs[0];
                        items2[num].y     = uvs[1];
                        items2[num + 1].x = uvs[6];
                        items2[num + 1].y = uvs[7];
                        items2[num + 2].x = uvs[2];
                        items2[num + 2].y = uvs[3];
                        items2[num + 3].x = uvs[4];
                        items2[num + 3].y = uvs[5];
                        if (num5 < v.x)
                        {
                            v.x = num5;
                        }
                        if (num5 > v2.x)
                        {
                            v2.x = num5;
                        }
                        if (num7 < v.x)
                        {
                            v.x = num7;
                        }
                        else if (num7 > v2.x)
                        {
                            v2.x = num7;
                        }
                        if (num9 < v.x)
                        {
                            v.x = num9;
                        }
                        else if (num9 > v2.x)
                        {
                            v2.x = num9;
                        }
                        if (num11 < v.x)
                        {
                            v.x = num11;
                        }
                        else if (num11 > v2.x)
                        {
                            v2.x = num11;
                        }
                        if (num6 < v.y)
                        {
                            v.y = num6;
                        }
                        if (num6 > v2.y)
                        {
                            v2.y = num6;
                        }
                        if (num8 < v.y)
                        {
                            v.y = num8;
                        }
                        else if (num8 > v2.y)
                        {
                            v2.y = num8;
                        }
                        if (num10 < v.y)
                        {
                            v.y = num10;
                        }
                        else if (num10 > v2.y)
                        {
                            v2.y = num10;
                        }
                        if (num12 < v.y)
                        {
                            v.y = num12;
                        }
                        else if (num12 > v2.y)
                        {
                            v2.y = num12;
                        }
                        num += 4;
                        continue;
                    }
                    MeshAttachment meshAttachment2 = attachment2 as MeshAttachment;
                    if (meshAttachment2 == null)
                    {
                        continue;
                    }
                    int worldVerticesLength2 = meshAttachment2.worldVerticesLength;
                    if (array.Length < worldVerticesLength2)
                    {
                        array = (tempVerts = new float[worldVerticesLength2]);
                    }
                    meshAttachment2.ComputeWorldVertices(slot2, array);
                    if (settings.pmaVertexColors)
                    {
                        color.a = (byte)(num3 * slot2.a * meshAttachment2.a);
                        color.r = (byte)(r * slot2.r * meshAttachment2.r * (float)(int)color.a);
                        color.g = (byte)(g * slot2.g * meshAttachment2.g * (float)(int)color.a);
                        color.b = (byte)(b * slot2.b * meshAttachment2.b * (float)(int)color.a);
                        if (slot2.data.blendMode == BlendMode.Additive)
                        {
                            color.a = 0;
                        }
                    }
                    else
                    {
                        color.a = (byte)(num3 * slot2.a * meshAttachment2.a);
                        color.r = (byte)(r * slot2.r * meshAttachment2.r * 255f);
                        color.g = (byte)(g * slot2.g * meshAttachment2.g * 255f);
                        color.b = (byte)(b * slot2.b * meshAttachment2.b * 255f);
                    }
                    float[] uvs2 = meshAttachment2.uvs;
                    if (num == 0)
                    {
                        float num13 = array[0];
                        float num14 = array[1];
                        if (num13 < v.x)
                        {
                            v.x = num13;
                        }
                        if (num13 > v2.x)
                        {
                            v2.x = num13;
                        }
                        if (num14 < v.y)
                        {
                            v.y = num14;
                        }
                        if (num14 > v2.y)
                        {
                            v2.y = num14;
                        }
                    }
                    for (int m = 0; m < worldVerticesLength2; m += 2)
                    {
                        float num15 = array[m];
                        float num16 = array[m + 1];
                        items[num].x  = num15;
                        items[num].y  = num16;
                        items[num].z  = z;
                        items3[num]   = color;
                        items2[num].x = uvs2[m];
                        items2[num].y = uvs2[m + 1];
                        if (num15 < v.x)
                        {
                            v.x = num15;
                        }
                        else if (num15 > v2.x)
                        {
                            v2.x = num15;
                        }
                        if (num16 < v.y)
                        {
                            v.y = num16;
                        }
                        else if (num16 > v2.y)
                        {
                            v2.y = num16;
                        }
                        num++;
                    }
                }
            }
            meshBoundsMin       = v;
            meshBoundsMax       = v2;
            meshBoundsThickness = (float)num2 * settings.zSpacing;
            if (!updateTriangles)
            {
                return;
            }
            int count2 = instruction.submeshInstructions.Count;

            if (submeshes.Count < count2)
            {
                submeshes.Resize(count2);
                int n = 0;
                for (int num17 = count2; n < num17; n++)
                {
                    ExposedList <int> exposedList = submeshes.Items[n];
                    if (exposedList == null)
                    {
                        submeshes.Items[n] = new ExposedList <int>();
                    }
                    else
                    {
                        exposedList.Clear(clearArray: false);
                    }
                }
            }
            SubmeshInstruction[] items7 = instruction.submeshInstructions.Items;
            int num18 = 0;

            for (int num19 = 0; num19 < count2; num19++)
            {
                SubmeshInstruction submeshInstruction2 = items7[num19];
                ExposedList <int>  exposedList2        = submeshes.Items[num19];
                int rawTriangleCount = submeshInstruction2.rawTriangleCount;
                if (rawTriangleCount > exposedList2.Items.Length)
                {
                    Array.Resize(ref exposedList2.Items, rawTriangleCount);
                }
                else if (rawTriangleCount < exposedList2.Items.Length)
                {
                    int[] items8 = exposedList2.Items;
                    int   num20  = rawTriangleCount;
                    for (int num21 = items8.Length; num20 < num21; num20++)
                    {
                        items8[num20] = 0;
                    }
                }
                exposedList2.Count = rawTriangleCount;
                int[]    items9    = exposedList2.Items;
                int      num22     = 0;
                Skeleton skeleton2 = submeshInstruction2.skeleton;
                Slot[]   items10   = skeleton2.drawOrder.Items;
                int      num23     = submeshInstruction2.startSlot;
                for (int endSlot2 = submeshInstruction2.endSlot; num23 < endSlot2; num23++)
                {
                    Attachment attachment3 = items10[num23].attachment;
                    if (attachment3 is RegionAttachment)
                    {
                        items9[num22]     = num18;
                        items9[num22 + 1] = num18 + 2;
                        items9[num22 + 2] = num18 + 1;
                        items9[num22 + 3] = num18 + 2;
                        items9[num22 + 4] = num18 + 3;
                        items9[num22 + 5] = num18 + 1;
                        num22            += 6;
                        num18            += 4;
                        continue;
                    }
                    MeshAttachment meshAttachment3 = attachment3 as MeshAttachment;
                    if (meshAttachment3 != null)
                    {
                        int[] triangles = meshAttachment3.triangles;
                        int   num24     = 0;
                        int   num25     = triangles.Length;
                        while (num24 < num25)
                        {
                            items9[num22] = num18 + triangles[num24];
                            num24++;
                            num22++;
                        }
                        num18 += meshAttachment3.worldVerticesLength >> 1;
                    }
                }
            }
        }
Ejemplo n.º 16
0
    public virtual void LateUpdate()
    {
        if (!valid)
        {
            return;
        }

        // Exit early if there is nothing to render
        if (!meshRenderer.enabled && submeshRenderers.Length == 0)
        {
            return;
        }

        // Count vertices and submesh triangles.
        int                vertexCount = 0;
        int                submeshTriangleCount = 0, submeshFirstVertex = 0, submeshStartSlotIndex = 0;
        Material           lastMaterial               = null;
        ExposedList <Slot> drawOrder                  = skeleton.drawOrder;
        int                drawOrderCount             = drawOrder.Count;
        int                submeshSeparatorSlotsCount = submeshSeparatorSlots.Count;
        bool               renderMeshes               = this.renderMeshes;

        // Clear last state of attachments and submeshes
        ExposedList <int> attachmentsTriangleCountTemp = lastState.attachmentsTriangleCountTemp;

        attachmentsTriangleCountTemp.GrowIfNeeded(drawOrderCount);
        attachmentsTriangleCountTemp.Count = drawOrderCount;
        ExposedList <bool> attachmentsFlipStateTemp = lastState.attachmentsFlipStateTemp;

        attachmentsFlipStateTemp.GrowIfNeeded(drawOrderCount);
        attachmentsFlipStateTemp.Count = drawOrderCount;

        ExposedList <LastState.AddSubmeshArguments> addSubmeshArgumentsTemp = lastState.addSubmeshArgumentsTemp;

        addSubmeshArgumentsTemp.Clear(false);
        for (int i = 0; i < drawOrderCount; i++)
        {
            Slot       slot       = drawOrder.Items[i];
            Bone       bone       = slot.bone;
            Attachment attachment = slot.attachment;

            object rendererObject;
            int    attachmentVertexCount, attachmentTriangleCount;
            bool   worldScaleXIsPositive = bone.worldScaleX >= 0f;
            bool   worldScaleYIsPositive = bone.worldScaleY >= 0f;
            bool   worldScaleIsSameSigns = (worldScaleXIsPositive && worldScaleYIsPositive) ||
                                           (!worldScaleXIsPositive && !worldScaleYIsPositive);
            bool flip = frontFacing && ((bone.worldFlipX != bone.worldFlipY) == worldScaleIsSameSigns);
            attachmentsFlipStateTemp.Items[i] = flip;

            attachmentsTriangleCountTemp.Items[i] = -1;
            RegionAttachment regionAttachment = attachment as RegionAttachment;
            if (regionAttachment != null)
            {
                rendererObject          = regionAttachment.RendererObject;
                attachmentVertexCount   = 4;
                attachmentTriangleCount = 6;
            }
            else
            {
                if (!renderMeshes)
                {
                    continue;
                }
                MeshAttachment meshAttachment = attachment as MeshAttachment;
                if (meshAttachment != null)
                {
                    rendererObject          = meshAttachment.RendererObject;
                    attachmentVertexCount   = meshAttachment.vertices.Length >> 1;
                    attachmentTriangleCount = meshAttachment.triangles.Length;
                }
                else
                {
                    SkinnedMeshAttachment skinnedMeshAttachment = attachment as SkinnedMeshAttachment;
                    if (skinnedMeshAttachment != null)
                    {
                        rendererObject          = skinnedMeshAttachment.RendererObject;
                        attachmentVertexCount   = skinnedMeshAttachment.uvs.Length >> 1;
                        attachmentTriangleCount = skinnedMeshAttachment.triangles.Length;
                    }
                    else
                    {
                        continue;
                    }
                }
            }

            // Populate submesh when material changes.
#if !SPINE_TK2D
            Material material = (Material)((AtlasRegion)rendererObject).page.rendererObject;
#else
            Material material = (rendererObject.GetType() == typeof(Material)) ? (Material)rendererObject : (Material)((AtlasRegion)rendererObject).page.rendererObject;
#endif
            if ((lastMaterial != null && lastMaterial.GetInstanceID() != material.GetInstanceID()) ||
                (submeshSeparatorSlotsCount > 0 && submeshSeparatorSlots.Contains(slot)))
            {
                addSubmeshArgumentsTemp.Add(
                    new LastState.AddSubmeshArguments(lastMaterial, submeshStartSlotIndex, i, submeshTriangleCount, submeshFirstVertex, false)
                    );
                submeshTriangleCount  = 0;
                submeshFirstVertex    = vertexCount;
                submeshStartSlotIndex = i;
            }
            lastMaterial = material;

            submeshTriangleCount += attachmentTriangleCount;
            vertexCount          += attachmentVertexCount;

            attachmentsTriangleCountTemp.Items[i] = attachmentTriangleCount;
        }
        addSubmeshArgumentsTemp.Add(
            new LastState.AddSubmeshArguments(lastMaterial, submeshStartSlotIndex, drawOrderCount, submeshTriangleCount, submeshFirstVertex, true)
            );

        bool mustUpdateMeshStructure = CheckIfMustUpdateMeshStructure(attachmentsTriangleCountTemp, attachmentsFlipStateTemp, addSubmeshArgumentsTemp);
        if (mustUpdateMeshStructure)
        {
            submeshMaterials.Clear();
            for (int i = 0, n = addSubmeshArgumentsTemp.Count; i < n; i++)
            {
                LastState.AddSubmeshArguments arguments = addSubmeshArgumentsTemp.Items[i];
                AddSubmesh(
                    arguments.material,
                    arguments.startSlot,
                    arguments.endSlot,
                    arguments.triangleCount,
                    arguments.firstVertex,
                    arguments.lastSubmesh,
                    attachmentsFlipStateTemp
                    );
            }

            // Set materials.
            if (submeshMaterials.Count == sharedMaterials.Length)
            {
                submeshMaterials.CopyTo(sharedMaterials);
            }
            else
            {
                sharedMaterials = submeshMaterials.ToArray();
            }

            meshRenderer.sharedMaterials = sharedMaterials;
        }

        // Ensure mesh data is the right size.
        Vector3[] vertices     = this.vertices;
        bool      newTriangles = vertexCount > vertices.Length;
        if (newTriangles)
        {
            // Not enough vertices, increase size.
            this.vertices = vertices = new Vector3[vertexCount];
            this.colors   = new Color32[vertexCount];
            this.uvs      = new Vector2[vertexCount];
            mesh1.Clear();
            mesh2.Clear();
        }
        else
        {
            // Too many vertices, zero the extra.
            Vector3 zero = Vector3.zero;
            for (int i = vertexCount, n = lastState.vertexCount; i < n; i++)
            {
                vertices[i] = zero;
            }
        }
        lastState.vertexCount = vertexCount;

        // Setup mesh.
        float     zSpacing     = this.zSpacing;
        float[]   tempVertices = this.tempVertices;
        Vector2[] uvs          = this.uvs;
        Color32[] colors       = this.colors;
        int       vertexIndex  = 0;
        Color32   color;
        float     a = skeleton.a * 255, r = skeleton.r, g = skeleton.g, b = skeleton.b;

        Vector3 meshBoundsMin;
        Vector3 meshBoundsMax;
        if (vertexCount == 0)
        {
            meshBoundsMin = new Vector3(0, 0, 0);
            meshBoundsMax = new Vector3(0, 0, 0);
        }
        else
        {
            meshBoundsMin.x = int.MaxValue;
            meshBoundsMin.y = int.MaxValue;
            meshBoundsMax.x = int.MinValue;
            meshBoundsMax.y = int.MinValue;
            if (zSpacing > 0f)
            {
                meshBoundsMin.z = 0f;
                meshBoundsMax.z = zSpacing * (drawOrderCount - 1);
            }
            else
            {
                meshBoundsMin.z = zSpacing * (drawOrderCount - 1);
                meshBoundsMax.z = 0f;
            }
            int i = 0;
            do
            {
                Slot             slot             = drawOrder.Items[i];
                Attachment       attachment       = slot.attachment;
                RegionAttachment regionAttachment = attachment as RegionAttachment;
                if (regionAttachment != null)
                {
                    regionAttachment.ComputeWorldVertices(slot.bone, tempVertices);

                    float z = i * zSpacing;
                    float x1 = tempVertices[RegionAttachment.X1], y1 = tempVertices[RegionAttachment.Y1];
                    float x2 = tempVertices[RegionAttachment.X2], y2 = tempVertices[RegionAttachment.Y2];
                    float x3 = tempVertices[RegionAttachment.X3], y3 = tempVertices[RegionAttachment.Y3];
                    float x4 = tempVertices[RegionAttachment.X4], y4 = tempVertices[RegionAttachment.Y4];
                    vertices[vertexIndex].x     = x1;
                    vertices[vertexIndex].y     = y1;
                    vertices[vertexIndex].z     = z;
                    vertices[vertexIndex + 1].x = x4;
                    vertices[vertexIndex + 1].y = y4;
                    vertices[vertexIndex + 1].z = z;
                    vertices[vertexIndex + 2].x = x2;
                    vertices[vertexIndex + 2].y = y2;
                    vertices[vertexIndex + 2].z = z;
                    vertices[vertexIndex + 3].x = x3;
                    vertices[vertexIndex + 3].y = y3;
                    vertices[vertexIndex + 3].z = z;

                    color.a = (byte)(a * slot.a * regionAttachment.a);
                    color.r = (byte)(r * slot.r * regionAttachment.r * color.a);
                    color.g = (byte)(g * slot.g * regionAttachment.g * color.a);
                    color.b = (byte)(b * slot.b * regionAttachment.b * color.a);
                    if (slot.data.blendMode == BlendMode.additive)
                    {
                        color.a = 0;
                    }
                    colors[vertexIndex]     = color;
                    colors[vertexIndex + 1] = color;
                    colors[vertexIndex + 2] = color;
                    colors[vertexIndex + 3] = color;

                    float[] regionUVs = regionAttachment.uvs;
                    uvs[vertexIndex].x     = regionUVs[RegionAttachment.X1];
                    uvs[vertexIndex].y     = regionUVs[RegionAttachment.Y1];
                    uvs[vertexIndex + 1].x = regionUVs[RegionAttachment.X4];
                    uvs[vertexIndex + 1].y = regionUVs[RegionAttachment.Y4];
                    uvs[vertexIndex + 2].x = regionUVs[RegionAttachment.X2];
                    uvs[vertexIndex + 2].y = regionUVs[RegionAttachment.Y2];
                    uvs[vertexIndex + 3].x = regionUVs[RegionAttachment.X3];
                    uvs[vertexIndex + 3].y = regionUVs[RegionAttachment.Y3];

                    // Calculate min/max X
                    if (x1 < meshBoundsMin.x)
                    {
                        meshBoundsMin.x = x1;
                    }
                    else if (x1 > meshBoundsMax.x)
                    {
                        meshBoundsMax.x = x1;
                    }
                    if (x2 < meshBoundsMin.x)
                    {
                        meshBoundsMin.x = x2;
                    }
                    else if (x2 > meshBoundsMax.x)
                    {
                        meshBoundsMax.x = x2;
                    }
                    if (x3 < meshBoundsMin.x)
                    {
                        meshBoundsMin.x = x3;
                    }
                    else if (x3 > meshBoundsMax.x)
                    {
                        meshBoundsMax.x = x3;
                    }
                    if (x4 < meshBoundsMin.x)
                    {
                        meshBoundsMin.x = x4;
                    }
                    else if (x4 > meshBoundsMax.x)
                    {
                        meshBoundsMax.x = x4;
                    }

                    // Calculate min/max Y
                    if (y1 < meshBoundsMin.y)
                    {
                        meshBoundsMin.y = y1;
                    }
                    else if (y1 > meshBoundsMax.y)
                    {
                        meshBoundsMax.y = y1;
                    }
                    if (y2 < meshBoundsMin.y)
                    {
                        meshBoundsMin.y = y2;
                    }
                    else if (y2 > meshBoundsMax.y)
                    {
                        meshBoundsMax.y = y2;
                    }
                    if (y3 < meshBoundsMin.y)
                    {
                        meshBoundsMin.y = y3;
                    }
                    else if (y3 > meshBoundsMax.y)
                    {
                        meshBoundsMax.y = y3;
                    }
                    if (y4 < meshBoundsMin.y)
                    {
                        meshBoundsMin.y = y4;
                    }
                    else if (y4 > meshBoundsMax.y)
                    {
                        meshBoundsMax.y = y4;
                    }

                    vertexIndex += 4;
                }
                else
                {
                    if (!renderMeshes)
                    {
                        continue;
                    }
                    MeshAttachment meshAttachment = attachment as MeshAttachment;
                    if (meshAttachment != null)
                    {
                        int meshVertexCount = meshAttachment.vertices.Length;
                        if (tempVertices.Length < meshVertexCount)
                        {
                            this.tempVertices = tempVertices = new float[meshVertexCount];
                        }
                        meshAttachment.ComputeWorldVertices(slot, tempVertices);

                        color.a = (byte)(a * slot.a * meshAttachment.a);
                        color.r = (byte)(r * slot.r * meshAttachment.r * color.a);
                        color.g = (byte)(g * slot.g * meshAttachment.g * color.a);
                        color.b = (byte)(b * slot.b * meshAttachment.b * color.a);
                        if (slot.data.blendMode == BlendMode.additive)
                        {
                            color.a = 0;
                        }

                        float[] meshUVs = meshAttachment.uvs;
                        float   z       = i * zSpacing;
                        for (int ii = 0; ii < meshVertexCount; ii += 2, vertexIndex++)
                        {
                            float x = tempVertices[ii], y = tempVertices[ii + 1];
                            vertices[vertexIndex].x = x;
                            vertices[vertexIndex].y = y;
                            vertices[vertexIndex].z = z;
                            colors[vertexIndex]     = color;
                            uvs[vertexIndex].x      = meshUVs[ii];
                            uvs[vertexIndex].y      = meshUVs[ii + 1];

                            if (x < meshBoundsMin.x)
                            {
                                meshBoundsMin.x = x;
                            }
                            else if (x > meshBoundsMax.x)
                            {
                                meshBoundsMax.x = x;
                            }
                            if (y < meshBoundsMin.y)
                            {
                                meshBoundsMin.y = y;
                            }
                            else if (y > meshBoundsMax.y)
                            {
                                meshBoundsMax.y = y;
                            }
                        }
                    }
                    else
                    {
                        SkinnedMeshAttachment skinnedMeshAttachment = attachment as SkinnedMeshAttachment;
                        if (skinnedMeshAttachment != null)
                        {
                            int meshVertexCount = skinnedMeshAttachment.uvs.Length;
                            if (tempVertices.Length < meshVertexCount)
                            {
                                this.tempVertices = tempVertices = new float[meshVertexCount];
                            }
                            skinnedMeshAttachment.ComputeWorldVertices(slot, tempVertices);

                            color.a = (byte)(a * slot.a * skinnedMeshAttachment.a);
                            color.r = (byte)(r * slot.r * skinnedMeshAttachment.r * color.a);
                            color.g = (byte)(g * slot.g * skinnedMeshAttachment.g * color.a);
                            color.b = (byte)(b * slot.b * skinnedMeshAttachment.b * color.a);
                            if (slot.data.blendMode == BlendMode.additive)
                            {
                                color.a = 0;
                            }

                            float[] meshUVs = skinnedMeshAttachment.uvs;
                            float   z       = i * zSpacing;
                            for (int ii = 0; ii < meshVertexCount; ii += 2, vertexIndex++)
                            {
                                float x = tempVertices[ii], y = tempVertices[ii + 1];
                                vertices[vertexIndex].x = x;
                                vertices[vertexIndex].y = y;
                                vertices[vertexIndex].z = z;
                                colors[vertexIndex]     = color;
                                uvs[vertexIndex].x      = meshUVs[ii];
                                uvs[vertexIndex].y      = meshUVs[ii + 1];

                                if (x < meshBoundsMin.x)
                                {
                                    meshBoundsMin.x = x;
                                }
                                else if (x > meshBoundsMax.x)
                                {
                                    meshBoundsMax.x = x;
                                }
                                if (y < meshBoundsMin.y)
                                {
                                    meshBoundsMin.y = y;
                                }
                                else if (y > meshBoundsMax.y)
                                {
                                    meshBoundsMax.y = y;
                                }
                            }
                        }
                    }
                }
            } while (++i < drawOrderCount);
        }

        // Double buffer mesh.
        Mesh mesh = useMesh1 ? mesh1 : mesh2;
        meshFilter.sharedMesh = mesh;

        mesh.vertices = vertices;
        mesh.colors32 = colors;
        mesh.uv       = uvs;

        if (mustUpdateMeshStructure)
        {
            int submeshCount = submeshMaterials.Count;
            mesh.subMeshCount = submeshCount;
            for (int i = 0; i < submeshCount; ++i)
            {
                mesh.SetTriangles(submeshes.Items[i].triangles, i);
            }
        }

        Vector3 meshBoundsExtents = meshBoundsMax - meshBoundsMin;
        Vector3 meshBoundsCenter  = meshBoundsMin + meshBoundsExtents * 0.5f;
        mesh.bounds = new Bounds(meshBoundsCenter, meshBoundsExtents);

        if (newTriangles && calculateNormals)
        {
            Vector3[] normals = new Vector3[vertexCount];
            Vector3   normal  = new Vector3(0, 0, -1);
            for (int i = 0; i < vertexCount; i++)
            {
                normals[i] = normal;
            }
            (useMesh1 ? mesh2 : mesh1).vertices = vertices;             // Set other mesh vertices.
            mesh1.normals = normals;
            mesh2.normals = normals;

            if (calculateTangents)
            {
                Vector4[] tangents = new Vector4[vertexCount];
                Vector3   tangent  = new Vector3(0, 0, 1);
                for (int i = 0; i < vertexCount; i++)
                {
                    tangents[i] = tangent;
                }
                mesh1.tangents = tangents;
                mesh2.tangents = tangents;
            }
        }

        // Update previous state
        ExposedList <int>  attachmentsTriangleCountCurrentMesh;
        ExposedList <bool> attachmentsFlipStateCurrentMesh;
        ExposedList <LastState.AddSubmeshArguments> addSubmeshArgumentsCurrentMesh;
        if (useMesh1)
        {
            attachmentsTriangleCountCurrentMesh = lastState.attachmentsTriangleCountMesh1;
            addSubmeshArgumentsCurrentMesh      = lastState.addSubmeshArgumentsMesh1;
            attachmentsFlipStateCurrentMesh     = lastState.attachmentsFlipStateMesh1;
            lastState.immutableTrianglesMesh1   = immutableTriangles;
        }
        else
        {
            attachmentsTriangleCountCurrentMesh = lastState.attachmentsTriangleCountMesh2;
            addSubmeshArgumentsCurrentMesh      = lastState.addSubmeshArgumentsMesh2;
            attachmentsFlipStateCurrentMesh     = lastState.attachmentsFlipStateMesh2;
            lastState.immutableTrianglesMesh2   = immutableTriangles;
        }

        attachmentsTriangleCountCurrentMesh.GrowIfNeeded(attachmentsTriangleCountTemp.Capacity);
        attachmentsTriangleCountCurrentMesh.Count = attachmentsTriangleCountTemp.Count;
        attachmentsTriangleCountTemp.CopyTo(attachmentsTriangleCountCurrentMesh.Items, 0);

        attachmentsFlipStateCurrentMesh.GrowIfNeeded(attachmentsFlipStateTemp.Capacity);
        attachmentsFlipStateCurrentMesh.Count = attachmentsFlipStateTemp.Count;
        attachmentsFlipStateTemp.CopyTo(attachmentsFlipStateCurrentMesh.Items, 0);

        addSubmeshArgumentsCurrentMesh.GrowIfNeeded(addSubmeshArgumentsTemp.Count);
        addSubmeshArgumentsCurrentMesh.Count = addSubmeshArgumentsTemp.Count;
        addSubmeshArgumentsTemp.CopyTo(addSubmeshArgumentsCurrentMesh.Items);

        if (submeshRenderers.Length > 0)
        {
            for (int i = 0; i < submeshRenderers.Length; i++)
            {
                SkeletonUtilitySubmeshRenderer submeshRenderer = submeshRenderers[i];
                if (submeshRenderer.submeshIndex < sharedMaterials.Length)
                {
                    submeshRenderer.SetMesh(meshRenderer, useMesh1 ? mesh1 : mesh2, sharedMaterials[submeshRenderer.submeshIndex]);
                }
                else
                {
                    submeshRenderer.GetComponent <Renderer>().enabled = false;
                }
            }
        }

        useMesh1 = !useMesh1;
    }
Ejemplo n.º 17
0
    public virtual void LateUpdate()
    {
        if (!valid)
        {
            return;
        }

        // Exit early if there is nothing to render
        if (!meshRenderer.enabled && submeshRenderers.Length == 0)
        {
            return;
        }

        // Count vertices and submesh triangles.
        int                vertexCount = 0;
        int                submeshTriangleCount = 0, submeshFirstVertex = 0, submeshStartSlotIndex = 0;
        Material           lastMaterial               = null;
        ExposedList <Slot> drawOrder                  = skeleton.drawOrder;
        int                drawOrderCount             = drawOrder.Count;
        int                submeshSeparatorSlotsCount = submeshSeparatorSlots.Count;
        bool               renderMeshes               = this.renderMeshes;

        // Clear last state of attachments and submeshes
        ExposedList <int> attachmentsTriangleCountTemp = lastState.attachmentsTriangleCountTemp;

        attachmentsTriangleCountTemp.GrowIfNeeded(drawOrderCount);
        attachmentsTriangleCountTemp.Count = drawOrderCount;
        ExposedList <bool> attachmentsFlipStateTemp = lastState.attachmentsFlipStateTemp;

        attachmentsFlipStateTemp.GrowIfNeeded(drawOrderCount);
        attachmentsFlipStateTemp.Count = drawOrderCount;

        ExposedList <LastState.AddSubmeshArguments> addSubmeshArgumentsTemp = lastState.addSubmeshArgumentsTemp;

        addSubmeshArgumentsTemp.Clear(false);
        bool noRender = false;

        for (int i = 0; i < drawOrderCount; i++)
        {
            Slot       slot       = drawOrder.Items[i];
            Bone       bone       = slot.bone;
            Attachment attachment = slot.attachment;

            object rendererObject;
            int    attachmentVertexCount, attachmentTriangleCount;
            bool   worldScaleXIsPositive = bone.worldScaleX >= 0f;
            bool   worldScaleYIsPositive = bone.worldScaleY >= 0f;
            bool   worldScaleIsSameSigns = (worldScaleXIsPositive && worldScaleYIsPositive) ||
                                           (!worldScaleXIsPositive && !worldScaleYIsPositive);
            bool flip = frontFacing && ((bone.worldFlipX != bone.worldFlipY) == worldScaleIsSameSigns);
            attachmentsFlipStateTemp.Items[i] = flip;

            attachmentsTriangleCountTemp.Items[i] = -1;
            RegionAttachment regionAttachment = attachment as RegionAttachment;
            if (regionAttachment != null)
            {
                rendererObject          = regionAttachment.RendererObject;
                attachmentVertexCount   = 4;
                attachmentTriangleCount = 6;
            }
            else
            {
                if (!renderMeshes)
                {
                    continue;
                }
                MeshAttachment meshAttachment = attachment as MeshAttachment;
                if (meshAttachment != null)
                {
                    rendererObject          = meshAttachment.RendererObject;
                    attachmentVertexCount   = meshAttachment.vertices.Length >> 1;
                    attachmentTriangleCount = meshAttachment.triangles.Length;
                }
                else
                {
                    SkinnedMeshAttachment skinnedMeshAttachment = attachment as SkinnedMeshAttachment;
                    if (skinnedMeshAttachment != null)
                    {
                        rendererObject          = skinnedMeshAttachment.RendererObject;
                        attachmentVertexCount   = skinnedMeshAttachment.uvs.Length >> 1;
                        attachmentTriangleCount = skinnedMeshAttachment.triangles.Length;
                    }
                    else
                    {
                        continue;
                    }
                }
            }

            // Populate submesh when material changes.

            // tsteil - added support for mask material
            Material material = null;
            if (useMaskMaterial)
            {
                if (maskProvider != null)
                {
                    if (maskMaterial == null)
                    {
                        var prefabMat = (Material)((AtlasRegion)rendererObject).page.rendererObjectMask;
                        material           = new Material(prefabMat);
                        material.hideFlags = HideFlags.HideAndDontSave;
                        maskMaterial       = material;
                        SetMaskId();
                    }
                    else
                    {
                        material = maskMaterial;
                    }
                }
                else
                {
                    material = (Material)((AtlasRegion)rendererObject).page.rendererObjectMask;
                }
            }
            else
            {
#if !SPINE_TK2D
                material = (Material)((AtlasRegion)rendererObject).page.rendererObject;
#else
                material = (rendererObject.GetType() == typeof(Material)) ? (Material)rendererObject : (Material)((AtlasRegion)rendererObject).page.rendererObject;
#endif
            }
            if ((lastMaterial != null && lastMaterial.GetInstanceID() != material.GetInstanceID()) ||
                (submeshSeparatorSlotsCount > 0 && submeshSeparatorSlots.Contains(slot)))
            {
                addSubmeshArgumentsTemp.Add(
                    new LastState.AddSubmeshArguments(lastMaterial, submeshStartSlotIndex, i, submeshTriangleCount, submeshFirstVertex, false)
                    );
                submeshTriangleCount  = 0;
                submeshFirstVertex    = vertexCount;
                submeshStartSlotIndex = i;
            }
            lastMaterial = material;

            submeshTriangleCount += attachmentTriangleCount;
            vertexCount          += attachmentVertexCount;

            attachmentsTriangleCountTemp.Items[i] = attachmentTriangleCount;
        }

        // tsteil - we need to keep track if we're rendering or not
        if (lastMaterial == null)
        {
            noRender = true;
        }

        addSubmeshArgumentsTemp.Add(
            new LastState.AddSubmeshArguments(lastMaterial, submeshStartSlotIndex, drawOrderCount, submeshTriangleCount, submeshFirstVertex, true)
            );

        bool mustUpdateMeshStructure = CheckIfMustUpdateMeshStructure(attachmentsTriangleCountTemp, attachmentsFlipStateTemp, addSubmeshArgumentsTemp);
        var  submeshMatCount         = 0;
        if (mustUpdateMeshStructure)
        {
            submeshMaterials.Clear();
            for (int i = 0, n = addSubmeshArgumentsTemp.Count; i < n; i++)
            {
                LastState.AddSubmeshArguments arguments = addSubmeshArgumentsTemp.Items[i];
                AddSubmesh(
                    arguments.material,
                    arguments.startSlot,
                    arguments.endSlot,
                    arguments.triangleCount,
                    arguments.firstVertex,
                    arguments.lastSubmesh,
                    attachmentsFlipStateTemp
                    );
            }

            // Set materials.
            submeshMatCount = submeshMaterials.Count;
            if (submeshMatCount == sharedMaterials.Length)
            {
                submeshMaterials.CopyTo(sharedMaterials);
            }
            else
            {
                sharedMaterials = submeshMaterials.ToArray();
            }
            meshRenderer.sharedMaterials = sharedMaterials;
        }

        // Ensure mesh data is the right size.
        Vector3[] vertices     = this.vertices;
        bool      newTriangles = vertexCount > vertices.Length;
        if (newTriangles)
        {
            // Not enough vertices, increase size.
            this.vertices = vertices = new Vector3[vertexCount];
            this.colors   = new Color32[vertexCount];
            this.uvs      = new Vector2[vertexCount];
            if (setupUv2)
            {
                this.uvs2 = new Vector2[vertexCount];
            }
            mesh1.Clear();
            mesh2.Clear();
        }
        else
        {
            // Too many vertices, zero the extra.
            Vector3 zero = Vector3.zero;
            for (int i = vertexCount, n = lastState.vertexCount; i < n; i++)
            {
                vertices[i] = zero;
            }
        }
        lastState.vertexCount = vertexCount;

        // Setup mesh.
        float     zSpacing     = this.zSpacing;
        float[]   tempVertices = this.tempVertices;
        Vector2[] uvs          = this.uvs;
        Vector2[] uvs2         = this.uvs2;
        Color32[] colors       = this.colors;
        int       vertexIndex  = 0;
        Color32   color;
        float     a = skeleton.a * 255, r = skeleton.r, g = skeleton.g, b = skeleton.b;

        Vector3 meshBoundsMin;
        meshBoundsMin.x = float.MaxValue;
        meshBoundsMin.y = float.MaxValue;
        meshBoundsMin.z = zSpacing > 0f ? 0f : zSpacing * (drawOrderCount - 1);
        Vector3 meshBoundsMax;
        meshBoundsMax.x = float.MinValue;
        meshBoundsMax.y = float.MinValue;
        meshBoundsMax.z = zSpacing < 0f ? 0f : zSpacing * (drawOrderCount - 1);
        for (int i = 0; i < drawOrderCount; i++)
        {
            Slot             slot             = drawOrder.Items[i];
            Attachment       attachment       = slot.attachment;
            RegionAttachment regionAttachment = attachment as RegionAttachment;
            if (regionAttachment != null)
            {
                regionAttachment.ComputeWorldVertices(slot.bone, tempVertices);

                float z = i * zSpacing;
                vertices[vertexIndex].x     = tempVertices[RegionAttachment.X1];
                vertices[vertexIndex].y     = tempVertices[RegionAttachment.Y1];
                vertices[vertexIndex].z     = z;
                vertices[vertexIndex + 1].x = tempVertices[RegionAttachment.X4];
                vertices[vertexIndex + 1].y = tempVertices[RegionAttachment.Y4];
                vertices[vertexIndex + 1].z = z;
                vertices[vertexIndex + 2].x = tempVertices[RegionAttachment.X2];
                vertices[vertexIndex + 2].y = tempVertices[RegionAttachment.Y2];
                vertices[vertexIndex + 2].z = z;
                vertices[vertexIndex + 3].x = tempVertices[RegionAttachment.X3];
                vertices[vertexIndex + 3].y = tempVertices[RegionAttachment.Y3];
                vertices[vertexIndex + 3].z = z;

                // Eugene - added
                if (overrideVertexColor)
                {
                    color = vertexColor;

                    colors[vertexIndex]     = color;
                    colors[vertexIndex + 1] = color;
                    colors[vertexIndex + 2] = color;
                    colors[vertexIndex + 3] = color;
                }
                else
                {
                    color.a = (byte)(a * slot.a * regionAttachment.a);
                    color.r = (byte)(r * slot.r * regionAttachment.r * color.a);
                    color.g = (byte)(g * slot.g * regionAttachment.g * color.a);
                    color.b = (byte)(b * slot.b * regionAttachment.b * color.a);
                    if (slot.data.blendMode == BlendMode.additive)
                    {
                        color.a = 0;
                    }
                    colors[vertexIndex]     = color;
                    colors[vertexIndex + 1] = color;
                    colors[vertexIndex + 2] = color;
                    colors[vertexIndex + 3] = color;
                }

                float[] regionUVs = regionAttachment.uvs;
                uvs[vertexIndex].x     = regionUVs[RegionAttachment.X1];
                uvs[vertexIndex].y     = regionUVs[RegionAttachment.Y1];
                uvs[vertexIndex + 1].x = regionUVs[RegionAttachment.X4];
                uvs[vertexIndex + 1].y = regionUVs[RegionAttachment.Y4];
                uvs[vertexIndex + 2].x = regionUVs[RegionAttachment.X2];
                uvs[vertexIndex + 2].y = regionUVs[RegionAttachment.Y2];
                uvs[vertexIndex + 3].x = regionUVs[RegionAttachment.X3];
                uvs[vertexIndex + 3].y = regionUVs[RegionAttachment.Y3];

                // Calculate min/max X
                if (tempVertices[RegionAttachment.X1] < meshBoundsMin.x)
                {
                    meshBoundsMin.x = tempVertices[RegionAttachment.X1];
                }
                else if (tempVertices[RegionAttachment.X1] > meshBoundsMax.x)
                {
                    meshBoundsMax.x = tempVertices[RegionAttachment.X1];
                }
                if (tempVertices[RegionAttachment.X2] < meshBoundsMin.x)
                {
                    meshBoundsMin.x = tempVertices[RegionAttachment.X2];
                }
                else if (tempVertices[RegionAttachment.X2] > meshBoundsMax.x)
                {
                    meshBoundsMax.x = tempVertices[RegionAttachment.X2];
                }
                if (tempVertices[RegionAttachment.X3] < meshBoundsMin.x)
                {
                    meshBoundsMin.x = tempVertices[RegionAttachment.X3];
                }
                else if (tempVertices[RegionAttachment.X3] > meshBoundsMax.x)
                {
                    meshBoundsMax.x = tempVertices[RegionAttachment.X3];
                }
                if (tempVertices[RegionAttachment.X4] < meshBoundsMin.x)
                {
                    meshBoundsMin.x = tempVertices[RegionAttachment.X4];
                }
                else if (tempVertices[RegionAttachment.X4] > meshBoundsMax.x)
                {
                    meshBoundsMax.x = tempVertices[RegionAttachment.X4];
                }

                // Calculate min/max Y
                if (tempVertices[RegionAttachment.Y1] < meshBoundsMin.y)
                {
                    meshBoundsMin.y = tempVertices[RegionAttachment.Y1];
                }
                else if (tempVertices[RegionAttachment.Y1] > meshBoundsMax.y)
                {
                    meshBoundsMax.y = tempVertices[RegionAttachment.Y1];
                }
                if (tempVertices[RegionAttachment.Y2] < meshBoundsMin.y)
                {
                    meshBoundsMin.y = tempVertices[RegionAttachment.Y2];
                }
                else if (tempVertices[RegionAttachment.Y2] > meshBoundsMax.y)
                {
                    meshBoundsMax.y = tempVertices[RegionAttachment.Y2];
                }
                if (tempVertices[RegionAttachment.Y3] < meshBoundsMin.y)
                {
                    meshBoundsMin.y = tempVertices[RegionAttachment.Y3];
                }
                else if (tempVertices[RegionAttachment.Y3] > meshBoundsMax.y)
                {
                    meshBoundsMax.y = tempVertices[RegionAttachment.Y3];
                }
                if (tempVertices[RegionAttachment.Y4] < meshBoundsMin.y)
                {
                    meshBoundsMin.y = tempVertices[RegionAttachment.Y4];
                }
                else if (tempVertices[RegionAttachment.Y4] > meshBoundsMax.y)
                {
                    meshBoundsMax.y = tempVertices[RegionAttachment.Y4];
                }

                vertexIndex += 4;
            }
            else
            {
                if (!renderMeshes)
                {
                    continue;
                }
                MeshAttachment meshAttachment = attachment as MeshAttachment;
                if (meshAttachment != null)
                {
                    int meshVertexCount = meshAttachment.vertices.Length;
                    if (tempVertices.Length < meshVertexCount)
                    {
                        this.tempVertices = tempVertices = new float[meshVertexCount];
                    }
                    meshAttachment.ComputeWorldVertices(slot, tempVertices);

                    // Eugene - added
                    if (overrideVertexColor)
                    {
                        color = vertexColor;
                    }
                    else
                    {
                        color.a = (byte)(a * slot.a * meshAttachment.a);
                        color.r = (byte)(r * slot.r * meshAttachment.r * color.a);
                        color.g = (byte)(g * slot.g * meshAttachment.g * color.a);
                        color.b = (byte)(b * slot.b * meshAttachment.b * color.a);
                        if (slot.data.blendMode == BlendMode.additive)
                        {
                            color.a = 0;
                        }
                    }

                    float[] meshUVs = meshAttachment.uvs;

                    float z = i * zSpacing;
                    for (int ii = 0; ii < meshVertexCount; ii += 2, vertexIndex++)
                    {
                        vertices[vertexIndex].x = tempVertices[ii];
                        vertices[vertexIndex].y = tempVertices[ii + 1];
                        vertices[vertexIndex].z = z;
                        colors[vertexIndex]     = color;
                        uvs[vertexIndex].x      = meshUVs[ii];
                        uvs[vertexIndex].y      = meshUVs[ii + 1];

                        if (tempVertices[ii] < meshBoundsMin.x)
                        {
                            meshBoundsMin.x = tempVertices[ii];
                        }
                        else if (tempVertices[ii] > meshBoundsMax.x)
                        {
                            meshBoundsMax.x = tempVertices[ii];
                        }
                        if (tempVertices[ii + 1] < meshBoundsMin.y)
                        {
                            meshBoundsMin.y = tempVertices[ii + 1];
                        }
                        else if (tempVertices[ii + 1] > meshBoundsMax.y)
                        {
                            meshBoundsMax.y = tempVertices[ii + 1];
                        }
                    }
                }
                else
                {
                    SkinnedMeshAttachment skinnedMeshAttachment = attachment as SkinnedMeshAttachment;
                    if (skinnedMeshAttachment != null)
                    {
                        int meshVertexCount = skinnedMeshAttachment.uvs.Length;
                        if (tempVertices.Length < meshVertexCount)
                        {
                            this.tempVertices = tempVertices = new float[meshVertexCount];
                        }
                        skinnedMeshAttachment.ComputeWorldVertices(slot, tempVertices);

                        // Eugene - added
                        if (overrideVertexColor)
                        {
                            color = vertexColor;
                        }
                        else
                        {
                            color.a = (byte)(a * slot.a * skinnedMeshAttachment.a);
                            color.r = (byte)(r * slot.r * skinnedMeshAttachment.r * color.a);
                            color.g = (byte)(g * slot.g * skinnedMeshAttachment.g * color.a);
                            color.b = (byte)(b * slot.b * skinnedMeshAttachment.b * color.a);
                            if (slot.data.blendMode == BlendMode.additive)
                            {
                                color.a = 0;
                            }
                        }

                        float[] meshUVs = skinnedMeshAttachment.uvs;
                        float   z       = i * zSpacing;
                        for (int ii = 0; ii < meshVertexCount; ii += 2, vertexIndex++)
                        {
                            vertices[vertexIndex].x = tempVertices[ii];
                            vertices[vertexIndex].y = tempVertices[ii + 1];
                            vertices[vertexIndex].z = z;
                            colors[vertexIndex]     = color;
                            uvs[vertexIndex].x      = meshUVs[ii];
                            uvs[vertexIndex].y      = meshUVs[ii + 1];

                            if (tempVertices[ii] < meshBoundsMin.x)
                            {
                                meshBoundsMin.x = tempVertices[ii];
                            }
                            else if (tempVertices[ii] > meshBoundsMax.x)
                            {
                                meshBoundsMax.x = tempVertices[ii];
                            }
                            if (tempVertices[ii + 1] < meshBoundsMin.y)
                            {
                                meshBoundsMin.y = tempVertices[ii + 1];
                            }
                            else if (tempVertices[ii + 1] > meshBoundsMax.y)
                            {
                                meshBoundsMax.y = tempVertices[ii + 1];
                            }
                        }
                    }
                }
            }
        }

        // Double buffer mesh.
        Mesh mesh = useMesh1 ? mesh1 : mesh2;
        meshFilter.sharedMesh = mesh;

        mesh.vertices = vertices;
        mesh.colors32 = colors;
        mesh.uv       = uvs;

        // tsteil - added UV2 stuff
        if (setupUv2)
        {
            float minX  = 1f;
            float minY  = 1f;
            float maxX  = 0f;
            float maxY  = 0f;
            float sizeX = 0f;
            float sizeY = 0f;

            // go through our vertices and find the min and max so we can normalize the UVs against it
            for (int i = 0; i < vertexCount; ++i)
            {
                var x = vertices[i].x;
                var y = vertices[i].y;
                if (x < minX)
                {
                    minX = x;
                }
                else if (x > maxX)
                {
                    maxX = x;
                }
                if (y < minY)
                {
                    minY = y;
                }
                else if (y > maxY)
                {
                    maxY = y;
                }
            }
            sizeX = maxX - minX;
            sizeY = maxY - minY;

            // now set the uvs2
            for (int i = 0; i < vertexCount; ++i)
            {
                uvs2[i].x = (vertices[i].x - minX) / sizeX;
                uvs2[i].y = (vertices[i].y - minY) / sizeY;
            }
            mesh.uv2 = uvs2;
        }

        if (mustUpdateMeshStructure)
        {
            int submeshCount = submeshMatCount;
            mesh.subMeshCount = submeshCount;
            for (int i = 0; i < submeshCount; ++i)
            {
                mesh.SetTriangles(submeshes.Items[i].triangles, i);
            }
        }

        // tsteil: if we're not rendering, we dont need to calculate the bounds (this fixes the crazy AABB math errors)
        if (noRender == false)
        {
            Vector3 meshBoundsExtents = meshBoundsMax - meshBoundsMin;
            Vector3 meshBoundsCenter  = meshBoundsMin + meshBoundsExtents * 0.5f;
            mesh.bounds = new Bounds(meshBoundsCenter, meshBoundsExtents);
        }

        if (newTriangles && calculateNormals)
        {
            Vector3[] normals = new Vector3[vertexCount];
            Vector3   normal  = new Vector3(0, 0, -1);
            for (int i = 0; i < vertexCount; i++)
            {
                normals[i] = normal;
            }
            (useMesh1 ? mesh2 : mesh1).vertices = vertices;             // Set other mesh vertices.
            mesh1.normals = normals;
            mesh2.normals = normals;

            if (calculateTangents)
            {
                Vector4[] tangents = new Vector4[vertexCount];
                Vector3   tangent  = new Vector3(0, 0, 1);
                for (int i = 0; i < vertexCount; i++)
                {
                    tangents[i] = tangent;
                }
                mesh1.tangents = tangents;
                mesh2.tangents = tangents;
            }
        }

        // Update previous state
        ExposedList <int>  attachmentsTriangleCountCurrentMesh;
        ExposedList <bool> attachmentsFlipStateCurrentMesh;
        ExposedList <LastState.AddSubmeshArguments> addSubmeshArgumentsCurrentMesh;
        if (useMesh1)
        {
            attachmentsTriangleCountCurrentMesh = lastState.attachmentsTriangleCountMesh1;
            addSubmeshArgumentsCurrentMesh      = lastState.addSubmeshArgumentsMesh1;
            attachmentsFlipStateCurrentMesh     = lastState.attachmentsFlipStateMesh1;
            lastState.immutableTrianglesMesh1   = immutableTriangles;
        }
        else
        {
            attachmentsTriangleCountCurrentMesh = lastState.attachmentsTriangleCountMesh2;
            addSubmeshArgumentsCurrentMesh      = lastState.addSubmeshArgumentsMesh2;
            attachmentsFlipStateCurrentMesh     = lastState.attachmentsFlipStateMesh2;
            lastState.immutableTrianglesMesh2   = immutableTriangles;
        }

        attachmentsTriangleCountCurrentMesh.GrowIfNeeded(attachmentsTriangleCountTemp.Capacity);
        attachmentsTriangleCountCurrentMesh.Count = attachmentsTriangleCountTemp.Count;
        attachmentsTriangleCountTemp.CopyTo(attachmentsTriangleCountCurrentMesh.Items, 0);

        attachmentsFlipStateCurrentMesh.GrowIfNeeded(attachmentsFlipStateTemp.Capacity);
        attachmentsFlipStateCurrentMesh.Count = attachmentsFlipStateTemp.Count;
        attachmentsFlipStateTemp.CopyTo(attachmentsFlipStateCurrentMesh.Items, 0);

        addSubmeshArgumentsCurrentMesh.GrowIfNeeded(addSubmeshArgumentsTemp.Count);
        addSubmeshArgumentsCurrentMesh.Count = addSubmeshArgumentsTemp.Count;
        addSubmeshArgumentsTemp.CopyTo(addSubmeshArgumentsCurrentMesh.Items);

        if (submeshRenderers.Length > 0)
        {
            for (int i = 0; i < submeshRenderers.Length; i++)
            {
                SkeletonUtilitySubmeshRenderer submeshRenderer = submeshRenderers[i];
                if (submeshRenderer.submeshIndex < sharedMaterials.Length)
                {
                    submeshRenderer.SetMesh(meshRenderer, useMesh1 ? mesh1 : mesh2, sharedMaterials[submeshRenderer.submeshIndex]);
                }
                else
                {
                    submeshRenderer.GetComponent <Renderer>().enabled = false;
                }
            }
        }

        useMesh1 = !useMesh1;
    }
Ejemplo n.º 18
0
 public void Clear()
 {
     sharedMaterials = new Material[0];
     submeshMaterials.Clear();
 }
Ejemplo n.º 19
0
        public SubmeshedMeshInstruction GenerateInstruction(Skeleton skeleton)
        {
            if (skeleton == null)
            {
                throw new ArgumentNullException("skeleton");
            }
            int                num              = 0;
            int                num2             = 0;
            int                firstVertexIndex = 0;
            int                num3             = 0;
            int                startSlot        = 0;
            Material           material         = null;
            ExposedList <Slot> drawOrder        = skeleton.drawOrder;

            Slot[] items  = drawOrder.Items;
            int    count  = drawOrder.Count;
            int    count2 = this.separators.Count;
            ExposedList <SubmeshInstruction> submeshInstructions = this.currentInstructions.submeshInstructions;

            submeshInstructions.Clear(false);
            this.currentInstructions.attachmentList.Clear(false);
            int i = 0;

            while (i < count)
            {
                Slot             slot             = items[i];
                Attachment       attachment       = slot.attachment;
                RegionAttachment regionAttachment = attachment as RegionAttachment;
                object           rendererObject;
                int num4;
                int num5;
                if (regionAttachment != null)
                {
                    rendererObject = regionAttachment.RendererObject;
                    num4           = 4;
                    num5           = 6;
                    goto IL_E1;
                }
                MeshAttachment meshAttachment = attachment as MeshAttachment;
                if (meshAttachment != null)
                {
                    rendererObject = meshAttachment.RendererObject;
                    num4           = meshAttachment.worldVerticesLength >> 1;
                    num5           = meshAttachment.triangles.Length;
                    goto IL_E1;
                }
IL_1B8:
                i++;
                continue;
IL_E1:
                Material material2 = (Material)((AtlasRegion)rendererObject).page.rendererObject;
                bool flag = count2 > 0 && this.separators.Contains(slot);
                if ((num > 0 && material.GetInstanceID() != material2.GetInstanceID()) || flag)
                {
                    submeshInstructions.Add(new SubmeshInstruction
                    {
                        skeleton         = skeleton,
                        material         = material,
                        triangleCount    = num2,
                        vertexCount      = num3,
                        startSlot        = startSlot,
                        endSlot          = i,
                        firstVertexIndex = firstVertexIndex,
                        forceSeparate    = flag
                    });
                    num2             = 0;
                    num3             = 0;
                    firstVertexIndex = num;
                    startSlot        = i;
                }
                material = material2;
                num2    += num5;
                num3    += num4;
                num     += num4;
                this.currentInstructions.attachmentList.Add(attachment);
                goto IL_1B8;
            }
            submeshInstructions.Add(new SubmeshInstruction
            {
                skeleton         = skeleton,
                material         = material,
                triangleCount    = num2,
                vertexCount      = num3,
                startSlot        = startSlot,
                endSlot          = count,
                firstVertexIndex = firstVertexIndex,
                forceSeparate    = false
            });
            this.currentInstructions.vertexCount = num;
            return(this.currentInstructions);
        }
Ejemplo n.º 20
0
        public void ClipTriangles(float[] vertices, int verticesLength, int[] triangles, int trianglesLength, float[] uvs)
        {
            ExposedList <float> exposedList  = clipOutput;
            ExposedList <float> exposedList2 = clippedVertices;
            ExposedList <int>   exposedList3 = clippedTriangles;

            ExposedList <float>[] items = clippingPolygons.Items;
            int count = clippingPolygons.Count;
            int num   = 0;

            exposedList2.Clear();
            clippedUVs.Clear();
            exposedList3.Clear();
            for (int i = 0; i < trianglesLength; i += 3)
            {
                int   num2 = triangles[i] << 1;
                float num3 = vertices[num2];
                float num4 = vertices[num2 + 1];
                float num5 = uvs[num2];
                float num6 = uvs[num2 + 1];
                num2 = triangles[i + 1] << 1;
                float num7  = vertices[num2];
                float num8  = vertices[num2 + 1];
                float num9  = uvs[num2];
                float num10 = uvs[num2 + 1];
                num2 = triangles[i + 2] << 1;
                float num11 = vertices[num2];
                float num12 = vertices[num2 + 1];
                float num13 = uvs[num2];
                float num14 = uvs[num2 + 1];
                for (int j = 0; j < count; j++)
                {
                    int num15 = exposedList2.Count;
                    if (Clip(num3, num4, num7, num8, num11, num12, items[j], exposedList))
                    {
                        int count2 = exposedList.Count;
                        if (count2 != 0)
                        {
                            float   num16  = num8 - num12;
                            float   num17  = num11 - num7;
                            float   num18  = num3 - num11;
                            float   num19  = num12 - num4;
                            float   num20  = 1f / (num16 * num18 + num17 * (num4 - num12));
                            int     num21  = count2 >> 1;
                            float[] items2 = exposedList.Items;
                            float[] items3 = exposedList2.Resize(num15 + num21 * 2).Items;
                            float[] items4 = clippedUVs.Resize(num15 + num21 * 2).Items;
                            for (int k = 0; k < count2; k += 2)
                            {
                                float num22 = items2[k];
                                float num23 = items2[k + 1];
                                items3[num15]     = num22;
                                items3[num15 + 1] = num23;
                                float num24 = num22 - num11;
                                float num25 = num23 - num12;
                                float num26 = (num16 * num24 + num17 * num25) * num20;
                                float num27 = (num19 * num24 + num18 * num25) * num20;
                                float num28 = 1f - num26 - num27;
                                items4[num15]     = num5 * num26 + num9 * num27 + num13 * num28;
                                items4[num15 + 1] = num6 * num26 + num10 * num27 + num14 * num28;
                                num15            += 2;
                            }
                            num15 = exposedList3.Count;
                            int[] items5 = exposedList3.Resize(num15 + 3 * (num21 - 2)).Items;
                            num21--;
                            for (int l = 1; l < num21; l++)
                            {
                                items5[num15]     = num;
                                items5[num15 + 1] = num + l;
                                items5[num15 + 2] = num + l + 1;
                                num15            += 3;
                            }
                            num += num21 + 1;
                        }
                        continue;
                    }
                    float[] items6 = exposedList2.Resize(num15 + 6).Items;
                    float[] items7 = clippedUVs.Resize(num15 + 6).Items;
                    items6[num15]     = num3;
                    items6[num15 + 1] = num4;
                    items6[num15 + 2] = num7;
                    items6[num15 + 3] = num8;
                    items6[num15 + 4] = num11;
                    items6[num15 + 5] = num12;
                    items7[num15]     = num5;
                    items7[num15 + 1] = num6;
                    items7[num15 + 2] = num9;
                    items7[num15 + 3] = num10;
                    items7[num15 + 4] = num13;
                    items7[num15 + 5] = num14;
                    num15             = exposedList3.Count;
                    int[] items8 = exposedList3.Resize(num15 + 3).Items;
                    items8[num15]     = num;
                    items8[num15 + 1] = num + 1;
                    items8[num15 + 2] = num + 2;
                    num += 3;
                    break;
                }
            }
        }
        public MeshAndMaterials GenerateMesh(ExposedList <SubmeshInstruction> instructions, int startSubmesh, int endSubmesh)
        {
            SubmeshInstruction[] items = instructions.Items;
            this.currentInstructions.Clear(false);
            for (int i = startSubmesh; i < endSubmesh; i++)
            {
                this.currentInstructions.Add(items[i]);
            }
            SmartMesh next  = this.doubleBufferedSmartMesh.GetNext();
            Mesh      mesh  = next.mesh;
            int       count = this.currentInstructions.Count;

            SubmeshInstruction[] items2 = this.currentInstructions.Items;
            int num = 0;

            for (int j = 0; j < count; j++)
            {
                items2[j].firstVertexIndex = num;
                num += items2[j].vertexCount;
            }
            bool    flag     = ArraysMeshGenerator.EnsureSize(num, ref this.meshVertices, ref this.meshUVs, ref this.meshColors32);
            bool    flag2    = ArraysMeshGenerator.EnsureTriangleBuffersSize(this.submeshBuffers, count, items2);
            float   zspacing = this.ZSpacing;
            Vector3 boundsMin;
            Vector3 boundsMax;

            if (num <= 0)
            {
                boundsMin = new Vector3(0f, 0f, 0f);
                boundsMax = new Vector3(0f, 0f, 0f);
            }
            else
            {
                boundsMin.x = 2.14748365E+09f;
                boundsMin.y = 2.14748365E+09f;
                boundsMax.x = -2.14748365E+09f;
                boundsMax.y = -2.14748365E+09f;
                int endSlot = items2[count - 1].endSlot;
                if (zspacing > 0f)
                {
                    boundsMin.z = 0f;
                    boundsMax.z = zspacing * (float)endSlot;
                }
                else
                {
                    boundsMin.z = zspacing * (float)endSlot;
                    boundsMax.z = 0f;
                }
            }
            ExposedList <Attachment> exposedList = this.attachmentBuffer;

            exposedList.Clear(false);
            int num2 = 0;

            for (int k = 0; k < count; k++)
            {
                SubmeshInstruction submeshInstruction = items2[k];
                int      startSlot = submeshInstruction.startSlot;
                int      endSlot2  = submeshInstruction.endSlot;
                Skeleton skeleton  = submeshInstruction.skeleton;
                Slot[]   items3    = skeleton.DrawOrder.Items;
                for (int l = startSlot; l < endSlot2; l++)
                {
                    Attachment attachment = items3[l].attachment;
                    if (attachment != null)
                    {
                        exposedList.Add(attachment);
                    }
                }
                ArraysMeshGenerator.FillVerts(skeleton, startSlot, endSlot2, zspacing, base.PremultiplyVertexColors, this.meshVertices, this.meshUVs, this.meshColors32, ref num2, ref this.attachmentVertexBuffer, ref boundsMin, ref boundsMax, true);
            }
            bool flag3 = flag || flag2 || next.StructureDoesntMatch(exposedList, this.currentInstructions);

            for (int m = 0; m < count; m++)
            {
                SubmeshInstruction submeshInstruction2 = items2[m];
                if (flag3)
                {
                    SubmeshTriangleBuffer submeshTriangleBuffer = this.submeshBuffers.Items[m];
                    bool isLastSubmesh = m == count - 1;
                    ArraysMeshGenerator.FillTriangles(ref submeshTriangleBuffer.triangles, submeshInstruction2.skeleton, submeshInstruction2.triangleCount, submeshInstruction2.firstVertexIndex, submeshInstruction2.startSlot, submeshInstruction2.endSlot, isLastSubmesh);
                    submeshTriangleBuffer.triangleCount = submeshInstruction2.triangleCount;
                    submeshTriangleBuffer.firstVertex   = submeshInstruction2.firstVertexIndex;
                }
            }
            if (flag3)
            {
                mesh.Clear();
                this.sharedMaterials = this.currentInstructions.GetUpdatedMaterialArray(this.sharedMaterials);
            }
            next.Set(this.meshVertices, this.meshUVs, this.meshColors32, exposedList, this.currentInstructions);
            mesh.bounds = ArraysMeshGenerator.ToBounds(boundsMin, boundsMax);
            if (flag3)
            {
                mesh.subMeshCount = count;
                for (int n = 0; n < count; n++)
                {
                    mesh.SetTriangles(this.submeshBuffers.Items[n].triangles, n);
                }
                base.TryAddNormalsTo(mesh, num);
            }
            if (this.addTangents)
            {
                ArraysMeshGenerator.SolveTangents2DEnsureSize(ref this.meshTangents, ref this.tempTanBuffer, num);
                int num3 = 0;
                int num4 = count;
                while (num3 < num4)
                {
                    SubmeshTriangleBuffer submeshTriangleBuffer2 = this.submeshBuffers.Items[num3];
                    ArraysMeshGenerator.SolveTangents2DTriangles(this.tempTanBuffer, submeshTriangleBuffer2.triangles, submeshTriangleBuffer2.triangleCount, this.meshVertices, this.meshUVs, num);
                    num3++;
                }
                ArraysMeshGenerator.SolveTangents2DBuffer(this.meshTangents, this.tempTanBuffer, num);
            }
            return(new MeshAndMaterials(next.mesh, this.sharedMaterials));
        }
Ejemplo n.º 22
0
        internal bool Clip(float x1, float y1, float x2, float y2, float x3, float y3, ExposedList <float> clippingArea, ExposedList <float> output)
        {
            ExposedList <float> exposedList = output;
            bool result = false;
            ExposedList <float> exposedList2 = null;

            if (clippingArea.Count % 4 >= 2)
            {
                exposedList2 = output;
                output       = scratch;
            }
            else
            {
                exposedList2 = scratch;
            }
            exposedList2.Clear();
            exposedList2.Add(x1);
            exposedList2.Add(y1);
            exposedList2.Add(x2);
            exposedList2.Add(y2);
            exposedList2.Add(x3);
            exposedList2.Add(y3);
            exposedList2.Add(x1);
            exposedList2.Add(y1);
            output.Clear();
            float[] items = clippingArea.Items;
            int     num   = clippingArea.Count - 4;
            int     num2  = 0;

            while (true)
            {
                float   num3   = items[num2];
                float   num4   = items[num2 + 1];
                float   num5   = items[num2 + 2];
                float   num6   = items[num2 + 3];
                float   num7   = num3 - num5;
                float   num8   = num4 - num6;
                float[] items2 = exposedList2.Items;
                int     num9   = exposedList2.Count - 2;
                int     count  = output.Count;
                for (int i = 0; i < num9; i += 2)
                {
                    float num10 = items2[i];
                    float num11 = items2[i + 1];
                    float num12 = items2[i + 2];
                    float num13 = items2[i + 3];
                    bool  flag  = num7 * (num13 - num6) - num8 * (num12 - num5) > 0f;
                    if (num7 * (num11 - num6) - num8 * (num10 - num5) > 0f)
                    {
                        if (flag)
                        {
                            output.Add(num12);
                            output.Add(num13);
                            continue;
                        }
                        float num14 = num13 - num11;
                        float num15 = num12 - num10;
                        float num16 = (num15 * (num4 - num11) - num14 * (num3 - num10)) / (num14 * (num5 - num3) - num15 * (num6 - num4));
                        output.Add(num3 + (num5 - num3) * num16);
                        output.Add(num4 + (num6 - num4) * num16);
                    }
                    else if (flag)
                    {
                        float num17 = num13 - num11;
                        float num18 = num12 - num10;
                        float num19 = (num18 * (num4 - num11) - num17 * (num3 - num10)) / (num17 * (num5 - num3) - num18 * (num6 - num4));
                        output.Add(num3 + (num5 - num3) * num19);
                        output.Add(num4 + (num6 - num4) * num19);
                        output.Add(num12);
                        output.Add(num13);
                    }
                    result = true;
                }
                if (count == output.Count)
                {
                    exposedList.Clear();
                    return(true);
                }
                output.Add(output.Items[0]);
                output.Add(output.Items[1]);
                if (num2 == num)
                {
                    break;
                }
                ExposedList <float> exposedList3 = output;
                output = exposedList2;
                output.Clear();
                exposedList2 = exposedList3;
                num2        += 2;
            }
            if (exposedList != output)
            {
                exposedList.Clear();
                int j = 0;
                for (int num20 = output.Count - 2; j < num20; j++)
                {
                    exposedList.Add(output.Items[j]);
                }
            }
            else
            {
                exposedList.Resize(exposedList.Count - 2);
            }
            return(result);
        }
Ejemplo n.º 23
0
    public void UpdateMesh()
    {
        //Debug.Log("UpdateMesh");
        if (!valid)
        {
            return;
        }

        float scale = canvas.referencePixelsPerUnit;

        // Count vertices and submesh triangles.
        int                vertexCount = 0;
        int                submeshTriangleCount = 0, submeshFirstVertex = 0, submeshStartSlotIndex = 0;
        Material           lastMaterial   = null;
        ExposedList <Slot> drawOrder      = skeleton.drawOrder;
        int                drawOrderCount = drawOrder.Count;
        bool               renderMeshes   = this.renderMeshes;

        // Clear last state of attachments and submeshes
        ExposedList <int> attachmentsTriangleCountTemp = lastState.attachmentsTriangleCountTemp;

        attachmentsTriangleCountTemp.GrowIfNeeded(drawOrderCount);
        attachmentsTriangleCountTemp.Count = drawOrderCount;
        ExposedList <bool> attachmentsFlipStateTemp = lastState.attachmentsFlipStateTemp;

        attachmentsFlipStateTemp.GrowIfNeeded(drawOrderCount);
        attachmentsFlipStateTemp.Count = drawOrderCount;

        ExposedList <LastState.AddSubmeshArguments> addSubmeshArgumentsTemp = lastState.addSubmeshArgumentsTemp;

        addSubmeshArgumentsTemp.Clear(false);
        for (int i = 0; i < drawOrderCount; i++)
        {
            Slot       slot       = drawOrder.Items[i];
            Bone       bone       = slot.bone;
            Attachment attachment = slot.attachment;

            object rendererObject;
            int    attachmentVertexCount, attachmentTriangleCount;
            bool   worldScaleXIsPositive = bone.worldScaleX >= 0f;
            bool   worldScaleYIsPositive = bone.worldScaleY >= 0f;
            bool   worldScaleIsSameSigns = (worldScaleXIsPositive && worldScaleYIsPositive) ||
                                           (!worldScaleXIsPositive && !worldScaleYIsPositive);
            bool flip = frontFacing && ((bone.worldFlipX != bone.worldFlipY) == worldScaleIsSameSigns);
            attachmentsFlipStateTemp.Items[i] = flip;

            attachmentsTriangleCountTemp.Items[i] = -1;
            var regionAttachment = attachment as RegionAttachment;
            if (regionAttachment != null)
            {
                rendererObject          = regionAttachment.RendererObject;
                attachmentVertexCount   = 4;
                attachmentTriangleCount = 6;
            }
            else
            {
                if (!renderMeshes)
                {
                    continue;
                }
                var meshAttachment = attachment as MeshAttachment;
                if (meshAttachment != null)
                {
                    rendererObject          = meshAttachment.RendererObject;
                    attachmentVertexCount   = meshAttachment.vertices.Length >> 1;
                    attachmentTriangleCount = meshAttachment.triangles.Length;
                }
                else
                {
                    var skinnedMeshAttachment = attachment as SkinnedMeshAttachment;
                    if (skinnedMeshAttachment != null)
                    {
                        rendererObject          = skinnedMeshAttachment.RendererObject;
                        attachmentVertexCount   = skinnedMeshAttachment.uvs.Length >> 1;
                        attachmentTriangleCount = skinnedMeshAttachment.triangles.Length;
                    }
                    else
                    {
                        continue;
                    }
                }
            }

            // Populate submesh when material changes.
#if !SPINE_TK2D
            var currentMaterial = (Material)((AtlasRegion)rendererObject).page.rendererObject;
#else
            var currentMaterial = (rendererObject.GetType() == typeof(Material)) ? (Material)rendererObject : (Material)((AtlasRegion)rendererObject).page.rendererObject;
#endif
            if ((lastMaterial != null && lastMaterial.GetInstanceID() != currentMaterial.GetInstanceID()))
            {
                addSubmeshArgumentsTemp.Add(
                    new LastState.AddSubmeshArguments(lastMaterial, submeshStartSlotIndex, i, submeshTriangleCount, submeshFirstVertex, false)
                    );
                submeshTriangleCount  = 0;
                submeshFirstVertex    = vertexCount;
                submeshStartSlotIndex = i;
            }
            lastMaterial = currentMaterial;

            submeshTriangleCount += attachmentTriangleCount;
            vertexCount          += attachmentVertexCount;

            attachmentsTriangleCountTemp.Items[i] = attachmentTriangleCount;
        }
        addSubmeshArgumentsTemp.Add(
            new LastState.AddSubmeshArguments(lastMaterial, submeshStartSlotIndex, drawOrderCount, submeshTriangleCount, submeshFirstVertex, true)
            );

        bool mustUpdateMeshStructure = CheckIfMustUpdateMeshStructure(attachmentsTriangleCountTemp, attachmentsFlipStateTemp, addSubmeshArgumentsTemp);
        if (mustUpdateMeshStructure)
        {
            submeshMaterials.Clear();
            for (int i = 0, n = addSubmeshArgumentsTemp.Count; i < n; i++)
            {
                LastState.AddSubmeshArguments arguments = addSubmeshArgumentsTemp.Items[i];
                AddSubmesh(
                    arguments.material,
                    arguments.startSlot,
                    arguments.endSlot,
                    arguments.triangleCount,
                    arguments.firstVertex,
                    arguments.lastSubmesh,
                    attachmentsFlipStateTemp
                    );
            }

            // Set materials.
            if (submeshMaterials.Count == sharedMaterials.Length)
            {
                submeshMaterials.CopyTo(sharedMaterials);
            }
            else
            {
                sharedMaterials = submeshMaterials.ToArray();
            }

            //meshRenderer.sharedMaterials = sharedMaterials;
            this.material = sharedMaterials[0];
            canvasRenderer.SetMaterial(sharedMaterials[0], (Texture)null);
        }

        // Ensure mesh data is the right size.
        Vector3[] vertices     = this.vertices;
        bool      newTriangles = vertexCount > vertices.Length;
        if (newTriangles)
        {
            // Not enough vertices, increase size.
            this.vertices = vertices = new Vector3[vertexCount];
            this.colors   = new Color32[vertexCount];
            this.uvs      = new Vector2[vertexCount];
            mesh1.Clear();
            mesh2.Clear();
        }
        else
        {
            // Too many vertices, zero the extra.
            Vector3 zero = Vector3.zero;
            for (int i = vertexCount, n = lastState.vertexCount; i < n; i++)
            {
                vertices[i] = zero;
            }
        }
        lastState.vertexCount = vertexCount;

        // Setup mesh.
        float     zSpacing = this.zSpacing;
        float[]   tempVertices = this.tempVertices;
        Vector2[] uvs = this.uvs;
        Color32[] colors = this.colors;
        int       vertexIndex = 0;
        Color32   vertColor = new Color32();
        Color     graphicColor = base.color;
        float     a = skeleton.a * 255, r = skeleton.r, g = skeleton.g, b = skeleton.b;


        // Mesh bounds
        Vector3 meshBoundsMin;
        meshBoundsMin.x = float.MaxValue;
        meshBoundsMin.y = float.MaxValue;
        meshBoundsMin.z = zSpacing > 0f ? 0f : zSpacing * (drawOrderCount - 1);
        Vector3 meshBoundsMax;
        meshBoundsMax.x = float.MinValue;
        meshBoundsMax.y = float.MinValue;
        meshBoundsMax.z = zSpacing < 0f ? 0f : zSpacing * (drawOrderCount - 1);

        for (int i = 0; i < drawOrderCount; i++)
        {
            Slot       slot             = drawOrder.Items[i];
            Attachment attachment       = slot.attachment;
            var        regionAttachment = attachment as RegionAttachment;
            if (regionAttachment != null)
            {
                regionAttachment.ComputeWorldVertices(slot.bone, tempVertices);

                float z = i * zSpacing;
                vertices[vertexIndex].x     = tempVertices[RegionAttachment.X1] * scale;
                vertices[vertexIndex].y     = tempVertices[RegionAttachment.Y1] * scale;
                vertices[vertexIndex].z     = z;
                vertices[vertexIndex + 1].x = tempVertices[RegionAttachment.X4] * scale;
                vertices[vertexIndex + 1].y = tempVertices[RegionAttachment.Y4] * scale;
                vertices[vertexIndex + 1].z = z;
                vertices[vertexIndex + 2].x = tempVertices[RegionAttachment.X2] * scale;
                vertices[vertexIndex + 2].y = tempVertices[RegionAttachment.Y2] * scale;
                vertices[vertexIndex + 2].z = z;
                vertices[vertexIndex + 3].x = tempVertices[RegionAttachment.X3] * scale;
                vertices[vertexIndex + 3].y = tempVertices[RegionAttachment.Y3] * scale;
                vertices[vertexIndex + 3].z = z;

                vertColor.a = (byte)(a * slot.a * regionAttachment.a * graphicColor.a);
                vertColor.r = (byte)(r * slot.r * regionAttachment.r * graphicColor.r * vertColor.a);
                vertColor.g = (byte)(g * slot.g * regionAttachment.g * graphicColor.g * vertColor.a);
                vertColor.b = (byte)(b * slot.b * regionAttachment.b * graphicColor.b * vertColor.a);
                if (slot.data.blendMode == BlendMode.additive)
                {
                    vertColor.a = 0;
                }
                colors[vertexIndex]     = vertColor;
                colors[vertexIndex + 1] = vertColor;
                colors[vertexIndex + 2] = vertColor;
                colors[vertexIndex + 3] = vertColor;

                float[] regionUVs = regionAttachment.uvs;
                uvs[vertexIndex].x     = regionUVs[RegionAttachment.X1];
                uvs[vertexIndex].y     = regionUVs[RegionAttachment.Y1];
                uvs[vertexIndex + 1].x = regionUVs[RegionAttachment.X4];
                uvs[vertexIndex + 1].y = regionUVs[RegionAttachment.Y4];
                uvs[vertexIndex + 2].x = regionUVs[RegionAttachment.X2];
                uvs[vertexIndex + 2].y = regionUVs[RegionAttachment.Y2];
                uvs[vertexIndex + 3].x = regionUVs[RegionAttachment.X3];
                uvs[vertexIndex + 3].y = regionUVs[RegionAttachment.Y3];


                // Calculate Bounds min/max X
                if (tempVertices[RegionAttachment.X1] < meshBoundsMin.x)
                {
                    meshBoundsMin.x = tempVertices[RegionAttachment.X1];
                }
                else if (tempVertices[RegionAttachment.X1] > meshBoundsMax.x)
                {
                    meshBoundsMax.x = tempVertices[RegionAttachment.X1];
                }
                if (tempVertices[RegionAttachment.X2] < meshBoundsMin.x)
                {
                    meshBoundsMin.x = tempVertices[RegionAttachment.X2];
                }
                else if (tempVertices[RegionAttachment.X2] > meshBoundsMax.x)
                {
                    meshBoundsMax.x = tempVertices[RegionAttachment.X2];
                }
                if (tempVertices[RegionAttachment.X3] < meshBoundsMin.x)
                {
                    meshBoundsMin.x = tempVertices[RegionAttachment.X3];
                }
                else if (tempVertices[RegionAttachment.X3] > meshBoundsMax.x)
                {
                    meshBoundsMax.x = tempVertices[RegionAttachment.X3];
                }
                if (tempVertices[RegionAttachment.X4] < meshBoundsMin.x)
                {
                    meshBoundsMin.x = tempVertices[RegionAttachment.X4];
                }
                else if (tempVertices[RegionAttachment.X4] > meshBoundsMax.x)
                {
                    meshBoundsMax.x = tempVertices[RegionAttachment.X4];
                }

                // Calculate Bounds min/max Y
                if (tempVertices[RegionAttachment.Y1] < meshBoundsMin.y)
                {
                    meshBoundsMin.y = tempVertices[RegionAttachment.Y1];
                }
                else if (tempVertices[RegionAttachment.Y1] > meshBoundsMax.y)
                {
                    meshBoundsMax.y = tempVertices[RegionAttachment.Y1];
                }
                if (tempVertices[RegionAttachment.Y2] < meshBoundsMin.y)
                {
                    meshBoundsMin.y = tempVertices[RegionAttachment.Y2];
                }
                else if (tempVertices[RegionAttachment.Y2] > meshBoundsMax.y)
                {
                    meshBoundsMax.y = tempVertices[RegionAttachment.Y2];
                }
                if (tempVertices[RegionAttachment.Y3] < meshBoundsMin.y)
                {
                    meshBoundsMin.y = tempVertices[RegionAttachment.Y3];
                }
                else if (tempVertices[RegionAttachment.Y3] > meshBoundsMax.y)
                {
                    meshBoundsMax.y = tempVertices[RegionAttachment.Y3];
                }
                if (tempVertices[RegionAttachment.Y4] < meshBoundsMin.y)
                {
                    meshBoundsMin.y = tempVertices[RegionAttachment.Y4];
                }
                else if (tempVertices[RegionAttachment.Y4] > meshBoundsMax.y)
                {
                    meshBoundsMax.y = tempVertices[RegionAttachment.Y4];
                }

                vertexIndex += 4;
            }
            else
            {
                if (!renderMeshes)
                {
                    continue;
                }
                var meshAttachment = attachment as MeshAttachment;
                if (meshAttachment != null)
                {
                    int meshVertexCount = meshAttachment.vertices.Length;
                    if (tempVertices.Length < meshVertexCount)
                    {
                        this.tempVertices = tempVertices = new float[meshVertexCount];
                    }
                    meshAttachment.ComputeWorldVertices(slot, tempVertices);

                    vertColor.a = (byte)(a * slot.a * meshAttachment.a * graphicColor.a);
                    vertColor.r = (byte)(r * slot.r * meshAttachment.r * graphicColor.r * vertColor.a);
                    vertColor.g = (byte)(g * slot.g * meshAttachment.g * graphicColor.g * vertColor.a);
                    vertColor.b = (byte)(b * slot.b * meshAttachment.b * graphicColor.b * vertColor.a);
                    if (slot.data.blendMode == BlendMode.additive)
                    {
                        vertColor.a = 0;
                    }

                    float[] meshUVs = meshAttachment.uvs;
                    float   z       = i * zSpacing;
                    for (int ii = 0; ii < meshVertexCount; ii += 2, vertexIndex++)
                    {
                        vertices[vertexIndex].x = tempVertices[ii] * scale;
                        vertices[vertexIndex].y = tempVertices[ii + 1] * scale;
                        vertices[vertexIndex].z = z;
                        colors[vertexIndex]     = vertColor;
                        uvs[vertexIndex].x      = meshUVs[ii];
                        uvs[vertexIndex].y      = meshUVs[ii + 1];

                        // Calculate Bounds
                        if (tempVertices[ii] < meshBoundsMin.x)
                        {
                            meshBoundsMin.x = tempVertices[ii];
                        }
                        else if (tempVertices[ii] > meshBoundsMax.x)
                        {
                            meshBoundsMax.x = tempVertices[ii];
                        }
                        if (tempVertices[ii + 1] < meshBoundsMin.y)
                        {
                            meshBoundsMin.y = tempVertices[ii + 1];
                        }
                        else if (tempVertices[ii + 1] > meshBoundsMax.y)
                        {
                            meshBoundsMax.y = tempVertices[ii + 1];
                        }
                    }
                }
                else
                {
                    var skinnedMeshAttachment = attachment as SkinnedMeshAttachment;
                    if (skinnedMeshAttachment != null)
                    {
                        int meshVertexCount = skinnedMeshAttachment.uvs.Length;
                        if (tempVertices.Length < meshVertexCount)
                        {
                            this.tempVertices = tempVertices = new float[meshVertexCount];
                        }
                        skinnedMeshAttachment.ComputeWorldVertices(slot, tempVertices);

                        vertColor.a = (byte)(a * slot.a * skinnedMeshAttachment.a * graphicColor.a);
                        vertColor.r = (byte)(r * slot.r * skinnedMeshAttachment.r * graphicColor.r * vertColor.a);
                        vertColor.g = (byte)(g * slot.g * skinnedMeshAttachment.g * graphicColor.g * vertColor.a);
                        vertColor.b = (byte)(b * slot.b * skinnedMeshAttachment.b * graphicColor.b * vertColor.a);
                        if (slot.data.blendMode == BlendMode.additive)
                        {
                            vertColor.a = 0;
                        }

                        float[] meshUVs = skinnedMeshAttachment.uvs;
                        float   z       = i * zSpacing;
                        for (int ii = 0; ii < meshVertexCount; ii += 2, vertexIndex++)
                        {
                            vertices[vertexIndex].x = tempVertices[ii] * scale;
                            vertices[vertexIndex].y = tempVertices[ii + 1] * scale;
                            vertices[vertexIndex].z = z;
                            colors[vertexIndex]     = vertColor;
                            uvs[vertexIndex].x      = meshUVs[ii];
                            uvs[vertexIndex].y      = meshUVs[ii + 1];


                            // Calculate Bounds
                            if (tempVertices[ii] < meshBoundsMin.x)
                            {
                                meshBoundsMin.x = tempVertices[ii];
                            }
                            else if (tempVertices[ii] > meshBoundsMax.x)
                            {
                                meshBoundsMax.x = tempVertices[ii];
                            }
                            if (tempVertices[ii + 1] < meshBoundsMin.y)
                            {
                                meshBoundsMin.y = tempVertices[ii + 1];
                            }
                            else if (tempVertices[ii + 1] > meshBoundsMax.y)
                            {
                                meshBoundsMax.y = tempVertices[ii + 1];
                            }
                        }
                    }
                }
            }
        }


        // Double buffer mesh.
        Mesh mesh = useMesh1 ? mesh1 : mesh2;

        // Push data from buffers.
        mesh.vertices = vertices;
        mesh.colors32 = colors;
        mesh.uv       = uvs;

        // Set Mesh bounds.
        Vector3 meshBoundsExtents = (meshBoundsMax - meshBoundsMin) * scale;    // scaled
        Vector3 meshBoundsCenter  = meshBoundsMin + meshBoundsExtents * 0.5f;
        mesh.bounds = new Bounds(meshBoundsCenter, meshBoundsExtents);
        //mesh.RecalculateBounds();

        canvasRenderer.SetMesh(mesh);
        //this.SetVerticesDirty();


        if (mustUpdateMeshStructure)
        {
            int submeshCount = submeshMaterials.Count;
            mesh.subMeshCount = submeshCount;
            for (int i = 0; i < submeshCount; ++i)
            {
                mesh.SetTriangles(submeshes.Items[i].triangles, i);
            }

            /*
             * TODO: Check fix with a known repro case.
             *          if (useMesh1)
             *                  lastState.forceUpdateMesh1 = false;
             *          else
             *                  lastState.forceUpdateMesh2 = false;
             */
        }

        if (newTriangles && calculateNormals)
        {
            var normals = new Vector3[vertexCount];
            var normal  = new Vector3(0, 0, -1);
            for (int i = 0; i < vertexCount; i++)
            {
                normals[i] = normal;
            }
            (useMesh1 ? mesh2 : mesh1).vertices = vertices; // Set other mesh vertices.
            mesh1.normals = normals;
            mesh2.normals = normals;

            if (calculateTangents)
            {
                var tangents = new Vector4[vertexCount];
                var tangent  = new Vector3(0, 0, 1);
                for (int i = 0; i < vertexCount; i++)
                {
                    tangents[i] = tangent;
                }
                mesh1.tangents = tangents;
                mesh2.tangents = tangents;
            }
        }

        // Update previous state
        ExposedList <int>  attachmentsTriangleCountCurrentMesh;
        ExposedList <bool> attachmentsFlipStateCurrentMesh;
        ExposedList <LastState.AddSubmeshArguments> addSubmeshArgumentsCurrentMesh;
        if (useMesh1)
        {
            attachmentsTriangleCountCurrentMesh = lastState.attachmentsTriangleCountMesh1;
            addSubmeshArgumentsCurrentMesh      = lastState.addSubmeshArgumentsMesh1;
            attachmentsFlipStateCurrentMesh     = lastState.attachmentsFlipStateMesh1;
            lastState.immutableTrianglesMesh1   = immutableTriangles;
        }
        else
        {
            attachmentsTriangleCountCurrentMesh = lastState.attachmentsTriangleCountMesh2;
            addSubmeshArgumentsCurrentMesh      = lastState.addSubmeshArgumentsMesh2;
            attachmentsFlipStateCurrentMesh     = lastState.attachmentsFlipStateMesh2;
            lastState.immutableTrianglesMesh2   = immutableTriangles;
        }

        attachmentsTriangleCountCurrentMesh.GrowIfNeeded(attachmentsTriangleCountTemp.Capacity);
        attachmentsTriangleCountCurrentMesh.Count = attachmentsTriangleCountTemp.Count;
        attachmentsTriangleCountTemp.CopyTo(attachmentsTriangleCountCurrentMesh.Items, 0);

        attachmentsFlipStateCurrentMesh.GrowIfNeeded(attachmentsFlipStateTemp.Capacity);
        attachmentsFlipStateCurrentMesh.Count = attachmentsFlipStateTemp.Count;
        attachmentsFlipStateTemp.CopyTo(attachmentsFlipStateCurrentMesh.Items, 0);

        addSubmeshArgumentsCurrentMesh.GrowIfNeeded(addSubmeshArgumentsTemp.Count);
        addSubmeshArgumentsCurrentMesh.Count = addSubmeshArgumentsTemp.Count;
        addSubmeshArgumentsTemp.CopyTo(addSubmeshArgumentsCurrentMesh.Items);

        useMesh1 = !useMesh1;
    }
Ejemplo n.º 24
0
        public void AddSubmesh(SubmeshInstruction instruction, bool updateTriangles = true)
        {
            Settings settings = this.settings;

            if (submeshes.Count - 1 < submeshIndex)
            {
                submeshes.Resize(submeshIndex + 1);
                if (submeshes.Items[submeshIndex] == null)
                {
                    submeshes.Items[submeshIndex] = new ExposedList <int>();
                }
            }
            ExposedList <int> exposedList = submeshes.Items[submeshIndex];

            exposedList.Clear(clearArray: false);
            Skeleton skeleton = instruction.skeleton;

            Slot[]  items           = skeleton.drawOrder.Items;
            Color32 color           = default(Color32);
            float   num             = skeleton.a * 255f;
            float   r               = skeleton.r;
            float   g               = skeleton.g;
            float   b               = skeleton.b;
            Vector2 vector          = meshBoundsMin;
            Vector2 vector2         = meshBoundsMax;
            float   zSpacing        = settings.zSpacing;
            bool    pmaVertexColors = settings.pmaVertexColors;
            bool    tintBlack       = settings.tintBlack;
            bool    flag            = settings.useClipping && instruction.hasClipping;

            if (flag && instruction.preActiveClippingSlotSource >= 0)
            {
                Slot slot = items[instruction.preActiveClippingSlotSource];
                clipper.ClipStart(slot, slot.attachment as ClippingAttachment);
            }
            for (int i = instruction.startSlot; i < instruction.endSlot; i++)
            {
                Slot             slot2            = items[i];
                Attachment       attachment       = slot2.attachment;
                float            z                = zSpacing * (float)i;
                float[]          array            = tempVerts;
                Color            color2           = default(Color);
                RegionAttachment regionAttachment = attachment as RegionAttachment;
                float[]          array2;
                int[]            array3;
                int num2;
                int num3;
                if (regionAttachment != null)
                {
                    regionAttachment.ComputeWorldVertices(slot2.bone, array, 0);
                    array2   = regionAttachment.uvs;
                    array3   = regionTriangles;
                    color2.r = regionAttachment.r;
                    color2.g = regionAttachment.g;
                    color2.b = regionAttachment.b;
                    color2.a = regionAttachment.a;
                    num2     = 4;
                    num3     = 6;
                }
                else
                {
                    MeshAttachment meshAttachment = attachment as MeshAttachment;
                    if (meshAttachment == null)
                    {
                        if (flag)
                        {
                            ClippingAttachment clippingAttachment = attachment as ClippingAttachment;
                            if (clippingAttachment != null)
                            {
                                clipper.ClipStart(slot2, clippingAttachment);
                            }
                        }
                        continue;
                    }
                    int worldVerticesLength = meshAttachment.worldVerticesLength;
                    if (array.Length < worldVerticesLength)
                    {
                        array = (tempVerts = new float[worldVerticesLength]);
                    }
                    meshAttachment.ComputeWorldVertices(slot2, 0, worldVerticesLength, array, 0);
                    array2   = meshAttachment.uvs;
                    array3   = meshAttachment.triangles;
                    color2.r = meshAttachment.r;
                    color2.g = meshAttachment.g;
                    color2.b = meshAttachment.b;
                    color2.a = meshAttachment.a;
                    num2     = worldVerticesLength >> 1;
                    num3     = meshAttachment.triangles.Length;
                }
                if (pmaVertexColors)
                {
                    color.a = (byte)(num * slot2.a * color2.a);
                    color.r = (byte)(r * slot2.r * color2.r * (float)(int)color.a);
                    color.g = (byte)(g * slot2.g * color2.g * (float)(int)color.a);
                    color.b = (byte)(b * slot2.b * color2.b * (float)(int)color.a);
                    if (slot2.data.blendMode == BlendMode.Additive)
                    {
                        color.a = 0;
                    }
                }
                else
                {
                    color.a = (byte)(num * slot2.a * color2.a);
                    color.r = (byte)(r * slot2.r * color2.r * 255f);
                    color.g = (byte)(g * slot2.g * color2.g * 255f);
                    color.b = (byte)(b * slot2.b * color2.b * 255f);
                }
                if (flag && clipper.IsClipping())
                {
                    clipper.ClipTriangles(array, num2 << 1, array3, num3, array2);
                    array  = clipper.clippedVertices.Items;
                    num2   = clipper.clippedVertices.Count >> 1;
                    array3 = clipper.clippedTriangles.Items;
                    num3   = clipper.clippedTriangles.Count;
                    array2 = clipper.clippedUVs.Items;
                }
                if (num2 != 0 && num3 != 0)
                {
                    if (tintBlack)
                    {
                        AddAttachmentTintBlack(slot2.r2, slot2.g2, slot2.b2, num2);
                    }
                    int count = vertexBuffer.Count;
                    int num4  = count + num2;
                    if (num4 > vertexBuffer.Items.Length)
                    {
                        Array.Resize(ref vertexBuffer.Items, num4);
                        Array.Resize(ref uvBuffer.Items, num4);
                        Array.Resize(ref colorBuffer.Items, num4);
                    }
                    vertexBuffer.Count = (uvBuffer.Count = (colorBuffer.Count = num4));
                    Vector3[] items2 = vertexBuffer.Items;
                    Vector2[] items3 = uvBuffer.Items;
                    Color32[] items4 = colorBuffer.Items;
                    if (count == 0)
                    {
                        for (int j = 0; j < num2; j++)
                        {
                            int   num5 = count + j;
                            int   num6 = j << 1;
                            float num7 = array[num6];
                            float num8 = array[num6 + 1];
                            items2[num5].x = num7;
                            items2[num5].y = num8;
                            items2[num5].z = z;
                            items3[num5].x = array2[num6];
                            items3[num5].y = array2[num6 + 1];
                            items4[num5]   = color;
                            if (num7 < vector.x)
                            {
                                vector.x = num7;
                            }
                            if (num7 > vector2.x)
                            {
                                vector2.x = num7;
                            }
                            if (num8 < vector.y)
                            {
                                vector.y = num8;
                            }
                            if (num8 > vector2.y)
                            {
                                vector2.y = num8;
                            }
                        }
                    }
                    else
                    {
                        for (int k = 0; k < num2; k++)
                        {
                            int   num9  = count + k;
                            int   num10 = k << 1;
                            float num11 = array[num10];
                            float num12 = array[num10 + 1];
                            items2[num9].x = num11;
                            items2[num9].y = num12;
                            items2[num9].z = z;
                            items3[num9].x = array2[num10];
                            items3[num9].y = array2[num10 + 1];
                            items4[num9]   = color;
                            if (num11 < vector.x)
                            {
                                vector.x = num11;
                            }
                            else if (num11 > vector2.x)
                            {
                                vector2.x = num11;
                            }
                            if (num12 < vector.y)
                            {
                                vector.y = num12;
                            }
                            else if (num12 > vector2.y)
                            {
                                vector2.y = num12;
                            }
                        }
                    }
                    if (updateTriangles)
                    {
                        int count2 = exposedList.Count;
                        int num13  = count2 + num3;
                        if (num13 > exposedList.Items.Length)
                        {
                            Array.Resize(ref exposedList.Items, num13);
                        }
                        exposedList.Count = num13;
                        int[] items5 = exposedList.Items;
                        for (int l = 0; l < num3; l++)
                        {
                            items5[count2 + l] = array3[l] + count;
                        }
                    }
                }
                clipper.ClipEnd(slot2);
            }
            clipper.ClipEnd();
            meshBoundsMin       = vector;
            meshBoundsMax       = vector2;
            meshBoundsThickness = (float)instruction.endSlot * zSpacing;
            int[] items6 = exposedList.Items;
            int   m      = exposedList.Count;

            for (int num14 = items6.Length; m < num14; m++)
            {
                items6[m] = 0;
            }
            submeshIndex++;
        }