Jump to content


Photo

Sprite Set Chroma Key


  • Please log in to reply
18 replies to this topic

#1 Rixeno

Rixeno

    The One 'n' Only

  • New Member
  • 513 posts

Posted 26 January 2008 - 12:34 AM

Sprite Set Chroma Key
What it does:
This script takes a sprite and a color and completely removes the color and returns the modified sprite! It gives the effect of the chroma key. It also now supports animated sprites.
Does this work only once?
Not at all! I tried it using the same modified sprite many times and got it to disappear one color at a time.
Tips:
If you want to use color from a position on the screen, just put draw_getpixel(x,y) for the chroma color.
Is credit required?
Yes please, since this is my first script using surfaces, plus, I find it very useful.
To-do for next version:
-Animation support (DONE)
The script:
//sprite_set_chroma
//Technically adds another transparent color
//The returned sprite is automatically made transparent at first
//Argument(s):
//argument0 = sprite index
//argument1 = chroma color
//Returns: The sprite with color removed
//Written by Rixeno
var surface,sprite,num;
surface=surface_create(sprite_get_width(argument0),sprite_get_height(argument0));
surface_set_target(surface);
draw_clear_alpha(argument1,0);
draw_sprite(argument0,0,sprite_get_xoffset(argument0),sprite_get_yoffset(argument0));
surface_reset_target();
sprite=sprite_create_from_surface(surface,0,0,sprite_get_width(argument0),sprite_get_height(argument0),sprite_get_precise(argument0),true,sprite_get_smooth(argument0),sprite_get_preload(argument0),sprite_get_xoffset(argument0),sprite_get_yoffset(argument0));
for (num=1;num<sprite_get_number(argument0);num+=1)
{
surface_set_target(surface);
draw_clear_alpha(argument1,0);
draw_sprite(argument0,num,sprite_get_xoffset(argument0),sprite_get_yoffset(argument0));
surface_reset_target();
sprite_add_from_surface(sprite,surface,0,0,sprite_get_width(argument0),sprite_get_height(argument0));
}
surface_free(surface);
return sprite;
Enjoy :D

Edited by Rixeno, 26 January 2008 - 10:31 PM.

  • 0
Do you need an example? You can PM me a request and I'll try my best.
Creations:
- Extra String Functions - Sprite Chroma Key - String at Position - Word Censor - Draw_Line_Width (Unreg GM6-) - Shadowed Text - Draw N-Gon - PIXEL PERFECT COLLISION ENGINE-

#2 dark_master4

dark_master4

    GMC Member

  • GMC Member
  • 1232 posts
  • Version:GM:Studio

Posted 26 January 2008 - 04:07 AM

If you could make it so it replaces 1 color in the sprite to any color you want, then that would make a really useful script because right now I can't find any use for this.
  • 0

My YoYoGames Marketplace (Has scripts, shaders and poker chips)


GM:Studio
Desktop Intel i5-4440 3.1ghz, 8gb RAM, NVIDIA GeForce GTX 770 2gb
Laptop AMD Athlon x64 2.1ghz, 4gb RAM, 256mb shared VRAM


#3 xot

xot

    GMC Dismember

  • GMC Elder
  • 4785 posts
  • Version:GM:Studio

Posted 26 January 2008 - 04:27 AM

Dark_master4, http://www.gmlscript...e_replace_color is what you are looking for, it uses the same basic technique. Sorry about the thread-jack.

Edited by xot, 26 January 2008 - 04:27 AM.

  • 0
GMLscripts.com, rise from your grave!

If any of my posts contain broken images or links, I can probably supply them for you. PM with a link to the post.

#4 Rixeno

Rixeno

    The One 'n' Only

  • New Member
  • 513 posts

Posted 26 January 2008 - 02:43 PM

Sorry about the thread-jack.

<{POST_SNAPBACK}>

No problem...
@dark_master4: This script isn't for replacing colors in a sprite, it's for adding full transparency to colors in a sprite :)
  • 0
Do you need an example? You can PM me a request and I'll try my best.
Creations:
- Extra String Functions - Sprite Chroma Key - String at Position - Word Censor - Draw_Line_Width (Unreg GM6-) - Shadowed Text - Draw N-Gon - PIXEL PERFECT COLLISION ENGINE-

#5 _IceCalibre_

_IceCalibre_

    Pro Graphic Design

  • New Member
  • 457 posts

Posted 26 January 2008 - 02:48 PM

I guess I have to ask, why not simply use an alpha mask?

I mean, I guess this would be easier for a huge amount of sprites, when a sprite and an alpha mask would be too big and redundant.

Chroma-keying doesn't seem useful outside of movie editing anyway, but, good job anyway...

If I'm wrong, please correct me,
_IceCalibre_
  • 0

#6 Rixeno

Rixeno

    The One 'n' Only

  • New Member
  • 513 posts

Posted 26 January 2008 - 05:10 PM

@ _IceCaliber_ : This is not for actions that are "expected". This can be used to add a transparent color (as much as you want) in-game. Which means you can remove any color without having to have another in sprite in the resources.
What it does, is that it makes a surface with sprite, then it uses the draw_clear_alpha to make a color transparent and then makes a sprite from the surface and returns it's index, so that you can use it afterwards. This could also work for some kind of cloaking effect :)
  • 0
Do you need an example? You can PM me a request and I'll try my best.
Creations:
- Extra String Functions - Sprite Chroma Key - String at Position - Word Censor - Draw_Line_Width (Unreg GM6-) - Shadowed Text - Draw N-Gon - PIXEL PERFECT COLLISION ENGINE-

#7 Chronic

Chronic

    Administrator

  • Global Moderators
  • 2729 posts
  • Version:GM:Studio

Posted 26 January 2008 - 05:38 PM

The issue you have here is that it doesn't support animated sprites, or at least my glance over the script seems to suggest that. Anyway, its a very interesting approach to doing this.

Also, chroma key seems to be the wrong title of this, as chroma key doesn't replace one color with another, it shows a image on to a certain color.
  • 0

My recent tutorials: [Auto-Tiling] [Text Based Menu] [Sprite Based Menu] [View more at my site]
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
R.I.P Mom (1951-2008)


#8 Rixeno

Rixeno

    The One 'n' Only

  • New Member
  • 513 posts

Posted 26 January 2008 - 06:12 PM

The issue you have here is that it doesn't support animated sprites, or at least my glance over the script seems to suggest that. Anyway, its a very interesting approach to doing this.

Also, chroma key seems to be the wrong title of this, as chroma key doesn't replace one color with another, it shows a image on to a certain color.

<{POST_SNAPBACK}>

Oops, sorry for the chroma key part, I'll fix that right-away. And for the animation, although it may take a lot of memory, I think that if you create the surface once in the create event and then put the rest of the code without the surface_free i the step event, I think it could work, I'll try it out and maybe post an example about it.
  • 0
Do you need an example? You can PM me a request and I'll try my best.
Creations:
- Extra String Functions - Sprite Chroma Key - String at Position - Word Censor - Draw_Line_Width (Unreg GM6-) - Shadowed Text - Draw N-Gon - PIXEL PERFECT COLLISION ENGINE-

#9 Chronic

Chronic

    Administrator

  • Global Moderators
  • 2729 posts
  • Version:GM:Studio

Posted 26 January 2008 - 07:46 PM

hmm, actually you can just use a loop and the sprite_add_from_surface function to recreate the sprite.

Something like..

- Make surface
- draw frame 1
- create sprite from surface
- start loop
- clear surface
- draw next frame
- add sprite from surface
- end loop
- delete old sprite
- delete surface
- return new sprite index

Maybe you could also make the deletion of the old sprite optional (boolean argument).
  • 0

My recent tutorials: [Auto-Tiling] [Text Based Menu] [Sprite Based Menu] [View more at my site]
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
R.I.P Mom (1951-2008)


#10 xot

xot

    GMC Dismember

  • GMC Elder
  • 4785 posts
  • Version:GM:Studio

Posted 26 January 2008 - 07:53 PM

I think chroma key is a suitable name for this, you select a color and it becomes transparent. I think Chronic misunderstood what this script actually does.

Check out the script I linked to see an example of sprite animation handling, it works just like Chronic describes.

Edited by xot, 26 January 2008 - 07:57 PM.

  • 0
GMLscripts.com, rise from your grave!

If any of my posts contain broken images or links, I can probably supply them for you. PM with a link to the post.

#11 Rixeno

Rixeno

    The One 'n' Only

  • New Member
  • 513 posts

Posted 26 January 2008 - 09:52 PM

Oh I see what Chronic, it's not to edit the animation every step in real-time, it would be more to make a complete animated sprite in the script... Or something like that...
@Xot : Yeah, for the chroma key, it sounds catchy, once I update the script, I'll change... Once again :P

EDIT: I got it to support animations now, thanks for the tip Chronic :)...

Edited by Rixeno, 26 January 2008 - 10:32 PM.

  • 0
Do you need an example? You can PM me a request and I'll try my best.
Creations:
- Extra String Functions - Sprite Chroma Key - String at Position - Word Censor - Draw_Line_Width (Unreg GM6-) - Shadowed Text - Draw N-Gon - PIXEL PERFECT COLLISION ENGINE-

#12 Games

Games

    GMC Member

  • Banned Users
  • 2968 posts

Posted 27 January 2008 - 07:32 AM

You know what I wish for?

Choose where the transparent color comes from in an image.

Until then, we have to find a work around, but I think that would be such a cool feature to have. However, until then, yours is very good.
Click to visit Toydaker.com
For Sale: 3 Editables, 40 dollars (demo on site above). Click to purchase.

#13 Rixeno

Rixeno

    The One 'n' Only

  • New Member
  • 513 posts

Posted 29 January 2008 - 11:01 PM

That is very easy, simply use draw_getpixel and set the coordinates:
sprite=sprite_set_chroma(sprite_index,draw_getpixel(x+1,y+1))
In there, I assume that the sprites xy origins are 0 and I decided to get the pixel that is found +1 down and +1 right, so for a sprite, I chose like this:
[ ] [ ] [ ] [ ] [ ] [ ] [ ]
[ ] [X][ ] [ ] [ ] [ ] [ ]
[ ] [ ] [ ] [ ] [ ] [ ] [ ]
[ ] [ ] [ ] [ ] [ ] [ ] [ ]
[ ] [ ] [ ] [ ] [ ] [ ] [ ]
The [X] is where I chose to get the sprites color. There are other complicated ways too. Like getting the surface color before removing it. but the first way would work if nothing is over the sprites image ^_^
  • 0
Do you need an example? You can PM me a request and I'll try my best.
Creations:
- Extra String Functions - Sprite Chroma Key - String at Position - Word Censor - Draw_Line_Width (Unreg GM6-) - Shadowed Text - Draw N-Gon - PIXEL PERFECT COLLISION ENGINE-

#14 molbal

molbal

    GMC Member

  • GMC Member
  • 59 posts
  • Version:GM:Studio

Posted 24 May 2009 - 02:13 PM

Chroma key? You are another Adobe Premiere Pro user! :):D:D:D

BTW, it's a great thing! And i think it is good to have a thing like this in GML!
  • 0

#15 Rixeno

Rixeno

    The One 'n' Only

  • New Member
  • 513 posts

Posted 24 May 2009 - 04:58 PM

Sorry, I don't use Premier Pro, it's too expensive, although I've had the opportunity to try it once. I prefer Wax Debugger, which is sorta where I got the idea from.

NOTE: This topic is very old, but if there's still interest, I don't mind making it usable with animated sprites.
  • 0
Do you need an example? You can PM me a request and I'll try my best.
Creations:
- Extra String Functions - Sprite Chroma Key - String at Position - Word Censor - Draw_Line_Width (Unreg GM6-) - Shadowed Text - Draw N-Gon - PIXEL PERFECT COLLISION ENGINE-

#16 molbal

molbal

    GMC Member

  • GMC Member
  • 59 posts
  • Version:GM:Studio

Posted 24 May 2009 - 08:17 PM

Ok, I never mind. :)
  • 0

#17 jsorgeagames

jsorgeagames

    GMC Member

  • GMC Member
  • 710 posts
  • Version:GM:Studio

Posted 28 May 2009 - 03:05 PM

Chroma key? You are another Adobe Premiere Pro user! :):D:D:D

BTW, it's a great thing! And i think it is good to have a thing like this in GML!


You do realize that Chroma Key is a widely used term and is not just a feature of Adobe Premiere Pro, right?
Well, anyway, this shouldn't be called Sprite Set Chroma Key, it should be Sprite Multiple Transparency Colors or something. Chroma Key implies that this script automatically removes every shade of a color in an image so it can be used in complex composites.

Good job though, could be very useful.
  • 0

#18 molbal

molbal

    GMC Member

  • GMC Member
  • 59 posts
  • Version:GM:Studio

Posted 28 May 2009 - 07:33 PM

You do realize that Chroma Key is a widely used term and is not just a feature of Adobe Premiere Pro, right?

I have just seen it in adobe products...
  • 0

#19 xot

xot

    GMC Dismember

  • GMC Elder
  • 4785 posts
  • Version:GM:Studio

Posted 31 May 2009 - 01:11 AM

Chroma Key implies that this script automatically removes every shade of a color in an image ...

No it doesn't. If a video chroma key worked that way it would be almost useless. It's meant to replace one color of a specific shade. In video systems you often can use threshold and expand/choke controls to expand the range of colors and shades and to enlarge/shrink the key. This may lack those controls but it almost has to to be practical. It's still a chroma keyer. If it really replaced every shade, then virtually every pixel would have some amount transparency, which is obviously not a useful key.
  • 0
GMLscripts.com, rise from your grave!

If any of my posts contain broken images or links, I can probably supply them for you. PM with a link to the post.