Hello,community, as always,I’m new in programming and I have question. So, i’m making spmeth like FPS ,and i need weapons with optical scope,but i don’t want to just do the GUI and zoom,I want something more. My question is - how to do that when you r look through optical scope,that part,wich have lenses (inside scope) 'll zoom,but all around it will be as normal (I included image how I want it to be)
Thanks all for help)
And sorry for my bad english.
One solution that comes to mind is to have a second camera setup so that it sees what you want to see in scope and then project that on scope surface
EDIT:
put a camera in front of the scope and set its render texture as metial of the lens
You’d need render to texture as Pirs01 said, but this is a pro-only feature.
use a second camera as you would with a minimap, use a GUI texture over this camera and use this as the scope.
This image shows a second camera with a GUI texture overlay, just use the same sort of thing.
For the free version, i guess you could put a texture over a camera for the scope and still have your normal camera.
Then have a small animation to move into place, and disable the main camera and enable the scope camera.
It’s doable with the free version because it doesn’t use render textures, but your scope’s lens won’t show anything through it when you move out of scope.
So just use the old way of just using a cubemap for the lens for environment reflections.
there are ways to do it without Render Textures but it is very resources heavy. I have done some research just because I was curious myself.
Here is, what I came up with.
*for some resons I cant get a webplayer up and runng … so screenshots, the script and a unitypackage
using UnityEngine;
using System.Collections;
public class Test : MonoBehaviour
{
public Camera gunCam = null;
public Camera otherCam = null;
private Texture2D _tex = null;
private Material _mat = null;
private int _size = 512;
private WaitForEndOfFrame _wait = new WaitForEndOfFrame();
private void Start()
{
_tex = new Texture2D( _size, _size, TextureFormat.RGB24, false, true );
_mat = renderer.sharedMaterial;
StartCoroutine( Cam() );
}
private IEnumerator Cam()
{
gunCam.enabled = false;
otherCam.enabled = false;
while( true )
{
yield return _wait;
gunCam.Render();
_tex.ReadPixels(new Rect( Screen.width * 0.5f - _size * 0.5f, Screen.height * 0.5f - _size * 0.5f, _size, _size ),
0, 0, false );
_tex.Apply();
_mat.SetTexture( "_MainTex", _tex );
otherCam.Render();
}
}
}
I’m sure there are ways to optimise it and it is also very quickly done, so errors might occur. Hope it helps a bit
EDIT: Removed Link. See 3 posts below
Woh nice one.
I have done some more work on it. Greatly Improved performance from ~75 FPS to ~350 FPS on my mashine. Still webplayer isnt working but it seems to be a problem on my end. I’m currently downloading the newest Unity version, maybe this fixes my webplayer problem. Will update you guys later
here we go
webplayer: Link *there seems to be a problem with Chrome. if it doesnt work use another browser
Windows: Link
Mac: Link
UnityPackage: Link
would like to know what kind of performance you guys are getting
Damn,man,it is AWESOME! You are the God of sights
I have 60 FPS in web player, but on windows directly it’s 500-600 when look on “sky”,100-350 when look on ground.
I get around 150 fps on the webplayer when it’s in fullscreen mode.
Very nice. Is it possible to modify this into a real time mirror effect? I’d buy the package if both of them are possible.
I don’t mean for a car to reflect everything, i mean like a mirror you’d have in the bathroom.
theoretically it should work. you can try it out. Create a 3d model that acts as your mirror, a plane should do it and it needs to be UV unwrapped. Create a new 2nd camera and place it infront of your mirror facing away.
Create a new Material and assign it to the mirror.
Create a new script and copy paste the code in (see below). Attach this script to your mirror camera. The scripts has a fieled called “Mat”, you need to drag and drop you mirror material onto that.
Last be sure that your mirrors camera has a lower Depth than your main camera
using UnityEngine;
using System.Collections;
public class Scope : MonoBehaviour
{
public Material _mat = null; // our Scope Material schould only be applied to the lens
public bool limitUpdateRate = true;
public int desiredUpdateRate = 60;
private Camera _cam = null; // the "Scope Camera"
private int _size = 512; // desired texture size. need some error handling. should not be greater than Screen.width or Screen.height
private Texture2D _tex = null; // cached texture object
private float _timer = 0f;
private void Start()
{
// get our Camera Compopnent
_cam = GetComponent<Camera>();
// set up Texture
_tex = new Texture2D( _size, _size, TextureFormat.RGB24, false, true );
// Calculate our Camera Rect
CalculateCameraRect();
}
// called when our camera is done rendering
private void OnPostRender()
{
if( limitUpdateRate == true )
{
_timer += Time.deltaTime;
if( _timer < 1f / desiredUpdateRate )
return;
else
_timer = 0f;
}
// read pixels
_tex.ReadPixels( new Rect( 0f, 0f, _size, _size ), 0, 0, false );
_tex.Apply();
// set our texture to the material
_mat.SetTexture( 0, _tex ); //_mat.SetTexture( "_MainTex", _tex );
}
// should be called when ever the screen resolution changes
public void CalculateCameraRect()
{
//TODO: error handling. check width and height not greater than 1
_cam.rect = new Rect( 0, 0, _size / (float)Screen.width, _size / (float)Screen.height );
}
}
Nice. will try it as soon as i can and save it for when i actually need something like it
Still think you should make a simple editor panel for it (for quick setup) and put it in the asset store for a couple €
yeah I will deffinatly put it in to the Asset Store at some point but probably for free. If you want to spend money so badly you can check out the FXLab Asset from Marrrk. He has implementet Post Processing effects and render texture in Unity free. I dont own this asset but it looks nice and has good reviews on the store. http://forum.unity3d.com/threads/204481-FXLab-Unity-Effects-(Does-NOT-require-Unity-Pro)
For some reason this code doesn’t work for me. I think that it’s even not compiling.
Do you get any error messages?
Here
have you copied the scrpit from here or is it from the downloaded UnityPackage? If you copied it the name of the script needs to be the same as the class name in it. so if you called you script ScopeScript.cs you would need to change line 4 to public class ScopeScript : MonoBehaviour
D’oh! Thanks again. I really don’t know how i did not notice this easy mistake.