Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by scipiothegreat · Feb 27, 2012 at 12:39 AM · shadergraphicssightdotred

Shader based red dot sight

I've seen examples of realistic red dot sights made using shaders for Unity. What I'm trying to do is recreate this because the poster of the example has only posted pictures. I know the basics of writing a shader, and have already tried transforming the vertices of plane the dot is on, but this makes the dot get smaller as it gets further away, and can be seen from anywhere, not just inside the window its on. What other ways could this effect be achieved?

EDIT: In case someone is good with shaders but doesn't know what I'm talking about, a red dot sight is a type of scope on guns that projects an image onto the lens that the user looks through. This dot can only be seen from behind the sight, and appears to be 50 or so meters away, making it so that whatever the dot is on is what the gun is pointed at. This is not the same as a laser pointer used for aiming.

Comment
Add comment · Show 5
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Berenger · Feb 27, 2012 at 12:46 AM 0
Share

Why not using a projector orthographic ?

avatar image scipiothegreat · Feb 27, 2012 at 12:51 AM 0
Share

How would I go about doing that?

avatar image Berenger · Feb 27, 2012 at 01:06 AM 0
Share

Import the projector package from the standard assets, try the light prefab, the it to orthographic and play along. I don't remember if there is a color property on the shader though. Anyway, make it child of the camera, pointing forward, and voila !

avatar image scipiothegreat · Feb 27, 2012 at 01:19 AM 0
Share

Hmm I played around with this, but this would act more like a laser pointer, and would need to intersect geometry to be seen.

avatar image Berenger · Feb 27, 2012 at 01:40 AM 0
Share

And that's not what you want. Ok, so red dot sights isn't what I thought, sorry ^^

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by aldonaletto · Feb 28, 2012 at 12:35 AM

I know this is not exactly what you're looking for, but maybe it's useful for you or other guys reading this question: you could have a simple plane with the red dot assigned to a material with the Cutout/Diffuse shader, then move the plane to the hit position and align its rotation to the hit normal.
Create a red dot image with a transparent background (download it from here, if you want) and import it to Unity. Create a new material with the shader Transparent/Cutout/Diffuse, and assign the dot image to the material texture, then create a plane in Unity and assign the new material to it. Finally, attach the script below to the plane you've created. This script destroys the original plane at Start and replace it with a simple 1 x 1 mesh composed by only 2 triangles (the original plane had 200!). It also destroys the original mesh collider because the laser dot must not have a collider.

var size: float = 0.01; // dot diameter

function Start () { var mesh = GetComponent(MeshFilter).mesh; mesh.Clear(); // destroy the original plane... mesh.vertices = new Vector3[4]; // and replace it with a simple 1 x 1 mesh.triangles = new int[6]; // mesh created with only 2 triangles mesh.uv = new Vector2[4]; mesh.vertices = [ Vector3(-0.5, 0.0, -0.5), Vector3(-0.5, 0.0, 0.5), Vector3(0.5, 0.0, 0.5), Vector3(0.5, 0.0, -0.5) ]; mesh.triangles = [0,1,2,2,3,0]; mesh.uv = [ Vector2(0f, 0f), Vector2(0f, 1f), Vector2(1f, 1f), Vector2(1f, 0f) ]; mesh.RecalculateNormals(); if (collider) Destroy(collider); }

function LateUpdate(){ // create a ray from the center of the screen: var ray = Camera.main.ViewportPointToRay(Vector3(0.5, 0.5, 0.0)); var hit: RaycastHit; if (Physics.Raycast(ray, hit)){ // if something hit by the ray... renderer.enabled = true; // enable the dot rendering // position the dot a little ahead of the hit point: transform.position = hit.point+0.01*hit.normal; // align the plane normal to the hit point normal: transform.rotation = Quaternion.FromToRotation(Vector3.up, hit.normal); // make the point grow with the distance: transform.localScale = (hit.distance + 0.5) size Vector3.one; } else { // if nothing hit... renderer.enabled = false; // disable the dot } } NOTE 1: transform.localScale is adjusted to provide a size correction as a function of the distance;
NOTE 2: the laser dot only appears on objects which have a collider;

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Always included shaders (Graphics settings) 1 Answer

Dymanic Mesh Hiding 2 Answers

Shader effect gone when I hit Play 0 Answers

Effects of Graphics.Blit to rendertexture are temporary 2 Answers

Do TEXCOORDS need to be in sequence? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges