Пример #1
0
        /// <summary>
        /// Draws the texture after a delay triggered by the invalidate function
        /// </summary>
        private void drawTexture()
        {
            dirty = false;
            //Back in 1.0 we only had one material we cared about which was the entire body which was ALWAYS at slot0, now we need to care about HEAD and BODY and order is not guaranteed so we now use a lookup

            //Debug.Log ("REDRAWING ALPHA INJECTION: " + Time.frameCount + " | " + figure.gameObject.transform.parent.name );

            //Material[] materials = figure.GetSkinnedMeshRenderer().sharedMaterials;

            Material[]          materials = GetMaterialsClone();
            SkinnedMeshRenderer smr       = figure.GetSkinnedMeshRenderer();

            for (int i = 0; i < materials.Length; i++)
            {
                if (materials[i] == null || !materials[i].HasProperty("_AlphaTex"))
                {
                    continue;
                }

                MATERIAL_SLOT slot = MATERIAL_SLOT.UNKNOWN;

                Texture2D[] masks = null;
                //TODO this is a TERRIBLE way of checking, we need to build a map
                if (materials[i].name.ToLower().Contains("head"))
                {
                    slot = MATERIAL_SLOT.HEAD;
                }
                else if (materials[i].name.ToLower().Contains("body"))
                {
                    slot = MATERIAL_SLOT.BODY;
                }
                else if (materials[i].name.ToLower().Contains("genesis2"))
                {
                    slot = MATERIAL_SLOT.BODY;
                }

                if (slot == MATERIAL_SLOT.UNKNOWN)
                {
                    //do nothing, we're not sure what slot to process
                    continue;
                }

                if (subAlphaTextures.ContainsKey(slot))
                {
                    masks = new Texture2D[subAlphaTextures[slot].Count];
                    subAlphaTextures[slot].Values.CopyTo(masks, 0);
                }

                RenderTexture rt  = null;
                Texture2D     tex = null;

                if (masks != null && masks.Length > 0)
                {
                    switch (textureMode)
                    {
                    case TEXTURE_MODE.FAST:
                        tex = new Texture2D(1024, 1024, TextureFormat.ARGB32, true);
                        TextureUtilities.OverlayArrayOfTexturesGPU(ref tex, masks);

                        //uncomment if you want to debug the masks
                        //TextureUtilities.OverlayArrayOfTexturesGPU(ref tex, masks,"Unlit/AlphaCombiner",true);
                        temporaryTexture2D.Add(tex);
                        materials[i].SetTexture("_AlphaTex", tex);

                        break;

                    case TEXTURE_MODE.FASTEST:
                        rt = TextureUtilities.OverlayArrayOfTexturesGPU(masks);
                        //rt = TextureUtilities.OverlayArrayOfTexturesGPU(masks, "Unlit/AlphaCombiner", true);
                        temporaryRenderTextures[i] = rt;
                        //UnityEngine.Debug.Log("Assigning rt: " + smr.name + " | " + slot);
                        materials[i].SetTexture("_AlphaTex", rt);
                        break;
                    }
                }
                else
                {
                    //no textures, clear it
                    materials[i].SetTexture("_AlphaTex", null);
                }

                /*
                 * RenderTexture rtOld = materials[i].GetTexture("_AlphaTex") as RenderTexture;
                 * if(rtOld != null)
                 * {
                 *  RenderTexture.Destroy(rtOld);
                 * }
                 */

                //UnityEngine.Debug.Log("Drawing Alpha: " + materials[i].name + " | " + materials.Length + " | " + (masks != null ? masks.Length.ToString() : "null") + " | " + slot + " | " + (tex != null ? "Tex is not null" : "tex is null"));
                //install the texture
                //materials[i].SetTexture("_AlphaTex", tex);
            }

            if (Application.isPlaying)
            {
                smr.materials = materials;
            }
            else
            {
                smr.sharedMaterials = materials;
            }
        }