Light2D.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682
  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/Viewport.h"
  8. #include "../Scene/Scene.h"
  9. #include "../Graphics/Camera.h"
  10. #include "../Graphics/Octree.h"
  11. #include "../Atomic2D/Light2D.h"
  12. #include "../Graphics/Renderer.h"
  13. #include "../Resource/XMLFile.h"
  14. #include "../Graphics/GraphicsEvents.h"
  15. #include "../Graphics/RenderPath.h"
  16. #include "../Graphics/Material.h"
  17. #include "../Graphics/Technique.h"
  18. #include "../Atomic2D/RigidBody2D.h"
  19. #include "../Atomic2D/Renderer2D.h"
  20. #include "../IO/Log.h"
  21. #include "../Resource/ResourceCache.h"
  22. namespace Atomic
  23. {
  24. static Viewport* __fixmeViewport = NULL;
  25. void FixMeSetLight2DGroupViewport(Viewport *viewport)
  26. {
  27. __fixmeViewport = viewport;
  28. }
  29. extern const char* ATOMIC2D_CATEGORY;
  30. Light2D::Light2D(Context* context) : Component(context),
  31. castShadows_(false),
  32. softShadows_(false),
  33. softShadowLength_(2.5f),
  34. backtrace_(false)
  35. {
  36. SetNumRays(32);
  37. }
  38. Light2D::~Light2D()
  39. {
  40. }
  41. void Light2D::SetNumRays(int numRays)
  42. {
  43. rays_.Resize(numRays);
  44. }
  45. void Light2D::OnSetEnabled()
  46. {
  47. Component::OnSetEnabled();
  48. if (lightgroup_)
  49. lightgroup_->SetDirty();
  50. if (!enabled_)
  51. {
  52. vertices_.Clear();
  53. }
  54. }
  55. void Light2D::AddVertices(Vector<Vertex2D>& vertices)
  56. {
  57. vertices += vertices_;
  58. }
  59. void Light2D::RegisterObject(Context* context)
  60. {
  61. context->RegisterFactory<Light2D>(ATOMIC2D_CATEGORY);
  62. COPY_BASE_ATTRIBUTES(Component);
  63. }
  64. void Light2D::CastRays()
  65. {
  66. PhysicsWorld2D* physicsWorld = lightgroup_->GetPhysicsWorld();
  67. if (physicsWorld && castShadows_) {
  68. for (unsigned i = 0; i < rays_.Size(); i++) {
  69. Light2DRay& ray = rays_[i];
  70. Vector2 oend = ray.end_;
  71. float distance = (ray.end_ - ray.start_).Length();
  72. if (Abs(distance < .01f))
  73. distance = .01f;
  74. float bestDistance = 999999;
  75. PODVector<PhysicsRaycastResult2D> results;
  76. physicsWorld->Raycast(results, ray.start_, ray.end_);
  77. RigidBody2D* body = NULL;
  78. for (PODVector<PhysicsRaycastResult2D>::ConstIterator itr = results.Begin(); itr != results.End(); itr++)
  79. {
  80. const PhysicsRaycastResult2D& result = *itr;
  81. if (result.body_->GetCastShadows() && result.distance_ < distance && result.distance_ < bestDistance)
  82. {
  83. bestDistance = result.distance_;
  84. body = result.body_;
  85. ray.fraction_ = result.distance_ / distance;
  86. ray.end_.x_ = result.position_.x_;
  87. ray.end_.y_ = result.position_.y_;
  88. }
  89. }
  90. // backtracing can introduce artifacts, for now don't backtrace
  91. // on static hit... also, may need to clamp on a body size
  92. if (backtrace_ && body && body->GetBodyType() != BT_STATIC)
  93. {
  94. results.Clear();
  95. bestDistance = -999999;
  96. physicsWorld->Raycast(results, oend, ray.end_);
  97. for (PODVector<PhysicsRaycastResult2D>::ConstIterator itr = results.Begin(); itr != results.End(); itr++)
  98. {
  99. const PhysicsRaycastResult2D& result = *itr;
  100. if (result.distance_ > bestDistance && result.body_->GetBodyType() != BT_STATIC)
  101. {
  102. bestDistance = result.distance_;
  103. ray.fraction_ = (distance - result.distance_) / distance;
  104. ray.end_.x_ = result.position_.x_;
  105. ray.end_.y_ = result.position_.y_;
  106. }
  107. }
  108. }
  109. }
  110. }
  111. }
  112. DirectionalLight2D::DirectionalLight2D(Context* context) : Light2D(context),
  113. direction_(-45.0f)
  114. {
  115. lightType_ = LIGHT2D_DIRECTIONAL;
  116. }
  117. DirectionalLight2D::~DirectionalLight2D()
  118. {
  119. }
  120. void DirectionalLight2D::RegisterObject(Context* context)
  121. {
  122. context->RegisterFactory<DirectionalLight2D>(ATOMIC2D_CATEGORY);
  123. COPY_BASE_ATTRIBUTES(Light2D);
  124. }
  125. void DirectionalLight2D::UpdateVertices()
  126. {
  127. vertices_.Clear();
  128. if (!lightgroup_ || !enabled_)
  129. return;
  130. const BoundingBox& frustumBox = lightgroup_->GetFrustumBox();
  131. float cos = Cos(direction_);
  132. float sin = Sin(direction_);
  133. float width = frustumBox.Size().x_;
  134. float height = frustumBox.Size().y_;
  135. float sizeOfScreen = width > height ? width : height;
  136. float xAxelOffSet = sizeOfScreen * cos;
  137. float yAxelOffSet = sizeOfScreen * sin;
  138. // preventing length <0 assertion error on box2d.
  139. if ((xAxelOffSet * xAxelOffSet < 0.1f) && (yAxelOffSet * yAxelOffSet < 0.1f)) {
  140. xAxelOffSet = 1;
  141. yAxelOffSet = 1;
  142. }
  143. float widthOffSet = sizeOfScreen * -sin;
  144. float heightOffSet = sizeOfScreen * cos;
  145. float x = (frustumBox.min_.x_ + frustumBox.max_.x_) * 0.5f - widthOffSet;
  146. float y = (frustumBox.min_.y_ + frustumBox.max_.y_) * 0.5f - heightOffSet;
  147. unsigned rayNum = rays_.Size();
  148. float portionX = 2.0f * widthOffSet / (rayNum - 1);
  149. x = (floorf(x / (portionX * 2))) * portionX * 2;
  150. float portionY = 2.0f * heightOffSet / (rayNum - 1);
  151. y = (ceilf(y / (portionY * 2))) * portionY * 2;
  152. for (unsigned i = 0; i < rayNum; i++)
  153. {
  154. Light2DRay& ray = rays_[i];
  155. float steppedX = float(i) * portionX + x;
  156. float steppedY = float(i) * portionY + y;
  157. ray.start_.x_ = steppedX - xAxelOffSet;
  158. ray.start_.y_ = steppedY - yAxelOffSet;
  159. ray.end_.x_ = steppedX + xAxelOffSet;
  160. ray.end_.y_ = steppedY + yAxelOffSet;
  161. ray.fraction_ = 0.0f;
  162. }
  163. CastRays();
  164. Vertex2D vertex0;
  165. Vertex2D vertex1;
  166. Vertex2D vertex2;
  167. Vertex2D vertex3;
  168. vertex0.color_ = vertex1.color_ = vertex2.color_ = vertex3.color_ = color_.ToUInt();
  169. for (unsigned i = 0; i < rayNum - 1; i++) {
  170. Light2DRay& ray0 = rays_[i];
  171. Light2DRay& ray1 = rays_[i + 1];
  172. vertex0.position_ = Vector3( ray0.start_.x_, ray0.start_.y_, 0.0f);
  173. vertex1.position_ = Vector3( ray0.end_.x_, ray0.end_.y_, 0.0f);
  174. vertex2.position_ = Vector3( ray1.start_.x_, ray1.start_.y_, 0.0f);
  175. vertex3.position_ = Vector3( ray1.end_.x_, ray1.end_.y_, 0.0f);
  176. vertex0.uv_.x_ = 1.0f;
  177. vertex2.uv_.x_ = 1.0 ;
  178. vertex1.uv_.x_ = 1.0f;
  179. vertex3.uv_.x_ = 1.0f;
  180. vertices_.Push(vertex0);
  181. vertices_.Push(vertex1);
  182. vertices_.Push(vertex3);
  183. vertices_.Push(vertex0);
  184. vertices_.Push(vertex3);
  185. vertices_.Push(vertex2);
  186. }
  187. if (softShadows_ && castShadows_)
  188. {
  189. unsigned uambient = lightgroup_->GetAmbientColor().ToUInt();
  190. // THIS CAN BE OPTIMIZED!
  191. for (unsigned i = 0; i < rays_.Size() - 1; i++) {
  192. Light2DRay& ray0 = rays_[i];
  193. Light2DRay& ray1 = rays_[i + 1];
  194. vertex0.position_ = Vector3( ray0.end_.x_, ray0.end_.y_, 0.0f);
  195. Vector2 v0 = (ray0.end_ - ray0.start_).Normalized();
  196. Vector2 v1 = (ray1.end_ - ray1.start_).Normalized();
  197. vertex1.position_ = Vector3( ray0.end_.x_ + v0.x_ * softShadowLength_,
  198. ray0.end_.y_ + v0.y_ * softShadowLength_, 0.0f);
  199. vertex2.position_ = Vector3( ray1.end_.x_ + v1.x_ * softShadowLength_,
  200. ray1.end_.y_ + v1.y_ * softShadowLength_, 0.0f);
  201. vertex3.position_ = Vector3( ray1.end_.x_, ray1.end_.y_, 0.0f);
  202. vertex0.color_ = 0;
  203. vertex3.color_ = 0;
  204. vertex0.uv_.x_ = 0;
  205. vertex3.uv_.x_ = 0;
  206. vertex1.color_ = 0;
  207. vertex2.color_ = 0;
  208. vertex1.uv_.x_ = 1.0;
  209. vertex2.uv_.x_ = 1.0;
  210. vertices_.Push(vertex0);
  211. vertices_.Push(vertex1);
  212. vertices_.Push(vertex2);
  213. vertices_.Push(vertex0);
  214. vertices_.Push(vertex2);
  215. vertices_.Push(vertex3);
  216. }
  217. }
  218. }
  219. PositionalLight2D::PositionalLight2D(Context* context) : Light2D(context)
  220. {
  221. }
  222. PositionalLight2D::~PositionalLight2D()
  223. {
  224. }
  225. void PositionalLight2D::RegisterObject(Context* context)
  226. {
  227. context->RegisterFactory<PositionalLight2D>(ATOMIC2D_CATEGORY);
  228. COPY_BASE_ATTRIBUTES(Light2D);
  229. }
  230. void PositionalLight2D::UpdateVertices()
  231. {
  232. vertices_.Clear();
  233. if (!lightgroup_ || !enabled_)
  234. return;
  235. CastRays();
  236. Vertex2D vertex0;
  237. Vertex2D vertex1;
  238. Vertex2D vertex2;
  239. Vertex2D vertex3;
  240. vertex0.color_ = vertex1.color_ = vertex2.color_ = vertex3.color_ = color_.ToUInt();
  241. for (unsigned i = 0; i < rays_.Size() - 1; i++) {
  242. Light2DRay& ray0 = rays_[i];
  243. Light2DRay& ray1 = rays_[i + 1];
  244. vertex0.position_ = Vector3( ray0.start_.x_, ray0.start_.y_, 0.0f);
  245. vertex1.position_ = Vector3( ray0.end_.x_, ray0.end_.y_, 0.0f);
  246. vertex2.position_ = Vector3( ray1.end_.x_, ray1.end_.y_, 0.0f);
  247. vertex0.uv_.x_ = 1.0f;
  248. vertex1.uv_.x_ = 1.0f - ray0.fraction_;
  249. vertex2.uv_.x_ = 1.0f - ray1.fraction_;
  250. vertices_.Push(vertex0);
  251. vertices_.Push(vertex1);
  252. vertices_.Push(vertex2);
  253. }
  254. if (softShadows_ && castShadows_)
  255. {
  256. // THIS CAN BE OPTIMIZED!
  257. for (unsigned i = 0; i < rays_.Size(); i++) {
  258. Light2DRay& ray0 = rays_[i];
  259. Light2DRay& ray1 = rays_[i + 1 == rays_.Size() ? 0 : i + 1];
  260. float s0 = (1.0f - ray0.fraction_);
  261. float s1 = (1.0f - ray1.fraction_);
  262. vertex0.position_ = Vector3( ray0.end_.x_, ray0.end_.y_, 0.0f);
  263. vertex1.position_ = Vector3( ray0.end_.x_ + s0 * softShadowLength_ * ray0.cos_,
  264. (ray0.end_.y_ + s0 * softShadowLength_ * ray0.sin_), 0.0f);
  265. vertex2.position_ = Vector3( ray1.end_.x_ + s1 * softShadowLength_ * ray1.cos_,
  266. (ray1.end_.y_ + s1 * softShadowLength_ * ray1.sin_), 0.0f);
  267. vertex3.position_ = Vector3( ray1.end_.x_, ray1.end_.y_, 0.0f);
  268. vertex1.uv_.x_ = 0;
  269. vertex2.uv_.x_ = 0;
  270. vertex1.color_ = 0x00000000;
  271. vertex2.color_ = 0x00000000;
  272. // darkness of shadow, we should break this all out of uint
  273. vertex0.uv_.x_ = s0 * .65f;
  274. vertex3.uv_.x_ = s1 * .65f;
  275. vertices_.Push(vertex0);
  276. vertices_.Push(vertex1);
  277. vertices_.Push(vertex2);
  278. vertices_.Push(vertex0);
  279. vertices_.Push(vertex2);
  280. vertices_.Push(vertex3);
  281. }
  282. }
  283. }
  284. PointLight2D::PointLight2D(Context* context) : PositionalLight2D(context),
  285. radius_(1.0f)
  286. {
  287. lightType_ = LIGHT2D_POINT;
  288. }
  289. PointLight2D::~PointLight2D()
  290. {
  291. }
  292. void PointLight2D::RegisterObject(Context* context)
  293. {
  294. context->RegisterFactory<PointLight2D>(ATOMIC2D_CATEGORY);
  295. COPY_BASE_ATTRIBUTES(PositionalLight2D);
  296. }
  297. void PointLight2D::UpdateVertices()
  298. {
  299. vertices_.Clear();
  300. if (!lightgroup_ || !enabled_)
  301. return;
  302. const PhysicsWorld2D* physicsWorld = lightgroup_->GetPhysicsWorld();
  303. const Node* lightNode = GetNode();
  304. Vector2 start = lightNode->GetWorldPosition2D();
  305. float angleNum = 360.0f / float((rays_.Size() - 1));
  306. for (unsigned i = 0; i < rays_.Size(); i++) {
  307. float angle = angleNum * i;
  308. Light2DRay& ray = rays_[i];
  309. ray.start_ = start;
  310. ray.sin_ = Sin(angle);
  311. ray.cos_ = Cos(angle);
  312. ray.end_.x_ = ray.start_.x_ + radius_ * ray.cos_;
  313. ray.end_.y_ = ray.start_.y_ + radius_ * ray.sin_;
  314. ray.fraction_ = 1.0f;
  315. }
  316. PositionalLight2D::UpdateVertices();
  317. }
  318. void Light2DGroup::RegisterObject(Context* context)
  319. {
  320. context->RegisterFactory<Light2DGroup>(ATOMIC2D_CATEGORY);
  321. COPY_BASE_ATTRIBUTES(Drawable2D);
  322. }
  323. void Light2DGroup::OnNodeSet(Node* node)
  324. {
  325. // Do not call Drawable2D::OnNodeSet(node)
  326. if (node)
  327. {
  328. if (renderer_.Null())
  329. {
  330. renderer_ = node->GetOrCreateComponent<Renderer2D>();
  331. renderer_->SetUseTris(true);
  332. }
  333. if (light2DMaterial_.Null())
  334. CreateLight2DMaterial();
  335. Scene* scene = GetScene();
  336. if (scene)
  337. {
  338. if (IsEnabledEffective())
  339. renderer_->AddDrawable(this);
  340. }
  341. node->AddListener(this);
  342. }
  343. else
  344. {
  345. if (renderer_)
  346. renderer_->RemoveDrawable(this);
  347. }
  348. }
  349. Light2DGroup::Light2DGroup(Context* context) : Drawable2D(context),
  350. ambientColor_(0, 0, 0, 0),
  351. frustum_(0)
  352. {
  353. sourceBatches_.Resize(1);
  354. SubscribeToEvent(E_BEGINRENDERING, HANDLER(Light2DGroup, HandleBeginRendering));
  355. SubscribeToEvent(E_BEGINVIEWUPDATE, HANDLER(Light2DGroup, HandleBeginViewUpdate));
  356. }
  357. Light2DGroup::~Light2DGroup()
  358. {
  359. Renderer* renderer = GetSubsystem<Renderer>();
  360. if (renderer)
  361. {
  362. Viewport* viewport = __fixmeViewport ? __fixmeViewport : renderer->GetViewport(0);
  363. if (viewport)
  364. {
  365. RenderPath* renderpath = viewport->GetRenderPath();
  366. if (renderpath)
  367. renderpath->RemoveCommands("Light2D");
  368. }
  369. }
  370. __fixmeViewport = NULL;
  371. }
  372. void Light2DGroup::HandleBeginViewUpdate(StringHash eventType, VariantMap& eventData)
  373. {
  374. using namespace BeginViewUpdate;
  375. Scene* scene = GetScene();
  376. // Check that we are updating the correct scene
  377. if (scene != eventData[P_SCENE].GetPtr())
  378. return;
  379. Camera* camera = static_cast<Camera*>(eventData[P_CAMERA].GetPtr());
  380. frustum_ = &camera->GetFrustum();
  381. if (camera->IsOrthographic())// && camera->GetNode()->GetWorldDirection() == Vector3::FORWARD)
  382. {
  383. // Define bounding box with min and max points
  384. // todo: handle rotation
  385. frustumBoundingBox_.Define(frustum_->vertices_[2], frustum_->vertices_[4]);
  386. }
  387. }
  388. void Light2DGroup::HandleBeginRendering(StringHash eventType, VariantMap& eventData)
  389. {
  390. sourceBatchesDirty_ = true;
  391. }
  392. void Light2DGroup::OnWorldBoundingBoxUpdate()
  393. {
  394. boundingBox_.Define(-M_LARGE_VALUE, M_LARGE_VALUE);
  395. worldBoundingBox_.Define(-M_LARGE_VALUE, M_LARGE_VALUE);
  396. }
  397. void Light2DGroup::OnDrawOrderChanged()
  398. {
  399. sourceBatches_[0].drawOrder_ = GetDrawOrder();
  400. }
  401. void Light2DGroup::UpdateSourceBatches()
  402. {
  403. if (!sourceBatchesDirty_)
  404. return;
  405. Vector<Vertex2D>& vertices = sourceBatches_[0].vertices_;
  406. vertices.Clear();
  407. for (Vector<WeakPtr<Light2D> >::Iterator itr = lights_.Begin(); itr != lights_.End(); itr++)
  408. {
  409. Light2D* light = *itr;
  410. if (!light->IsEnabled())
  411. continue;
  412. light->UpdateVertices();
  413. light->AddVertices(vertices);
  414. }
  415. sourceBatchesDirty_ = false;
  416. }
  417. void Light2DGroup::AddLight(Light2D* light)
  418. {
  419. for (Vector<WeakPtr<Light2D> >::ConstIterator itr = lights_.Begin(); itr != lights_.End(); itr++)
  420. if (*itr == light)
  421. return;
  422. light->SetLightGroup(this);
  423. lights_.Push(WeakPtr<Light2D>(light));
  424. }
  425. void Light2DGroup::SetPhysicsWorld(PhysicsWorld2D* physicsWorld)
  426. {
  427. physicsWorld_ = physicsWorld;
  428. }
  429. void Light2DGroup::SetAmbientColor(const Color& color)
  430. {
  431. if (color == ambientColor_)
  432. return;
  433. ambientColor_ = color;
  434. Renderer* renderer = GetSubsystem<Renderer>();
  435. // only on main viewport atm and viewport must first be set
  436. if (renderer)
  437. {
  438. Viewport* viewport = __fixmeViewport ? __fixmeViewport : renderer->GetViewport(0);
  439. if (viewport)
  440. {
  441. RenderPath* renderpath = viewport->GetRenderPath();
  442. renderpath->SetShaderParameter("ShadowAmbient", ambientColor_);
  443. }
  444. }
  445. }
  446. void Light2DGroup::CreateLight2DMaterial()
  447. {
  448. Renderer* renderer = GetSubsystem<Renderer>();
  449. // only on main viewport atm and viewport must first be set
  450. Viewport* viewport = __fixmeViewport ? __fixmeViewport : renderer->GetViewport(0);
  451. RenderPath* renderpath = viewport->GetRenderPath();
  452. RenderTargetInfo ntarget;
  453. ntarget.enabled_ = true;
  454. ntarget.name_ = "light2dtarget";
  455. ntarget.tag_ = "Light2D";
  456. ntarget.format_ = Graphics::GetRGBAFormat();
  457. ntarget.sizeMode_ = SIZE_VIEWPORTDIVISOR;
  458. ntarget.size_ = Vector2(4, 4);
  459. // try true
  460. ntarget.filtered_ = false;
  461. renderpath->AddRenderTarget(ntarget);
  462. RenderPathCommand clearCommand;
  463. clearCommand.tag_ = "Light2D";
  464. clearCommand.type_ = CMD_CLEAR;
  465. clearCommand.clearColor_ = Color(0, 0, 0, 0);
  466. clearCommand.clearFlags_ = CLEAR_COLOR;
  467. clearCommand.enabled_ = true;
  468. clearCommand.SetNumOutputs(1);
  469. clearCommand.SetOutputName(0, "light2dtarget");
  470. renderpath->AddCommand(clearCommand);
  471. RenderPathCommand passCommand;
  472. passCommand.tag_ = "Light2D";
  473. passCommand.type_ = CMD_SCENEPASS;
  474. passCommand.pass_ = "light2d";
  475. passCommand.enabled_ = true;
  476. passCommand.SetNumOutputs(1);
  477. passCommand.SetOutputName(0, "light2dtarget");
  478. renderpath->AddCommand(passCommand);
  479. ResourceCache* cache = GetSubsystem<ResourceCache>();
  480. XMLFile* light2DBlur = cache->GetResource<XMLFile>("PostProcess/Light2DBlur.xml");
  481. renderpath->Append(light2DBlur);
  482. RenderPathCommand copyCommand;
  483. copyCommand.enabled_ = true;
  484. copyCommand.tag_ = "Light2D";
  485. copyCommand.type_ = CMD_QUAD;
  486. copyCommand.vertexShaderName_ = "Shadow2D";
  487. copyCommand.pixelShaderName_ = "Shadow2D";
  488. copyCommand.SetTextureName(TU_DIFFUSE, "viewport");
  489. copyCommand.SetTextureName(TU_EMISSIVE, "light2dtarget");
  490. copyCommand.SetShaderParameter("ShadowAmbient", ambientColor_);
  491. copyCommand.SetNumOutputs(1);
  492. copyCommand.SetOutputName(0, "viewport");
  493. renderpath->AddCommand(copyCommand);
  494. light2DMaterial_ = new Material(context_);
  495. light2DMaterial_->SetName("Light2DMaterial");
  496. Technique* tech = new Technique(context_);
  497. Pass* pass = tech->CreatePass("light2d");
  498. pass->SetBlendMode(BLEND_ADDALPHA);
  499. pass->SetDepthTestMode(CMP_ALWAYS);
  500. pass->SetVertexShader("Light2D");
  501. pass->SetPixelShader("Light2D");
  502. pass->SetDepthWrite(false);
  503. light2DMaterial_->SetTechnique(0, tech);
  504. light2DMaterial_->SetCullMode(CULL_NONE);
  505. sourceBatches_[0].material_ = light2DMaterial_;
  506. }
  507. }