scene.monkey2 9.1 KB

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