Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Volumetric scope scripting

Discussion in 'Scripting' started by Deleted User, Jan 28, 2014.

  1. Deleted User

    Deleted User

    Guest

    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) $Leupold_Canted_6-20LRTweb.jpg
    Thanks all for help)
    And sorry for my bad english.
     
  2. Pirs01

    Pirs01

    Joined:
    Sep 30, 2012
    Posts:
    389
  3. Dave-xP

    Dave-xP

    Joined:
    Nov 8, 2013
    Posts:
    106
    put a camera in front of the scope and set its render texture as metial of the lens
     
  4. Tee_Pee

    Tee_Pee

    Joined:
    Aug 18, 2013
    Posts:
    54
    You'd need render to texture as Pirs01 said, but this is a pro-only feature.
     
  5. softwizz

    softwizz

    Joined:
    Mar 12, 2011
    Posts:
    793
    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.

    $minicam.png
     
  6. screenname_taken

    screenname_taken

    Joined:
    Apr 8, 2013
    Posts:
    663
    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.
    http://answers.unity3d.com/questions/16146/changing-between-cameras.html
    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.
     
  7. Deleted User

    Deleted User

    Guest

    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
    $3DScope-zoomedOut.png
    $3DScope-zoomedIn.png

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class Test : MonoBehaviour
    6. {
    7.     public Camera gunCam = null;
    8.     public Camera otherCam = null;
    9.  
    10.     private Texture2D _tex = null;
    11.     private Material _mat = null;
    12.  
    13.     private int _size = 512;
    14.  
    15.     private WaitForEndOfFrame _wait = new WaitForEndOfFrame();
    16.  
    17.     private void Start()
    18.     {
    19.         _tex = new Texture2D( _size, _size, TextureFormat.RGB24, false, true );
    20.         _mat = renderer.sharedMaterial;
    21.  
    22.         StartCoroutine( Cam() );
    23.     }
    24.  
    25.    
    26.     private IEnumerator Cam()
    27.     {
    28.         gunCam.enabled = false;
    29.         otherCam.enabled = false;
    30.         while( true )
    31.         {
    32.             yield return _wait;
    33.  
    34.             gunCam.Render();
    35.             _tex.ReadPixels(new Rect( Screen.width * 0.5f - _size * 0.5f, Screen.height * 0.5f - _size * 0.5f, _size, _size ),
    36.                                 0, 0, false );
    37.            
    38.             _tex.Apply();
    39.             _mat.SetTexture( "_MainTex", _tex );
    40.            
    41.             otherCam.Render();
    42.         }
    43.     }
    44. }
    45.  

    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
     
    Last edited by a moderator: Jan 29, 2014
  8. screenname_taken

    screenname_taken

    Joined:
    Apr 8, 2013
    Posts:
    663
    Woh nice one.
     
  9. Deleted User

    Deleted User

    Guest

    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
     
  10. Deleted User

    Deleted User

    Guest

    here we go
    $Untitled-1.png

    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
     
  11. Deleted User

    Deleted User

    Guest

    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.
     
    Last edited by a moderator: Jan 29, 2014
  12. screenname_taken

    screenname_taken

    Joined:
    Apr 8, 2013
    Posts:
    663
    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.
     
  13. Deleted User

    Deleted User

    Guest

    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

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class Scope : MonoBehaviour
    6. {
    7.     public Material _mat = null;            // our Scope Material schould only be applied to the lens
    8.  
    9.     public bool limitUpdateRate = true;
    10.     public int desiredUpdateRate = 60;
    11.  
    12.     private Camera _cam = null;             // the "Scope Camera"
    13.     private int _size = 512;                // desired texture size. need some error handling. should not be greater than Screen.width or Screen.height
    14.     private Texture2D _tex = null;          // cached texture object
    15.  
    16.     private float _timer = 0f;
    17.  
    18.     private void Start()
    19.     {
    20.         // get our Camera Compopnent
    21.         _cam = GetComponent<Camera>();     
    22.  
    23.         // set up Texture
    24.         _tex = new Texture2D( _size, _size, TextureFormat.RGB24, false, true );
    25.         // Calculate our Camera Rect
    26.         CalculateCameraRect();
    27.     }
    28.  
    29.     // called when our camera is done rendering
    30.     private void OnPostRender()
    31.     {
    32.         if( limitUpdateRate == true )
    33.         {
    34.             _timer += Time.deltaTime;
    35.  
    36.             if( _timer < 1f / desiredUpdateRate )
    37.                 return;
    38.             else
    39.                 _timer = 0f;
    40.         }
    41.  
    42.         // read pixels
    43.         _tex.ReadPixels( new Rect( 0f, 0f, _size, _size ), 0, 0, false );
    44.         _tex.Apply();
    45.  
    46.         // set our texture to the material
    47.         _mat.SetTexture( 0, _tex );  //_mat.SetTexture( "_MainTex", _tex );
    48.     }
    49.  
    50.  
    51.     // should be called when ever the screen resolution changes
    52.     public void CalculateCameraRect()
    53.     {
    54.         //TODO: error handling. check width and height not greater than 1
    55.         _cam.rect = new Rect( 0, 0, _size / (float)Screen.width, _size / (float)Screen.height );
    56.     }
    57. }
    58.  
     
  14. screenname_taken

    screenname_taken

    Joined:
    Apr 8, 2013
    Posts:
    663
    Nice. will try it as soon as i can and save it for when i actually need something like it :D
    Still think you should make a simple editor panel for it (for quick setup) and put it in the asset store for a couple €
     
  15. Deleted User

    Deleted User

    Guest

  16. Deleted User

    Deleted User

    Guest

    For some reason this code doesn't work for me. I think that it's even not compiling.
     
  17. Deleted User

    Deleted User

    Guest

    Do you get any error messages?
     
  18. Deleted User

    Deleted User

    Guest

    Here
    $1234.JPG
     
    Last edited by a moderator: Jan 30, 2014
  19. Deleted User

    Deleted User

    Guest

    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
     
  20. Deleted User

    Deleted User

    Guest

    D'oh! Thanks again. I really don't know how i did not notice this easy mistake.
     
  21. Deleted User

    Deleted User

    Guest

    If I'll release my game,you'll be in credits !