2019年12月4日 星期三

(完) PhysicsRaycaster + RenderTexture camera

繼續前面的筆記

https://riveragamer.blogspot.com/2019/12/unity-render-to-rendertexture.html
https://riveragamer.blogspot.com/2019/12/render-to-rendertexture.html

在網路社群上發問, 經別人提點後
想起一開始需求其實是跟分割畫面比較有關係
所以根本不用搞這麼複雜
就算是在 LWRP 流程中多個 camera viewport 不重疊就可以順利進行分割畫面繪圖

但所謂頭都洗下去了
forum 文章, source codes 也爬了(一些些 ....
就改寫了一個自己的 RTPhysicsRaycaster class (繼承 PhysicsRaycaster)
原理其實很簡單
只是將計算 raycast 用途的 ray 從 Camera.ScreenPointToRay() 改採用 Camera.ViewportPointToRay() 而已
https://github.com/riveranb/UnityRTEvents/blob/master/Assets/RTPhysicsRaycaster.cs

目前這樣改之後, 所有 EventSystem發出的事件在 off-screen rendering情況下都可以正確接收/處理了

2019年12月2日 星期一

(續) render to RenderTexture 下的事件

上一篇筆記
為了嘗試在同時使用到多個 Cameras情境下
render to RenderTexture時讓 game object正確接收/處理 mouse events
因此爬了幾個文章

https://forum.unity.com/threads/interaction-with-objects-displayed-on-render-texture.517175/
(更完整個重新實現自己的 event input module)
https://forum.unity.com/threads/handling-event-input-offscreen.378470/

我試著參考這些資料試驗
但是因為自己尚未深入去爬 UnityEngine.UI source codes
所以對於部分 coding 還是無法理解

偶然才發現到其實 Unity 提供的 ***Raycaster 就是在處理這方面的事情
https://docs.unity3d.com/2019.2/Documentation/Manual/script-GraphicRaycaster.html
https://docs.unity3d.com/2019.2/Documentation/Manual/script-PhysicsRaycaster.html
只是之前從官方文件上看不夠仔細沒有想到而已

因此嘗試在 RenderTexture camera 中裝上 PhysicsRaycaster component
然後呢....
場景中 3D game object 就可以接收到 EventTrigger component 支援的所有事件了!
雖然我還是沒有正確接收到所有 mouse events
(OnMouseEnter, OnMouseExit, OnMouseDown, OnMouseOver ....)
但已可以實現很完整的一系列事件了
(OnPointerEnter, OnPointerExit, OnMove, OnDrag, .... 等)

可是事情總是沒有憨人想的這麼簡單 =  =
EventSystems 發出的 raycasted events 僅在 RenderTexture resolution = real screen resulotion的情況下正確運作
如果 RenderTexture大小與 screen大小不符合
接收到事件的 pointer (mouse) position 就會出錯
這.....不知道算是誰的鍋了

難怪 forum 裡面建議的還是自己延伸改寫 GraphicRaycaster / PhysicsRaycaster ...

2019年12月1日 星期日

Unity render to RenderTexture 下的事件

摸 Unity 至少也 2 年多了
不能說自己不熟
但還是有很多基本的運作方式有點摸不清
感覺自己很嫩

這次因為需求將場景繪製到指定的 RenderTexture
一切那麼自然的情況下
才發現因為使用到多個 Camera
所以繪製 RenderTexture的時候我無法正確抓到 mouse events了

因此開了一個小測試專案
場景中的 Cube會列印 OnMouseOver() 事件.

在 TestMain.unity 場景, 因為僅單純使用一個 MainCamera
因此即使繪製過程指定至 RenderTexture
仍可以正確接收並處理 OnMouseOver() 事件

TestCamera2.unity 場景中, 使用 2 個 Camera
SceneCamera將場景繪製到指定 RenderTexture
ScreenCamera僅將 RenderTexture畫到全螢幕做顯示用
但在這個情境下我就無法讓 Cube接收到 OnMouseOver() 事件了
目前無論我怎麼測試 Camera的設置調整都無法接收事件.

(1)
SceneCamera
tag='MainCamera'
Depth=-1
ScreenCamera tag='Untagged'
Depth=-10 or 10
(2)
SceneCamera
tag='MainCamera'
Depth=-1
tag='MainCamera'
Depth=-10 or 10
(3) SceneCamera透過 MainCamControl.cs控制, Update()之前先 Disable其他 cameras

目前的結論大概是
場景中同時存在多個 Cameras之下
render to RenderTexture時就無法順利接收/處理 mouse event.
不確定是不是自己有哪部分搞錯了
只能繼續爬文查資料