scene.monkey2 7.8 KB

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