About Monkey 2 › Forums › Monkey 2 Programming Help › Teaching myself GLSL shaders
This topic contains 0 replies, has 1 voice, and was last updated by
Ethernaut
1 year, 7 months ago.
-
AuthorPosts
-
September 16, 2017 at 7:39 am #10517
So I finally started learning GLSL. The best resource I’ve found so far is this (still incomplete) amazing interactive book: https://thebookofshaders.com
I’ve been converting the examples in the book to work with Monkey2:
https://github.com/DoctorWhoof/Mojo-Shader-TestsIt started pretty well, but I stopped in example #7 when I tried to make a shader that works in 3D. I get no errors, but nothing displays either… switching the material to a PbrMaterial displays fine, so it’s definitely either the shader or the TestMaterial class. I was hoping for a very simple “unlit” shader just to begin with. What am I missing?
My second question is: where can I find any reference on what the Mojo’s render passes are? I’m ignoring them for now… maybe that’s where the problem is?
Here’s the M2 code:
Monkey123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293#Import "<std>"#Import "<mojo>"#Import "<mojo3d>"#Import "shaders/test07_3d.glsl"Using std..Using mojo..Using mojo3d..Class MyWindow Extends WindowField img :ImageField testShader :ShaderField scene:SceneField cube:ModelField camera:CameraField light:LightMethod New()Super.New( "Shader test",1024,600,WindowFlags.Resizable )scene = Scene.GetCurrent()scene.ClearColor = Color.DarkGreyLocal testShader := New Shader( "test06", LoadString("asset::test07_3d.glsl"), "" )img = New Image( 512, 512, TextureFlags.FilterMipmap, testShader )img.Handle = New Vec2f( 0.5 )Local mat := New TestMaterial( testShader )' Local mat := New PbrMaterial( Color.Red )cube = Model.CreateBox( New Boxf(-1,-1,-1,1,1,1), 1, 1, 1, mat )camera = New Cameracamera.Move( 0, 5, -5 )camera.PointAt( cube )camera.FOV = 45light=New Lightlight.Rotate( 30, 60, 0 )EndMethod OnRender( canvas:Canvas ) OverrideApp.RequestRender()cube.Rotate( 0, .5, 0 )scene.Render( canvas, camera )EndEndFunction Main()New AppInstanceNew MyWindowApp.Run()End'********************************* Materials *********************************Class TestMaterial Extends MaterialProperty ColorTexture:Texture()Return Uniforms.GetTexture( "ColorTexture" )Setter( texture:Texture )Uniforms.SetTexture( "ColorTexture",texture )EndProperty ColorFactor:Color()Return Uniforms.GetColor( "ColorFactor" )Setter( color:Color )Uniforms.SetColor( "ColorFactor",color )EndMethod New( shader:Shader )Super.New( shader )BlendMode=BlendMode.AlphaCullMode=CullMode.NoneColorTexture=Texture.ColorTexture( Color.White )ColorFactor = Color.WhiteEndMethod New( material:TestMaterial )Super.New( material )EndMethod Copy:TestMaterial() OverrideReturn New TestMaterial( Self )EndEndAnd here’s the shader. I made the color a simple red just to test it.
C123456789101112131415161718192021222324252627282930//@renderpasses 0varying vec4 v_Color;varying vec2 v_TexCoord0;//@vertexattribute vec4 a_Position;attribute vec2 a_TexCoord0;attribute vec4 a_Color;uniform mat4 r_ModelViewProjectionMatrix;void main(){v_TexCoord0 = a_TexCoord0;v_Color = a_Color;gl_Position=r_ModelViewProjectionMatrix * a_Position;}//@fragmentuniform sampler2D m_ColorTexture;uniform vec4 m_ColorFactor;uniform float m_Time;void main(){// vec3 color=pow( texture2D( m_ColorTexture,v_TexCoord0 ).rgb,vec3( 2.2 ) ) * m_ColorFactor.rgb;vec3 color = vec3( 1.0, 0.0, 0.0 ) * v_Color;float alpha=texture2D( m_ColorTexture, v_TexCoord0 ).a * m_ColorFactor.a;gl_FragColor=vec4( color * alpha, alpha );} -
AuthorPosts
You must be logged in to reply to this topic.