private void Awake()
 {
     toMute     = GetComponent <AudioSource>();
     bounds     = Camera.main.GetComponent <RenderCullingBounds>();
     _transform = transform;
 }
Esempio n. 2
0
    protected sealed override JobHandle OnUpdate(JobHandle inputDeps)
    {
        if (camera == null)
        {
            camera = Camera.main;
            renderCullingBounds = camera.GetComponent <RenderCullingBounds>();
        }

        var maxX = 0f;
        var minX = 0f;
        var maxY = 0f;
        var minY = 0f;

        if (renderCullingBounds != null)
        {
            var bounds = renderCullingBounds.CullingBounds;

            maxX = bounds.xMax;
            minX = bounds.xMin;
            maxY = bounds.yMax;
            minY = bounds.yMin;
        }
        else
        {
            var cameraPosition = camera.transform.position;
            var camHeight      = camera.orthographicSize;
            var camWidth       = camHeight * camera.aspect;

            var minMaxExpand = 1;//чтобы спрайты, которые входят в сцену из за экрана, появлялиль не внезапно

            maxX = cameraPosition.x + camWidth + minMaxExpand;
            minX = cameraPosition.x - camWidth - minMaxExpand;
            maxY = cameraPosition.y + camHeight + minMaxExpand;
            minY = cameraPosition.y - camHeight - minMaxExpand;
        }

        //Debug.DrawLine(new Vector2(maxX, maxY), new Vector2(minX, minY));
        //Debug.DrawLine(new Vector2(maxX, minY), new Vector2(minX, maxY));

        var queryDesc       = UseAsDefault ? defaultQueryDesc : specialQueryDesc;
        var shadowQueryDesc = UseAsDefault ? defaultWithShadowsQueryDesc : specialWithShadowsQueryDesc;

        var query       = GetEntityQuery(queryDesc);
        var shadowQuery = GetEntityQuery(shadowQueryDesc);

        var entitiesCount           = query.CalculateEntityCount();
        var entitiesWithShadowCount = shadowQuery.CalculateEntityCount();
        //для каждого энтити с тенью надо будет отрисовать и тень,
        //так что для них надо в 2 раза больше памяти выделить
        //и всего выйдет (entitiesCount - entitiesWithShadowCount) + entitiesWithShadowCount * 2.
        var maxCount = Mathf.Max(entitiesCount, entitiesCount + entitiesWithShadowCount);

        chunkDataMap.Dispose();
        chunkDataMap = new NativeMultiHashMap <int, RenderData>(maxCount, Allocator.TempJob);
        var chunkJob = new SortByChunkIdAndCalcMatrixJobAndCull()
        {
            maxX                  = maxX,
            minX                  = minX,
            maxY                  = maxY,
            minY                  = minY,
            chunkDataMap          = chunkDataMap.AsParallelWriter(),
            spriteType            = GetArchetypeChunkComponentType <SpriteRendererComponentData>(true),
            translationType       = GetArchetypeChunkComponentType <Translation>(true),
            renderScaleType       = GetArchetypeChunkComponentType <RenderScaleComponentdata>(true),
            scaleType             = GetArchetypeChunkComponentType <Scale>(true),
            renderDataType        = GetArchetypeChunkSharedComponentType <RenderSharedComponentData>(),
            rotationType          = GetArchetypeChunkComponentType <Rotation>(true),
            shadowType            = GetArchetypeChunkComponentType <CastSpritesShadowComponentData>(true),
            spriteTintType        = GetArchetypeChunkComponentType <SpriteTintComponentData>(true),
            disableCullingTagType = GetArchetypeChunkComponentType <DisableRenderCullingTagComponentData>(true),
            spriteCracksType      = GetArchetypeChunkComponentType <SpriteCracksComponentData>(true)
        };

        jobHandle = chunkJob.Schedule(query, inputDeps);
        return(jobHandle);
    }