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 DanTup · Jan 07, 2014 at 07:24 PM · 2dtexture2d gametexture2dtiles

Is this the correct way to render 2D textures programatically?

I've been bashing my head trying to render tiles in 2D for my game. I've had issues with the sizes and coordindates not coming out as expected, but I've finally got some working code.

However; it seems like a lot of noise (three GL calls, creating Vector3s, converting them to move Vector3s, then finally drawing). Is this the correct/expected way to draw a 2D image at a given location?

(I can easily abstract this away; I just want to know if it's the best way, or if I'm missing some 2D flag that might make some of this redundant... Googling for terms like '2D LoadPixelMatrix DrawTexture WorldToScreenPoint' doesn't return any results!)

 using UnityEngine;
 
 public class RenderGrass : MonoBehaviour
 {
     public Texture texture;
 
     const int tileSizePixels = 70;
     const int pixelsPerUnit = 100;
     const float tileSizeUnits = (float)tileSizePixels / pixelsPerUnit;
 
     public void OnPostRender()
     {
         // Not really sure why I need this?
         GL.PushMatrix();
         GL.LoadPixelMatrix();

         // Render 5 adjacent tiles from 0 going horizontally 
         for (int tileX = 0; tileX < 5; tileX++)
         {
             // Create world coordindates from tile locations
             var topLeftWorld = new Vector3(tileX * tileSizeUnits, 0, 0);
             var bottomRightWorld = topLeftWorld + new Vector3(tileSizeUnits, -tileSizeUnits, 0);
 
             // Convert to screen coordinates
             // Not really sure why I need this; surely everyone is working in world coordindates?
             var topLeftScreen = camera.WorldToScreenPoint(topLeftWorld);
             var bottomRightScreen = camera.WorldToScreenPoint(bottomRightWorld);
 
             // Render to screen
             Graphics.DrawTexture(new Rect(topLeftScreen.x, topLeftScreen.y, bottomRightScreen.x - topLeftScreen.x, bottomRightScreen.y - topLeftScreen.y), texture);
         }

         // Not really sure why I need this?
         GL.PopMatrix();
     }
 }
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 The Kracken · Jan 07, 2014 at 07:39 PM

2 things that come to mind quickly.

1: Is this script attached to a camera? OnPostRender() only gets called if the script is attached to a camera.
2: Try Graphics.DrawTexture(new Rect(0,0,100,100),texture) and see if you get a 100 pixel square to show up on screen. if that works then your co-ordinates are off.

Hope this helps.

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 DanTup · Jan 08, 2014 at 12:17 PM 0
Share

I think you have mis-understood. $$anonymous$$y code works fine. I'm asking whether it's the correct way to do what I'm doing; since it seems to have a lot of noisy boilerplate code, and I can't find many people with code calling these methods like this: https://www.google.co.uk/search?q=LoadPixel$$anonymous$$atrix+WorldToScreenPoint+"Graphics.DrawTexture"

avatar image The Kracken · Jan 08, 2014 at 11:01 PM 0
Share

Ahh ok I see now. The matrix pushing is to convert your current space from world (unity units) to screen space (coordinates are now in pixels). You can avoid the matrix pushing and popping but that would take extra work to convert all coordinates from world to screen space.

avatar image DanTup · Jan 13, 2014 at 07:07 PM 0
Share

I don't follow - I already have code to convert from world to screen space, and also the matrix pushing. Removing ether of these breaks stuff (as does removing both).

It seems like I have a lot of code doing very little. If I remove the $$anonymous$$atrix push, rendering doesn't seem to be either in world units or screen coordindates. I've no idea what DrawTexture is doing if you just pass it raw numbers. Passing 0,0 to 100,100 does not draw a 100px square (as the docs kinda suggest), nor does it render from 0,0 to 100,100 in world units :-(

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

19 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

Related Questions

How to make my sprite face way Im walking 1 Answer

Making an array for Texture2D?? 2 Answers

Recover Missing Tile Textures 1 Answer

Generating 2D voxel circles using a noise function 0 Answers

How to push an object in 2D grid style 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