- Home /
Render Textures and Raycasting
Hi guys,
I have a few questions regarding Render Textures and Ray Casting which are causing some problems within my game.
1 - Firstly, is there any way to hot-swap out the texture to which a camera is rendering? Currently my game has pretty low FPS on mobile devices (sometimes getting sub-20 FPS) which is obviously not good for the game. I am trying to stop the game from updating the RenderTexture until something has actually changed if this isn't already done. My game does not have a lot of objects within the scene at present, and the profiler is saying that Camera.render for RenderForwardOpaque is taking up about 80-90% of the CPU.
2 - My second question is to do with Ray casting, Render Textures and scrolling UVs. I am currently ray casting at the object to which my RenderTexture is being applied. Using RaycastHit.textureCoord, I am then performing another ray cast from my RenderTexture camera using the textureCoord and ViewportPointToRay so that I can get the corresponding object to where the "touch" event happened.
This is all working fine until I get towards the edges of my objects/texture area. The problem is arising because I am scrolling the UVs and incrementing a uvOffset value. This incrementing of the offset value is causing my ray cast direction to move as the texture scrolls, but when the texture scrolls past a certain point the ray cast is no longer hitting an object and thus returning null which is causing problems for the gameplay. Basically, what I need is a way in which I can "fix" the offset programmatically so that my ray casts are no longer missing my objects.
I am sorry if this is a bit rambly. I just typed all of this out initially and then my Macbook decided to drop from the internet as I hit submit, causing me to lose it all :(
If you need any more explanation as to what the problems are, please ask and I will try to create some (badly drawn) diagrams for you.
Any help you can give regarding this would be greatly appreciated!
Thanks, Sospitas
Turns out I fixed one of my own problems. The uv scrolling was as simple as just setting it back to 0.5f if it went below -0.5f. It was showing a visible "jump" when I tried it earlier, so I must have been overcomplicating my code somehow.
I still have the performance issue though, so any help with that will still be appreciated! :)
Try to isolate what the render texture camera needs to see vs. the main camera. Do this by turning off some layers for the render camera and also reduce its field of view.
I have positioned the camera so that all that it can see is the objects that it is rendering to a texture (The edges of the camera preview are lined up with the exact edges of the objects. I used an editor extension to maximize the camera preview to allow me to line this up better).
There is nothing else in the scene apart from the objects that the RenderTexture is aligned with, the sphere on which the RenderTexture is applied, a point light, a directional light and the 2 cameras.
Any chance you can batch some more draw calls? Atlased textures and static batching can save a lot of performance trouble on mobile. Culling may help, too, but it sounds like your frustum cull should handle most of that.
I haven't tried it, but what happens if you disable the camera that's generating the RenderTexture? You might try enabling it only when you need the texture updated.
Okay, I just tried to disable the render texture camera, and it left the texture visible on the sphere. I'm pretty sure I've tried that before and the sphere went black, maybe I did something else?
Anyways, thank you :) I'll give this some testing, but I'm not sure how much of a performance boost I'll get. The texture needs to scroll a considerable amount during the gameplay
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
Multiple Cars not working 1 Answer
Mouse Aim for a 2.5D game 1 Answer
modify textures at runtime? 3 Answers
An OS design issue: File types associated with their appropriate programs 1 Answer