static public System.WeakReference GetMaterial(SpriteRenderMode rm, Texture tex)
        {
            int idx = rm == SpriteRenderMode.None ? 0 : (int)rm;
            SpriteRenderModeGroup group = renderModeGroup[idx];

            System.WeakReference ret = null;

            int id = tex == null ? 0 : tex.GetInstanceID();

            if (group != null)
            {
                if (group.setting.IsAlive)
                {
                    ret = group.materials[id] as System.WeakReference;
                    if (ret == null)
                    {
                        SpriteRenderModeSetting setting = group.setting.Target as SpriteRenderModeSetting;

                        Material mat = new Material(setting.shader);
                        mat.mainTexture = tex;

                        ret = new System.WeakReference(mat);
                        group.materials[id] = ret;
                        setting.materials.Add(ret);
                    }
                    else
                    {
                        if (!ret.IsAlive)
                        {
                            SpriteRenderModeSetting setting = group.setting.Target as SpriteRenderModeSetting;

                            Material mat = new Material(setting.shader);
                            mat.mainTexture = tex;

                            ret = new System.WeakReference(mat);
                            group.materials[id] = ret;
                            setting.materials.Add(ret);
                        }
                    }
                }
                else
                {
                    renderModeGroup[idx] = null;
                }
            }
            else
            {
                Debug.LogError("unknown RenderMode " + rm);
            }
            return(ret);
        }
        static internal void RegisterRenderMode(SpriteRenderModeSetting setting)
        {
            if (setting == null)
            {
                return;
            }
            int idx = (int)setting.renderMode;
            SpriteRenderModeGroup group = renderModeGroup[idx];

            if (group == null)
            {
                renderModeGroup[idx] = group = new SpriteRenderModeGroup();
                group.isValid        = true;
            }
            group.setting = setting;
        }
        static internal void UnregisterRenderMode(SpriteRenderModeSetting settings)
        {
            SpriteRenderModeGroup group = renderModeGroup[(int)settings.renderMode];

            foreach (Material mat in group.materials.Values)
            {
                if (Application.isPlaying)
                {
                    Object.Destroy(mat);
                }
                else
                {
                    Object.DestroyImmediate(mat);
                }
            }
        }
        internal ulong precalcKey( uint gameObjectLayer, uint depthInLayer, SpriteRenderModeSetting renderMode, bool applyLayerToZ)
        {
            ulong ret = 0;

            ulong idx = (ulong)renderMode.renderMode & 0xf;
            uint layer = applyLayerToZ ?
                ((gameObjectLayer <= 0xf ? 0 : gameObjectLayer) & 0xf) : 0;


            uint z = (uint)depthInLayer & 0x1ff;
            preCalcZ = (layer * 64) +
                ((float)z * 0.125f);


            if (renderMode.sortByDepth)
            {
                uint oid = ((uint)instanceID) & 0x3fffffff;

                // usage                 start    bits
                ret = (0x1ul << 63) |                                   // isSemiTransparent     63       1
                    ((0xful - layer) << 58) |                           // gameObjectLayer       58 - 62  4
                    ((0x1fful - z) << 47) |                             // transform z          48 - 57  9
                    (oid << 17) |                                      // group by owner       18 - 47  30
                    (idx << 9);                                        // rendermode           10 -  13  4
            }
            else
            {
                ret = (idx << 59);
            }

            return ret;
        }
        void Reset()
        {
            if (Application.isPlaying)
                return;

            UnityEngine.Object[] rms = Resources.FindObjectsOfTypeAll(typeof(SpriteRenderModeSetting));
            foreach (SpriteRenderModeSetting rm in rms)
            {
                if (rm.renderMode == SpriteRenderMode.AlphaBlend)
                {
                    renderMode = rm;
                    break;
                }
            }


            UnityEngine.Object[] sprs = Resources.FindObjectsOfTypeAll(typeof(Sprite));
            foreach (Sprite sp in sprs)
            {
                if (sp.name == "DefaultSpriteRendererSprite")
                {
                    Clear();
                    AttachSprite(sp);
                    Apply();

                    //foreach (Camera cam in Camera.allCameras)
                    //{
                    //    Camera2D c2d = cam.GetComponent<Camera2D>();
                    //    if (c2d != null)
                    //        c2d.Render();
                    //}
                    break;
                }
            }
        }
        internal ulong calcSortKey(ulong texId, SpritePrimitive pri, uint gameObjectLayer, uint depthInLayer, uint subLayer, SpriteRenderModeSetting renderMode, bool applyLayerToZ)
        {
            ulong ret = 0;
            if (renderMode.sortByDepth)
            {
                pri.z = preCalcZ +
                    (float)subLayer * 0.00390625f;

                ret = preCalcKey |
                    ((0x1ful - subLayer) << 13) |                        // sublayer             14 - 17  4
                    (texId & 0x3ff);                                     // texture              0  -  9  10
            }
            else
            {
                pri.z = preCalcZ +
                    (float)subLayer * 0.00390625f;

                ret = preCalcKey |
                    (texId << 52);

            }
            return ret;
        }
        static internal ulong calcSortKey(ulong texId, SpritePrimitive pri, uint gameObjectLayer, uint depthInLayer, uint subLayer, SpriteRenderModeSetting renderMode, bool applyLayerToZ)
        {
            ulong ret = 0;
            ulong idx = (ulong)renderMode.renderMode & 0xf;

            uint layer = applyLayerToZ ? 
                ( (gameObjectLayer <= 0xf ? 0 : gameObjectLayer) & 0xf) : 0;

            if (renderMode.sortByDepth)
            {
                uint z = (uint)depthInLayer & 0x1ff;

                float _z = (layer * 64) +
                    ((float)z * 0.125f) +
                    (float)subLayer * 0.00390625f;

                pri.z = _z;

                uint oid = ((uint)pri.ownerID) & 0x7fffffff;
                                                                        // usage                 start    bits
                ret = (0x1ul << 63) |                                   // isSemiTransparent     63       1
                    ((0xful - layer) << 59) |                           // gameObjectLayer       58 - 62  4
                    ((0x1fful - z) << 49) |                             // transform z          48 - 57  9
                    ( oid << 15) |                     // group by owner       15 - 47  32
                    ((0x1ful - subLayer) << 10) |                        // sublayer             11 - 14  3
                    (idx << 6) |                                        // rendermode           7 -  10  4
                    (texId)                                             // texture              0  -  6  7     
                ;
            }
            else
            {
                uint z = (uint)depthInLayer & 0x1ff;

                float _z = (layer * 64) +
                    ((float)z * 0.125f) +
                    (float)subLayer * 0.0078125f;

                pri.z = _z;

                ret = (idx << 59) |
                    (texId << 52);

            }
            return ret;
        }
        static internal void UnregisterRenderMode(SpriteRenderModeSetting settings)
        {
            SpriteRenderModeGroup group = renderModeGroup[(int)settings.renderMode];

            //todo:remove material owner by settings
        }
 static internal void RegisterRenderMode(SpriteRenderModeSetting setting)
 {
     if (setting == null) return;
     int idx = (int)setting.renderMode;
     SpriteRenderModeGroup group = renderModeGroup[idx];
     if (group == null)
     {
         renderModeGroup[idx] = group = new SpriteRenderModeGroup();
         group.isValid = true;
     }
     group.setting = setting;
 }
 static internal void RegisterRenderMode(SpriteRenderModeSetting[] settings)
 {
     foreach (SpriteRenderModeSetting setting in settings)
         RegisterRenderMode(setting);
 }
 static internal void UnregisterRenderMode(SpriteRenderModeSetting settings)
 {
     SpriteRenderModeGroup group = renderModeGroup[ (int)settings.renderMode];
     foreach( Material mat in group.materials.Values)
     {
         if ( Application.isPlaying)
             Object.Destroy( mat);
         else
             Object.DestroyImmediate(mat);
     }
 }
        static internal void UnregisterRenderMode(SpriteRenderModeSetting settings)
        {
            SpriteRenderModeGroup group = renderModeGroup[ (int)settings.renderMode];

            //todo:remove material owner by settings
        }
Esempio n. 13
0
        static internal ulong calcSortKey(ulong texId, SpritePrimitive pri, uint gameObjectLayer, uint depthInLayer, uint subLayer, SpriteRenderModeSetting renderMode)
        {
            ulong ret = 0;
            ulong idx = (ulong)renderMode.renderMode & 0xf;

            uint layer = (gameObjectLayer <= 0xf ? 0 : gameObjectLayer) & 0xf;

            if (renderMode.sortByDepth)
            {
                uint z = (uint)depthInLayer & 0x1ff;

                float _z = (layer * 64) +
                           ((float)z * 0.125f) +
                           (float)subLayer * 0.00390625f;

                pri.z = _z;

                uint oid = ((uint)pri.ownerID) & 0x7fffffff;
                // usage                 start    bits
                ret = (0x1ul << 63) |                  // isSemiTransparent     63       1
                      ((0xful - layer) << 59) |        // gameObjectLayer       58 - 62  4
                      ((0x1fful - z) << 49) |          // transform z          48 - 57  9
                      (oid << 15) |                    // group by owner       15 - 47  32
                      ((0x1ful - subLayer) << 10) |    // sublayer             11 - 14  3
                      (idx << 6) |                     // rendermode           7 -  10  4
                      (texId)                          // texture              0  -  6  7
                ;
            }
            else
            {
                uint z = (uint)depthInLayer & 0x1ff;

                float _z = (layer * 64) +
                           ((float)z * 0.125f) +
                           (float)subLayer * 0.0078125f;

                pri.z = _z;

                ret = (idx << 59) |
                      (texId << 52);
            }
            return(ret);
        }