Example #1
0
        protected void Awake()
        {
            // setup static fields
            s_Instance = this;
            OpenTibiaUnity.GraphicsVendor  = SystemInfo.graphicsDeviceVendor;
            OpenTibiaUnity.GraphicsDevice  = SystemInfo.graphicsDeviceName;
            OpenTibiaUnity.GraphicsVersion = SystemInfo.graphicsDeviceVersion;
            OpenTibiaUnity.MainThread      = Thread.CurrentThread;

            WindowPtr = FindWindow(null, "OpenTibiaUnity");

            Features = new System.Collections.BitArray(new bool[(int)GameFeatures.LastGameFeature]);

            // setup threading mutex & lockers
            m_ActionQueue = new Queue <UnityAction>();

            // quit-notification related
            m_ExitWindow           = null;
            OpenTibiaUnity.Quiting = false;

            // events
            OnSecondaryTimeCheck = new ElapsedEvent();
            OnTacticsChangeEvent = new TacticsChangeEvent();

            // setup ingame facilities
            OptionStorage     = new Options.OptionStorage();
            InputHandler      = new InputManagment.InputHandler();
            AppearanceStorage = new Appearances.AppearanceStorage();
            CreatureStorage   = new Creatures.CreatureStorage();
            MiniMapStorage    = new MiniMap.MiniMapStorage();
            MiniMapRenderer   = new MiniMap.Rendering.MiniMapRenderer();
            WorldMapStorage   = new WorldMap.WorldMapStorage(
                LabelMessageBoxBottom,
                LabelMessageBoxLow,
                LabelMessageBoxHigh,
                LabelMessageBoxTop,
                LabelOnscreenMessageBoxPrefab);
            WorldMapRenderer = new WorldMap.Rendering.WorldMapRenderer();
            ChatStorage      = new Chat.ChatStorage(OptionStorage);
            MessageStorage   = new Chat.MessageStorage();
            ContainerStorage = new Container.ContainerStorage();

            // Load options
            OptionStorage.LoadOptions();

            // update input settings
            InputHandler.UpdateMapping();

            // initialize rendering textures
            WorldMapRenderingTexture.Initialize();
            WorldMapRenderingTexture.IncrementUpdateCount();

            // initializing events
            onClientVersionChange   = new VersionChangeEvent();
            onProtocolVersionChange = new VersionChangeEvent();
        }
Example #2
0
    // Update one custom render texture.
    public static void UpdateCustomRenderTexture(CommandBuffer cmd, CustomRenderTexture crt)
    {
        bool firstPass = crt.updateCount == 0;

        // Handle initialization here too:
        if (crt.initializationMode == CustomRenderTextureUpdateMode.Realtime || needsInitialization.Contains(crt) || (firstPass && crt.initializationMode == CustomRenderTextureUpdateMode.OnLoad))
        {
            switch (crt.initializationSource)
            {
            case CustomRenderTextureInitializationSource.Material:
                // TODO
                break;

            case CustomRenderTextureInitializationSource.TextureAndColor:
                // TODO
                break;
            }
            needsInitialization.Remove(crt);
        }

        needsUpdate.TryGetValue(crt, out int updateCount);

        if (crt.material != null && (crt.updateMode == CustomRenderTextureUpdateMode.Realtime || updateCount > 0 || (firstPass && crt.updateMode == CustomRenderTextureUpdateMode.OnLoad)))
        {
            onBeforeCustomTextureUpdated?.Invoke(cmd, crt);

#if CUSTOM_TEXTURE_PROFILING
            customRenderTextureSamplers.TryGetValue(crt, out var sampler);
            if (sampler == null)
            {
                sampler = customRenderTextureSamplers[crt] = CustomSampler.Create($"{crt.name} - {crt.GetInstanceID()}", true);
                sampler.GetRecorder().enabled = true;
            }
            cmd.BeginSample(sampler);
#endif

            // using (new ProfilingScope(cmd, new ProfilingSampler($"Update {crt.name}")))
            {
                updateCount = Mathf.Max(updateCount, 1);
                crtExecInfo.TryGetValue(crt, out var execInfo);
                if (execInfo != null && execInfo.runOnAllMips)
                {
                    for (int mipLevel = 0; mipLevel < crt.mipmapCount; mipLevel++)
                    {
                        UpdateCustomRenderTexture(cmd, crt, updateCount, mipLevel: mipLevel);
                    }
                }
                else
                {
                    UpdateCustomRenderTexture(cmd, crt, updateCount);
                }

                needsUpdate.Remove(crt);
            }

#if CUSTOM_TEXTURE_PROFILING
            cmd.EndSample(sampler);
#endif
            crt.IncrementUpdateCount();
        }
    }
    // Update one custom render texture.
    public static void UpdateCustomRenderTexture(CommandBuffer cmd, CustomRenderTexture crt)
    {
        bool firstPass = crt.updateCount == 0;

        // Handle initialization here too:
        if (crt.initializationMode == CustomRenderTextureUpdateMode.Realtime || needsInitialization.Contains(crt) || (firstPass && crt.initializationMode == CustomRenderTextureUpdateMode.OnLoad))
        {
            switch (crt.initializationSource)
            {
            case CustomRenderTextureInitializationSource.Material:
                // TODO
                break;

            case CustomRenderTextureInitializationSource.TextureAndColor:
                // TODO
                break;
            }
            needsInitialization.Remove(crt);
        }

        needsUpdate.TryGetValue(crt, out int updateCount);

        if (crt.material != null && (crt.updateMode == CustomRenderTextureUpdateMode.Realtime || updateCount > 0 || (firstPass && crt.updateMode == CustomRenderTextureUpdateMode.OnLoad)))
        {
            onBeforeCustomTextureUpdated?.Invoke(cmd, crt);

#if CUSTOM_TEXTURE_PROFILING
            customRenderTextureSamplers.TryGetValue(crt, out var sampler);
            if (sampler == null)
            {
                sampler = customRenderTextureSamplers[crt] = CustomSampler.Create($"{crt.name} - {crt.GetInstanceID()}", true);
                sampler.GetRecorder().enabled = true;
            }
            cmd.BeginSample(sampler);
#endif

            using (new ProfilingScope(cmd, new ProfilingSampler($"Update {crt.name}")))
            {
                // Prepare "self" texture for reading in the shader for double buffered custom textures
                RenderTexture textureSelf2D   = null;
                RenderTexture textureSelf3D   = null;
                RenderTexture textureSelfCube = null;
                if (crt.doubleBuffered)
                {
                    if (crt.dimension == TextureDimension.Tex2D)
                    {
                        textureSelf2D = crt;
                    }
                    if (crt.dimension == TextureDimension.Cube)
                    {
                        textureSelfCube = crt;
                    }
                    if (crt.dimension == TextureDimension.Tex3D)
                    {
                        textureSelf3D = crt;
                    }
                }

                if (crt.doubleBuffered)
                {
                    // Update the internal double buffered render texture (resize / alloc / ect.)
                    crt.EnsureDoubleBufferConsistency();
                }

                MaterialPropertyBlock block = new MaterialPropertyBlock();

                // If the user didn't called the update on CRT, we still process it because it's realtime
                updateCount = Mathf.Max(updateCount, 1);
                for (int i = 0; i < updateCount; i++)
                {
                    // TODO: cache everything
                    List <CustomRenderTextureUpdateZone> updateZones = new List <CustomRenderTextureUpdateZone>();
                    crt.GetUpdateZones(updateZones);

                    if (updateZones.Count == 0)
                    {
                        updateZones.Add(new CustomRenderTextureUpdateZone {
                            needSwap = false, updateZoneCenter = new Vector3(0.5f, 0.5f, 0.5f), updateZoneSize = Vector3.one, rotation = 0, passIndex = 0
                        });
                    }

                    foreach (var zone in updateZones)
                    // int sliceCount = GetSliceCount(crt);
                    // for (int slice = 0; slice < sliceCount; slice++)
                    {
                        var zoneCenters          = updateZones.Select(z => new Vector4(z.updateZoneCenter.x, z.updateZoneCenter.y, z.updateZoneCenter.z, 0)).ToList();
                        var zoneSizesAndRotation = updateZones.Select(z => new Vector4(z.updateZoneSize.x, z.updateZoneSize.y, z.updateZoneSize.z, z.rotation)).ToList();
                        var zonePrimitiveIDs     = Enumerable.Range(0, updateZones.Count).Select(j => (float)j).ToList();// updateZones.Select(z => 0.0f).ToList();
                        int sliceCount           = GetSliceCount(crt);

                        // Copy all the slices in case the texture is double buffered
                        if (zone.needSwap)
                        {
                            var doubleBuffer = crt.GetDoubleBufferRenderTexture();
                            if (doubleBuffer != null)
                            {
                                // For now, it's just a copy, once we actually do the swap of pointer, be careful to reset the Active Render Texture
                                for (int slice = 0; slice < sliceCount; slice++)
                                {
                                    cmd.CopyTexture(doubleBuffer, slice, crt, slice);
                                }
                            }
                        }

                        // foreach (var zone in updateZones)
                        for (int slice = 0; slice < sliceCount; slice++)
                        {
                            RenderTexture renderTexture = crt.doubleBuffered ? crt.GetDoubleBufferRenderTexture() : crt;
                            cmd.SetRenderTarget(renderTexture, 0, (crt.dimension == TextureDimension.Cube) ? (CubemapFace)slice : 0, (crt.dimension == TextureDimension.Tex3D) ? slice : 0);
                            cmd.SetViewport(new Rect(0, 0, crt.width, crt.height));
                            block.SetVector(kCustomRenderTextureInfo, GetTextureInfos(crt, slice));
                            block.SetVector(kCustomRenderTextureParameters, GetTextureParameters(crt, slice));
                            if (textureSelf2D != null)
                            {
                                block.SetTexture(kSelf2D, textureSelf2D);
                            }
                            if (textureSelf3D != null)
                            {
                                block.SetTexture(kSelf3D, textureSelf3D);
                            }
                            if (textureSelfCube != null)
                            {
                                block.SetTexture(kSelfCube, textureSelfCube);
                            }

                            int passIndex = zone.passIndex == -1 ? 0 : zone.passIndex;

                            block.SetVectorArray(kUpdateDataCenters, zoneCenters);
                            block.SetVectorArray(kUpdateDataSizesAndRotation, zoneSizesAndRotation);
                            block.SetFloatArray(kUpdateDataPrimitiveIDs, zonePrimitiveIDs);

                            cmd.DrawProcedural(Matrix4x4.identity, crt.material, passIndex, MeshTopology.Triangles, 6 * updateZones.Count, 1, block);
                        }
                    }
                }

                needsUpdate.Remove(crt);
            }

#if CUSTOM_TEXTURE_PROFILING
            cmd.EndSample(sampler);
#endif
            crt.IncrementUpdateCount();
        }
    }