Shader power beam

Jimmy

Active member
3rd Party Module Dev
Tutorial Author
Joined
Jan 2, 2020
Messages
921
Power beam shader. Just a simple world-overtaking-power beam.

It uses 5 LOC to create the actual visual effect. Look into the glsl file and play around with it.


Screenshot.png


powerbeam.cxs
Cerberus:
Import mojo2
Function Main()
    New MyApp
End

Class MyApp Extends App

    Field sourceImage : Image
    Field targetImage : Image
    Field canvas : Canvas
    Field effect : ShaderEffect
    Field value : Float
    Field timer:Float = 0
  
    Method OnCreate()
        sourceImage = Image.Load("graphics2.png",0,0,0)
        targetImage = New Image(sourceImage.Width,sourceImage.Height)
        effect = New ShaderEffect
        canvas = New Canvas
          SetUpdateRate 0
    End

    Method OnUpdate:Int()
         timer = Millisecs()/2000.0
'         timer = MouseX()/100.0
        Return 0
    End

    Method OnRender()
        canvas.Clear 0, 0.5, 1, 1
        effect.SetValue value
        value = value + 0.01     
        effect.SetValue timer
        effect.Render sourceImage, sourceImage,targetImage
        canvas.DrawRect 0,0, 256*1,256*1,targetImage,0,0,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.SetScalar "Timer",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 SetValue:Void( value:Float )
        _material.SetScalar "Timer",value
    End

    Method Render:Void(source:Image,source2:Image,target:Image)
        _material.SetTexture "ColorTexture",source.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

shader.glsl
Code:
uniform sampler2D ColorTexture;
uniform float Timer;

void shader(){
// -------------------------------------------------------------------------------------------------
    // Get coordinate
    vec2 uv=(b3d_ClipPosition.st/b3d_ClipPosition.w)*0.5+0.5 ;

    // y = mod(x,0.5); // return x modulo of 0.5
    // y = fract(x); // return only the fraction part of a number

    // Draw power beam
    float bg = (cos(uv.x * 3.14159 * 2.0) + sin((uv.y) * 3.14159)) * 0.016 ;
    vec2 p = (uv * 2.0 - 1.0) * 150.0 ;
    vec2 ss = vec2(p.x, p.y + 5.0 * sin(uv.x * 1000.0 - Timer * 2.0 + cos(Timer * 7.0) ) + 2.0 * cos(uv.x * 24.0 + Timer * 4.0)) ;
    ss.y = ss.y * (uv.x * 2.0 + 0.04) * (2.0 - uv.x + 2.0 + 0.04) * 10.0;
    vec3 c = pow(vec3(abs(ss.y)), vec3(-0.4)) * vec3(0.3,1.0,0.4) ;

    b3d_FragColor = vec4(c,1.0) ;
// -------------------------------------------------------------------------------------------------
}
 

Attachments

  • shader.zip
    34.5 KB · Views: 24
Last edited:

MikeHart

Administrator
Joined
Jun 19, 2017
Messages
3,167
I see no license here to this shader code. Can you add one But? But it looks nice
 

Phil7

Administrator
CX Code Contributor
3rd Party Tool Dev
Joined
Jun 26, 2017
Messages
643
I think you need to add this to the top of the license:

Copyright (c) <year> <copyright holders>
 
Top Bottom