Create a sprite atlus procedurally?

This is the place to find out how to accomplish something specific with SM2.

Create a sprite atlus procedurally?

Postby hima » Thu Apr 21, 2011 4:26 am

In the game I'm going to make, I want players to be able to create their own character from their drawing/graphics. I'm thinking about having them put the graphics in one folder, following the format I provide. Then in the game, it will load images from that folder and create a sprite atlus and show the graphic correctly on the screen.

Is this possible? Could someone please provide me a simple example of how to do this?

Thank you in advance!
hima
 
Posts: 37
Joined: Mon Jan 03, 2011 2:25 pm

Re: Create a sprite atlus procedurally?

Postby Brady » Thu Apr 21, 2011 7:48 am

Perhaps. But what you could do is save the positions of your 'template" graphics on the atlas, and then always write the player's graphics to those positions. Of course, that would mean the player's artwork would have to be confined to the dimensions of your template graphics.

But you might be able to do this by using Unity's Texture2D.PackTextures() method, and then take the resulting UVs and assign them back to your sprite objects using the SetUVs() method.
Brady
 
Posts: 5361
Joined: Tue Jul 06, 2010 11:33 pm

Re: Create a sprite atlus procedurally?

Postby hima » Thu Apr 21, 2011 10:36 am

Yeah, I think that''ll be a lot easier. There's a free atlas packing service that can export the atas along with a plist that describe where each image is on the atlas -> http://zwoptexapp.com/flashversion

Maybe I can parse the plist and use the position datas with the SetUVsFromPixelCoords() method?
hima
 
Posts: 37
Joined: Mon Jan 03, 2011 2:25 pm

Re: Create a sprite atlus procedurally?

Postby Brady » Thu Apr 21, 2011 6:44 pm

I think it would be way easier just to use Unity's built-in Texture2D.PackTextures(). That does the same thing, but entirely within your game.
Brady
 
Posts: 5361
Joined: Tue Jul 06, 2010 11:33 pm

Re: Create a sprite atlus procedurally?

Postby hima » Mon Jun 27, 2011 9:06 am

Ok, I'm back to try this again but still cannot achieve what I want :(
I can get external file and load it in. After I load the texture, this is the function I call to create a sprite using that texture
Code: Select all
function CreateSprite(tex : Texture2D):Sprite
{
   var spr : Sprite = Sprite.Create("TestSprite",Vector3(0,0,0), Quaternion.identity );
   spr.Setup(64,64,Vector2(0,0),Vector2(64,64),spriteMaterial);
   spr.gameObject.renderer.sharedMaterial.mainTexture = tex;
   spr.SetUVsFromPixelCoords(Rect(0,0,64,64));
   spr.UpdateUVs();
}


Can somebody please create a simple example to demonstrate how to do this?
hima
 
Posts: 37
Joined: Mon Jan 03, 2011 2:25 pm

Re: Create a sprite atlus procedurally?

Postby Brady » Mon Jun 27, 2011 10:01 pm

Actually, your code pretty much worked for me, except that I found a bug in SetUVsFromPixelCoords() where it can set the UVs upside-down. But actually, you should be able to trim it down to:
Code: Select all
    function CreateSprite(tex : Texture2D):Sprite
    {
       var spr : Sprite = Sprite.Create("TestSprite",Vector3(0,0,0), Quaternion.identity );
       spriteMaterial.mainTexture = tex;
       spr.Setup(64,64,Vector2(0,0),Vector2(64,64),spriteMaterial);
    }


If you're using the current beta, you may need to work around one small bug by adding this to the end of your method:
Code: Select all
spr.InitUVs();
spr.UpdateUVs();


If none of that works, please describe the nature of the problem you're having.
Brady
 
Posts: 5361
Joined: Tue Jul 06, 2010 11:33 pm

Re: Create a sprite atlus procedurally?

Postby hima » Tue Jun 28, 2011 4:15 pm

My code probably hates me. I copy and paste your code into the game and it works perfectly fine now >_< Thanks!
hima
 
Posts: 37
Joined: Mon Jan 03, 2011 2:25 pm

Re: Create a sprite atlus procedurally?

Postby Brady » Tue Jun 28, 2011 7:06 pm

I forgot to mention one thing: the "Vector2(0,0)" in my code above (which was copied from your original code) is likely the wrong value. This argument is the lower-left corner of the image on the atlas, which would be x=0, and y=imageHeight-1 (since coordinates run from 0 to height-1). The coordinate space used here is the same as in any image editor, with 0,0 at the top left, and width-1,height-1 at the bottom-right. So by passing 0,0, you're actually specifying the top-left corner of the image, not the bottom-left. The reason it still sort of works is because the wrap mode is set to repeat. But it means that on the Y-axis, the image is 1-pixel off. If the image is 64x64 and you want to display the entire image, you need to put in "Vector2(0,63)" for the bottom-left.
Brady
 
Posts: 5361
Joined: Tue Jul 06, 2010 11:33 pm

Re: Create a sprite atlus procedurally?

Postby hima » Wed Jun 29, 2011 7:14 am

Ah, ok. Thank you very much for that! I thought 0,0 is at bottom-left like Unity coordinate space. I'm sure there will be lot of problems and chaos in the future if I don't get this thing straight first :)
hima
 
Posts: 37
Joined: Mon Jan 03, 2011 2:25 pm

Re: Create a sprite atlus procedurally?

Postby hima » Wed Feb 01, 2012 6:32 pm

Wow, it's been a year! Truth be told, I haven't touched this project since I got the image to load X( But now that I have time and I'm thinking about procedural content again, I guess it's better to continue in this thread instead of having to start a new one

Now, I got PackedSprite to work with external texture using the technique of Texture2D.PackTextures and pass the UVs like you said. What I'm wondering is that, how should one add a UVAnimation to PackedSprite through scripting? Thank you in advance :)

Here's what I got so far. It work, but if there's anything I should know please tell me!

Code: Select all
PackedSprite CreateSprite( Texture2D texture, Rect[] uvs )
   {
      // Create a sprite
      PackedSprite sprite = PackedSprite.Create("TestSprite", new Vector3(0, 0, 0), Quaternion.identity );
      spriteMaterial.mainTexture = texture;
      sprite.Setup(texture.width * uvs[0].width, texture.height * uvs[0].height, spriteMaterial);
      
      // Setup UV information
      sprite.InitUVs();
      sprite.SetUVs(uvs[0]);
      sprite.UpdateUVs();
      sprite.autoResize = true;
      
      // Create a new animation
      UVAnimation anim = new UVAnimation();
      
      // Construct animation frames
      SPRITE_FRAME[] frames = new SPRITE_FRAME[2];
      for( int i = 0; i < 2; i++ )
      {
         frames[i] = new SPRITE_FRAME(1);
         frames[i].uvs = uvs[i];
      }
      
      // Add frames to the animation
      anim.AppendAnim(frames);
      
      // Setup animation properties
      anim.framerate = 10;
      anim.loopCycles = -1;
      
      // Add the created animation to the sprite and play it
      sprite.AddAnimation(anim);
      sprite.PlayAnim(0);
      return sprite;
   }
hima
 
Posts: 37
Joined: Mon Jan 03, 2011 2:25 pm

Next

Return to SM2 How to...

Who is online

Users browsing this forum: No registered users and 8 guests

cron