-
Notifications
You must be signed in to change notification settings - Fork 51
/
Ogre2Material.cc
1558 lines (1363 loc) · 48.1 KB
/
Ogre2Material.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) 2018 Open Source Robotics Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
// Note this include is placed in the src file because
// otherwise ogre produces compile errors
#ifdef _MSC_VER
#pragma warning(push, 0)
#endif
#include <Hlms/Pbs/OgreHlmsPbs.h>
#include <Hlms/Pbs/OgreHlmsPbsDatablock.h>
#include <Hlms/Unlit/OgreHlmsUnlit.h>
#include <Hlms/Unlit/OgreHlmsUnlitDatablock.h>
#include <OgreHighLevelGpuProgram.h>
#include <OgreHighLevelGpuProgramManager.h>
#include <OgreHlmsManager.h>
#include <OgreItem.h>
#include <OgreMaterialManager.h>
#include <OgrePixelFormatGpuUtils.h>
#include <OgreTechnique.h>
#include <OgreTextureGpuManager.h>
#include <Vao/OgreVaoManager.h>
#ifdef _MSC_VER
#pragma warning(pop)
#endif
#include <gz/common/Console.hh>
#include <gz/common/Filesystem.hh>
#include <gz/common/Image.hh>
#include "gz/rendering/GraphicsAPI.hh"
#include "gz/rendering/ShaderParams.hh"
#include "gz/rendering/ShaderType.hh"
#include "gz/rendering/ogre2/Ogre2Material.hh"
#include "gz/rendering/ogre2/Ogre2Conversions.hh"
#include "gz/rendering/ogre2/Ogre2RenderEngine.hh"
#include "gz/rendering/ogre2/Ogre2Scene.hh"
/// \brief Private data for the Ogre2Material class
class gz::rendering::Ogre2MaterialPrivate
{
/// \brief Ogre stores the name using hashes. This variable will
/// store the material hash name
public: std::string hashName;
/// \brief Pointer to image containing the texture data if it was
/// loaded from memory
public: std::shared_ptr<const common::Image> textureData;
/// \brief Pointer to image containing the normal map data if it was
/// loaded from memory
public: std::shared_ptr<const common::Image> normalMapData;
/// \brief Pointer to image containing the roughness map data if it was
/// loaded from memory
public: std::shared_ptr<const common::Image> roughnessMapData;
/// \brief Pointer to image containing the metalness map data if it was
/// loaded from memory
public: std::shared_ptr<const common::Image> metalnessMapData;
/// \brief Pointer to image containing the emissive map data if it was
/// loaded from memory
public: std::shared_ptr<const common::Image> emissiveMapData;
/// \brief Pointer to image containing the environment map data if it was
/// loaded from memory
public: std::shared_ptr<const common::Image> environmentMapData;
/// \brief Pointer to image containing the light map data if it was
/// loaded from memory
public: std::shared_ptr<const common::Image> lightMapData;
/// \brief Path to vertex shader program.
public: std::string vertexShaderPath;
/// \brief Path to fragment shader program.
public: std::string fragmentShaderPath;
/// \brief Parameters to be bound to the vertex shader
public: ShaderParamsPtr vertexShaderParams;
/// \brief Parameters to be bound to the fragment shader
public: ShaderParamsPtr fragmentShaderParams;
/// \brief Material to be used when rendering to special
/// cameras (e.g. sensors) like Ogre2GpuRays,
/// Ogre2LaserRetroMaterialSwitcher, etc
///
/// It shares the same Vertex Shader, but uses a different
/// Pixel Shader
public: Ogre::MaterialPtr ogreSolidColorMat;
/// \brief A clone of plaincolor_fs. We need a clone
/// because some Metal needs to pair with the vertex shader they're
/// going to be used and Vulkan needs to have the same or a compatible
/// Root Layout profile.
///
/// Used in ogreSolidColorMat
public: Ogre::HighLevelGpuProgramPtr ogreSolidColorShader;
/// \brief Returns the shader language code.
/// \param[in] _graphicsAPI The graphic API.
/// \return The shader language code string.
public: static std::string shaderLanguageCode(GraphicsAPI _graphicsAPI)
{
switch (_graphicsAPI)
{
case GraphicsAPI::OPENGL:
return "glsl";
case GraphicsAPI::VULKAN:
return "glslvk";
case GraphicsAPI::METAL:
return "metal";
default:
return "invalid";
}
}
};
using namespace gz;
using namespace rendering;
//////////////////////////////////////////////////
Ogre2Material::Ogre2Material()
: dataPtr(std::make_unique<Ogre2MaterialPrivate>())
{
}
//////////////////////////////////////////////////
Ogre2Material::~Ogre2Material()
{
this->Destroy();
}
//////////////////////////////////////////////////
void Ogre2Material::Destroy()
{
if (!this->Scene()->IsInitialized())
return;
if (!this->ogreDatablock)
return;
this->ogreHlmsPbs->destroyDatablock(this->ogreDatablockId);
this->ogreDatablock = nullptr;
if (this->ogreUnlitDatablock)
{
this->ogreUnlitDatablock->getCreator()->destroyDatablock(
this->ogreUnlitDatablock->getName());
this->ogreUnlitDatablock = nullptr;
}
// remove low level ogre material used by render targets
if (this->ogreMaterial)
{
Ogre::MaterialManager &matManager = Ogre::MaterialManager::getSingleton();
matManager.remove(this->ogreMaterial);
this->ogreMaterial.reset();
matManager.remove(this->dataPtr->ogreSolidColorMat);
this->dataPtr->ogreSolidColorMat.reset();
Ogre::HighLevelGpuProgramManager::getSingleton().remove(
this->dataPtr->ogreSolidColorShader);
this->dataPtr->ogreSolidColorShader.reset();
}
Ogre::Root *root = Ogre2RenderEngine::Instance()->OgreRoot();
Ogre::TextureGpuManager *textureManager =
root->getRenderSystem()->getTextureGpuManager();
textureManager->setStagingTextureMaxBudgetBytes(
8u * 1024u * 1024u);
textureManager->setWorkerThreadMaxPreloadBytes(
8u * 1024u * 1024u);
textureManager->setWorkerThreadMaxPerStagingTextureRequestBytes(
4u * 1024u * 1024u);
Ogre::TextureGpuManager::BudgetEntryVec budget;
textureManager->setWorkerThreadMinimumBudget( budget );
Ogre::HlmsManager *hlmsManager = root->getHlmsManager();
Ogre::TextureGpu* textureToRemove = nullptr;
bool textureIsUse = false;
// Check each material from each Hlms (except low level) to see if their
// material is currently in use. If it's not, then its textures may be
// not either
for (size_t i = Ogre::HLMS_PBS; i < Ogre::HLMS_MAX; ++i)
{
Ogre::Hlms *hlms = hlmsManager->getHlms(static_cast<Ogre::HlmsTypes>(i));
if(hlms)
{
const Ogre::Hlms::HlmsDatablockMap &datablocks = hlms->getDatablockMap();
Ogre::Hlms::HlmsDatablockMap::const_iterator itor = datablocks.begin();
Ogre::Hlms::HlmsDatablockMap::const_iterator end = datablocks.end();
while (itor != end)
{
if (i == Ogre::HLMS_PBS)
{
Ogre::HlmsPbsDatablock *derivedDatablock =
static_cast<Ogre::HlmsPbsDatablock*>(itor->second.datablock);
for (size_t texUnit = 0; texUnit < Ogre::NUM_PBSM_TEXTURE_TYPES;
++texUnit)
{
// Check each texture from the material
Ogre::TextureGpu *tex = derivedDatablock->getTexture(texUnit);
if (tex)
{
// If getLinkedRenderables is empty, then the material is
// not in use, and thus so is potentially the texture
if (!itor->second.datablock->getLinkedRenderables().empty())
{
if (tex->getNameStr() == this->textureName)
{
textureIsUse = true;
}
}
else
{
if (tex->getNameStr() == this->textureName)
{
textureToRemove = tex;
}
}
}
}
}
++itor;
}
}
}
if (textureToRemove && !textureIsUse)
{
Ogre2ScenePtr s = std::dynamic_pointer_cast<Ogre2Scene>(this->Scene());
s->ClearMaterialsCache(this->textureName);
this->Scene()->UnregisterMaterial(this->name);
textureManager->destroyTexture(textureToRemove);
}
Ogre2ScenePtr s = std::dynamic_pointer_cast<Ogre2Scene>(this->Scene());
Ogre::SceneManager *sceneManager = s->OgreSceneManager();
sceneManager->shrinkToFitMemoryPools();
try
{
Ogre::VaoManager *vaoManager = textureManager->getVaoManager();
vaoManager->cleanupEmptyPools();
}
catch (Ogre::UnimplementedException &)
{
// Do nothing. Vulkan does not implement this and is not needed
// It might be implemented in the future though
}
}
//////////////////////////////////////////////////
math::Color Ogre2Material::Diffuse() const
{
Ogre::Vector3 color =
this->ogreDatablock->getDiffuse();
return math::Color(color.x, color.y, color.z, this->diffuse.A());
}
//////////////////////////////////////////////////
void Ogre2Material::SetDiffuse(const math::Color &_color)
{
BaseMaterial::SetDiffuse(_color);
this->ogreDatablock->setDiffuse(
Ogre::Vector3(_color.R(), _color.G(), _color.B()));
this->UpdateTransparency();
}
//////////////////////////////////////////////////
math::Color Ogre2Material::Specular() const
{
Ogre::Vector3 color =
this->ogreDatablock->getSpecular();
return math::Color(color.x, color.y, color.z, 1.0);
}
//////////////////////////////////////////////////
void Ogre2Material::SetSpecular(const math::Color &_color)
{
this->ogreDatablock->setSpecular(
Ogre::Vector3(_color.R(), _color.G(), _color.B()));
}
//////////////////////////////////////////////////
math::Color Ogre2Material::Emissive() const
{
Ogre::Vector3 color =
this->ogreDatablock->getEmissive();
return math::Color(color.x, color.y, color.z, 1.0);
}
//////////////////////////////////////////////////
void Ogre2Material::SetEmissive(const math::Color &_color)
{
this->ogreDatablock->setEmissive(
Ogre::Vector3(_color.R(), _color.G(), _color.B()));
}
//////////////////////////////////////////////////
void Ogre2Material::SetTransparency(const double _transparency)
{
this->transparency = std::min(std::max(_transparency, 0.0), 1.0);
this->UpdateTransparency();
}
//////////////////////////////////////////////////
void Ogre2Material::UpdateTransparency()
{
Ogre::HlmsPbsDatablock::TransparencyModes mode;
double opacity = (1.0 - this->transparency) * this->diffuse.A();
if (math::equal(opacity, 1.0))
mode = Ogre::HlmsPbsDatablock::None;
else
mode = Ogre::HlmsPbsDatablock::Transparent;
// from ogre documentation: 0 = full transparency and 1 = fully opaque
this->ogreDatablock->setTransparency(opacity, mode);
// set transparent objects to be in a higher render queue group
// so they blend properly with heightmaps (render queue 11)
auto renderables = this->ogreDatablock->getLinkedRenderables();
for (auto & renderable : renderables)
{
auto subItem = dynamic_cast<Ogre::SubItem *>(renderable);
if (subItem)
{
if (mode == Ogre::HlmsPbsDatablock::None)
{
// by default, ogre items are in render queue 10
// these are hardcoded in ogre-next and there does not seem to be
// an enum of function to retrieve this default render queue group
subItem->getParent()->setRenderQueueGroup(10);
}
else
{
// put in render queue group 200
// v2 entities can be placed in groups 0-99 or 200-224
subItem->getParent()->setRenderQueueGroup(200);
}
}
}
}
//////////////////////////////////////////////////
void Ogre2Material::SetAlphaFromTexture(bool _enabled,
double _alpha, bool _twoSided)
{
BaseMaterial::SetAlphaFromTexture(_enabled, _alpha, _twoSided);
if (_enabled)
{
this->ogreDatablock->setAlphaTest(Ogre::CMPF_GREATER_EQUAL);
Ogre::HlmsBlendblock block;
block.setBlendType(Ogre::SBT_TRANSPARENT_ALPHA);
this->ogreDatablock->setBlendblock(block);
}
else
{
this->ogreDatablock->setAlphaTest(Ogre::CMPF_ALWAYS_PASS);
}
this->ogreDatablock->setAlphaTestThreshold(_alpha);
this->ogreDatablock->setTwoSidedLighting(_twoSided);
}
//////////////////////////////////////////////////
float Ogre2Material::RenderOrder() const
{
Ogre::HlmsMacroblock macroblock(
*this->ogreDatablock->getMacroblock());
return macroblock.mDepthBiasConstant;
}
//////////////////////////////////////////////////
void Ogre2Material::SetRenderOrder(const float _renderOrder)
{
this->renderOrder = _renderOrder;
Ogre::HlmsMacroblock macroblock(
*this->ogreDatablock->getMacroblock());
Ogre::Root *root = Ogre2RenderEngine::Instance()->OgreRoot();
Ogre::RenderSystem *renderSystem = root->getRenderSystem();
if (renderSystem->isReverseDepth())
{
// Reverse depth needs 100x scale AND ends up being superior
// See https://github.com/gazebosim/gz-rendering/
// issues/427#issuecomment-991800352
// and see https://www.youtube.com/watch?v=s2XdH3fYUac
macroblock.mDepthBiasConstant = _renderOrder * 100.0f;
}
else
{
macroblock.mDepthBiasConstant = _renderOrder;
}
this->ogreDatablock->setMacroblock(macroblock);
}
//////////////////////////////////////////////////
bool Ogre2Material::ReceiveShadows() const
{
return this->ogreDatablock->getReceiveShadows();
}
//////////////////////////////////////////////////
void Ogre2Material::SetReceiveShadows(const bool _receiveShadows)
{
this->ogreDatablock->setReceiveShadows(_receiveShadows);
}
//////////////////////////////////////////////////
bool Ogre2Material::HasTexture() const
{
return !this->textureName.empty();
}
//////////////////////////////////////////////////
std::string Ogre2Material::Texture() const
{
return this->textureName;
}
//////////////////////////////////////////////////
void Ogre2Material::SetTexture(const std::string &_name,
const std::shared_ptr<const common::Image> &_img)
{
if (_name.empty())
{
this->ClearTexture();
return;
}
this->textureName = _name;
this->dataPtr->textureData = _img;
if (_img == nullptr)
this->SetTextureMapImpl(this->textureName, Ogre::PBSM_DIFFUSE);
else
this->SetTextureMapDataImpl(this->textureName, _img, Ogre::PBSM_DIFFUSE);
}
//////////////////////////////////////////////////
std::shared_ptr<const common::Image> Ogre2Material::TextureData() const
{
return this->dataPtr->textureData;
}
//////////////////////////////////////////////////
void Ogre2Material::ClearTexture()
{
this->textureName = "";
this->dataPtr->textureData = nullptr;
this->ogreDatablock->setTexture(Ogre::PBSM_DIFFUSE, this->textureName);
}
//////////////////////////////////////////////////
bool Ogre2Material::HasNormalMap() const
{
return !this->normalMapName.empty();
}
//////////////////////////////////////////////////
std::string Ogre2Material::NormalMap() const
{
return this->normalMapName;
}
//////////////////////////////////////////////////
std::shared_ptr<const common::Image> Ogre2Material::NormalMapData() const
{
return this->dataPtr->normalMapData;
}
//////////////////////////////////////////////////
void Ogre2Material::SetNormalMap(const std::string &_name,
const std::shared_ptr<const common::Image> &_img)
{
if (_name.empty())
{
this->ClearNormalMap();
return;
}
this->normalMapName = _name;
this->dataPtr->normalMapData = _img;
if (_img == nullptr)
this->SetTextureMapImpl(this->normalMapName, Ogre::PBSM_NORMAL);
else
this->SetTextureMapDataImpl(this->normalMapName, _img, Ogre::PBSM_NORMAL);
}
//////////////////////////////////////////////////
void Ogre2Material::ClearNormalMap()
{
this->normalMapName = "";
this->dataPtr->normalMapData = nullptr;
this->ogreDatablock->setTexture(Ogre::PBSM_NORMAL, this->normalMapName);
}
//////////////////////////////////////////////////
bool Ogre2Material::HasRoughnessMap() const
{
return !this->roughnessMapName.empty();
}
//////////////////////////////////////////////////
std::string Ogre2Material::RoughnessMap() const
{
return this->roughnessMapName;
}
//////////////////////////////////////////////////
std::shared_ptr<const common::Image> Ogre2Material::RoughnessMapData() const
{
return this->dataPtr->roughnessMapData;
}
//////////////////////////////////////////////////
void Ogre2Material::SetRoughnessMap(const std::string &_name,
const std::shared_ptr<const common::Image> &_img)
{
if (_name.empty())
{
this->ClearRoughnessMap();
return;
}
this->roughnessMapName = _name;
this->dataPtr->roughnessMapData = _img;
if (_img == nullptr)
{
this->SetTextureMapImpl(this->roughnessMapName, Ogre::PBSM_ROUGHNESS);
}
else
{
this->SetTextureMapDataImpl(this->roughnessMapName,
_img, Ogre::PBSM_ROUGHNESS);
}
}
//////////////////////////////////////////////////
void Ogre2Material::ClearRoughnessMap()
{
this->roughnessMapName = "";
this->dataPtr->roughnessMapData = nullptr;
this->ogreDatablock->setTexture(Ogre::PBSM_ROUGHNESS, this->roughnessMapName);
}
//////////////////////////////////////////////////
bool Ogre2Material::HasMetalnessMap() const
{
return !this->metalnessMapName.empty();
}
//////////////////////////////////////////////////
std::string Ogre2Material::MetalnessMap() const
{
return this->metalnessMapName;
}
//////////////////////////////////////////////////
std::shared_ptr<const common::Image> Ogre2Material::MetalnessMapData() const
{
return this->dataPtr->metalnessMapData;
}
//////////////////////////////////////////////////
void Ogre2Material::SetMetalnessMap(const std::string &_name,
const std::shared_ptr<const common::Image> &_img)
{
if (_name.empty())
{
this->ClearMetalnessMap();
return;
}
this->metalnessMapName = _name;
this->dataPtr->metalnessMapData = _img;
if (_img == nullptr)
{
this->SetTextureMapImpl(this->metalnessMapName, Ogre::PBSM_METALLIC);
}
else
{
this->SetTextureMapDataImpl(this->metalnessMapName,
_img, Ogre::PBSM_METALLIC);
}
}
//////////////////////////////////////////////////
void Ogre2Material::ClearMetalnessMap()
{
this->metalnessMapName = "";
this->dataPtr->metalnessMapData = nullptr;
this->ogreDatablock->setTexture(Ogre::PBSM_METALLIC, this->metalnessMapName);
}
//////////////////////////////////////////////////
bool Ogre2Material::HasEnvironmentMap() const
{
return !this->environmentMapName.empty();
}
//////////////////////////////////////////////////
std::string Ogre2Material::EnvironmentMap() const
{
return this->environmentMapName;
}
//////////////////////////////////////////////////
std::shared_ptr<const common::Image> Ogre2Material::EnvironmentMapData() const
{
return this->dataPtr->environmentMapData;
}
//////////////////////////////////////////////////
void Ogre2Material::SetEnvironmentMap(const std::string &_name,
const std::shared_ptr<const common::Image> &_img)
{
if (_name.empty())
{
this->ClearEnvironmentMap();
return;
}
this->environmentMapName = _name;
this->dataPtr->environmentMapData = _img;
if (_img == nullptr)
{
this->SetTextureMapImpl(this->environmentMapName, Ogre::PBSM_REFLECTION);
}
else
{
this->SetTextureMapDataImpl(this->environmentMapName,
_img, Ogre::PBSM_REFLECTION);
}
}
//////////////////////////////////////////////////
void Ogre2Material::ClearEnvironmentMap()
{
this->environmentMapName = "";
this->dataPtr->environmentMapData = nullptr;
this->ogreDatablock->setTexture(
Ogre::PBSM_REFLECTION, this->environmentMapName);
}
//////////////////////////////////////////////////
bool Ogre2Material::HasEmissiveMap() const
{
return !this->emissiveMapName.empty();
}
//////////////////////////////////////////////////
std::string Ogre2Material::EmissiveMap() const
{
return this->emissiveMapName;
}
//////////////////////////////////////////////////
std::shared_ptr<const common::Image> Ogre2Material::EmissiveMapData() const
{
return this->dataPtr->emissiveMapData;
}
//////////////////////////////////////////////////
void Ogre2Material::SetEmissiveMap(const std::string &_name,
const std::shared_ptr<const common::Image> &_img)
{
if (_name.empty())
{
this->ClearEmissiveMap();
return;
}
this->emissiveMapName = _name;
this->dataPtr->emissiveMapData = _img;
if (_img == nullptr)
{
this->SetTextureMapImpl(this->emissiveMapName, Ogre::PBSM_EMISSIVE);
}
else
{
this->SetTextureMapDataImpl(this->emissiveMapName,
_img, Ogre::PBSM_EMISSIVE);
}
}
//////////////////////////////////////////////////
void Ogre2Material::ClearEmissiveMap()
{
this->emissiveMapName = "";
this->dataPtr->emissiveMapData = nullptr;
this->ogreDatablock->setTexture(Ogre::PBSM_EMISSIVE, this->emissiveMapName);
}
//////////////////////////////////////////////////
bool Ogre2Material::HasLightMap() const
{
return !this->lightMapName.empty();
}
//////////////////////////////////////////////////
std::string Ogre2Material::LightMap() const
{
return this->lightMapName;
}
//////////////////////////////////////////////////
unsigned int Ogre2Material::LightMapTexCoordSet() const
{
return this->lightMapUvSet;
}
//////////////////////////////////////////////////
void Ogre2Material::SetLightMap(const std::string &_name,
const std::shared_ptr<const common::Image> &_img,
unsigned int _uvSet)
{
if (_name.empty())
{
this->ClearLightMap();
return;
}
this->lightMapName = _name;
this->dataPtr->lightMapData = _img;
this->lightMapUvSet = _uvSet;
// in gz-rendering5 + ogre 2.1, we reserved detail map 0 for light map
// and set a blend mode (PBSM_BLEND_OVERLAY AND PBSM_BLEND_MULTIPLY2X
// produces better results) to blend with base albedo map. However, this
// creates unwanted red highlights with ogre 2.2. So switching to use the
// emissive map slot and calling setUseEmissiveAsLightmap(true)
// Ogre::PbsTextureTypes type = Ogre::PBSM_DETAIL0;
// this->ogreDatablock->setDetailMapBlendMode(0, Ogre::PBSM_BLEND_OVERLAY);
Ogre::PbsTextureTypes type = Ogre::PBSM_EMISSIVE;
// lightmap usually uses a different tex coord set
if (_img == nullptr)
this->SetTextureMapImpl(this->lightMapName, type);
else
this->SetTextureMapDataImpl(this->lightMapName, _img, type);
this->ogreDatablock->setTextureUvSource(type, this->lightMapUvSet);
this->ogreDatablock->setUseEmissiveAsLightmap(true);
}
//////////////////////////////////////////////////
std::shared_ptr<const common::Image> Ogre2Material::LightMapData() const
{
return this->dataPtr->lightMapData;
}
//////////////////////////////////////////////////
void Ogre2Material::ClearLightMap()
{
this->lightMapName = "";
this->dataPtr->lightMapData = nullptr;
this->lightMapUvSet = 0u;
// in ogre 2.2, we swtiched to use the emissive map slot for light map
if (this->ogreDatablock->getUseEmissiveAsLightmap())
this->ogreDatablock->setTexture(Ogre::PBSM_EMISSIVE, this->lightMapName);
this->ogreDatablock->setUseEmissiveAsLightmap(false);
}
//////////////////////////////////////////////////
void Ogre2Material::SetRoughness(const float _roughness)
{
this->ogreDatablock->setRoughness(_roughness);
}
//////////////////////////////////////////////////
float Ogre2Material::Roughness() const
{
return this->ogreDatablock->getRoughness();
}
//////////////////////////////////////////////////
void Ogre2Material::SetMetalness(const float _metalness)
{
this->ogreDatablock->setMetalness(_metalness);
}
//////////////////////////////////////////////////
float Ogre2Material::Metalness() const
{
return this->ogreDatablock->getMetalness();
}
//////////////////////////////////////////////////
void Ogre2Material::PreRender()
{
this->UpdateShaderParams();
}
//////////////////////////////////////////////////
void Ogre2Material::UpdateShaderParams()
{
if (this->dataPtr->vertexShaderParams &&
this->dataPtr->vertexShaderParams->IsDirty())
{
Ogre::MaterialPtr mat[2] = { this->Material(),
this->dataPtr->ogreSolidColorMat };
for (int i = 0; i < 2; ++i)
{
Ogre::GpuProgramParametersSharedPtr ogreParams;
auto pass = mat[i]->getTechnique(0u)->getPass(0);
ogreParams = pass->getVertexProgramParameters();
this->UpdateShaderParams(this->dataPtr->vertexShaderParams, ogreParams);
}
this->dataPtr->vertexShaderParams->ClearDirty();
}
if (this->dataPtr->fragmentShaderParams &&
this->dataPtr->fragmentShaderParams->IsDirty())
{
Ogre::GpuProgramParametersSharedPtr ogreParams;
auto mat = this->Material();
auto pass = mat->getTechnique(0u)->getPass(0);
ogreParams = pass->getFragmentProgramParameters();
this->UpdateShaderParams(this->dataPtr->fragmentShaderParams, ogreParams);
this->dataPtr->fragmentShaderParams->ClearDirty();
}
}
//////////////////////////////////////////////////
void Ogre2Material::UpdateShaderParams(ConstShaderParamsPtr _params,
Ogre::GpuProgramParametersSharedPtr _ogreParams)
{
for (const auto &name_param : *_params)
{
auto *constantDef =
Ogre::GpuProgramParameters::getAutoConstantDefinition(name_param.first);
if (constantDef)
{
_ogreParams->setNamedAutoConstant(name_param.first, constantDef->acType);
continue;
}
if (!_ogreParams->_findNamedConstantDefinition(name_param.first) &&
!(Ogre2RenderEngine::Instance()->GraphicsAPI() !=
GraphicsAPI::OPENGL &&
(ShaderParam::PARAM_TEXTURE == name_param.second.Type() ||
ShaderParam::PARAM_TEXTURE_CUBE == name_param.second.Type())))
{
gzwarn << "Unable to find GPU program parameter: "
<< name_param.first << std::endl;
continue;
}
if (ShaderParam::PARAM_FLOAT == name_param.second.Type())
{
float value;
name_param.second.Value(&value);
_ogreParams->setNamedConstant(name_param.first, value);
}
else if (ShaderParam::PARAM_INT == name_param.second.Type())
{
int value;
name_param.second.Value(&value);
_ogreParams->setNamedConstant(name_param.first, value);
}
else if (ShaderParam::PARAM_FLOAT_BUFFER == name_param.second.Type())
{
std::shared_ptr<void> buffer;
name_param.second.Buffer(buffer);
uint32_t count = name_param.second.Count();
// multiple other than 4 is currently only supported by GLSL
uint32_t multiple = 1;
_ogreParams->setNamedConstant(name_param.first,
reinterpret_cast<float*>(buffer.get()), count, multiple);
}
else if (ShaderParam::PARAM_INT_BUFFER == name_param.second.Type())
{
std::shared_ptr<void> buffer;
name_param.second.Buffer(buffer);
uint32_t count = name_param.second.Count();
// multiple other than 4 is currently only supported by GLSL
uint32_t multiple = 1;
_ogreParams->setNamedConstant(name_param.first,
reinterpret_cast<int*>(buffer.get()), count, multiple);
}
else if (ShaderParam::PARAM_TEXTURE == name_param.second.Type() ||
ShaderParam::PARAM_TEXTURE_CUBE == name_param.second.Type())
{
// add the textures to the resource path
std::string value;
uint32_t uvSetIndex = 0;
name_param.second.Value(value, uvSetIndex);
ShaderParam::ParamType type = name_param.second.Type();
std::string baseName = value;
std::string dirPath = value;
if (common::isFile(value))
{
baseName = common::basename(value);
size_t idx = value.rfind(baseName);
if (idx != std::string::npos)
{
dirPath = value.substr(0, idx);
if (!dirPath.empty() &&
!Ogre::ResourceGroupManager::getSingleton().resourceLocationExists(
dirPath))
{
Ogre::ResourceGroupManager::getSingleton().addResourceLocation(
dirPath, "FileSystem", "General");
}
}
}
else
{
gzerr << "Shader param texture not found: " << value << std::endl;
continue;
}
// get the material and create the texture unit state it does not exist
auto mat = this->Material();
auto pass = mat->getTechnique(0u)->getPass(0);
auto texUnit = pass->getTextureUnitState(name_param.first);
if (!texUnit)
{
texUnit = pass->createTextureUnitState();
texUnit->setName(name_param.first);
}
// make sure to cast to int before calling setNamedConstant later
// to set the texture index
int texIndex = static_cast<int>(pass->getTextureUnitStateIndex(texUnit));
// set texture coordinate set
texUnit->setTextureCoordSet(uvSetIndex);
// set to wrap mode otherwise default is clamp mode
Ogre::HlmsSamplerblock samplerBlockRef;
samplerBlockRef.mU = Ogre::TAM_WRAP;
samplerBlockRef.mV = Ogre::TAM_WRAP;
samplerBlockRef.mW = Ogre::TAM_WRAP;
texUnit->setSamplerblock(samplerBlockRef);
// regular 2d texture
if (type == ShaderParam::ParamType::PARAM_TEXTURE)
{
texUnit->setTextureName(baseName, Ogre::TextureTypes::Type2D);
}
// cube maps
else if (type == ShaderParam::ParamType::PARAM_TEXTURE_CUBE)
{
texUnit->setCubicTextureName(baseName, true);
// must apply this check for Metal rendering to work
// (i.e. not segfault). See the discussion in:
// https://github.com/gazebosim/gz-rendering/pull/541
if (texUnit->isLoaded())
{
texUnit->_load();
}
}
else
{
gzerr << "Unrecognized texture type set for shader param: "
<< name_param.first << std::endl;
continue;
}
if (Ogre2RenderEngine::Instance()->GraphicsAPI() ==
GraphicsAPI::OPENGL)
{
// set the texture map index
_ogreParams->setNamedConstant(name_param.first, &texIndex, 1, 1);
}
}
}
}
//////////////////////////////////////////////////
Ogre::MaterialPtr Ogre2Material::Material()
{
if (!this->ogreMaterial)
{
// low level ogre material used by render targets