godrayseffect.monkey2 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. Namespace mojo3d
  2. #rem monkeydoc The MonochromeEffect class.
  3. #end
  4. Class GodraysEffect Extends PostEffect
  5. #rem monkeydoc Creates a new monochrome effect shader.
  6. #end
  7. Method New( light:Light=Null )
  8. _shader=Shader.Open( "effects/godrays" )
  9. _uniforms=New UniformBlock( 3 )
  10. Light=light
  11. NumSamples=100
  12. Exposure=.0034
  13. Decay=1.0
  14. Density=0.84
  15. Color=Color.White
  16. Weight=1
  17. End
  18. Property Light:Light()
  19. Return _light
  20. Setter( light:Light )
  21. _light=light
  22. End
  23. Property NumSamples:Int()
  24. Return _uniforms.GetInt( "NumSamples" )
  25. Setter( samples:Int )
  26. _uniforms.SetInt( "NumSamples",samples )
  27. End
  28. Property Exposure:Float()
  29. Return _uniforms.GetFloat( "Exposure" )
  30. Setter( exposure:Float )
  31. _uniforms.SetFloat( "Exposure",exposure )
  32. End
  33. Property Decay:Float()
  34. Return _uniforms.GetFloat( "Decay" )
  35. Setter( decay:Float )
  36. _uniforms.SetFloat( "Decay",decay )
  37. End
  38. Property Density:Float()
  39. Return _uniforms.GetFloat( "Density" )
  40. Setter( density:Float )
  41. _uniforms.SetFloat( "Density",density )
  42. End
  43. Property Color:Color()
  44. Return _uniforms.GetColor( "Color" )
  45. Setter( color:Color )
  46. _uniforms.SetColor( "Color",color )
  47. End
  48. Property Weight:Float()
  49. Return _uniforms.GetFloat( "Weight" )
  50. Setter( weight:Float )
  51. _uniforms.SetFloat( "Weight",weight )
  52. End
  53. Protected
  54. Method OnRender( target:RenderTarget,viewport:Recti ) Override
  55. If Not _light Return
  56. 'computer light pos in buffer coords
  57. Local viewProj:=Uniforms.GetMat4f( "ViewProjectionMatrix" )
  58. Local lightClipPos:=viewProj * New Vec4f( -_light.Basis.k,0 ) 'clip
  59. If lightClipPos.w<=0 return
  60. ' If lightClipPos.x<-lightClipPos.w Or lightClipPos.x>lightClipPos.w Return
  61. ' If lightClipPos.y<-lightClipPos.w Or lightClipPos.y>lightClipPos.w Return
  62. If lightClipPos.z<-lightClipPos.w Return 'Or lightClipPos.z>lightClipPos.w Return
  63. Local lightPos:=lightClipPos.XY/lightClipPos.w * 0.5 + 0.5 'NDC
  64. Local bcscale:=Uniforms.GetVec2f( "BufferCoordScale" )
  65. ' Print "SourceBufferSize="+SourceBufferSize+" SourceBufferScale="+SourceBufferScale
  66. lightPos*=bcscale
  67. 'set effect uniforms
  68. _uniforms.SetVec2f( "LightPosBufferCoords",lightPos )
  69. 'render!
  70. #rem
  71. Local size:=viewport.Size
  72. Local source:=target.GetColorTexture( 0 )
  73. If Not _target Or size.x>_target.Size.x Or size.y>_target.Size.y
  74. _target=CreateRenderTarget( size,source.Format,TextureFlags.Dynamic )
  75. End
  76. Super.SetRenderTarget( _target,New Recti( 0,0,size ) )
  77. #end
  78. Device.Shader=_shader
  79. Device.BindUniformBlock( _uniforms )
  80. Device.BlendMode=BlendMode.Additive
  81. RenderQuad()
  82. End
  83. Private
  84. Field _light:Light
  85. Field _color:Color
  86. Field _weight:Float
  87. Field _shader:Shader
  88. Field _uniforms:UniformBlock
  89. Field _target:RenderTarget
  90. End