Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
0
Question by DanVioletSagmiller · Oct 18, 2018 at 09:03 PM · cameramathsceneviewpixels

Given a position, how do I figure out Pixel's Per Unit?

I have a custom editor for my scene which demonstrates the playable radius (how close my game ships will get to an object before slowing. The problem is that the circle draws a single line, and I want it a little thicker, say 3 pixels wide.


I can set that with this for now:

         Handles.DrawWireDisc(transform.position, Vector3.up, Radius);
         Handles.DrawWireDisc(transform.position, Vector3.up, Radius-.05f);
         Handles.DrawWireDisc(transform.position, Vector3.up, Radius-.1f);



But if I zoom in or out, then the lines become just 1 line, or spread apart, loosing the effect. I'm looking for away to guarantee the next radius will be 1 pixel out.

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by andrew-lukasik · Oct 20, 2018 at 12:00 AM

Not pixel perfect but does the job (keeps constant size):

https://i.imgur.com/RoZzeAG.gif

 using System.Collections.Generic;
 using UnityEngine;
 
 public class GlGizmo : MonoBehaviour
 {
     [SerializeField][Range(3,32)] int _lineCount = 12;
     [SerializeField][Range(0.001f,100f)] float _radius = 1.5f;
     [SerializeField][Range(0.001f,1f)] float _unitWidth = 0.1f;//width when distance to camera is 1.0
 
     void OnDrawGizmos ()
     {
         System.Func<int,int,Vector3> CalcPoint = ( i , max ) =>
         {
             float f = i / (float)max;
             float angle = f * Mathf.PI * 2;
             return new Vector3( Mathf.Cos(angle)*_radius , Mathf.Sin(angle)*_radius );
         };
 
         float dist = Vector3.Distance( Camera.current.transform.position , transform.position );
 
         GL.PushMatrix();
         GL.MultMatrix( transform.localToWorldMatrix );
         ( new Material( Shader.Find("Sprites/Default") ) ).SetPass(0);//replace material to customize render style
         GL.Begin( GL.QUADS );
         {
             //set color:
             GL.Color( Color.white );
 
             //warm up:
             {
                 Vector3 A0 = CalcPoint( 0 , _lineCount );
                 Vector3 B0 = A0 + A0.normalized*_unitWidth*dist;
                 GL.Vertex( A0 );
                 GL.Vertex( B0 );
             }
 
             //segments:
             for( int i=1 ; i<=_lineCount ; i++ )
             {
                 Vector3 A = CalcPoint( i , _lineCount );
                 Vector3 B = A + A.normalized*_unitWidth*dist;
                 
                 if( i%2==0 )//even
                 {
                     //end started quad:
                     GL.Vertex( A );
                     GL.Vertex( B );
 
                     //start new quad
                     GL.Vertex( A );
                     GL.Vertex( B );
                 }
                 else//odd
                 {
                     //end started quad:
                     GL.Vertex( B );
                     GL.Vertex( A );
 
                     //start new quad
                     GL.Vertex( B );
                     GL.Vertex( A );
                 }
             }
         }
         GL.End();
         GL.PopMatrix();
     }
 }

Comment
Add comment · Show 3 · 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
avatar image andrew-lukasik · Oct 20, 2018 at 12:00 AM 0
Share

As a bonus, you can customize it more, for example, adding gradients and what not https://i.imgur.com/7l06Y$$anonymous$$S.png

avatar image andrew-lukasik · Oct 20, 2018 at 12:14 AM 0
Share

Whatever drawing concept one uses, core of this consists of just linear proportion:

unit value * distance to camera

avatar image andrew-lukasik · Oct 20, 2018 at 12:22 AM 0
Share

Note: To add precision, distance can be calculated not once (for circle center) but for every vertex pair A-B.

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

155 People are following this question.

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

Related Questions

Smooth Follow Camera that Keeps Up 0 Answers

Scene-like camera control in Game 1 Answer

How to turn a camera inversely from another camera (Similar to a realistic mirror)? 1 Answer

Using Command Buffers with the Scene View Camera 0 Answers

Scene View Camera Depth Texture? 0 Answers


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