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 /
  • Help Room /
avatar image
0
Question by toto88x · Apr 04, 2016 at 04:42 PM · unity 52dmeshmeshfilter

Display a simple mesh (2D square) on the screen?

Hi,

For a test I'm trying to just display a simple mesh (2D square) on the screen.

I created an empty gameobject with just a MeshFilter component and a Script component. Here's the simple code in UnityScript. There's no errors, but it doesn't show anything on the screen.

 function Start () {
     // Create the vertices
     var Vertices = Array();
     Vertices[0] = Vector3(0, 0, 0);
     Vertices[1] = Vector3(1, 0, 0);
     Vertices[2] = Vector3(0, 1, 0);
     Vertices[3] = Vector3(1, 1, 1);

     // Create the UVs
     var UVs = Array();
     UVs[0] = Vector2(0, 1);
     UVs[1] = Vector2(1, 1);
     UVs[2] = Vector2(0, 0);
     UVs[3] = Vector2(1, 0);

     // Create the 2 triangles
     var triangles = [ 0, 1, 3, 3, 2, 0 ];

     // Display the mesh
     var m = Mesh();
     GetComponent.<MeshFilter>().mesh = m;
     m.vertices = Vertices;
     m.uv = UVs;
     m.triangles = triangles;
 }

Can you see where's my mistake? Thanks!

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 Fredex8 · Apr 04, 2016 at 08:06 PM

I use C# not JS so you're going to have to search some of this on your own but I can see a few mistakes. First you actually need to create a new gameobject to create the mesh on.

 var square: GameObject;
 square= new GameObject ("Square");

You then need to use AddComponent to create a MeshRenderer and a MeshFilter or nothing is going to render.

I'm not sure why var m = Mesh(); isn't resulting in an error, perhaps in JS it doesn't but if you check the documentation you'll see what you should be using.

Also your vertices do not describe a square. A two dimensional shape is always going to have one axis that is all the same but you are modifying all three, hence creating a three dimensional shape. Your vertices describe a square folded over on itself with dodgy normals.

  Vertices[0] = Vector3(-1, 0, -1);
  Vertices[1] = Vector3(-1, 0, 1);
  Vertices[2] = Vector3(1, 0, 1);
  Vertices[3] = Vector3(1, 0, -1);

That is for a square. Whichever axis you want it to be drawn on should all be the same, ie 0.

Your triangles are also wrong.

  var triangles = [ 0, 1, 2, 0, 2, 3 ];

Those are the values you want to go with the above vertices.

Which makes the UV co-ordinates:

      UVs[0] = Vector2(0, 0);
      UVs[1] = Vector2(0, 1);
      UVs[2] = Vector2(1, 1);
      UVs[3] = Vector2(1, 0);

It may then be necessary to use RecalculateNormals for it to display correctly.

You may also want to declare the values of m.vertices and m.uv directly rather than in a separate array.

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

103 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

Related Questions

Raycast on Graphic with Custom Mesh, 1 Answer

Making a level for a 2D platform Racing game? 0 Answers

I somehow broke Unity. 0 Answers

Rendering Shaders on Windows does not work but does on Mac (Unity 5.5.0f3) 0 Answers

Changing a mesh with C# and Resource.Load() 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