Either a cool effect or good performance. How do I eat cake and have cake?
I'm doing pixelated 3D using this tutorial: http://www.gamasutra.com/blogs/AhmetKamilKeles/20160421/271027/Creating_perfectly_pixelated_games_with_Unity_2D_or_3D.php
Unfortunately in part of my game clicking on objects is required (which is implemented using OnMouseUpAsButton()).
So if I render on game camera everything to render texture and on "actual" camera (that is child of game camera and has as a child quad rendertexture is displayed on so cameras are aligned perfectly) I only render quad texture is displayed on, using Culling Mask, everything is shown just fine (as game camera's culling mask is set to everything but the layer display quad is on).
But then I can't click things. Alignment is not an issue (as cameras are always aligned perfectly), neither is display quad blocking casts (as it is on IgnoreRaycast layer).
There's solution to this: Enable rendering everything on both cameras. Now I can click everything, but because game has effectively to render level two times, one of which is unnecessary (as it is quickly covered by display quad), performance suffers greatly as a result.
How can I do it so I can get good performance AND be able to click things?
I'm not quite sure why you need the second camera or the quad. Can't this simply be applied as an image effect and blitted to the screen of the main camera?
Unfortunately no, as I need GUI to be unpixelated or either it won't be playable. Render texture+separate cam is easiest solution to do pixelation, just read gamasutra tutorial I've linked.
So put the GUI on a separate camera then if you don't want that pixelated - that's got to be a much more efficient solution than rendering the whole scene twice...
Hi, I'm the owner of the article.
I don't know an easy workaround, unfortunately. If I were you, I'd try writing my own system ins$$anonymous$$d of On$$anonymous$$ouseUpAsButton event. I'd modify the value of a boolean with mouse button getting pressed down and up. If mouse is up, I'd raycast according to the player camera's position and the ray corresponds to the mouse position (see Camera.ScreenPointToRay).
Answer by Gord10 · Apr 24, 2016 at 06:13 PM
Hi, I am the writer of the article. Hmm, I hadn't thought of it before.
I don't know an easy solution, unfortunately, but if I were you I would write my own code instead of OnMouseUpAsButton() event. I'd check whether the mouse is up and then raycast on the player camera by myself.
Hm, seems like it is a step in the right direction, but still not enough. You see, player can unlock mouse from the screen so simple cast from camera's position (i.e. center of the screen) won't do.
For example, there are two buttons. Button A is in the center of the screen, while button B is a little to the left. Player unlock mouse to not loose view of nearby enemy patrolling the area while clicking button B (which is correct solution to the puzzle). Ins$$anonymous$$d cast is done from the camera's center and game thinks user clicked button A which initiates trap and the player dies.
Another thing On$$anonymous$$ouseUpAsButton does is checking whether mouse started pressed on the object in question. This way, when you click, say, skybox, and then drag mouse cursor over the object and then let go of a button, the game doesn't accidentally fire up code related to mouse click for that object.
I'm sure there's a way to solve both of those issues, but I don't know it.
If you use camera.ScreenPointToRay(Input.mousePosition), I think you get the ray that the mouse is on. If player clicks B, B's ray is created.
For the second problem, you can do another raycast at the moment mouse is down. You store the object clicked on. When mouse is up, you do another raycast and then compare the two objects. (Also don't forget to check if the objects are null, in which no clicking should be executed)
Your answer
Follow this Question
Related Questions
UI performance. FPS drops. Overdraw. 0 Answers
Raycast can not detect collider 1 Answer
Ray-casting Problem 0 Answers
Raycast positioning problem 1 Answer
RayCast Rotation 1 Answer