ProcSky.cpp 11 KB

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