public static int PickupObject(Ray ray)
        {
            int[] ids = SpriteManager.GetLayersID();

            foreach (int id in ids)
            {
                SpriteLayer layer = SpriteManager.GetLayer(id);

                for (int i = 0, e = layer.size; i < e; i++)
                {
                    SpritePrimitiveGroup group = layer[i];

                    for (int idx = 0; idx < group.count.value; idx++)
                    {
                        SpritePrimitive pri = group.primitives[idx];
                        if (pri.visible)
                        {
                            bool ret = IntersectTriangle(ray.origin, ray.direction, pri.position[0], pri.position[1], pri.position[2], 0, 0, 0) ||
                                       IntersectTriangle(ray.origin, ray.direction, pri.position[0], pri.position[2], pri.position[3], 0, 0, 0);

                            if (ret)
                            {
                                return(pri.ownerID);
                            }
                        }
                    }
                }
            }

            return(0);
        }
Esempio n. 2
0
        internal void DetachSprite(int idx)
        {
            int lastIdx = activePrimitiveCount - 1;

            SpriteTransform tmpSprTransform = spriteTransforms[idx];

            if (tmpSprTransform.overrideSprite != null)
            {
                OverrideSprite os = new OverrideSprite();

                os.pathHash = spriteTransforms[idx].component._fullPathHash;
                os.sprite   = spriteTransforms[idx].overrideSprite;
                overrideList.Add(os);
            }

            tmpSprTransform.overrideHash   = 0;
            tmpSprTransform.overrideSprite = null;

            if (lastIdx != idx)
            {
                SpritePrimitive tmpPri = primitives[idx];

                spriteTransforms[idx]    = spriteTransforms[lastIdx];
                spriteTransforms[idx].id = idx;
                primitives[idx]          = primitives[lastIdx];

                tmpSprTransform.id = lastIdx;

                spriteTransforms[lastIdx] = tmpSprTransform;
                primitives[lastIdx]       = tmpPri;
            }

            activePrimitiveCount--;
            _activePrimitiveCount.value = activePrimitiveCount;
        }
Esempio n. 3
0
        /// <summary>
        /// Set the max <see cref="Sprite">Sprite</see>'s count can be attach to renderer.
        /// </summary>
        /// <param name="maxSprites">How many sprite the renderer can be attach.</param>
        public void Resize(int maxSprites)
        {
            int layer  = gameObject.layer;
            int oldLen = primitives.Length;


            if (oldLen < maxSprites)
            {
                System.Array.Resize <SpritePrimitive>(ref primitives, maxSprites * 2);
                System.Array.Resize <SpriteTransform>(ref spriteTransforms, maxSprites * 2);

                int ownerID = instanceID;
                for (int i = oldLen; i < primitives.Length; i++)
                {
                    SpritePrimitive pri = new SpritePrimitive(1);
                    pri.ownerID   = ownerID;
                    primitives[i] = pri;

                    spriteTransforms[i] = new SpriteTransform();
                    spriteTransforms[i].ResetTransform();
                }
            }

            primitiveCount = primitives.Length;

            primitiveGroup.primitives = primitives;
        }
Esempio n. 4
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);
        }
        override protected int getPrimitive(int priCount, bool isClipping, Vector3 cameraPos, float radius)
        {
            if (_primitives == null)
            {
                Init(maxPrimitiveCount);
            }

            int queueMaxLength = _primitives.Length;

            if (spriteRenderer != null)
            {
                SpritePrimitiveGroup group = spriteRenderer.primitiveGroup;

                for (int pi = 0; pi < group.count.value; pi++)
                {
                    SpritePrimitive pri = group.primitives[pi];
                    if (pri.visible == false || pri.texId == 0)
                    {
                        continue;
                    }

                    _primitives[priCount] = pri;
                    priCount++;

                    if (priCount >= queueMaxLength)
                    {
                        Debug.Log("Commited SpritePrimitive's count is more than " + queueMaxLength + "!.\n" +
                                  "if in editor, you should disable some layer.\n" +
                                  "if in runtime, you should enable Camera2D clipping..\n");

                        return(priCount);
                    }
                }
            }

            return(priCount);
        }
        private int groupPrimitive(int priCount, ref int visTriangles)
        {
            subMeshCount = 0;

            int              vertexIdx   = 0;
            Texture          lastTex     = null;
            uint             lastTexId   = 0;
            int              triangleCnt = 0;
            int              startIdx    = 0;
            SpriteRenderMode lastSRM     = SpriteRenderMode.None;


            SpritePrimitive primitive   = null;
            int             visPriCount = 0;

            for (int pi = 0; pi < priCount; pi++)
            {
                primitive = _primitives[pi];
                if (lastSRM != primitive.renderMode || lastTexId != primitive.texId)
                {
                    if (lastTexId != 0)
                    {
                        SubMeshInfo info = subMeshs[subMeshCount];

                        info.indexBase   = startIdx;
                        info.triangleCnt = triangleCnt;
                        info.mat         = SpriteMaterialManager.GetMaterial(lastSRM, lastTex);

                        subMeshCount++;

                        startIdx      = (vertexIdx >> 2) * 6;
                        visTriangles += triangleCnt;
                        triangleCnt   = 0;
                    }

                    lastTexId = primitive.texId;
                    lastTex   = primitive.texture;
                    lastSRM   = primitive.renderMode;
                }

                int c = primitive.size << 2;
                for (int i = 0; i < c; i++)
                {
                    int idx = vertexIdx + i;
                    vertices[idx] = primitive.position[i];
                    UVs[idx]      = primitive.uv[i];
                    colors[idx]   = primitive.color[i];
                }

                vertexIdx   += c;
                triangleCnt += (c >> 1);
                visPriCount++;
            }

            {
                SubMeshInfo _info = subMeshs[subMeshCount];

                _info.indexBase   = startIdx;
                _info.triangleCnt = triangleCnt;
                _info.mat         = SpriteMaterialManager.GetMaterial(lastSRM, lastTex);

                visTriangles += triangleCnt;
            }

            subMeshCount++;
            return(visPriCount);
        }
        virtual protected int getPrimitive(int priCount, bool isClipping, Vector3 cameraPos, float radius)
        {
            int[] layerid = SpriteManager.GetLayersID();


            if (_primitives == null)
            {
                Init(maxPrimitiveCount);
            }

            int queueMaxLength = _primitives.Length;


            for (int layerIdx = 0, e = layerid.Length; layerIdx < e; layerIdx++)
            {
                int id = layerid[layerIdx];
                int t  = cullingMask.value & (1 << id);

                if (t != 0)
                {
                    SpriteLayer layer = SpriteManager.GetLayer(id);

                    int l = layer.size;
                    if (l == 0)
                    {
                        continue;
                    }

                    if (isClipping)
                    {
                        int ls = layer.size;
                        for (int ri = 0; ri < l; ri++)
                        {
                            layer[ri].renderable.DoClipping(cameraPos, radius);
                        }
                    }

                    for (int gourpIdx = 0; gourpIdx < l; gourpIdx++)
                    {
                        SpritePrimitiveGroup group = layer.baseContainer[gourpIdx];

                        if (!group.visible)
                        {
                            continue;
                        }

                        for (int pi = 0; pi < group.count.value; pi++)
                        {
                            SpritePrimitive pri = group.primitives[pi];
                            if (pri.visible == false || pri.texId == 0)
                            {
                                continue;
                            }

                            _primitives[priCount] = pri;
                            priCount++;

                            if (priCount >= queueMaxLength)
                            {
                                Debug.Log("Commited SpritePrimitive's count is more than " + queueMaxLength + "!.\n" +
                                          "if in editor, you should disable some layer.\n" +
                                          "if in runtime, you should enable Camera2D clipping..\n");

                                return(priCount);
                            }
                        }
                    }
                }
            }

            return(priCount);
        }
Esempio n. 8
0
        void SetupSpritePrimitiveXZ(SpritePrimitive pri, SpriteTransform sTransform, Vector2 parentPosition, float parentRotation, Vector2 parentScale, int layer, ref bool isFirst)
        {
            sTransform.primitive = pri;

            if (!sTransform.visible || (!sTransform.isSpriteValid && !sTransform.isOverride))
            {
                pri.visible = false;
                return;
            }

            Sprite spr = sTransform.isOverride ? sTransform._override : sTransform._sprite;


            layer = (layer - 0xf <= 0 ? 0 : layer) & 0xf;

            float z = (layer << 3) | sTransform.layer;

            pri.compareKey = calcSortKey((ulong)spr.texID, pri, gameObjectLayer, (uint)depth, (uint)sTransform.layer, renderMode);

            //TRS
            {
                float ry = -pri.z;

                float r   = Mathf.Deg2Rad * (sTransform.rotation);
                float sin = Mathf.Sin(r);
                float cos = Mathf.Cos(r);

                float pr   = Mathf.Deg2Rad * (parentRotation);
                float psin = Mathf.Sin(pr);
                float pcos = Mathf.Cos(pr);


                float sx  = 0;
                float sy  = 0;
                float tx  = 0;
                float ty  = 0;
                float psx = 0;
                float psy = 0;
                float px  = 0;
                float py  = 0;


                for (int i = 0, e = spr.vertices.Length; i < e; i++)
                {
                    sx = (spr.vertices[i].x + (spr.vertices[i].y * -sTransform.shear.y)) * sTransform.scale.x;
                    sy = (spr.vertices[i].y + (spr.vertices[i].x * -sTransform.shear.x)) * sTransform.scale.y;

                    tx = (sx * cos - sy * sin);
                    ty = (sx * sin + sy * cos);

                    psx = (sTransform.position.x + tx) * parentScale.x;
                    psy = (sTransform.position.y + ty) * parentScale.y;

                    tx = parentPosition.x + (psx * pcos - psy * psin);
                    ty = parentPosition.y + (psx * psin + psy * pcos);

                    pri.position[i].x = tx;
                    pri.position[i].z = ty;
                    pri.position[i].y = ry;

                    //Vector2
                    if (isFirst)
                    {
                        boundingAABB.position_minx = tx;
                        boundingAABB.position_miny = ty;
                        boundingAABB.position_maxx = tx;
                        boundingAABB.position_maxy = ty;
                        isFirst = false;
                    }
                    else
                    {
                        boundingAABB.position_minx = tx < boundingAABB.position_minx ? tx : boundingAABB.position_minx;
                        boundingAABB.position_miny = ty < boundingAABB.position_miny ? ty : boundingAABB.position_miny;
                        boundingAABB.position_maxx = tx > boundingAABB.position_maxx ? tx : boundingAABB.position_maxx;
                        boundingAABB.position_maxy = ty > boundingAABB.position_maxy ? ty : boundingAABB.position_maxy;
                    }
                }
            }

            {
                float tmpColorR = color.r * sTransform.color.r;
                float tmpColorG = color.g * sTransform.color.g;
                float tmpColorB = color.b * sTransform.color.b;
                float tmpColorA = color.a * sTransform.color.a;

                pri.color[3].a = tmpColorA * __colorLB.a;
                pri.color[3].r = tmpColorR * __colorLB.r;
                pri.color[3].g = tmpColorG * __colorLB.g;
                pri.color[3].b = tmpColorB * __colorLB.b;

                pri.color[2].a = tmpColorA * __colorRB.a;
                pri.color[2].r = tmpColorR * __colorRB.r;
                pri.color[2].g = tmpColorG * __colorRB.g;
                pri.color[2].b = tmpColorB * __colorRB.b;

                pri.color[1].a = tmpColorA * __colorRT.a;
                pri.color[1].r = tmpColorR * __colorRT.r;
                pri.color[1].g = tmpColorG * __colorRT.g;
                pri.color[1].b = tmpColorB * __colorRT.b;

                pri.color[0].a = tmpColorA * __colorLT.a;
                pri.color[0].r = tmpColorR * __colorLT.r;
                pri.color[0].g = tmpColorG * __colorLT.g;
                pri.color[0].b = tmpColorB * __colorLT.b;
            }

            pri.renderMode = _renderMode;

            //if (pri.texId != spr.texID)
            {
                pri.uv[0] = spr.uvs[0];
                pri.uv[1] = spr.uvs[1];
                pri.uv[2] = spr.uvs[2];
                pri.uv[3] = spr.uvs[3];

                pri.texture = spr.image;
                pri.texId   = spr.texID;
            }

            pri.visible = _visible;
        }