scene.monkey2 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  1. Namespace mojo3d
  2. #rem monkeydoc The Scene class.
  3. #end
  4. Class Scene
  5. #rem monkeydoc Creates a new scene.
  6. If there is no current scene when a new scene is created, the new scene becomes the current scene.
  7. #end
  8. Method New( editable:Bool=False )
  9. If Not _current _current=Self
  10. _editable=editable And TypeInfo.GetType( "mojo3d.Scene" )<>Null
  11. _clearColor=Color.Sky
  12. _skyColor=Color.White
  13. _ambientDiffuse=Color.DarkGrey
  14. _envColor=Color.White
  15. _world=New World( Self )
  16. If _editable
  17. _jsonifier=New Jsonifier
  18. _jsonifier.AddInstance( Self,New Variant[]( true ) )
  19. _editing=True
  20. Endif
  21. End
  22. Property World:World()
  23. Return _world
  24. End
  25. #rem monkeydoc The sky texture.
  26. The sky texture is used to clear the scene.
  27. If there is no sky texture, the clear color is used instead.
  28. This must currently be a valid cubemap texture.
  29. #end
  30. [jsonify=1]
  31. Property SkyTexture:Texture()
  32. Return _skyTexture
  33. Setter( texture:Texture )
  34. _skyTexture=texture
  35. End
  36. #rem monkeydoc The sky color.
  37. The sky color is used to modulate the sky texture.
  38. Sky color is only used if there is also a sky texture.
  39. #end
  40. [jsonify=1]
  41. Property SkyColor:Color()
  42. Return _skyColor
  43. Setter( color:Color )
  44. _skyColor=color
  45. End
  46. #rem monkeydoc The environment texture.
  47. The environment textures is used to render specular reflections within the scene.
  48. If there is no environment texture, the sky texture is used instead.
  49. If there is no environment texture and no sky texture, a default internal environment texture is used.
  50. This must currently be a valid cubemap texture.
  51. #end
  52. [jsonify=1]
  53. Property EnvTexture:Texture()
  54. Return _envTexture
  55. Setter( texture:Texture )
  56. _envTexture=texture
  57. End
  58. #rem monkey The environment color.
  59. #end
  60. [jsonify=1]
  61. Property EnvColor:Color()
  62. Return _envColor
  63. Setter( color:Color )
  64. _envColor=color
  65. End
  66. #rem monkeydoc The clear color.
  67. The clear color is used to clear the scene.
  68. The clear color is only used if there is no sky texture.
  69. #end
  70. [jsonify=1]
  71. Property ClearColor:Color()
  72. Return _clearColor
  73. Setter( color:Color )
  74. _clearColor=color
  75. End
  76. [jsonify=1]
  77. Property FogColor:Color()
  78. Return _fogColor
  79. Setter( color:Color )
  80. _fogColor=color
  81. End
  82. [jsonify=1]
  83. Property FogNear:Float()
  84. Return _fogNear
  85. Setter( near:Float )
  86. _fogNear=near
  87. End
  88. [jsonify=1]
  89. Property FogFar:Float()
  90. Return _fogFar
  91. Setter( far:Float )
  92. _fogFar=far
  93. End
  94. [jsonify=1]
  95. Property ShadowAlpha:Float()
  96. Return _shadowAlpha
  97. Setter( alpha:Float )
  98. _shadowAlpha=alpha
  99. End
  100. #rem monkeydoc Update rate.
  101. #end
  102. [jsonify=1]
  103. Property UpdateRate:Float()
  104. Return _updateRate
  105. Setter( updateRate:Float )
  106. _updateRate=updateRate
  107. End
  108. [jsonify=1]
  109. #rem monkeydoc Number of update steps.
  110. #end
  111. Property MaxSubSteps:Int()
  112. Return _maxSubSteps
  113. Setter( maxSubSteps:Int )
  114. _maxSubSteps=maxSubSteps
  115. End
  116. #rem monkeydoc Ambient diffuse lighting.
  117. #end
  118. [jsonify=1]
  119. Property AmbientLight:Color()
  120. Return _ambientDiffuse
  121. Setter( color:Color )
  122. _ambientDiffuse=color
  123. End
  124. #rem monkeydoc Array containing the cascaded shadow map frustum splits for directional light shadows.
  125. Defaults to Float[]( 8.0,16.0,64.0,256.0 )
  126. Must have length 4.
  127. #end
  128. [jsonify=1]
  129. Property CSMSplits:Float[]()
  130. Return _csmSplits
  131. Setter( splits:Float[] )
  132. Assert( splits.Length=4,"CSMSplits array must have 4 elements" )
  133. _csmSplits=splits.Slice( 0 )
  134. End
  135. #rem monkeydoc Finds an entity in the scene.
  136. Finds an entity in the scene with the given name.
  137. #end
  138. Method FindEntity:Entity( name:String )
  139. For Local entity:=Eachin _rootEntities
  140. Local found:=entity.Find( name )
  141. If found Return found
  142. Next
  143. Return Null
  144. End
  145. #rem monkeydoc Adds a post effect to the scene.
  146. #end
  147. Method AddPostEffect( postEffect:PostEffect )
  148. _postEffects.Add( postEffect )
  149. End
  150. #rem monkeydoc Removes a post effect from the scene
  151. #end
  152. Method RemovePostEffect( postEffect:PostEffect )
  153. _postEffects.Remove( postEffect )
  154. End
  155. #rem monkeydocs Get all post effect that have been added to the scene
  156. #end
  157. Method GetPostEffects:PostEffect[]()
  158. Return _postEffects.ToArray()
  159. End
  160. #rem monkeydoc Destroys all entities in the scene.
  161. #end
  162. Method DestroyAllEntities()
  163. While Not _rootEntities.Empty
  164. _rootEntities.Top.Destroy()
  165. Wend
  166. End
  167. #rem monkeydoc Updates the scene.
  168. #end
  169. Method Update()
  170. Global time:=0.0
  171. Local elapsed:=0.0
  172. If time
  173. elapsed=Now()-time
  174. time+=elapsed
  175. Else
  176. time=Now()
  177. Endif
  178. Update( elapsed )
  179. End
  180. #rem monkeydoc Renders the scene to a canvas.
  181. #end
  182. Method Render( canvas:Canvas )
  183. For Local camera:=Eachin _cameras
  184. camera.Render( canvas )
  185. Next
  186. End
  187. Method RayCast:RayCastResult( rayFrom:Vec3f,rayTo:Vec3f,collisionMask:Int )
  188. Return _world.RayCast( rayFrom,rayTo,collisionMask )
  189. End
  190. #rem monkeydoc Enumerates all entities in the scene with null parents.
  191. #end
  192. Method GetRootEntities:Entity[]()
  193. Return _rootEntities.ToArray()
  194. End
  195. '***** serialization stuff *****
  196. Property Editable:Bool()
  197. Return _editable
  198. End
  199. Property Editing:Bool()
  200. Return _editing
  201. Setter( editing:Bool )
  202. If editing And Not _editable RuntimeError( "Scene is not editable" )
  203. _editing=editing
  204. End
  205. Property Jsonifier:Jsonifier()
  206. Return _jsonifier
  207. End
  208. Method LoadTexture:Texture( path:String,flags:TextureFlags=TextureFlags.FilterMipmap,flipNormalY:Bool=False )
  209. Local texture:=Texture.Load( path,flags,flipNormalY )
  210. If Not texture Return Null
  211. If Editing Jsonifier.AddInstance( texture,"mojo3d.Scene.LoadTexture",Self,New Variant[]( path,flags,flipNormalY ) )
  212. Return texture
  213. End
  214. #rem monkeydoc Saves the scene to a mojo3d scene file
  215. #end
  216. Method Save( path:String,assetsDir:String="" )
  217. Assert( _jsonifier,"Scene is not editable" )
  218. Local jobj:=_jsonifier.JsonifyInstances( assetsDir )
  219. Local json:=jobj.ToJson()
  220. SaveString( json,path )
  221. End
  222. #rem monkeydoc Loads a mojo3d scene file and makes it current
  223. #end
  224. Function Load:Scene( path:String )
  225. Local json:=LoadString( path )
  226. If Not json Return Null
  227. Local jobj:=JsonObject.Parse( json )
  228. If Not jobj Return Null
  229. Local scene:=New Scene( True )
  230. SetCurrent( scene )
  231. scene.Jsonifier.DejsonifyInstances( jobj )
  232. Return scene
  233. End
  234. #rem monkeydoc Sets the current scene.
  235. All newly created entities (including entites created using Entity.Copy]]) are automatically added to the current scene.
  236. #end
  237. Function SetCurrent( scene:Scene )
  238. _current=scene
  239. End
  240. #rem monkeydoc Gets the current scene.
  241. If there is no current scene, a new scene is automatically created and made current.
  242. #end
  243. Function GetCurrent:Scene()
  244. If Not _current New Scene
  245. Return _current
  246. End
  247. Internal
  248. Property PostEffects:Stack<PostEffect>()
  249. Return _postEffects
  250. End
  251. Property RootEntities:Stack<Entity>()
  252. Return _rootEntities
  253. End
  254. Property Cameras:Stack<Camera>()
  255. Return _cameras
  256. End
  257. Property Lights:Stack<Light>()
  258. Return _lights
  259. End
  260. Property Renderables:Stack<Renderable>()
  261. Return _renderables
  262. End
  263. Private
  264. Global _current:Scene
  265. Global _defaultEnv:Texture
  266. Field _skyTexture:Texture
  267. Field _skyColor:Color
  268. Field _envTexture:Texture
  269. Field _envColor:Color
  270. Field _clearColor:Color
  271. Field _ambientDiffuse:Color
  272. Field _fogColor:Color
  273. Field _fogNear:Float
  274. Field _fogFar:Float
  275. Field _shadowAlpha:Float=1
  276. Field _updateRate:Float=60
  277. Field _maxSubSteps:Int=1
  278. Field _csmSplits:=New Float[]( 8.0,16.0,64.0,256.0 )
  279. Field _rootEntities:=New Stack<Entity>
  280. Field _cameras:=New Stack<Camera>
  281. Field _lights:=New Stack<Light>
  282. Field _renderables:=New Stack<Renderable>()
  283. Field _postEffects:=New Stack<PostEffect>
  284. Field _world:World
  285. Field _jsonifier:Jsonifier
  286. Field _editable:Bool
  287. Field _editing:Bool
  288. Method Update( elapsed:Float )
  289. For Local e:=Eachin _rootEntities
  290. e.BeginUpdate()
  291. Next
  292. _world.Update( elapsed )
  293. For Local e:=Eachin _rootEntities
  294. e.Update( elapsed )
  295. Next
  296. End
  297. End