Skylicht Engine
Loading...
Searching...
No Matches
CMaterial.h
1/*
2!@
3MIT License
4
5Copyright (c) 2019 Skylicht Technology CO., LTD
6
7Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files
8(the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify,
9merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
10subject to the following conditions:
11
12The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
13
14THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
15INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
16IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
17WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19
20This file is part of the "Skylicht Engine".
21https://github.com/skylicht-lab/skylicht-engine
22!#
23*/
24
25#pragma once
26
27#include "Shader/CShaderParams.h"
28#include "Shader/CShader.h"
29
30#include "GameObject/CGameObject.h"
31
32namespace Skylicht
33{
60
83 class SKYLICHT_API CMaterial : public IReferenceCounted
84 {
85 public:
86
93 {
95 std::string Name;
96
98 std::string Path;
99
102
105
107 // @see irr::video::E_TEXTURE_CLAMP
108 int WrapU;
109
111 // @see irr::video::E_TEXTURE_CLAMP
112 int WrapV;
113
116
119
122
127 {
128 Texture = NULL;
129 TextureSlot = -1;
130 WrapU = 0; // Repeat
131 WrapV = 0; // Repeat
132 Anisotropic = 2;
133 Trilinear = false;
134 Bilinear = false;
135 }
136
142 {
144 c->Name = Name;
145 c->Path = Path;
146 c->Texture = Texture;
148 c->WrapU = WrapU;
149 c->WrapV = WrapV;
151 c->Trilinear = Trilinear;
152 c->Bilinear = Bilinear;
153 return c;
154 }
155 };
156
163 {
164 // Name of the uniform
165 std::string Name;
166
167 // Value data (up to 4 floats)
168 float FloatValue[4];
169
170 // Number of floats used
171 int FloatSize;
172
173 // True if value comes from shader default
174 bool ShaderDefaultValue;
175
176 // Shader uniform type
177 EUniformType Type;
178
179 // Shader parameter index
180 int ValueIndex;
181
186 {
187 ShaderDefaultValue = false;
188 FloatSize = 0;
189 Type = NUM_SHADER_TYPE;
190 ValueIndex = -1;
191 memset(FloatValue, 0, 4 * sizeof(float));
192 }
193
199 {
200 SUniformValue* c = new SUniformValue();
201 memcpy(c->FloatValue, FloatValue, sizeof(float) * 4);
202 c->Name = Name;
203 c->FloatSize = FloatSize;
204 c->ShaderDefaultValue = ShaderDefaultValue;
205 c->Type = Type;
206 c->ValueIndex = ValueIndex;
207 return c;
208 }
209 };
210
217 {
219 std::string ShaderPath;
220
222 std::vector<SUniformValue*> UniformParams;
223
225 std::vector<SUniformTexture*> UniformTextures;
226 };
227
228 private:
231 CShaderParams m_shaderParams;
232
234 std::string m_package;
235
237 std::string m_materialName;
238
240 std::string m_shaderPath;
241
243 std::string m_materialPath;
244
246 std::vector<SUniformValue*> m_uniformParams;
247
249 std::vector<SUniformTexture*> m_uniformTextures;
250
252 std::vector<SExtraParams*> m_extras;
253
255 ITexture* m_resourceTexture[MATERIAL_MAX_TEXTURES];
256
258 ITexture* m_textures[MATERIAL_MAX_TEXTURES];
259
261 ITexture* m_overrideTextures[CShader::ResourceCount];
262
264 video::E_COMPARISON_FUNC m_zBuffer;
265
267 bool m_zWriteEnable;
268
270 bool m_backfaceCulling;
271
273 bool m_frontfaceCulling;
274
276 bool m_doubleSided;
277
279 bool m_deferred;
280
282 bool m_castShadow;
283
285 bool m_manualInitMaterial;
286
288 CShader* m_shader;
289
290 public:
296 CMaterial(const char* name, const char* shaderPath);
297
301 virtual ~CMaterial();
302
308 {
309 return m_shader;
310 }
311
316 void rename(const char* name)
317 {
318 m_materialName = name;
319 }
320
325 inline const char* getName()
326 {
327 return m_materialName.c_str();
328 }
329
335 inline const char* getPackage()
336 {
337 return m_package.c_str();
338 }
339
345 inline void setPackage(const char* package)
346 {
347 m_package = package;
348 }
349
354 inline const char* getShaderPath()
355 {
356 return m_shaderPath.c_str();
357 }
358
363 inline const char* getMaterialPath()
364 {
365 return m_materialPath.c_str();
366 }
367
372 inline void setMaterialPath(const char* path)
373 {
374 m_materialPath = path;
375 }
376
381 inline bool isDeferred()
382 {
383 return m_deferred;
384 }
385
390 inline void setManualInitTexture(bool b)
391 {
392 m_manualInitMaterial = b;
393 }
394
399
404 void copyTo(CMaterial* mat);
405
411
416
421
427 void setUniform(const char* name, float f);
428
434 void setUniform2(const char* name, float* f);
435
441 void setUniform3(const char* name, float* f);
442
448 void setUniform4(const char* name, float* f);
449
455 void setUniform4(const char* name, const SColor& color);
456
462 const char* getUniformTextureName(int slot);
463
470 void setUniformTexture(const char* name, const char* path, bool loadTexture = true);
471
479 void setUniformTexture(const char* name, const char* path, std::vector<std::string>& folder, bool loadTexture = true);
480
486 void setUniformTexture(const char* name, ITexture* texture);
487
493 void setUniformTextureWrapU(const char* name, int wrapU);
494
500 void setUniformTextureWrapV(const char* name, int wrapV);
501
507 SExtraParams* newExtra(const char* shaderPath);
508
515 void setExtraUniformTexture(SExtraParams* e, const char* name, const char* path);
516
524 void setExtraUniform(SExtraParams* e, const char* name, float* f, int floatSize);
525
532 void setExtraUniformTextureWrapU(SExtraParams* e, const char* name, int wrapU);
533
540 void setExtraUniformTextureWrapV(SExtraParams* e, const char* name, int wrapV);
541
546 inline std::vector<SUniformValue*>& getUniformParams()
547 {
548 return m_uniformParams;
549 }
550
555 inline std::vector<SUniformTexture*>& getUniformTexture()
556 {
557 return m_uniformTextures;
558 }
559
564 inline std::vector<SExtraParams*>& getExtraParams()
565 {
566 return m_extras;
567 }
568
573 inline void setBackfaceCulling(bool b)
574 {
575 m_backfaceCulling = b;
576 }
577
582 inline bool isBackfaceCulling()
583 {
584 return m_backfaceCulling;
585 }
586
591 inline void setFrontfaceCulling(bool b)
592 {
593 m_frontfaceCulling = b;
594 }
595
600 inline bool isFrontfaceCulling()
601 {
602 return m_frontfaceCulling;
603 }
604
609 inline void setZWrite(bool b)
610 {
611 m_zWriteEnable = b;
612 }
613
618 inline bool isZWrite()
619 {
620 return m_zWriteEnable;
621 }
622
628 {
629 m_zBuffer = f;
630 }
631
637 {
638 return m_zBuffer;
639 }
640
646 SUniformValue* getUniform(const char* name);
647
654
662
670
676 {
677 return m_shaderParams;
678 }
679
685 bool haveUniform(const char* name);
686
691
697 void setTexture(ITexture** textures, int num);
698
704 void setTexture(int slot, ITexture* texture);
705
712
718 void setProperty(const std::string& name, const std::string& value);
719
725 std::string getProperty(const std::string& name);
726
731
736
741
746
751 void unloadUniformTexture(const char* name);
752
759 {
760 m_overrideTextures[type] = texture;
761 }
762
763 public:
768 void changeShader(CShader* shader);
769
774 void changeShader(const char* path);
775
781
786
792
799
805 void replaceTexture(ITexture* oldTexture, ITexture* newTexture);
806
811
812 protected:
817
822
827
834
841 SUniformValue* newUniform(const char* name, int floatSize);
842
849
857
865 SUniformValue* newExtraUniform(SExtraParams* e, const char* name, int floatSize);
866
872
878 SExtraParams* getExtraParams(const char* shaderPath);
879
884
889 void reloadExtraParams(const char* shaderPath);
890
897
904 SUniformValue* findExtraParam(const char* name, int floatSize);
905 };
906
907 typedef std::vector<CMaterial*> ArrayMaterial;
908}
void unloadUniformTexture()
Unload all uniform textures.
void setProperty(const std::string &name, const std::string &value)
Set a material property by name.
void setUniformTextureWrapV(const char *name, int wrapV)
Set the wrapping mode in V direction for a uniform texture.
SUniformValue * newUniform(const char *name, int floatSize)
Create a new uniform value with given name and float size.
void setExtraUniform(SExtraParams *e, const char *name, float *f, int floatSize)
Set a value for an extra uniform.
const char * getName()
Get the name of the material.
Definition CMaterial.h:325
void loadDefaultTexture()
Load default textures specified by the shader.
const char * getShaderPath()
Get the shader path of the material.
Definition CMaterial.h:354
void setZWrite(bool b)
Enable or disable Z-buffer writing for the material.
Definition CMaterial.h:609
void setUniform(const char *name, float f)
Set a float value for a named uniform.
CShaderParams & getShaderParams()
Get the shader parameter set for this material.
Definition CMaterial.h:675
bool haveUniform(const char *name)
Returns true if the uniform with the given name exists.
void bindUniformParam()
Bind uniform parameter slots to shader uniforms.
SUniformValue * getUniform(const char *name)
Get a uniform value by name (creates if not found).
void applyMaterial(SMaterial &mat)
Update textures for the passed SMaterial.
void unloadDefaultTexture()
Unload all default textures.
void setUniformTexture(const char *name, const char *path, std::vector< std::string > &folder, bool loadTexture=true)
Set a uniform texture by name and path, searching in specified folders.
void setUniform2(const char *name, float *f)
Set a vec2 value for a named uniform.
void updateShaderParams()
Update internal shader parameter values based on uniforms.
void updateSetTextureSlot()
Update and assign texture slots for all uniforms.
void setUniform3(const char *name, float *f)
Set a vec3 value for a named uniform.
SUniformTexture * newExtraUniformTexture(SExtraParams *e, const char *name)
Create a new extra uniform texture for a specific extra parameter block.
void setManualInitTexture(bool b)
Set manual initialization flag for textures.
Definition CMaterial.h:390
void setExtraUniformTextureWrapU(SExtraParams *e, const char *name, int wrapU)
Set wrapping mode U for extra uniform texture.
void updateTexture(SMaterial &mat)
Replace a texture resource with a new texture in all relevant slots and uniforms.
void setUniform4(const char *name, const SColor &color)
Set a vec4 value for a named uniform from an SColor.
const char * getMaterialPath()
Get the material file path.
Definition CMaterial.h:363
CMaterial(const char *name, const char *shaderPath)
Constructs a material with the given name and shader path.
void deleteAllParams()
Delete all uniform parameters and textures.
void copyParamsTo(CMaterial *mat)
Copy only uniform parameters to another material.
CShader * getShader()
Get the shader associated with this material.
Definition CMaterial.h:307
void copyTo(CMaterial *mat)
Copy all parameters and resources to another material.
void setTexture(ITexture **textures, int num)
Set textures for all slots using an array.
SUniformTexture * findExtraTexture(const char *name)
Find an extra texture by name among all extra parameter blocks.
void replaceTexture(ITexture *oldTexture, ITexture *newTexture)
Replace a texture resource with a new texture in all relevant slots and uniforms.
std::vector< SUniformValue * > & getUniformParams()
Get a reference to the uniform value parameters vector.
Definition CMaterial.h:546
void setDefaultValue(SUniformValue *v, SUniform *u)
Set default value for a uniform value using shader information.
void unloadUniformTexture(const char *name)
Unload a specific uniform texture by name.
bool isBackfaceCulling()
Returns whether backface culling is enabled.
Definition CMaterial.h:582
const char * getPackage()
Get the package name of the material.
Definition CMaterial.h:335
SUniformValue * findExtraParam(const char *name, int floatSize)
Find an extra uniform value by name and float size among all extra parameter blocks.
SUniformTexture * getUniformTexture(const char *name)
Get a uniform texture by name (creates if not found).
bool isFrontfaceCulling()
Returns whether frontface culling is enabled.
Definition CMaterial.h:600
void addShaderUI(CShader::SUniformUI *ui)
Add a shader UI control (from shader XML) to the material's uniforms.
SUniformValue * getExtraUniform(SExtraParams *e, const char *name)
Get an extra uniform value by name (creates if not found).
void reloadExtraParams(const char *shaderPath)
Reload extra parameters for the given shader path.
std::vector< SExtraParams * > & getExtraParams()
Get a reference to the extra parameters vector.
Definition CMaterial.h:564
void changeShader(CShader *shader)
Change the shader for this material.
CMaterial * clone()
Create a deep clone of this material.
const char * getUniformTextureName(int slot)
Get the name of a uniform texture by slot index.
SExtraParams * getExtraParams(const char *shaderPath)
Get extra parameter block by shader path.
void setZTest(video::E_COMPARISON_FUNC f)
Set the Z-buffer comparison function.
Definition CMaterial.h:627
void setFrontfaceCulling(bool b)
Enable or disable frontface culling for the material.
Definition CMaterial.h:591
void setUniformTexture(const char *name, ITexture *texture)
Set a uniform texture by name, using an ITexture pointer.
SUniformTexture * newUniformTexture(const char *name)
Create a new uniform texture with given name.
void applyMaterial()
Apply material properties and textures internally (no arguments).
void setOverrideResource(ITexture *texture, CShader::EResourceType type)
Override a resource texture for this material.
Definition CMaterial.h:758
bool isZWrite()
Returns whether Z-buffer writing is enabled.
Definition CMaterial.h:618
void setPackage(const char *package)
Set the package name for the material.
Definition CMaterial.h:345
void setExtraUniformTextureWrapV(SExtraParams *e, const char *name, int wrapV)
Set wrapping mode V for extra uniform texture.
void setMaterialPath(const char *path)
Set the material file path.
Definition CMaterial.h:372
video::E_COMPARISON_FUNC getZTest()
Get the Z-buffer comparison function.
Definition CMaterial.h:636
bool isDeferred()
Returns true if the material is used for deferred rendering.
Definition CMaterial.h:381
void setTexture(int slot, ITexture *texture)
Set the texture for a specific slot.
std::vector< SUniformTexture * > & getUniformTexture()
Get a reference to the uniform texture parameters vector.
Definition CMaterial.h:555
void setBackfaceCulling(bool b)
Enable or disable backface culling for the material.
Definition CMaterial.h:573
void loadUniformTexture()
Load all uniform textures assigned to this material.
void deleteExtraParams()
Delete all extra parameters (used for shader switching).
void setUniform4(const char *name, float *f)
Set a vec4 value for a named uniform.
void initDefaultValue()
Initialize default values for material uniforms based on shader defaults.
void setExtraUniformTexture(SExtraParams *e, const char *name, const char *path)
Set the path for an extra uniform texture.
void setUniformTextureWrapU(const char *name, int wrapU)
Set the wrapping mode in U direction for a uniform texture.
bool autoDetectLoadTexture()
Automatically detect and load textures based on shader and uniform settings.
void rename(const char *name)
Rename the material.
Definition CMaterial.h:316
void initMaterial()
Initializes the material by loading the shader and setting up all parameters.
ITexture * getTexture(int slot)
Get the texture assigned to a slot.
void changeShader(const char *path)
Change the shader for this material using a path.
SExtraParams * newExtra(const char *shaderPath)
Create a new extra parameter block for a specific shader path.
virtual ~CMaterial()
Destructor. Releases textures, deletes all parameters and cleans up resources.
void saveExtraParams()
Save extra parameters for the current shader path.
std::string getProperty(const std::string &name)
Get a material property value by name.
void setUniformTexture(const char *name, const char *path, bool loadTexture=true)
Set a uniform texture by name and path, optionally loading the texture.
SUniformValue * newExtraUniform(SExtraParams *e, const char *name, int floatSize)
Create a new extra uniform value for a specific extra parameter block.
SUniformTexture * getExtraUniformTexture(SExtraParams *e, const char *name)
Get an extra uniform texture by name (creates if not found).
Represents a programmable shader, including uniforms, UI, resources, instancing and platform-specific...
Definition CShader.h:198
EResourceType
Enum for resource types used in shaders.
Definition CShader.h:220
Definition CShaderParams.h:32
IReferenceCounted()
Constructor.
Definition IReferenceCounted.h:50
Interface of a Video Driver dependent Texture.
Definition ITexture.h:119
Class representing a 32 bit ARGB color.
Definition SColor.h:285
Struct for holding parameters for a material renderer.
Definition SMaterial.h:255
Everything in the Skylicht Engine. You can start by looking at the topics.
Definition AudioDebugLog.h:29
EUniformType
Enum for all supported uniform types in shaders. See documentation page: Shader information structure...
Definition CShader.h:39
const u32 MATERIAL_MAX_TEXTURES
Maximum number of texture an SMaterial can have.
Definition SMaterial.h:251
E_COMPARISON_FUNC
Comparison function, e.g. for depth buffer test.
Definition SMaterial.h:63
Structure for storing extra parameters, used when switching shaders.
Definition CMaterial.h:217
std::vector< SUniformTexture * > UniformTextures
Extra uniform textures.
Definition CMaterial.h:225
std::vector< SUniformValue * > UniformParams
Extra uniform values.
Definition CMaterial.h:222
std::string ShaderPath
Associated shader path.
Definition CMaterial.h:219
Structure for a uniform texture parameter in the material.
Definition CMaterial.h:93
bool Bilinear
Bilinear filtering enabled.
Definition CMaterial.h:121
std::string Path
Path to the texture file.
Definition CMaterial.h:98
SUniformTexture * clone()
Creates a deep copy of the uniform texture.
Definition CMaterial.h:141
bool Trilinear
Trilinear filtering enabled.
Definition CMaterial.h:118
int TextureSlot
Texture slot index in the shader.
Definition CMaterial.h:104
int WrapV
Wrapping mode in V direction.
Definition CMaterial.h:112
int Anisotropic
Anisotropic filtering level.
Definition CMaterial.h:115
ITexture * Texture
Pointer to loaded texture resource.
Definition CMaterial.h:101
int WrapU
Wrapping mode in U direction.
Definition CMaterial.h:108
SUniformTexture()
Default constructor initializing default values.
Definition CMaterial.h:126
std::string Name
Name of the uniform texture.
Definition CMaterial.h:95
Structure for a uniform value parameter in the material.
Definition CMaterial.h:163
SUniformValue()
Default constructor initializing default values.
Definition CMaterial.h:185
SUniformValue * clone()
Creates a deep copy of the uniform value.
Definition CMaterial.h:198
Structure for describing a UI element for a uniform. Used for editor integration and presentation.
Definition CShader.h:245
Structure describing a shader uniform. Holds name, type, values, binding info and platform specifics.
Definition CShader.h:98