Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class WasdBlitRendererFeature : ScriptableRendererFeature {
- public class WasdBlitPass : ScriptableRenderPass {
- private ProfilingSampler _profilingSampler = new ProfilingSampler("WasdBlitPass");
- private readonly Material _mat;
- private GameRenderWindow _renderWindow;
- private readonly bool _debug;
- public WasdBlitPass(GameRenderWindow gameRenderWindow, Shader shader, bool debug) {
- _renderWindow = gameRenderWindow;
- renderPassEvent = RenderPassEvent.AfterRenderingPostProcessing;
- _mat = new Material(shader);
- _debug = debug;
- }
- public override void Execute(ScriptableRenderContext context, ref RenderingData renderingData) {
- if (!_renderWindow || !_renderWindow.Rt) return;
- var camera = renderingData.cameraData.camera;
- if (camera.cameraType != CameraType.Game) return;
- if (!camera.name.Equals(_renderWindow.tgtCameraName)) return;
- CommandBuffer cmd = CommandBufferPool.Get();
- using (new ProfilingScope(cmd, _profilingSampler)) {
- float scaledCamWidth = camera.scaledPixelWidth;
- float scaledCamHeight = camera.scaledPixelHeight;
- float camAspectWh = scaledCamWidth / scaledCamHeight;
- float sourceAspectWh = (float)_renderWindow.Rt.width / (float) _renderWindow.Rt.height;
- float sourceAspectHw = (float)_renderWindow.Rt.height / (float)_renderWindow.Rt.width;
- int maxIntScale = Mathf.FloorToInt(scaledCamHeight / _renderWindow.Rt.height);
- float windowScale = 1; float uvHeight = 1;
- if (maxIntScale > 0) {
- windowScale = scaledCamHeight/_renderWindow.Rt.height;
- uvHeight = windowScale/maxIntScale;
- }
- float yMargin = (1 - uvHeight) / 2;
- //Debug.Log(maxIntScale + ", " + windowScale + ", " + uvHeight);
- float uvHeightAsWidth = uvHeight * camAspectWh;
- float uvWidth = uvHeightAsWidth / sourceAspectWh;
- float xMargin = (1 - uvWidth) / 2;
- Vector4 st = new Vector4(uvWidth, uvHeight, xMargin, yMargin);
- if (_debug && Application.isEditor) {
- Debug.Log("VirtualWindow: " + _renderWindow.Rt.width + " x / " + _renderWindow.Rt.height + " y\n"
- + "ActualWindow: " + scaledCamWidth + " x / " + scaledCamHeight + " y\n"
- + st);
- }
- _mat.SetTexture("_MainTex", _renderWindow.Rt);
- _mat.SetVector("_MainTex_ST", st);
- cmd.DrawMesh(RenderingUtils.fullscreenMesh, Matrix4x4.identity, _mat);
- }
- context.ExecuteCommandBuffer(cmd);
- cmd.Clear();
- CommandBufferPool.Release(cmd);
- }
- }
- public GameRenderWindow renderWindow;
- public Shader shader;
- public bool debug;
- private WasdBlitPass _renderPass;
- public override void AddRenderPasses(ScriptableRenderer renderer, ref RenderingData renderingData) {
- if (renderingData.cameraData.cameraType == CameraType.Game) {
- // Calling ConfigureInput with the ScriptableRenderPassInput.Color argument
- // ensures that the opaque texture is available to the Render Pass.
- _renderPass.ConfigureInput(ScriptableRenderPassInput.Color);
- renderer.EnqueuePass(_renderPass);
- }
- }
- public override void Create() {
- _renderPass = new WasdBlitPass(renderWindow, shader, debug);
- }
- protected override void Dispose(bool disposing) { }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement