Torque3D/Engine/source/materials/materialDefinition.h

434 lines
13 KiB
C
Raw Normal View History

2012-09-19 15:15:01 +00:00
//-----------------------------------------------------------------------------
// Copyright (c) 2012 GarageGames, LLC
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
//-----------------------------------------------------------------------------
#ifndef _MATERIALDEFINITION_H_
#define _MATERIALDEFINITION_H_
#ifndef _BASEMATERIALDEFINITION_H_
#include "materials/baseMaterialDefinition.h"
2012-09-19 15:15:01 +00:00
#endif
#ifndef _TDICTIONARY_H_
#include "core/util/tDictionary.h"
2012-09-19 15:15:01 +00:00
#endif
#ifndef _GFXTEXTUREHANDLE_H_
#include "gfx/gfxTextureHandle.h"
2012-09-19 15:15:01 +00:00
#endif
#ifndef _GFXSTRUCTS_H_
#include "gfx/gfxStructs.h"
2012-09-19 15:15:01 +00:00
#endif
#ifndef _GFXCUBEMAP_H_
#include "gfx/gfxCubemap.h"
2012-09-19 15:15:01 +00:00
#endif
#ifndef _DYNAMIC_CONSOLETYPES_H_
#include "console/dynamicTypes.h"
2012-09-19 15:15:01 +00:00
#endif
#ifndef IMAGE_ASSET_H
#include "T3D/assets/ImageAsset.h"
#endif
#ifndef _ASSET_PTR_H_
#include "assets/assetPtr.h"
#endif
2021-10-15 00:09:20 +00:00
#ifndef SOUND_ASSET_H
#include "T3D/assets/SoundAsset.h"
#endif
2012-09-19 15:15:01 +00:00
class CubemapData;
class SFXTrack;
struct SceneData;
class FeatureSet;
class FeatureType;
class MaterialSoundProfile;
class MaterialPhysicsProfile;
/// The basic material definition.
class Material : public BaseMaterialDefinition
{
typedef BaseMaterialDefinition Parent;
public:
static GFXCubemap* GetNormalizeCube();
2012-09-19 15:15:01 +00:00
//-----------------------------------------------------------------------
// Enums
//-----------------------------------------------------------------------
enum Constants
{
MAX_TEX_PER_PASS = 16, ///< Number of textures per pass
2012-09-19 15:15:01 +00:00
MAX_STAGES = 4,
NUM_EFFECT_COLOR_STAGES = 2, ///< Number of effect color definitions for transitioning effects.
};
enum TexType
{
NoTexture = 0,
Standard = 1,
Detail,
Bump,
DetailBump,
Env,
Cube,
SGCube, // scene graph cube - probably dynamic
Lightmap,
ToneMapTex,
Mask,
BackBuff,
ReflectBuff,
Misc,
DynamicLight,
DynamicLightMask,
PhotometricMask,
2012-09-19 15:15:01 +00:00
NormalizeCube,
TexTarget,
AccuMap,
2012-09-19 15:15:01 +00:00
};
enum BlendOp
{
None = 0,
Mul,
PreMul,
2012-09-19 15:15:01 +00:00
Add,
AddAlpha, // add modulated with alpha channel
Sub,
LerpAlpha, // linear interpolation modulated with alpha channel
ToneMap,
NumBlendTypes
};
enum AnimType
{
Scroll = BIT(0),
Rotate = BIT(1),
Wave = BIT(2),
Scale = BIT(3),
Sequence = BIT(4),
2012-09-19 15:15:01 +00:00
};
enum WaveType
{
Sin = 0,
Triangle,
Square,
};
class StageData
{
protected:
///
typedef HashTable<const FeatureType*, GFXTexHandle> TextureTable;
2012-09-19 15:15:01 +00:00
/// The sparse table of textures by feature index.
/// @see getTex
/// @see setTex
TextureTable mTextures;
/// The cubemap for this stage.
GFXCubemap* mCubemap;
2012-09-19 15:15:01 +00:00
public:
StageData()
: mCubemap(NULL)
2012-09-19 15:15:01 +00:00
{
}
/// Returns the texture object or NULL if there is no
/// texture entry for that feature type in the table.
inline GFXTextureObject* getTex(const FeatureType& type) const
2012-09-19 15:15:01 +00:00
{
TextureTable::ConstIterator iter = mTextures.find(&type);
if (iter == mTextures.end())
2012-09-19 15:15:01 +00:00
return NULL;
return iter->value.getPointer();
}
/// Assigns a texture object by feature type.
inline void setTex(const FeatureType& type, GFXTextureObject* tex)
2012-09-19 15:15:01 +00:00
{
if (!tex)
2012-09-19 15:15:01 +00:00
{
TextureTable::Iterator iter = mTextures.find(&type);
if (iter != mTextures.end())
mTextures.erase(iter);
2012-09-19 15:15:01 +00:00
return;
}
TextureTable::Iterator iter = mTextures.findOrInsert(&type);
2012-09-19 15:15:01 +00:00
iter->value = tex;
}
/// Returns true if we have a valid texture assigned to
/// any feature in the texture table.
inline bool hasValidTex() const
{
TextureTable::ConstIterator iter = mTextures.begin();
for (; iter != mTextures.end(); ++iter)
2012-09-19 15:15:01 +00:00
{
if (iter->value.isValid())
2012-09-19 15:15:01 +00:00
return true;
}
return false;
}
/// Returns the active texture features.
void getFeatureSet(FeatureSet* outFeatures) const;
2012-09-19 15:15:01 +00:00
/// Returns the stage cubemap.
GFXCubemap* getCubemap() const { return mCubemap; }
/// Set the stage cubemap.
void setCubemap(GFXCubemap* cubemap) { mCubemap = cubemap; }
2012-09-19 15:15:01 +00:00
};
public:
//-----------------------------------------------------------------------
// Data
//-----------------------------------------------------------------------
DECLARE_IMAGEASSET_ARRAY(Material, DiffuseMap, GFXStaticTextureSRGBProfile, MAX_STAGES)
DECLARE_IMAGEASSET_ARRAY(Material, NormalMap, GFXNormalMapProfile, MAX_STAGES)
DECLARE_IMAGEASSET_ARRAY(Material, DetailNormalMap, GFXNormalMapProfile, MAX_STAGES)
DECLARE_IMAGEASSET_ARRAY(Material, OverlayMap, GFXStaticTextureProfile, MAX_STAGES)
DECLARE_IMAGEASSET_ARRAY(Material, LightMap, GFXStaticTextureProfile, MAX_STAGES)
DECLARE_IMAGEASSET_ARRAY(Material, ToneMap, GFXStaticTextureProfile, MAX_STAGES)
DECLARE_IMAGEASSET_ARRAY(Material, DetailMap, GFXStaticTextureProfile, MAX_STAGES)
DECLARE_IMAGEASSET_ARRAY(Material, ORMConfigMap, GFXStaticTextureProfile, MAX_STAGES)
DECLARE_IMAGEASSET_ARRAY(Material, AOMap, GFXStaticTextureProfile, MAX_STAGES)
DECLARE_IMAGEASSET_ARRAY(Material, RoughMap, GFXStaticTextureProfile, MAX_STAGES)
DECLARE_IMAGEASSET_ARRAY(Material, MetalMap, GFXStaticTextureProfile, MAX_STAGES)
DECLARE_IMAGEASSET_ARRAY(Material, GlowMap, GFXStaticTextureProfile, MAX_STAGES)
2024-12-27 14:38:01 +00:00
bool mDiffuseMapSRGB[MAX_STAGES]; // SRGB diffuse
bool mIsSRGb[MAX_STAGES]; // SRGB ORM
U32 mAOChan[MAX_STAGES];
2020-09-30 18:51:12 +00:00
bool mInvertRoughness[MAX_STAGES];
U32 mRoughnessChan[MAX_STAGES];
U32 mMetalChan[MAX_STAGES];
F32 mGlowMul[MAX_STAGES];
2012-09-19 15:15:01 +00:00
/// The strength scalar for the detail normal map.
2024-12-27 14:38:01 +00:00
F32 mDetailNormalMapStrength[MAX_STAGES];
bool mAccuEnabled[MAX_STAGES];
F32 mAccuScale[MAX_STAGES];
F32 mAccuDirection[MAX_STAGES];
F32 mAccuStrength[MAX_STAGES];
F32 mAccuCoverage[MAX_STAGES];
F32 mAccuSpecular[MAX_STAGES];
2012-09-19 15:15:01 +00:00
/// This color is the diffuse color of the material
/// or if it has a texture it is multiplied against
/// the diffuse texture color.
LinearColorF mDiffuse[MAX_STAGES];
2020-09-30 18:51:12 +00:00
F32 mRoughness[MAX_STAGES];
F32 mMetalness[MAX_STAGES];
2012-09-19 15:15:01 +00:00
bool mVertLit[MAX_STAGES];
2012-09-19 15:15:01 +00:00
/// If true for a stage, vertex colors are multiplied
/// against diffuse colors.
bool mVertColor[MAX_STAGES];
F32 mParallaxScale[MAX_STAGES];
2012-09-19 15:15:01 +00:00
F32 mMinnaertConstant[MAX_STAGES];
bool mSubSurface[MAX_STAGES];
LinearColorF mSubSurfaceColor[MAX_STAGES];
2012-09-19 15:15:01 +00:00
F32 mSubSurfaceRolloff[MAX_STAGES];
/// The repetition scale of the detail texture
/// over the base texture.
Point2F mDetailScale[MAX_STAGES];
U32 mAnimFlags[MAX_STAGES];
Point2F mScrollDir[MAX_STAGES];
F32 mScrollSpeed[MAX_STAGES];
Point2F mScrollOffset[MAX_STAGES];
F32 mRotSpeed[MAX_STAGES];
Point2F mRotPivotOffset[MAX_STAGES];
F32 mRotPos[MAX_STAGES];
2012-09-19 15:15:01 +00:00
F32 mWavePos[MAX_STAGES];
F32 mWaveFreq[MAX_STAGES];
F32 mWaveAmp[MAX_STAGES];
U32 mWaveType[MAX_STAGES];
2012-09-19 15:15:01 +00:00
F32 mSeqFramePerSec[MAX_STAGES];
F32 mSeqSegSize[MAX_STAGES];
2012-09-19 15:15:01 +00:00
bool mGlow[MAX_STAGES]; // entire stage glows
bool mReceiveShadows[MAX_STAGES];
bool mIgnoreLighting[MAX_STAGES];
2012-09-19 15:15:01 +00:00
Point2I mCellIndex[MAX_STAGES];
Point2I mCellLayout[MAX_STAGES];
U32 mCellSize[MAX_STAGES];
bool mNormalMapAtlas[MAX_STAGES];
/// Special array of UVs for imposter rendering.
/// @see TSLastDetail
Vector<RectF> mImposterUVs;
/// Specual imposter rendering paramters.
/// @see TSLastDetail
Point4F mImposterLimits;
/// If the stage should use anisotropic filtering.
bool mUseAnisotropic[MAX_STAGES];
The final step (barring any overlooked missing bits, requested refactors, and of course, rolling in dependencies already submitted as PRs) consists of: renderPrePassMgr.cpp related: A) shifting .addFeature( MFT_XYZ); calls from ProcessedShaderMaterial::_determineFeatures to ProcessedPrePassMaterial::_determineFeatures B) mimicking the "// set the XXX if different" entries from RenderMeshMgr::render in RenderPrePassMgr::render C) fleshing out ProcessedPrePassMaterial::getNumStages() so that it shares a 1:1 correlation with ProcessedShaderMaterial::getNumStages() D) causing inline void Swizzle<T, mapLength>::ToBuffer( void *destination, const void *source, const dsize_t size ) to silently fail rather than fatally assert if a source or destination buffer is not yet ready to be filled. (support for #customTarget scripted render targets) Reflections: A) removing reflectRenderState.disableAdvancedLightingBins(true); entries. this would otherwise early out from prepass and provide no color data whatsoever. B) removing the fd.features.addFeature( MFT_ForwardShading ); entry forcing all materials to be forward lit when reflected. C) 2 things best described bluntly as working hacks: C1) when reflected, a scattersky is rotated PI along it's z then x axis in order to draw properly. C2) along similar lines, in terraincellmaterial, we shut off culling if it's a prepass material. Skies: scattersky is given a pair of rotations for reflection purposes, all sky objects are given a z value for depth testing.
2016-02-16 08:50:49 +00:00
2012-09-19 15:15:01 +00:00
bool mDoubleSided;
String mCubemapName;
CubemapData* mCubemapData;
bool mDynamicCubemap;
The final step (barring any overlooked missing bits, requested refactors, and of course, rolling in dependencies already submitted as PRs) consists of: renderPrePassMgr.cpp related: A) shifting .addFeature( MFT_XYZ); calls from ProcessedShaderMaterial::_determineFeatures to ProcessedPrePassMaterial::_determineFeatures B) mimicking the "// set the XXX if different" entries from RenderMeshMgr::render in RenderPrePassMgr::render C) fleshing out ProcessedPrePassMaterial::getNumStages() so that it shares a 1:1 correlation with ProcessedShaderMaterial::getNumStages() D) causing inline void Swizzle<T, mapLength>::ToBuffer( void *destination, const void *source, const dsize_t size ) to silently fail rather than fatally assert if a source or destination buffer is not yet ready to be filled. (support for #customTarget scripted render targets) Reflections: A) removing reflectRenderState.disableAdvancedLightingBins(true); entries. this would otherwise early out from prepass and provide no color data whatsoever. B) removing the fd.features.addFeature( MFT_ForwardShading ); entry forcing all materials to be forward lit when reflected. C) 2 things best described bluntly as working hacks: C1) when reflected, a scattersky is rotated PI along it's z then x axis in order to draw properly. C2) along similar lines, in terraincellmaterial, we shut off culling if it's a prepass material. Skies: scattersky is given a pair of rotations for reflection purposes, all sky objects are given a z value for depth testing.
2016-02-16 08:50:49 +00:00
// Deferred Shading
F32 mMatInfoFlags[MAX_STAGES];
bool mTranslucent;
2012-09-19 15:15:01 +00:00
BlendOp mTranslucentBlendOp;
bool mTranslucentZWrite;
/// A generic setting which tells the system to skip
/// generation of shadows from this material.
bool mCastShadows;
bool mAlphaTest;
U32 mAlphaRef;
bool mPlanarReflection;
bool mAutoGenerated;
static bool sAllowTextureTargetAssignment;
///@{
/// Behavioral properties.
bool mShowFootprints; ///< If true, show footprints when walking on surface with this material. Defaults to false.
bool mShowDust; ///< If true, show dust emitters (footpuffs, hover trails, etc) when on surface with this material. Defaults to false.
/// Color to use for particle effects and such when located on this material.
LinearColorF mEffectColor[NUM_EFFECT_COLOR_STAGES];
2012-09-19 15:15:01 +00:00
/// Footstep sound to play when walking on surface with this material.
/// Numeric ID of footstep sound defined on player datablock (0 == soft,
/// 1 == hard, 2 == metal, 3 == snow).
/// Defaults to -1 which deactivates default sound.
/// @see mFootstepSoundCustom
S32 mFootstepSoundId;
S32 mImpactSoundId;
S32 mImpactFXIndex;
2012-09-19 15:15:01 +00:00
/// Sound effect to play when walking on surface with this material.
/// If defined, overrides mFootstepSoundId.
/// @see mFootstepSoundId
2021-10-15 00:09:20 +00:00
DECLARE_SOUNDASSET(Material, CustomFootstepSound);
DECLARE_ASSET_SETGET(Material, CustomFootstepSound);
DECLARE_SOUNDASSET(Material, CustomImpactSound);
DECLARE_ASSET_SETGET(Material, CustomImpactSound);
2012-09-19 15:15:01 +00:00
F32 mFriction; ///< Friction coefficient when moving along surface.
F32 mDirectSoundOcclusion; ///< Amount of volume occlusion on direct sounds.
F32 mReverbSoundOcclusion; ///< Amount of volume occlusion on reverb sounds.
///@}
2012-09-19 15:15:01 +00:00
String mMapTo; // map Material to this texture name
///
/// Material interface
///
Material();
/// Allocates and returns a BaseMatInstance for this material. Caller is responsible
/// for freeing the instance
BaseMatInstance* createMatInstance() override;
bool isTranslucent() const override { return mTranslucent && mTranslucentBlendOp != Material::None; }
bool isAlphatest() const override { return mAlphaTest; }
bool isDoubleSided() const override { return mDoubleSided; }
2012-09-19 15:15:01 +00:00
virtual bool isAutoGenerated() const { return mAutoGenerated; }
virtual void setAutoGenerated(bool isAutoGenerated) { mAutoGenerated = isAutoGenerated; }
bool isLightmapped() const override;
bool castsShadows() const override { return mCastShadows; }
const String& getPath() const { return mPath; }
2012-09-19 15:15:01 +00:00
void flush();
/// Re-initializes all the material instances
/// that use this material.
void reload();
/// Called to update time based parameters for a material. Ensures
/// that it only happens once per tick.
void updateTimeBasedParams();
// SimObject
bool onAdd() override;
void onRemove() override;
void inspectPostApply() override;
bool writeField(StringTableEntry fieldname, const char* value) override;
2012-09-19 15:15:01 +00:00
//
// ConsoleObject interface
//
static void initPersistFields();
// Accumulation
static bool _setAccuEnabled(void* object, const char* index, const char* data);
2012-09-19 15:15:01 +00:00
DECLARE_CONOBJECT(Material);
protected:
// Per material animation parameters
U32 mLastUpdateTime;
String mPath;
static EnumTable mAnimFlagTable;
static EnumTable mBlendOpTable;
static EnumTable mWaveTypeTable;
/// Map this material to the texture specified
/// in the "mapTo" data variable.
virtual void _mapMaterial();
private:
static GFXCubemapHandle smNormalizeCube;
};
typedef Material::AnimType MaterialAnimType;
typedef Material::BlendOp MaterialBlendOp;
typedef Material::WaveType MaterialWaveType;
DefineBitfieldType(MaterialAnimType);
DefineEnumType(MaterialBlendOp);
DefineEnumType(MaterialWaveType);
2012-09-19 15:15:01 +00:00
#endif // _MATERIALDEFINITION_H_