renderqueue.monkey2 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. Namespace mojo3d
  2. Internal
  3. 'TODO: cache this stuff
  4. Class RenderInstance
  5. Field instance:Entity
  6. Field uniforms:UniformBlock
  7. Field bones:Mat4f
  8. End
  9. 'TODO: cache this too
  10. Class RenderGeometry
  11. Field material:Material
  12. Field vbuffer:VertexBuffer
  13. Field ibuffer:IndexBuffer
  14. Field bounds:Boxf
  15. Field order:Int
  16. Field count:Int
  17. Field first:Int
  18. End
  19. Struct SpriteOp
  20. Field sprite:Sprite
  21. Field distance:Float
  22. End
  23. Class RenderOp
  24. Field material:Material
  25. Field uniforms:UniformBlock 'could be in instance?
  26. Field instance:Entity
  27. Field bones:Mat4f[] 'should be in instance
  28. Field vbuffer:VertexBuffer
  29. Field ibuffer:IndexBuffer
  30. Field bounds:Boxf
  31. Field order:Int
  32. Field count:Int
  33. Field first:Int
  34. Field blendMode:BlendMode
  35. Field distance:Float
  36. Field shader:Shader
  37. End
  38. Public
  39. Class RenderQueue
  40. Property Time:Float()
  41. Return _time
  42. Setter( time:Float )
  43. _time=time
  44. End
  45. Property EyePos:Vec3f()
  46. Return _eyePos
  47. Setter( eyePos:Vec3f )
  48. _eyePos=eyePos
  49. _eyeLen=_eyePos.Length
  50. End
  51. Property AddShadowOps:Bool()
  52. Return _addShadowOps
  53. Setter( addShadowOps:Bool )
  54. _addShadowOps=addShadowOps
  55. End
  56. Property OpaqueOps:Stack<RenderOp>()
  57. Return _opaqueOps
  58. End
  59. Property SelfIlluminatedOps:Stack<RenderOp>()
  60. Return _selfillumOps
  61. End
  62. Property TransparentOps:Stack<RenderOp>()
  63. Return _transparentOps
  64. End
  65. Property ShadowOps:Stack<RenderOp>()
  66. Return _shadowOps
  67. End
  68. Property SpriteOps:Stack<SpriteOp>()
  69. Return _spriteOps
  70. End
  71. Method Clear()
  72. _opaqueOps.Clear()
  73. _selfillumOps.Clear()
  74. _transparentOps.Clear()
  75. _shadowOps.Clear()
  76. _spriteOps.Clear()
  77. End
  78. Method AddRenderOp( op:RenderOp )
  79. op.blendMode=op.material.BlendMode
  80. If op.instance And op.instance.Alpha<1 And op.blendMode=BlendMode.Opaque op.blendMode=BlendMode.Alpha
  81. Local stack:Stack<RenderOp>
  82. If op.blendMode=BlendMode.Opaque
  83. stack=_opaqueOps
  84. op.shader=op.material.GetRenderShader()
  85. If _addShadowOps _shadowOps.Add( op )
  86. Else
  87. stack=_transparentOps
  88. op.shader=op.material.GetRenderShader()
  89. If op.instance
  90. op.distance=(op.instance.Matrix * op.bounds.Center).Distance( _eyePos )
  91. Else
  92. op.distance=op.bounds.Center.Distance( _eyePos )
  93. Endif
  94. Endif
  95. stack.Add( op )
  96. End
  97. Method AddSpriteOp( op:SpriteOp )
  98. op.distance=op.sprite ? op.sprite.Position.Distance( _eyePos ) Else _eyeLen
  99. _spriteOps.Add( op )
  100. End
  101. Method AddRenderOp( material:Material,uniforms:UniformBlock,instance:Entity,vbuffer:VertexBuffer,ibuffer:IndexBuffer,bounds:Boxf,order:Int,count:Int,first:Int )
  102. Local op:=New RenderOp
  103. op.material=material
  104. op.uniforms=uniforms
  105. op.instance=instance
  106. op.vbuffer=vbuffer
  107. op.ibuffer=ibuffer
  108. op.bounds=bounds
  109. op.order=order
  110. op.count=count
  111. op.first=first
  112. AddRenderOp( op )
  113. End
  114. Method AddRenderOp( material:Material,instance:Entity,vbuffer:VertexBuffer,ibuffer:IndexBuffer,bounds:Boxf,order:Int,count:Int,first:Int )
  115. Local op:=New RenderOp
  116. op.material=material
  117. op.instance=instance
  118. op.vbuffer=vbuffer
  119. op.ibuffer=ibuffer
  120. op.bounds=bounds
  121. op.order=order
  122. op.count=count
  123. op.first=first
  124. AddRenderOp( op )
  125. End
  126. Method AddRenderOp( material:Material,bones:Mat4f[],vbuffer:VertexBuffer,ibuffer:IndexBuffer,bounds:Boxf,order:Int,count:Int,first:Int )
  127. Local op:=New RenderOp
  128. op.material=material
  129. op.bones=bones
  130. op.vbuffer=vbuffer
  131. op.ibuffer=ibuffer
  132. op.bounds=bounds
  133. op.order=order
  134. op.count=count
  135. op.first=first
  136. AddRenderOp( op )
  137. End
  138. Method AddRenderOp( material:Material,vbuffer:VertexBuffer,ibuffer:IndexBuffer,bounds:Boxf,order:Int,count:Int,first:Int )
  139. Local op:=New RenderOp
  140. op.material=material
  141. op.vbuffer=vbuffer
  142. op.ibuffer=ibuffer
  143. op.bounds=bounds
  144. op.order=order
  145. op.count=count
  146. op.first=first
  147. AddRenderOp( op )
  148. End
  149. Method AddSpriteOp( sprite:Sprite )
  150. DebugAssert( sprite.Material.GetRenderShader(),"Sprites must be transparent!" )
  151. Local op:=New SpriteOp
  152. op.sprite=sprite
  153. AddSpriteOp( op )
  154. End
  155. Private
  156. Field _time:float
  157. Field _eyePos:Vec3f
  158. Field _eyeLen:Float
  159. Field _addShadowOps:Bool
  160. Field _opaqueOps:=New Stack<RenderOp>
  161. Field _selfillumOps:=New Stack<RenderOp>
  162. Field _transparentOps:=New Stack<RenderOp>
  163. Field _shadowOps:=New Stack<RenderOp>
  164. Field _spriteOps:=New Stack<SpriteOp>
  165. End