Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 Cyclops · Jun 23, 2010 at 12:23 AM · screencoordinatespixel

Converting pixel dimensions to Unity's floating-point coordinates

So I'm creating a Procedural Mesh (a Plane) for a 2D app. The app is set to 640x480 pixels, and uses a camera set to Orthographic. The Vector3s used to create a Mesh are floating-point coordinates for Unity world-space. However, I want to set my Mesh dimensions in pixels, but haven't yet figured out a reliable conversion method. Unity scales are still a mystery to me. :)

For example - if I create a unit-1 sized Mesh (at Points (0,0,0),(1,0,0),(1,1,0),(0,1,0)), it looks like it fills about 40 pixels of the screen. Further experimentation seemed to put it at 36 pixels. So if I wanted a Plane/Mesh that filled the top-left quadrant of the screen, I'd use Points like (8,8, 6.6, 0).

That might work, but I'd prefer a more reliable algorithm than Programming by Coincidence... So is there any other way to backtrack from a set of pixel dimensions, to their floating-point coordinates?

Update: Andrew's answer worked well - and looking at his sample mesh project got me to change a few other things in my project. For instance, I was programmatically creating a GameObject to assign all the mesh Components to, whereas he just created an empty GameObject and drag/dropped the script to it. I still am not thinking in the Unity Way. :) Second, I used the numbers 320x240 hardcoded (just for testing), which, for unknown reasons, made the mesh way too large - when I switched to Screen.height/2 (as Andrew's code uses), it sized things correctly.

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
2
Best Answer

Answer by kilgore · Jun 23, 2010 at 03:14 PM

A package containing an example using ScreenToWorldPoint can be found here. It produces a triangle based on the coordinates of the Screen.

alt text

using UnityEngine;

using System.Collections; using System.Collections.Generic;

public class CreateMesh : MonoBehaviour {

public Material mat;

private MeshRenderer meshRender; private MeshFilter meshFilter; private int[] tris = new int[]{0,1,2}; private List<Vector2> uvs = new List<Vector2>(); private Vector3[] verts = new Vector3[]{}; private List<Vector3> normals = new List<Vector3>();

void Start() { meshRender = gameObject.AddComponent<MeshRenderer>(); meshFilter = gameObject.AddComponent<MeshFilter>(); verts = new Vector3[] { ToPixel(new Vector3(0, 0, 999)), ToPixel(new Vector3(Screen.width / 2, Screen.height / 2, 999)), ToPixel(new Vector3(Screen.width / 2, 0, 999)) };

 //Normals
 for (var e = 0; e &lt; verts.Length; e++)
 {
     normals.Add(transform.up);
 }
 //Verts
 for (int i = 0; i &lt; verts.Length; i++)
 {
     uvs.Add(new Vector2(verts[i].x, verts[i].z));
 }
 Mesh mesh = new Mesh();

 mesh.vertices = verts;
 mesh.uv = uvs.ToArray();
 mesh.normals = normals.ToArray();
 mesh.triangles = tris;
 meshFilter.mesh = mesh;

 renderer.material = mat;

}

private Vector3 ToPixel(Vector3 cord) { cord = Camera.main.ScreenToWorldPoint(cord); return (cord); }

}

Comment
Add comment · Show 1 · 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 Cyclops · Jun 24, 2010 at 12:22 AM 0
Share

Thanks, it worked (eventually :) Further comments in my 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

No one has followed this question yet.

Related Questions

How I get sizeof pixels of object 1 Answer

Is there a function for converting transform to screen coordinates? 1 Answer

Change Color Based On Pixel X Coordinate in Shader 1 Answer

How to retain absolute gameobject size in pixels no matter the resolution? 2 Answers

GLSL - Paint fragments in a certain view position 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