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
0
Question by qtownMWD · Aug 13, 2014 at 01:20 PM · c#vectrosity

How do you select 3d lines in Vectrosity by clicking?

i have created lines using the VectorLine.SetLine3D function and would like to be able to select a line by clicking on it.
I am using c# and would like to be able to do this on iOS and web platforms.
Thanks in advance and thanks for the answers to questions others have asked, for i have benefited as well.

Comment
Add comment · Show 2
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 robertbu · Aug 13, 2014 at 03:16 PM 0
Share

What does 'select' mean? What do you want to do with the line after you select it? I see three possibilities:

  • Dynamically add a mesh collier to the mesh of the line

  • Create a collider that is scaled and rotated to match the line

  • Do the calculation mathematically

avatar image qtownMWD · Aug 14, 2014 at 06:48 AM 0
Share

after selecting the line i would like to find the shortest distance between the curve (line) and a particular point on another curve. from the choices you proposed i think the mathematical solution would be best. could you explain further? thank you

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by robertbu · Aug 14, 2014 at 08:03 AM

Here is a bit of code that demonstrates how it can be done mathematically. What is needed is the minimum distance between a ray and a line segment, since you need to cast a ray into the scene for a perspective camera. This can be solved as single function mathematically. But I just went out and grabbed the Math3d class from the Unity Wiki, and did it in three function calls (per segment).

  1. Find the closest point on the ray to the line defined by the two segment points

  2. Find the closest point on the line segment to the point from step 1.

  3. Find the distance between the points in step 1 and 2.

If the distance is below the selection threshold, and if it is closer than any other distance found, it becomes the best candidate.

Note the sample code uses a LineRender. It generates a number of points in 3D space. When a line is selected, the code places a marker sphere at both ends of the segment. Note since the lines are in 3D space, the spheres will be at different distance from the camera and therefore usually very different in size. You could make the code more efficient by combining and simplify the function calls, but with 10,000 segments I found no lag when selecting (on my desktop).

 using UnityEngine;
 using System.Collections;
 
 [RequireComponent (typeof (LineRenderer))]
 public class SelectSegment : MonoBehaviour {
     
     public int ptCount = 25;
     public float selectionThreshold = 0.05f;
 
     private  Vector3[] points;
     private Transform marker1;
     private Transform marker2;
 
     void Start () {
         LineRenderer lr = GetComponent<LineRenderer>();
         lr.SetWidth(0.1f, 0.1f);
         lr.SetVertexCount(ptCount);
 
         points = new Vector3[ptCount];
         for (int i = 0; i < ptCount; i++) {
             points[i] = Random.insideUnitSphere * 6.0f;
             lr.SetPosition(i, points[i]);
         }
 
         marker1 = GameObject.CreatePrimitive(PrimitiveType.Sphere).transform;
         marker1.localScale = new Vector3(.15f, .15f, .15f);
         marker2 = GameObject.CreatePrimitive(PrimitiveType.Sphere).transform;
         marker1.localScale = new Vector3(.15f, .15f, .15f);
         marker1.renderer.enabled = false;
         marker2.renderer.enabled = false;
     }
     
     void Update () {
         if (Input.GetMouseButtonDown (0)) {
             int iSelect = AttemptSelect();
             if (iSelect != -1) {
                 marker1.transform.position = points[iSelect];
                 marker1.renderer.enabled = true;
                 marker2.transform.position = points[iSelect +1];
                 marker2.renderer.enabled = true;
             }
             else {
                 marker1.renderer.enabled = false;
                 marker2.renderer.enabled = false;
             }
         }
     }
 
     // Returns -1 if it did not find any lines within the threshold,
     //   or it returns the index of the first point in the closest segment
     int AttemptSelect() {
         Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
         
         int iSelect = -1;
         float closestYet = Mathf.Infinity;
         
         for (int i = 0; i < points.Length-1; i++) {
             Vector3 closest1;
             Vector3 closest2;
             if (Math3d.ClosestPointsOnTwoLines(out closest1, out  closest2, ray.origin, ray.direction, points[i], points[i+1] - points[i])) {
                 Vector3 v = Math3d.ProjectPointOnLineSegment(points[i], points[i+1], closest1);
                 float dist = (v - closest1).magnitude;
                 if (dist < closestYet && dist < selectionThreshold) {
                     iSelect = i;
                     closestYet = dist;
                 }
             }
         }
         return iSelect;
     }
 } 

Comment
Add comment · Show 2 · 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 qtownMWD · Aug 16, 2014 at 04:26 AM 0
Share

thank you robertbu. i don't have enough reputation to thumbs up your answer. very clever code. thank you.

avatar image robertbu · Aug 16, 2014 at 05:41 AM 0
Share

You can mark the question as answered by clicking on the checkmark to the left of the answer. When you accept an answer, it gives karma to both you and the person that answered your question.

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

2 People are following this question.

avatar image avatar image

Related Questions

Vectrosity: SetCamera(), aspect, resolution and rendertextures 1 Answer

Vectrosity: Clear vector2 array points 1 Answer

Vectrosity - lines disappear on certain camera angles 1 Answer

Collisions without physics 1 Answer

External Resource Folder -- Please Help 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