Fill image with custom shader data
September 5, 2017 at 11:15 am #10283
I want to fill image with some data that produced by shader.
What I need is to pass some variables to my shader:123uniform sampler1D tex; //I know that 1d not supporteduniform vec2 c;uniform int iter;
I tried to use Image.Material.SetTexture() and so on – no effect. Please help.September 6, 2017 at 4:02 am #10285
Yay, I got it myself! 🙂1234567891011121314151617#Import "assets/@/"' place your shaders into 'assets/shaders/' folder with extension .glsl.........Local tex:=Texture.Load( "asset::pal.png",TextureFlags.None )Local shader:=Shader.Open( "myShader" )_image=New Image( 400,400,,shader )' pass some params into shader' inside of shader them looks like m_center, m_scale, etc_image.Material.SetVec2f( "center", New Vec2f(.5,0) )_image.Material.SetFloat( "scale", 2.5 )_image.Material.SetFloat( "iterF", 70 )' textures inside of shader looks like' uniform sampler2D m_ImageTexture0;' where last 0 is index (m_ImageTexture0, m_ImageTexture1, etc)_image.SetTexture( 0,tex ).....canvas.DrawImage( _image,xx,yy ) ' render with our shader
No GLWindow required, just mojo.Window.
I missed Material.SetInt() method to pass iterations counter, so need to cast types inside of shader.September 6, 2017 at 5:10 am #10286
I’ll add SetInt if you want – just post an issue to github to remind me!September 6, 2017 at 6:15 am #10287
You can pass texture to shader by two ways:
- _image.Material.SetTexture( “myTex”,_tex )
- in shader name will be m_myTex
- _image.SetTexture( 0,_tex )
- in shader name will be m_ImageTexture0
Note: _image.Material.SetTexture( 0,_tex ) don’t raise an exception but is incorrect.
(0 -> toString -> “0”)September 6, 2017 at 6:34 am #10288
Great to see that custom glsl shaders have some kind of interface now!September 7, 2017 at 5:11 am #10294
Why Image.Shader property is read-only? I want to change shader on the fly. Is it possible?
Canvas class just grab shader when render image,1AddDrawOp( image.Shader,image.Material,image.BlendMode,4,1 )
so I think we can change it and that should break nothing.September 7, 2017 at 7:20 am #10295
Yes, it’s possible, although to be honest I can’t see why you’d want to change shader on the fly.
I do tend to make things read only by default if there doesn’t seem to be an obvious reason to allow them to be writeable. I find this generally keeps object logic much simpler and often allows for future optmizations.
Pushed to develop branch![/edit]September 7, 2017 at 7:27 am #10296
In my case, I want to bring MaskTexture property into Image class.
To apply texture as a mask I was going to use shader processing, and for that purpose I need to switch default ‘sprite’ shader with ‘sprite-masked’.
Maybe you’ll add this as a core part or mojo.
I’ll post my approach with masks in code libs soon.September 7, 2017 at 8:09 am #10301
Not sure what you mean by a MaskTexture here – is this like storing the alpha channel in a separate texture?
But the idea is/was that if you want to use a different shader with an image, you need to specify it when constructing or loading the Image. You could also do things like add Image.LoadMasked() style class extensions.
A quick thought though: Image is not really designed for extension the way mojo3d’s material is. Maybe it should be?
Material has a protected SetShader method so subclasses (like PbrMaterial and WaterMaterial) can change their shader ‘on the fly’ if necessary. The idea is that material will eventually have a protected InvalidateShader() method, and a protected virtual OnValidateShader:Shader() method. InvalidateShader() would be invoked when certain properties are modified, and OnValidateShader() would return different shaders depending on material property values. For example, if a material’s NormalTexture property is null, OnValidateShader() could return a non-bumpmapped variation of its shader. A lot of this is just theory though and has not been implmented yet. I’m kind of afraid it might be overkill, and will just be too complex to be useful.
In this case, Material.Shader is not writeable as this defeats the purpose of giving material subclass control over the shader. That said, it would be possible to write a CustomMaterial subclass or similar that did allow you to write Shader.
Image could work in a similar way, or perhaps even subclass Material too.September 7, 2017 at 8:15 am #10302
Thanks for information!
There is what I do: http://monkeycoder.co.nz/forums/topic/image-with-texture-mask/
Maybe there is built-in support for this and I missed it.
- _image.Material.SetTexture( “myTex”,_tex )
You must be logged in to reply to this topic.