• Advertisement
Sign in to follow this  

Unity Texture tiling algorithm

This topic is 4613 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Topic started in Graphics Programming and Theory I guess this is sort of a cross post, but I think it is warranted. Guys, I need help. For the library I am writing, I need to be able to break up images into multiple chunks, so it can play nice with OpenGL's power of 2 rules. There is that, and I also want it to be able to handle images greater than a video card usualy allows. This will be a feature rich library that you can adapt to SDL, Win32, or any other library once the interface has been ported. But for now, it is just a hunk of source code just sitting on my hard drive. My problem is this... I think that I have the algorithm for finding the UV coordinates down just right, but the problem comes to generating the screen coordinates. The UV was simple, if the block was in full view, then the coordinates would be (0,0)-(1,1), otherwise, it would take the modulus of the chunk size, and divide by the chunk size to get a value between 0 and 1. The screen coordinates are another matter entirely. I can't just make it a multipling function, that would work if it alligned perfectly with the grid, and the destination rect matched the source rect perfectly. But it doesn't in all cases. I know it's the screen coordinates that aren't working properly, because i forced the textures and the poly winding off, and fixed the color, and nothing shows up at all still. Does anybody know where I can find an algorithm for this? or a white paper on it? or anything? I have been googling, but don't have a clue on what to google for... I know it's long, but thanks for reading. And mods, if you feel the need to close a thread, close the original one.

Share this post


Link to post
Share on other sites
Advertisement
I really don't see why you would need a paper on this, unless I misunderstand you, I think most of this sounds pretty trivial, as splitting an image into chunks and drawing the chunks as one image isn't exactly magic.

I might be the one not understanding, but could you be more specific to _what_ that is the problem? Finding the UV-coordinates for what? (and why)...

Share this post


Link to post
Share on other sites
Check out this recent thread. . It discusses some of the issues you may have (as the previous poster I'm not exactly sure what your issues are though).

Tom

Share this post


Link to post
Share on other sites
dimebolt, thanks for the link, but I am looking at a more flexible solution than the one provided there.

Syranide, my problem is that I can't figure out how to handle the screen coordinates for the broken up image.

Share this post


Link to post
Share on other sites
Quote:
Original post by PnP Bios
Syranide, my problem is that I can't figure out how to handle the screen coordinates for the broken up image.


First thing first, are you working with 2D or 3D?
If working with 2D it should be a piece of cake as you SHOULD be working with an orthogonal projection thus one pixel is one pixel.

Working with 3D could prove more brain teasing, but, really, I don't see the problem here either, if you have a wall, which would come to use 3 textures, then you can split it up in 3 parts, and create 3 new walls, and their coordinates (X,Y,SX,SY) would be interpolated coordinates (from the "real wall"), and the texture coordinates would be clipped according to your needs.

Wrong again?

Share this post


Link to post
Share on other sites
Quote:
Original post by Syranide
Quote:
Original post by PnP Bios
Syranide, my problem is that I can't figure out how to handle the screen coordinates for the broken up image.


First thing first, are you working with 2D or 3D?
If working with 2D it should be a piece of cake as you SHOULD be working with an orthogonal projection thus one pixel is one pixel.

Working with 3D could prove more brain teasing, but, really, I don't see the problem here either, if you have a wall, which would come to use 3 textures, then you can split it up in 3 parts, and create 3 new walls, and their coordinates (X,Y,SX,SY) would be interpolated coordinates (from the "real wall"), and the texture coordinates would be clipped according to your needs.

Wrong again?


Fortunatly, I am working in 2d. I know this should be so incredibly simple, but for some reason, I just can't figure out how to do the screen coordinates. I figure I won't have to worry about scaling and stuff, for when the source rectangle doesn't match the destination rectangle, I can use glScale.

But since it isn't alligned to a grid, the increments are going to be different when things are on the edge slices.

Share this post


Link to post
Share on other sites
Quote:
Original post by PnP Bios
Fortunatly, I am working in 2d. I know this should be so incredibly simple, but for some reason, I just can't figure out how to do the screen coordinates. I figure I won't have to worry about scaling and stuff, for when the source rectangle doesn't match the destination rectangle, I can use glScale.

But since it isn't alligned to a grid, the increments are going to be different when things are on the edge slices.


I still feel your question is still lacking some context. What exactly do you mean with screen coordinates (screen coordinates of what?). Are trying to make an API that allows the user to draw a quad with an arbitrary size texture on it? In that case the user will have specified the coordinates of the corners of the quad which can be use in calculating the corners of the subquads. If that's not what you want, please give some more context.

Tom

Share this post


Link to post
Share on other sites
Well, I solved it, I think. I will have to try it with larger images, but it seems to work so far.

void hxSurface::Blit(hxRect sRect, hxRect dRect)
{
float u1, u2, v1, v2; //texture coordinates
float sx, sy, sw, sh; //screen coordinates
float w_inc, h_inc; //width and height of the increment marker

int l, r, t, b; //four edges of the image

l = sRect.x / this->chunk_width;
r = (sRect.x + sRect.w) / this->chunk_width;

t = sRect.y / this->chunk_height;
b = (sRect.y + sRect.h) / this->chunk_height;

glPushMatrix();

glTranslatef(FL(dRect.x), FL(dRect.y), 0.0f);
glScalef(FL(dRect.w)/FL(sRect.w), FL(dRect.h)/(sRect.h), 0.0f);

sx = 0;
sy = 0;

for (int i = t; i <= b; i += 1)
{
h_inc = this->chunk_height;

for(int j = l; j <= r; j += 1)
{
u1 = 0.0f;
v1 = 0.0f;

u2 = 1.0f;
v2 = 1.0f;

w_inc = this->chunk_width;

//do adjustments for UV coordinates
if(j == l)
{
u1 = FL(l % this->chunk_width) / FL(this->chunk_width);
w_inc = (sRect.x % this->chunk_width);
}

if(j == r)
{
u2 = FL(r % this->chunk_width) / FL(this->chunk_width);
w_inc = (sRect.h % this->chunk_width);
}

if(i == t)
{
v1 = FL(t % this->chunk_height) / FL(this->chunk_height);
h_inc = (sRect.y % this->chunk_height);
}

if(i == b)
{
v2 = FL(b % this->chunk_height) / FL(this->chunk_height);
h_inc = (sRect.h % this->chunk_height);
}

sw = w_inc;
sh = h_inc;

//blit object to the screen;
glBindTexture(GL_TEXTURE_2D, this->texList[(this->y_chunk * i)+j]);

glBegin(GL_QUADS);

glTexCoord2f(u1, v1); glVertex2f(sx, sy);
glTexCoord2f(u2, v1); glVertex2f(sx + sw, sy);
glTexCoord2f(u2, v2); glVertex2f(sx + sw, sy + sh);
glTexCoord2f(u1, v2); glVertex2f(sx, sy + sh);

glEnd();

sx = sx + w_inc;

}

sx = 0;
sy = sy + h_inc;
}

glPopMatrix();

return;
}


This seems to work prety well. Now I just have a few more things to figure out and I am good to go.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
  • Advertisement
  • Popular Tags

  • Advertisement
  • Popular Now

  • Similar Content

    • By Alexander Nazarov
      Hello. I'm newby in Unity and just start learning basics of this engine. I want to create a game like StackJump (links are below). And now I wondering what features do I have to use to create such my game. Should I use Physics engine or I can move objects changing transform manually in Update().
      If I should use Physics can you in several words direct me how can I implement and what I have to use. Just general info, no need for detailed description of developing process.
       
      Game in PlayMarket
      Video of the game
    • By GytisDev
      Hello,
      without going into any details I am looking for any articles or blogs or advice about city building and RTS games in general. I tried to search for these on my own, but would like to see your input also. I want to make a very simple version of a game like Banished or Kingdoms and Castles,  where I would be able to place like two types of buildings, make farms and cut trees for resources while controlling a single worker. I have some problem understanding how these games works in the back-end: how various data can be stored about the map and objects, how grids works, implementing work system (like a little cube (human) walks to a tree and cuts it) and so on. I am also pretty confident in my programming capabilities for such a game. Sorry if I make any mistakes, English is not my native language.
      Thank you in advance.
    • By Ovicior
      Hey,
      So I'm currently working on a rogue-like top-down game that features melee combat. Getting basic weapon stats like power, weight, and range is not a problem. I am, however, having a problem with coming up with a flexible and dynamic system to allow me to quickly create unique effects for the weapons. I want to essentially create a sort of API that is called when appropriate and gives whatever information is necessary (For example, I could opt to use methods called OnPlayerHit() or IfPlayerBleeding() to implement behavior for each weapon). The issue is, I've never actually made a system as flexible as this.
      My current idea is to make a base abstract weapon class, and then have calls to all the methods when appropriate in there (OnPlayerHit() would be called whenever the player's health is subtracted from, for example). This would involve creating a sub-class for every weapon type and overriding each method to make sure the behavior works appropriately. This does not feel very efficient or clean at all. I was thinking of using interfaces to allow for the implementation of whatever "event" is needed (such as having an interface for OnPlayerAttack(), which would force the creation of a method that is called whenever the player attacks something).
       
      Here's a couple unique weapon ideas I have:
      Explosion sword: Create explosion in attack direction.
      Cold sword: Chance to freeze enemies when they are hit.
      Electric sword: On attack, electricity chains damage to nearby enemies.
       
      I'm basically trying to create a sort of API that'll allow me to easily inherit from a base weapon class and add additional behaviors somehow. One thing to know is that I'm on Unity, and swapping the weapon object's weapon component whenever the weapon changes is not at all a good idea. I need some way to contain all this varying data in one Unity component that can contain a Weapon field to hold all this data. Any ideas?
       
      I'm currently considering having a WeaponController class that can contain a Weapon class, which calls all the methods I use to create unique effects in the weapon (Such as OnPlayerAttack()) when appropriate.
    • By Vu Chi Thien
      Hi fellow game devs,
      First, I would like to apologize for the wall of text.
      As you may notice I have been digging in vehicle simulation for some times now through my clutch question posts. And thanks to the generous help of you guys, especially @CombatWombat I have finished my clutch model (Really CombatWombat you deserve much more than a post upvote, I would buy you a drink if I could ha ha). 
      Now the final piece in my vehicle physic model is the differential. For now I have an open-differential model working quite well by just outputting torque 50-50 to left and right wheel. Now I would like to implement a Limited Slip Differential. I have very limited knowledge about LSD, and what I know about LSD is through readings on racer.nl documentation, watching Youtube videos, and playing around with games like Assetto Corsa and Project Cars. So this is what I understand so far:
      - The LSD acts like an open-diff when there is no torque from engine applied to the input shaft of the diff. However, in clutch-type LSD there is still an amount of binding between the left and right wheel due to preload spring.
      - When there is torque to the input shaft (on power and off power in 2 ways LSD), in ramp LSD, the ramp will push the clutch patch together, creating binding force. The amount of binding force depends on the amount of clutch patch and ramp angle, so the diff will not completely locked up and there is still difference in wheel speed between left and right wheel, but when the locking force is enough the diff will lock.
      - There also something I'm not sure is the amount of torque ratio based on road resistance torque (rolling resistance I guess)., but since I cannot extract rolling resistance from the tire model I'm using (Unity wheelCollider), I think I would not use this approach. Instead I'm going to use the speed difference in left and right wheel, similar to torsen diff. Below is my rough model with the clutch type LSD:
      speedDiff = leftWheelSpeed - rightWheelSpeed; //torque to differential input shaft. //first treat the diff as an open diff with equal torque to both wheels inputTorque = gearBoxTorque * 0.5f; //then modify torque to each wheel based on wheel speed difference //the difference in torque depends on speed difference, throttleInput (on/off power) //amount of locking force wanted at different amount of speed difference, //and preload force //torque to left wheel leftWheelTorque = inputTorque - (speedDiff * preLoadForce + lockingForce * throttleInput); //torque to right wheel rightWheelTorque = inputTorque + (speedDiff * preLoadForce + lockingForce * throttleInput); I'm putting throttle input in because from what I've read the amount of locking also depends on the amount of throttle input (harder throttle -> higher  torque input -> stronger locking). The model is nowhere near good, so please jump in and correct me.
      Also I have a few questions:
      - In torsen/geared LSD, is it correct that the diff actually never lock but only split torque based on bias ratio, which also based on speed difference between wheels? And does the bias only happen when the speed difference reaches the ratio (say 2:1 or 3:1) and below that it will act like an open diff, which basically like an open diff with an if statement to switch state?
      - Is it correct that the amount of locking force in clutch LSD depends on amount of input torque? If so, what is the threshold of the input torque to "activate" the diff (start splitting torque)? How can I get the amount of torque bias ratio (in wheelTorque = inputTorque * biasRatio) based on the speed difference or rolling resistance at wheel?
      - Is the speed at the input shaft of the diff always equals to the average speed of 2 wheels ie (left + right) / 2?
      Please help me out with this. I haven't found any topic about this yet on gamedev, and this is my final piece of the puzzle. Thank you guys very very much.
  • Advertisement