How to create a circle with soft edges

Discussion about using Moai SDK - post questions, bugs and issues here.

Moderators: seebs, franciscotufro

How to create a circle with soft edges

Postby zandegran » Fri Jul 20, 2012 7:30 am

Image How can i create circle with soft edges like this one??
I believe that I should use Moaishader :?
zandegran
 
Posts: 17
Joined: Thu Jul 12, 2012 3:49 am

Re: How to create a circle with soft edges

Postby pygy79 » Tue Jul 24, 2012 1:14 pm

Why don't you simply use an image?
User avatar
pygy79
 
Posts: 69
Joined: Wed Jan 04, 2012 3:13 am

Re: How to create a circle with soft edges

Postby zandegran » Wed Jul 25, 2012 1:30 am

I need to resize it set alpha, set texure, change colors etc so I need it to be dynamic
zandegran
 
Posts: 17
Joined: Thu Jul 12, 2012 3:49 am

Re: How to create a circle with soft edges

Postby dnhkng » Wed Jul 25, 2012 8:01 am

Seems pretty straightforward.
Ain't it just a just a bit of trigonometry, with the alpha channel a sigmoidal function of distance from the center point, all set with MOAIImage.setRGBA?
-Dave
dnhkng
 
Posts: 7
Joined: Sun Jul 08, 2012 7:37 am

Re: How to create a circle with soft edges

Postby zandegran » Tue Jul 31, 2012 5:33 am

I'm drawing with MOAIDraw.fillCircle()..How can I use MOAIImage.setRGBA..
I see the technique behind your idea...But How can I apply it for thangs that are drawn using MOAIDraw.fillCircle()..

Simply Put, How can I access (and control) the attributes every pixel in the shapes that are drawn using MOAIDraw
zandegran
 
Posts: 17
Joined: Thu Jul 12, 2012 3:49 am

Re: How to create a circle with soft edges

Postby nosheet » Tue Jul 31, 2012 5:57 am

How can I access (and control) the attributes every pixel in the shapes that are drawn using MOAIDraw

I don't think you can, unfortunately. MOAIDraw is quite basic in this respect.

I think the most efficient way to do this would be making a custom fragment shader.
If you're not comfortable with that, the advice of @dnhkng was quite solid too, create texture on the fly using trigonometry (shader approach would pretty much be the same though).
You may hate my haircut, but you'll love my games:
http://www.spin-up-game.com and http://www.foosballhero.com
User avatar
nosheet
 
Posts: 201
Joined: Mon May 28, 2012 2:40 pm
Location: Madrid, Spain

Re: How to create a circle with soft edges

Postby dnhkng » Tue Jul 31, 2012 9:21 am

but shaders are scarier than trigonometry :o
dnhkng
 
Posts: 7
Joined: Sun Jul 08, 2012 7:37 am

Re: How to create a circle with soft edges

Postby nosheet » Tue Jul 31, 2012 11:08 am

it's getting even worse, because shaders will also include trigonometry :)
but it will execute super fast, and you can change it in runtime
You may hate my haircut, but you'll love my games:
http://www.spin-up-game.com and http://www.foosballhero.com
User avatar
nosheet
 
Posts: 201
Joined: Mon May 28, 2012 2:40 pm
Location: Madrid, Spain

Re: How to create a circle with soft edges

Postby adam » Tue Jul 31, 2012 11:49 am

You could pull it off with a shader, I'm not sure how much faster it would be though, as doing that much in a nearly full screen fragment shader is very expensive.

Personally, I'd use a large texture, mipmapping should make it scale down well. You can still set the alpha and color of it from the color attribute of a prop.
User avatar
adam
 
Posts: 262
Joined: Wed Sep 14, 2011 11:50 am

Re: How to create a circle with soft edges

Postby zandegran » Thu Aug 02, 2012 5:40 am

Thank you so much Guys The following code works, which can be extended to create a circle with soft edges but I need it to be applied for all the shapes like rectangle, regular polygon etc

Code: Select all
  1. texture = MOAIImageTexture.new ()

  2. texture:init ( 124, 124 )

  3. for i = 1,122 do texture:setRGBA ( i, 0, 1, 1, 0, .5 ) texture:setRGBA ( i, 123, 1, 1, 0, 1 ) end

  4. for i = 1,122 do texture:setRGBA ( 0, i, 1, 1, 0, .5 ) texture:setRGBA ( 123, i, 1, 1, 0, 1 ) end

  5.  

  6. for i = 1,122 do

  7.     for j = 0,122 do

  8.         texture:setRGBA ( i, j, 1,  1, 0, math.sin(i) )

  9.     end

  10. end

  11. gfxQuad = MOAIGfxQuad2D.new ()

  12. gfxQuad:setTexture ( texture )

  13. gfxQuad:setRect ( -64, -64, 64, 64 )

  14. gfxQuad:setUVRect ( 0, 0, 1, 1 )

  15.  

  16. prop = MOAIProp2D.new ()

  17. prop:setBlendMode(1)

  18. prop:setDeck ( gfxQuad )

  19. layer:insertProp ( prop )

  20.  

  21. prop:moveRot ( 360, 5 )

  22.  



http://getmoai.com/forums/textureimage-for-regular-polygon-t1068/ see this. The last step would be applying this texture to rounded squares and rects created like this http://getmoai.com/forums/creating-rounded-square-t1004/
zandegran
 
Posts: 17
Joined: Thu Jul 12, 2012 3:49 am

Re: How to create a circle with soft edges

Postby derickdong » Thu Aug 02, 2012 11:51 am

For a quick and dirty method, if you want arbitrary shapes, you could do something like this:

1. Render the base shape to the texture
2. Loop through all the pixels:
a. Get the color value of each of the surrounding pixels
b. Sum the nine values, and divide by 9
c. Set the current pixel to this value

You'd probably want to run step 2 a couple times to increase the size and amount of blur. You may also want to play around with how you're sampling the texture (steps 2a and 2b) and calculating the pixel value.
derickdong
 
Posts: 63
Joined: Wed Nov 09, 2011 2:54 pm

Re: How to create a circle with soft edges

Postby sgeos » Thu Aug 02, 2012 1:47 pm

You could create objects that return an alpha value for an X, Y pair. Then you could loop through all the pixels. It doesn't feel very efficient but it should work.
Code: Select all
  1. -- create a new object

  2. function new ( pParams )

  3.   local result = {}

  4.   result.init = function ( self, pParams )

  5.     -- initialize object and return self

  6.     return self

  7.   end

  8.   result.getAlpha = function ( self, pX, pY )

  9.     local result

  10.     -- set result using a calculation based on pX and pY

  11.     -- the object state, such as bounding box or radius may factor into the calculation

  12.     return result

  13.   end

  14.   -- return object

  15.   return result:init ( pParams )

  16. end

Any original code posted by me is released via the CC0 Public Domain Dedication. It is in the public domain. Do whatever you want with it.
User avatar
sgeos
 
Posts: 241
Joined: Sat Apr 28, 2012 4:42 am
Location: Married in Japan.


Return to Moai SDK

Who is online

Users browsing this forum: JoseGutierrez and 0 guests

x