ProcSky.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  1. // Copyright (c) 2014-2015, THUNDERBEAST GAMES LLC All rights reserved
  2. // Please see LICENSE.md in repository root for license information
  3. // https://github.com/AtomicGameEngine/AtomicGameEngine
  4. #include "Precompiled.h"
  5. #include "../Core/Context.h"
  6. #include "../Graphics/Graphics.h"
  7. #include "../Graphics/GraphicsEvents.h"
  8. #include "../Graphics/View.h"
  9. #include "../Scene/Scene.h"
  10. #include "../Graphics/Camera.h"
  11. #include "../Scene/SceneEvents.h"
  12. #include "../Graphics/Light.h"
  13. #include "../Resource/ResourceCache.h"
  14. #include "../Graphics/Technique.h"
  15. #include "../Environment/ProcSky.h"
  16. #include "../Graphics/Renderer.h"
  17. #if defined(_MSC_VER)
  18. #include "stdint.h"
  19. #endif
  20. #if defined(EMSCRIPTEN) || defined(ATOMIC_PLATFORM_LINUX)
  21. #include <stdint.h>
  22. #endif
  23. namespace Atomic
  24. {
  25. extern const char* GEOMETRY_CATEGORY;
  26. float ProcSky::timeOfDay_ = 0.0f;
  27. ProcSky::ProcSky(Context* context) :
  28. Drawable(context, DRAWABLE_GEOMETRY),
  29. geometry_(new Geometry(context)),
  30. vertexBuffer_(new VertexBuffer(context_)),
  31. indexBuffer_(new IndexBuffer(context_)),
  32. autoUpdate_(false),
  33. dayTime_(0.0f),
  34. lastDayTimeUpdate_(-1.0f),
  35. shadowFade_(1.0f),
  36. customWorldTransform_(Matrix3x4::IDENTITY),
  37. initialized_(false),
  38. flipped_(false)
  39. {
  40. }
  41. ProcSky::~ProcSky()
  42. {
  43. }
  44. void ProcSky::RegisterObject(Context* context)
  45. {
  46. context->RegisterFactory<ProcSky>(GEOMETRY_CATEGORY);
  47. COPY_BASE_ATTRIBUTES(Drawable);
  48. }
  49. void ProcSky::UpdateBatches(const FrameInfo& frame)
  50. {
  51. if (!initialized_)
  52. Initialize();
  53. distance_ = 0.0f;
  54. // Follow only the camera rotation, not position
  55. Matrix3x4 customView(Vector3::ZERO, frame.camera_->GetNode()->GetWorldRotation().Inverse(), Vector3::ONE);
  56. customWorldTransform_ = customView * node_->GetWorldTransform();
  57. for (unsigned i = 0; i < batches_.Size(); ++i)
  58. {
  59. batches_[i].worldTransform_ = &customWorldTransform_;
  60. batches_[i].numWorldTransforms_ = 1;
  61. batches_[i].distance_ = 0.0f;
  62. }
  63. }
  64. void ProcSky::UpdateGeometry(const FrameInfo& frame)
  65. {
  66. if (!initialized_)
  67. Initialize();
  68. if ( indexBuffer_->IsDataLost())
  69. UpdateIndexBuffer();
  70. UpdateVertexBuffer(frame);
  71. }
  72. UpdateGeometryType ProcSky::GetUpdateGeometryType()
  73. {
  74. // updates vertex buffer per frame
  75. return UPDATE_MAIN_THREAD;
  76. }
  77. void ProcSky::OnWorldBoundingBoxUpdate()
  78. {
  79. // The skybox is supposed to be visible everywhere, so set a humongous bounding box
  80. worldBoundingBox_.Define(-M_LARGE_VALUE, M_LARGE_VALUE);
  81. }
  82. void ProcSky::OnNodeSet(Node* node)
  83. {
  84. Drawable::OnNodeSet(node);
  85. if (node && node->GetScene())
  86. {
  87. SubscribeToEvent(node->GetScene(), E_SCENEUPDATE, HANDLER(ProcSky, HandleSceneUpdate));
  88. SubscribeToEvent(E_BEGINVIEWUPDATE, HANDLER(ProcSky, HandleBeginViewUpdate));
  89. }
  90. }
  91. void ProcSky::HandleBeginViewUpdate(StringHash eventType, VariantMap& eventData)
  92. {
  93. #ifdef ATOMIC_OPENGL
  94. flipped_ = false;
  95. View* view = static_cast<View*>(eventData[BeginViewUpdate::P_VIEW].GetPtr());
  96. if (view && view->GetRenderTarget())
  97. flipped_ = true;
  98. #endif
  99. }
  100. void ProcSky::HandleSceneUpdate(StringHash eventType, VariantMap& eventData)
  101. {
  102. using namespace SceneUpdate;
  103. if (!initialized_)
  104. Initialize();
  105. float timeStep = eventData[P_TIMESTEP].GetFloat();
  106. if (autoUpdate_)
  107. {
  108. dayTime_ += timeStep * .1f;
  109. SetDayTime(dayTime_);
  110. }
  111. }
  112. float ProcSky::SetDayTime(float time)
  113. {
  114. if (!initialized_)
  115. Initialize();
  116. sunAngle_ = (time+0.5f) * M_PI;
  117. int degrees = int(sunAngle_ * M_RADTODEG);
  118. degrees %= 360;
  119. timeOfDay_ = ((degrees + 90) % 360) / 15.0f;
  120. if (degrees >= 150 || degrees <= 15)
  121. {
  122. // fade out
  123. if (shadowFade_ < 0.0f || shadowFade_ != 1.0f)
  124. {
  125. shadowFade_ += .05f;
  126. if (shadowFade_ > 1.0f)
  127. shadowFade_ = 1.0f;
  128. sunlight_->SetShadowIntensity(shadowFade_);
  129. if (!sunlight_->GetCastShadows())
  130. sunlight_->SetCastShadows(true);
  131. }
  132. else
  133. {
  134. if (sunlight_->GetCastShadows())
  135. sunlight_->SetCastShadows(false);
  136. }
  137. }
  138. else
  139. {
  140. // fade in
  141. if (shadowFade_ < 0.0f || shadowFade_ != 0.0f)
  142. {
  143. shadowFade_ -= .05f;
  144. if (shadowFade_ < 0.0f)
  145. shadowFade_ = 0.0f;
  146. sunlight_->SetShadowIntensity(shadowFade_);
  147. if (!sunlight_->GetCastShadows())
  148. sunlight_->SetCastShadows(true);
  149. }
  150. else
  151. {
  152. if (!sunlight_->GetCastShadows())
  153. sunlight_->SetCastShadows(true);
  154. }
  155. }
  156. Quaternion rot(degrees, Vector3::RIGHT);
  157. node_->SetRotation(rot);
  158. float daylight = cosf( time * M_PI ) * 0.8f + 0.4f;
  159. if( daylight > 1.0f )
  160. daylight = 1.0f;
  161. if( daylight < 0.0f )
  162. daylight = 0.0f;
  163. daylight = sqrtf( daylight );
  164. float dawnGlow = sinf( time * M_PI * 1.0f );
  165. int glowX = dawnGlow > 0.0f ? 0 : 7;
  166. dawnGlow = Atomic::Max( 0.0f, fabs( dawnGlow * dawnGlow * dawnGlow ) * 1.1f - 0.1f );
  167. Color top = Color(0,0,0,1).Lerp(topColor_, daylight);
  168. Color horiz = Color(.1,.1,.1,1).Lerp(horizColor_, daylight);
  169. uint8_t skytex[32 * 8 * 4 ];
  170. uint8_t* p;
  171. // bottom row is black
  172. p = &skytex[31*8*4];
  173. for( uint32_t x = 0; x < 8; x++ )
  174. {
  175. *p++ = 0;
  176. *p++ = 0;
  177. *p++ = 0;
  178. *p++ = 255;
  179. }
  180. p = skytex;
  181. for( uint32_t z = 0; z < 31; z++ )
  182. {
  183. for( uint32_t x = 0; x < 8; x++ )
  184. {
  185. float xGlow = Atomic::Max( 0, -abs((int)x-glowX) + 5 ) / 5.0f;
  186. float hGlow = z/31.0f;
  187. float glow = (dawnGlow * hGlow * hGlow * sqrtf( xGlow ));
  188. Color baseColor = top.Lerp(horiz, Atomic::Min( 1.0f, float (z*11) / 255.0f));
  189. baseColor = baseColor.Lerp(lerpColor_, glow );
  190. // pic a color from the mid - horizon (this is cheesy!)
  191. if (z == 28 && x == 4)
  192. {
  193. fogColor_ = baseColor;
  194. zone_->SetFogColor(fogColor_);
  195. Color sunColor (.3f, .2f, .1f);
  196. sunColor += fogColor_;
  197. sunlight_->SetColor(sunColor);
  198. Color ambient = sunColor * .75f;
  199. if (ambient.r_ < .25f)
  200. ambient.r_ = .25f;
  201. if (ambient.g_ < .25f)
  202. ambient.g_ = .25f;
  203. if (ambient.b_ < .25f)
  204. ambient.b_ = .25f;
  205. zone_->SetAmbientColor(ambient);
  206. }
  207. unsigned r = Clamp(((int)(baseColor.r_ * 255.0f)), 0, 255);
  208. unsigned g = Clamp(((int)(baseColor.g_ * 255.0f)), 0, 255);
  209. unsigned b = Clamp(((int)(baseColor.b_ * 255.0f)), 0, 255);
  210. *p++ = (uint8_t) r;
  211. *p++ = (uint8_t) g;
  212. *p++ = (uint8_t) b;
  213. *p++ = 255;
  214. }
  215. }
  216. //if (fabs(lastDayTimeUpdate_ - dayTime_) > .005f)
  217. {
  218. skyTexture_->SetData(0,0,0,8,32,skytex);
  219. lastDayTimeUpdate_ = dayTime_;
  220. }
  221. return Atomic::Max( 4.0f/15.0f, daylight );
  222. }
  223. void ProcSky::UpdateIndexBuffer()
  224. {
  225. unsigned short* dest = (unsigned short*)indexBuffer_->Lock(0, 6, true);
  226. *dest++ = 2; *dest++ = 1; *dest++ = 0;
  227. *dest++ = 0; *dest++ = 3; *dest++ = 2;
  228. indexBuffer_->Unlock();
  229. indexBuffer_->ClearDataLost();
  230. }
  231. void ProcSky::UpdateVertexBuffer(const FrameInfo &frame)
  232. {
  233. float* vdest = (float*)vertexBuffer_->Lock(0, 4, true);
  234. const Frustum& frustum = frame.camera_->GetFrustum();
  235. // Upper left corner
  236. Vector3 n;
  237. Vector3 planes[4];
  238. planes[0] = frustum.planes_[PLANE_LEFT].normal_;
  239. planes[1] = frustum.planes_[PLANE_RIGHT].normal_;
  240. planes[2] = frustum.planes_[PLANE_UP].normal_;
  241. planes[3] = frustum.planes_[PLANE_DOWN].normal_;
  242. float top = 1.0f;
  243. float bottom = -1.0f;
  244. #ifdef ATOMIC_OPENGL
  245. if (flipped_)
  246. {
  247. top = -1.0f;
  248. bottom = 1.0f;
  249. }
  250. #endif
  251. // 0 2
  252. n = planes[0].CrossProduct(planes[2]);
  253. *vdest++ = -1.0f;
  254. *vdest++ = top;
  255. *vdest++ = 1.0f;
  256. *vdest++ = n.x_;
  257. *vdest++ = -n.y_;
  258. *vdest++ = n.z_ ;
  259. // Lower left corner
  260. // 3 0
  261. n = planes[3].CrossProduct(planes[0]);
  262. *vdest++ = -1.0f;
  263. *vdest++ = bottom;
  264. *vdest++ = 1.0f;
  265. *vdest++ = n.x_;
  266. *vdest++ = -n.y_;
  267. *vdest++ = n.z_;
  268. // Lower right corner
  269. // 1 3
  270. n = planes[1].CrossProduct(planes[3]);
  271. *vdest++ = 1.0f;
  272. *vdest++ = bottom;
  273. *vdest++ = 1.0f;
  274. *vdest++ = n.x_;
  275. *vdest++ = -n.y_;
  276. *vdest++ = n.z_;
  277. // Upper right corner
  278. // 2 1
  279. n = planes[2].CrossProduct(planes[1]);
  280. *vdest++ = 1.0f;
  281. *vdest++ = top;
  282. *vdest++ = 1.0f;
  283. *vdest++ = n.x_;
  284. *vdest++ = -n.y_;
  285. *vdest++ = n.z_;
  286. vertexBuffer_->Unlock();
  287. vertexBuffer_->ClearDataLost();
  288. }
  289. void ProcSky::Initialize()
  290. {
  291. initialized_ = true;
  292. topColor_.b_ = float(0x76) / 255.0f;
  293. topColor_.g_ = float(0x4a) / 255.0f;
  294. topColor_.r_ = float(0x1f) / 255.0f;
  295. topColor_.a_ = 1.0f;
  296. horizColor_.b_ = float(0xca) / 255.0f;
  297. horizColor_.g_ = float(0x9b) / 255.0f;
  298. horizColor_.r_ = float(0x65) / 255.0f;
  299. horizColor_.a_ = 1.0f;
  300. lerpColor_.b_ = float(0x16) / 255.0f;
  301. lerpColor_.g_ = float(0x40) / 255.0f;
  302. lerpColor_.r_ = float(0x9b) / 255.0f;
  303. lerpColor_.a_ = 1.0f;
  304. sunlight_ = node_->CreateComponent<Light>();
  305. sunlight_->SetLightType(LIGHT_DIRECTIONAL);
  306. sunlight_->SetCastShadows(true);
  307. sunlight_->SetShadowBias(BiasParameters(0.0001f, 0.5f));
  308. // Set cascade splits at 10, 50 and 200 world units, fade shadows out at 80% of maximum shadow distance
  309. #if ANDROID
  310. sunlight_->SetShadowCascade(CascadeParameters(50.0f, 0.0f, 0.0f, 0.0f, 0.8f));
  311. #else
  312. sunlight_->SetShadowCascade(CascadeParameters(30.0f, 60.0f, 120.0f, 200.0f, 0.8f));
  313. #endif
  314. ResourceCache* cache = GetSubsystem<ResourceCache>();
  315. skyMaterial_ = cache->GetResource<Material>("Materials/ProcSky.xml");
  316. geometry_->SetVertexBuffer(0, vertexBuffer_, MASK_POSITION | MASK_NORMAL );
  317. geometry_->SetIndexBuffer(indexBuffer_);
  318. indexBuffer_->SetSize( 6, false);
  319. UpdateIndexBuffer();
  320. // updated per frame
  321. vertexBuffer_->SetSize(4, MASK_POSITION | MASK_NORMAL , true);
  322. batches_.Resize(1);
  323. batches_[0].geometry_ = geometry_;
  324. batches_[0].geometryType_ = GEOM_STATIC_NOINSTANCING;
  325. batches_[0].worldTransform_ = &customWorldTransform_;
  326. batches_[0].material_ = skyMaterial_;
  327. batches_[0].geometry_->SetDrawRange(TRIANGLE_LIST, 0, 6, false);
  328. skyTexture_ = new Texture2D(context_);
  329. skyMaterial_->SetTexture(TU_DIFFUSE , skyTexture_);
  330. skyTexture_->SetMipsToSkip(QUALITY_LOW, 0); // No quality reduction
  331. skyTexture_->SetNumLevels(1); // No mipmaps
  332. skyTexture_->SetAddressMode(COORD_U, ADDRESS_CLAMP);
  333. skyTexture_->SetAddressMode(COORD_V, ADDRESS_CLAMP);
  334. int width = 8;
  335. int height = 32;
  336. skyTexture_->SetSize(width, height, Graphics::GetRGBAFormat(), TEXTURE_DYNAMIC);
  337. SharedArrayPtr<unsigned char> emptyBitmap(new unsigned char[width * height * 4]);
  338. memset(emptyBitmap.Get(), 0, width * height * 4);
  339. skyTexture_->SetData(0, 0, 0, width, height, emptyBitmap.Get());
  340. PODVector<Component*> components;
  341. node_->GetScene()->GetComponents(components, Zone::GetTypeStatic(), true);
  342. zone_ = (Zone*)components[0];
  343. }
  344. void ProcSky::ProcessRayQuery(const RayOctreeQuery& query, PODVector<RayQueryResult>& results)
  345. {
  346. // Do not record a raycast result for a skybox, as it would block all other results
  347. }
  348. }