Exemple #1
0
        void CreateRenderGraph()
        {
            Dependency.Complete();
#if RENDERING_FORCE_DIRECT
            Assert.IsTrue(false, "Obsolete script define RENDERING_FORCE_DIRECT enabled. Use the RenderGraphConfig singleton to configure a render graph");
#endif
            if (!HasSingleton <RenderGraphConfig>())
            {
                Entity eConfig = GetSingletonEntity <DisplayInfo>();
                EntityManager.AddComponentData <RenderGraphConfig>(eConfig, RenderGraphConfig.Default);
            }

            RenderGraphConfig config = GetSingleton <RenderGraphConfig>();
            if (!config.Equals(currentConfig) || eMainViewNode == Entity.Null)
            {
                RenderDebug.LogAlways("RenderGraphConfig changed, building a new render graph!");
                DestroyRenderGraph();
                // we should run the bgfx system here once, so textures are cleaned up and ready for re-use
                currentConfig = config;
                // we only build a default graph if there are no existing nodes - otherwise assume they are already built
                eMainViewNode = BuildRenderGraph();
                CreateScreenToWorldChain(RenderPassType.Opaque, ScreenToWorldId.MainCamera);
                CreateScreenToWorldChain(RenderPassType.Sprites, ScreenToWorldId.Sprites);
                CreateScreenToWorldChain(RenderPassType.UI, ScreenToWorldId.UILayer);
            }

            // build light nodes for lights that have no node associated - need to do this always
            BuildAllLightNodes(eMainViewNode);
        }
Exemple #2
0
        protected unsafe override void OnUpdate()
        {
#if !UNITY_EDITOR
            var env              = World.TinyEnvironment();
            var input            = World.GetExistingSystem <InputSystem>();
            var renderer         = World.GetExistingSystem <RendererBGFXSystem>();
            var rendererInstance = renderer.InstancePointer();

            bool anyShift = input.GetKey(KeyCode.LeftShift) || input.GetKey(KeyCode.RightShift);
            bool anyCtrl  = input.GetKey(KeyCode.LeftControl) || input.GetKey(KeyCode.RightControl);
            bool anyAlt   = input.GetKey(KeyCode.LeftAlt) || input.GetKey(KeyCode.RightAlt);

            if (!m_configAlwaysRun && !anyShift)
            {
                return;
            }

            // debug bgfx stuff
            if (input.GetKey(KeyCode.F2) || (input.GetKey(KeyCode.Alpha2) && anyAlt))
            {
                rendererInstance->SetFlagThisFrame(bgfx.DebugFlags.Stats);
            }

            if (input.GetKeyDown(KeyCode.F3) || (input.GetKeyDown(KeyCode.Alpha3) && anyAlt))
            {
                var di = env.GetConfigData <DisplayInfo>();
                if (di.colorSpace == ColorSpace.Gamma)
                {
                    di.colorSpace = ColorSpace.Linear;
                }
                else
                {
                    di.colorSpace = ColorSpace.Gamma;
                }
                env.SetConfigData(di);
                renderer.DestroyAllTextures();
                renderer.ReloadAllImages();
                Debug.LogFormatAlways("Color space is now {0}.", di.colorSpace == ColorSpace.Gamma ? "Gamma (no srgb sampling)" : "Linear");
            }

            if (input.GetKeyDown(KeyCode.F4) || (input.GetKeyDown(KeyCode.Alpha4) && anyAlt))
            {
                var di = env.GetConfigData <DisplayInfo>();
                di.disableVSync = !di.disableVSync;
                env.SetConfigData(di);
                Debug.LogFormatAlways("VSync is now {0}.", di.disableVSync ? "disabled" : "enabled");
            }

            if (input.GetKeyDown(KeyCode.F5) || (input.GetKeyDown(KeyCode.Alpha5) && anyAlt))
            {
                RenderGraphConfig cfg = GetNextConfig();
                SetSingleton(cfg);
                Debug.LogFormatAlways("Target config is now {0}*{1} {2}.", cfg.RenderBufferHeight, cfg.RenderBufferWidth, cfg.Mode == RenderGraphMode.DirectToFrontBuffer?"direct":"buffer");
            }

            rendererInstance->m_outputDebugSelect = new float4(0, 0, 0, 0);
            if (input.GetKey(KeyCode.Alpha1))
            {
                rendererInstance->m_outputDebugSelect = new float4(1, 0, 0, 0);
            }
            if (input.GetKey(KeyCode.Alpha2))
            {
                rendererInstance->m_outputDebugSelect = new float4(0, 1, 0, 0);
            }
            if (input.GetKey(KeyCode.Alpha3))
            {
                rendererInstance->m_outputDebugSelect = new float4(0, 0, 1, 0);
            }
            if (input.GetKey(KeyCode.Alpha4))
            {
                rendererInstance->m_outputDebugSelect = new float4(0, 0, 0, 1);
            }
            if (input.GetKeyDown(KeyCode.Z))
            {
                string fn = StringFormatter.Format("screenshot{0}.tga", m_nshots++);
                renderer.RequestScreenShot(fn);
            }
            if (input.GetKeyDown(KeyCode.Escape))
            {
                Debug.LogFormatAlways("Reloading all textures.");
                // free all textures - this releases the bgfx textures and invalidates all cached bgfx state that might contain texture handles
                // note that this does not free system textures like single pixel white, default spotlight etc.
                renderer.DestroyAllTextures();
                // now force a reload on all image2d's from files
                renderer.ReloadAllImages();
                // once images are loaded, but don't have a texture, the texture will be uploaded and the cpu memory freed
            }
            if (renderer.HasScreenShot())
            {
                // TODO: save out 32bpp pixel data:
                Debug.LogFormat("Write screen shot to disk: {0}, {1}*{2}",
                                renderer.m_screenShotPath, renderer.m_screenShotWidth, renderer.m_screenShotHeight);
                renderer.ResetScreenShot();
            }

            // camera related stuff
            var ecam = FindCamera();
            if (ecam == Entity.Null)
            {
                return;
            }

            // sync scene camera with game camera position
            if (input.GetKeyDown(KeyCode.F))
            {
                SyncSceneViewToCamera(ref ecam);
            }

            ControlCamera(ecam);

            Entities.WithoutBurst().WithAll <Light>().ForEach(
                (Entity eLight, ref LightFromCameraByKey lk, ref Translation tPos, ref Rotation tRot) =>
            {
                if (input.GetKeyDown(lk.key))
                {
                    tPos = EntityManager.GetComponentData <Translation>(ecam);
                    tRot = EntityManager.GetComponentData <Rotation>(ecam);
                    Debug.LogFormat("Set light {0} to {1} {2}", eLight, tPos.Value, tRot.Value.value);
                }
            }).Run();
#endif
        }