Shader with multiple source-textures

Jimmy

Active member
3rd Party Module Dev
Tutorial Author
Joined
Jan 2, 2020
Messages
576
Here's an example how to read multiple textures and write to one.
This simple example creates a 50% mix of two textures.

It allows many creative possibilities, one example which is my personal interest in this is to use this technique to
create retro palettes, where you map certain palettes to other ones in realtime-time (essentially the second texture becomes an efficient look-up table).

code_language.cerberus:
Import mojo2
Function Main()
    New MyApp
End

Class MyApp Extends App

    Global image2:Image
    Global imagecanvas2:Canvas
    Global image3:Image
    Global imagecanvas3:Canvas
    Field sourceImage : Image
    Field sourceImage2 : Image
    Field targetImage : Image
    Field canvas : Canvas
    Field effect : ShaderEffect

    Method OnCreate()
        sourceImage = Image.Load("graphics.png")
        sourceImage2 = Image.Load("graphics2.png")
        targetImage = New Image(sourceImage.Width,sourceImage.Height)
        effect = New ShaderEffect
        canvas = New Canvas
        image2=New Image(256,256,1,1)
         imagecanvas2=New Canvas(image2)
             image3=New Image(256,256,1,1)
         imagecanvas3=New Canvas(image3)
         SetUpdateRate 0
    End

    Method OnRender()
  
        ' effect.SetLevel 0.5
        effect.Render sourceImage, sourceImage2,image2
        canvas.DrawImage image2,256,256
        
        ' effect.Render image3,image2
        canvas.Clear 0, 0.5, 1, 1
        canvas.SetBlendMode BlendMode.Alpha
                
        canvas.DrawImage image2,256,256
        canvas.Flush

    End

End

Class myShader Extends Shader

    Method New()
        Build(LoadString("shader.glsl"))
    End

    Method OnInitMaterial:Void(material:Material)
        material.SetTexture "ColorTexture",Texture.White()
        material.SetTexture "ColorTexture2",Texture.White()
        material.SetScalar "EffectLevel",1
    End

    Function Instance:myShader()
        If Not _instance _instance=New myShader
        Return _instance
    End

    Private

    Global _instance:myShader
End

Class ShaderEffect

    Private
    Global _canvas:Canvas
    Field _material:Material

    Method New()
        If Not _canvas _canvas=New Canvas
        _material=New Material(myShader.Instance())
    End

    Method SetLevel:Void(level:Float)
        _material.SetScalar "EffectLevel",level
    End

    Method Render:Void(source:Image,source2:Image,target:Image)
        _material.SetTexture "ColorTexture",source.Material.ColorTexture
        _material.SetTexture "ColorTexture2",source2.Material.ColorTexture
        _canvas.Clear 0, 0, 0, 0
        _canvas.SetRenderTarget target
        _canvas.SetViewport 0,0,target.Width,target.Height
        _canvas.SetProjection2d 0,target.Width,0,target.Height
        _canvas.DrawRect 0,0,target.Width,target.Height,_material
        _canvas.Flush
    End

End
 

Attachments

  • multipletexturesinsameshader.zip
    60.1 KB · Views: 11
Last edited:
Top Bottom