- Home /
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();
}
}
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.
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"
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.
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
Follow this Question
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