About Monkey 2 › Forums › Monkey 2 Code Library › Image with texture mask
Tagged: mask image shaders
This topic contains 9 replies, has 3 voices, and was last updated by 
 degac 1 year, 5 months ago.
- 
		AuthorPosts
 - 
		
			
				
September 7, 2017 at 8:00 am #10298
Hi. Here is an example of using texture mask.
We combine image with texture and get result – see sreenshot.
This time you can create masked image, but can’t change mask on the fly (Image.Shader property is read-only).
Worked by Image extension, added LoadMasked func.
Monkey123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101Namespace masked#Import "<std>"#Import "<mojo>"Using std..Using mojo..#Import "assets/@/"Class MyWindow Extends WindowMethod New( title:String="Masked images demo",width:Int=1024,height:Int=768,flags:WindowFlags=WindowFlags.Resizable )Super.New( title,width,height,flags )_scale=0.66_image11=GetImage( "asset::sky.jpg" )_image12=GetImage( "asset::mask-car.png" )_image13=GetMaskedImage( "asset::sky.jpg","asset::mask-car.png" )_image21=GetImage( "asset::flowers.jpg" )_image22=GetImage( "asset::mask-glass.png" )_image23=GetMaskedImage( "asset::flowers.jpg","asset::mask-glass.png" )_image31=GetImage( "asset::fire.jpg" )_image32=GetImage( "asset::mask-text.png" )_image33=GetMaskedImage( "asset::fire.jpg","asset::mask-text.png" )EndMethod OnRender( canvas:Canvas ) OverrideApp.RequestRender()canvas.DrawImage( _image11,0,0 )canvas.DrawImage( _image12,(Width-_image12.Width)/2,0 )canvas.DrawImage( _image13,(Width-_image13.Width),0 )canvas.DrawImage( _image21,0,(Height-_image21.Height)/2 )canvas.DrawImage( _image22,(Width-_image22.Width)/2,(Height-_image22.Height)/2 )canvas.DrawImage( _image23,Width-_image23.Width,(Height-_image23.Height)/2 )canvas.DrawImage( _image31,0,Height-_image31.Height )canvas.DrawImage( _image32,(Width-_image32.Width)/2,Height-_image32.Height )canvas.DrawImage( _image33,(Width-_image33.Width),Height-_image33.Height )EndPrivateField _scale:=1.0Field _image11:Image,_image12:Image,_image13:ImageField _image21:Image,_image22:Image,_image23:ImageField _image31:Image,_image32:Image,_image33:ImageMethod GetImage:Image( path:String )Local img:=Image.Load( path )img.Scale=New Vec2f( _scale,_scale )Return imgEndMethod GetMaskedImage:Image( path:String,maskPath:String )Local img:=Image.LoadMasked( path,maskPath )img.Scale=New Vec2f( _scale,_scale )Return imgEndEndFunction Main()New AppInstanceNew MyWindowApp.Run()EndClass Image ExtensionFunction LoadMasked:Image( imagePath:String,maskPath:String,textureFlags:TextureFlags=TextureFlags.FilterMipmap )Local pixmap:=Pixmap.Load( imagePath,Null,True )If Not pixmap Return NullLocal shader:=mojo.graphics.Shader.GetShader( "sprite-masked" )Local image:=New Image( pixmap,textureFlags,shader )Local tex:=mojo.graphics.Texture.Load( maskPath,textureFlags )If tex Then image.Material.SetTexture( "MaskTexture",tex )Return imageEndEndSources: https://www.dropbox.com/s/v4q3mv7rkwy8aik/masked-image.zip?dl=0
Attachments:
October 21, 2017 at 7:01 am #11246Nice. This looks really useful! Thanks for sharing.
October 21, 2017 at 7:30 am #11248Since Image.Shader property became writable, we can create Class MaskedImage extends Image with Property Mask:Image and change mask on-the-fly.
October 21, 2017 at 7:56 am #11249Oh, awesome! I was wondering about how to get around that limitation.
October 21, 2017 at 11:07 am #11261Quick question
Monkey12345Class Image ExtensionFunction LoadMasked:Image( imagePath:String,maskPath:String,textureFlags:TextureFlags=TextureFlags.FilterMipmap )I (surely) missed this piece of code
Monkey1Class Image ExtensionIt seems new to me!
I mean, I know thisMonkey1Class X Extend Bbut no the ‘Extension’ thing (seems quite clear what it does… but I would know better!)
October 21, 2017 at 1:30 pm #11262but no the ‘Extension’ thing (seems quite clear what it does… but I would know better!)
Extensions are awesome part of language!
It’s very useful to everybody learn it.
You can open monkey2 folder as a project in Ted2Go and right-click on the root and choose Find..., then find by Extension keyword. And see what monkey2 already have.
Class SomeClassName Extension is familiar for me, but I found something new:
Monkey1234567Class cpBody Extends VoidProperty Type:cpBodyType() Extension="cpBodyGetType"Setter( type:cpBodyType ) Extension="cpBodySetType".......EndIt looks like a magic.
PS. I was surprised c# does not support operators overloading via extensions but monkey2 do that !
Attachments:
October 21, 2017 at 1:39 pm #11264There is an updated code for MaskedImage:
Monkey12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061Class MaskedImage Extends ImageFunction Create:MaskedImage( imagePath:String,maskPath:String,textureFlags:TextureFlags=TextureFlags.FilterMipmap )Local pixmap:=Pixmap.Load( imagePath,Null,True )If Not pixmap Return NullLocal shader:=mojo.graphics.Shader.GetShader( "sprite-masked" )Local image:=New MaskedImage( pixmap,textureFlags,shader )Local tex:=mojo.graphics.Texture.Load( maskPath,textureFlags )image._mask=teximage.MaskEnabled=TrueReturn imageEnd' you can override any (all) constructor of Image classMethod New( pixmap:Pixmap,textureFlags:TextureFlags=TextureFlags.FilterMipmap,shader:Shader=Null )Super.New( pixmap,textureFlags,shader )EndProperty Mask:Texture()Return _maskSetter( value:Texture )_mask=valueIf _maskShader=Shader.GetShader( "sprite-masked" )Material.SetTexture( "MaskTexture",_mask )ElseShader=Shader.GetShader( "sprite" )EndifEndProperty MaskEnabled:Bool()Return _maskEnabledSetter( value:Bool )_maskEnabled=valueIf _maskEnabled And _maskShader=Shader.GetShader( "sprite-masked" )Material.SetTexture( "MaskTexture",_mask )ElseShader=Shader.GetShader( "sprite" )EndifEndPrivateField _mask:TextureField _maskEnabled:BoolEndFull code is here: https://www.dropbox.com/s/x7uvftczs7ie9pg/masked-image2.zip?dl=0
Attachments:
October 21, 2017 at 5:34 pm #11268Ok, I’ve noticed in the source code the keyword Extension, and I could ‘translate’ it as a ‘bridge’ to other function/field/etc in external libraries.
But in your first example – maybe I’m wrong – it seems to tell to compiler
Class Image has ‘new’ function added now.. In this case LoadMasked().
Is it the right interpretation?So, basically, instead to create/extend the base Class (in this case Image) (your last post) it will be possible to ‘extends’ the entire class with this keyword.
It’s like to write something like Class Image Extends Image ?!?
Handy!Quick question: you could use Class Image Extension many times or there’s a limit to use it ONCE, for all the ‘new features’?
Honestly I looked (following your hint) at the Monkey2 code, but it seems that this keyword (maybe because is a recent feature?) is not used normally, excluded in the case to ‘use external library’ (ie: Json, Bullet etc)October 22, 2017 at 4:20 am #11272But in your first example – maybe I’m wrong – it seems to tell to compiler
Class Image has ‘new’ function added now..Not quite. Extension members are translated into global functions but allow us to use ‘dot’ to get access to them.
Monkey123456789Class Image ExtensionMethod Crop( r:Recti ).....EndEnd' converted version is like this:Function Crop( me:Image,r:Recti ).....EndQuick question: you could use Class Image Extension many times or there’s a limit to use it ONCE, for all the ‘new features’?
Many times, in different files.
More info:
http://monkeycoder.co.nz/forums/topic/possible-to-add-method-to-rect-with-extention/
http://monkeycoder.co.nz/forums/topic/class-extensions/
Let’s continue discussion in topics above.
October 22, 2017 at 6:05 am #11283Thanks!
September 2016 and I discovered just now! - 
		AuthorPosts
 
You must be logged in to reply to this topic.

		
		
	
