@@ -0,0 +1,240 @@
//-----------------------------------------------------------------------------
// 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.
//-----------------------------------------------------------------------------

#include "platform/platform.h"
#include "shaderGen/HLSL/shaderFeatureHLSL.h"
#include "shaderGen/HLSL/materialDamageHLSL.h"

#include "shaderGen/langElement.h"
#include "shaderGen/shaderOp.h"
#include "shaderGen/shaderGenVars.h"
#include "gfx/gfxDevice.h"
#include "materials/matInstance.h"
#include "materials/processedMaterial.h"
#include "materials/materialFeatureTypes.h"
#include "core/util/autoPtr.h"

#include "lighting/advanced/advancedLightBinManager.h"

//****************************************************************************
// Damage Texture -Albedo
//****************************************************************************
U32 AlbedoDamageFeatHLSL::getOutputTargets(const MaterialFeatureData &fd) const
{
return fd.features[MFT_isDeferred] ? ShaderFeature::RenderTarget1 : ShaderFeature::DefaultTarget;
}

void AlbedoDamageFeatHLSL::processPix(Vector<ShaderComponent*> &componentList,
const MaterialFeatureData &fd)
{
//determine output target
Var* targ = (Var*)LangElement::find(getOutputTargetVarName(ShaderFeature::DefaultTarget));
if (fd.features[MFT_isDeferred])
{
targ = (Var*)LangElement::find(getOutputTargetVarName(ShaderFeature::RenderTarget1));
}

// Get the texture coord.
Var *texCoord = getInTexCoord("texCoord", "float2", true, componentList);

Var *damage = new Var("materialDamage", "float");
damage->uniform = true;
damage->constSortPos = cspPrimitive;

// create texture var
Var *albedoDamage = new Var;
albedoDamage->setType("sampler2D");
albedoDamage->setName("albedoDamage");
albedoDamage->uniform = true;
albedoDamage->sampler = true;
albedoDamage->constNum = Var::getTexUnitNum(); // used as texture unit num here

LangElement *statement = NULL;
if (fd.features[MFT_Imposter])
{
statement = new GenOp("tex2D(@, @)", albedoDamage, texCoord);
}
else
{
statement = new GenOp("tex2DLinear(@, @)", albedoDamage, texCoord);
}

output = new GenOp(" @ = lerp(@,@,@);\r\n", targ, targ, statement, damage);
}

ShaderFeature::Resources AlbedoDamageFeatHLSL::getResources(const MaterialFeatureData &fd)
{
Resources res;
res.numTex = 1;
res.numTexReg = 1;

return res;
}

void AlbedoDamageFeatHLSL::setTexData(Material::StageData &stageDat,
const MaterialFeatureData &fd,
RenderPassData &passData,
U32 &texIndex)
{
GFXTextureObject *tex = stageDat.getTex(MFT_AlbedoDamage);
if (tex)
{
passData.mTexType[texIndex] = Material::Standard;
passData.mSamplerNames[texIndex] = "albedoDamageMap";
passData.mTexSlot[texIndex++].texObject = tex;
}
}

void AlbedoDamageFeatHLSL::processVert(Vector<ShaderComponent*> &componentList,
const MaterialFeatureData &fd)
{
MultiLine *meta = new MultiLine;
getOutTexCoord("texCoord",
"float2",
true,
fd.features[MFT_TexAnim],
meta,
componentList);
output = meta;
}

//****************************************************************************
// Damage Texture -Composite
//****************************************************************************
void CompositeDamageFeatHLSL::processPix(Vector<ShaderComponent*> &componentList,
const MaterialFeatureData &fd)
{
// Get the texture coord.
Var *texCoord = getInTexCoord("texCoord", "float2", true, componentList);

Var *damage = (Var*)LangElement::find("materialDamage");
if (!damage){
damage = new Var("materialDamage", "float");
damage->uniform = true;
damage->constSortPos = cspPrimitive;
}

// create texture var
Var *damageCMap = new Var;
damageCMap->setType("sampler2D");
damageCMap->setName("compositeDamageMap");
damageCMap->uniform = true;
damageCMap->sampler = true;
damageCMap->constNum = Var::getTexUnitNum(); // used as texture unit num here

Var *damageComposite = new Var;
damageComposite->setType("float4");
damageComposite->setName("compositeDamageColor");

//uniforms
bool declareSpec, declareMetal, declareSmooth;
declareSpec = declareMetal = declareSmooth = false;

Var *specularColor = (Var*)LangElement::find("specularColor");
if (!specularColor) {
specularColor = new Var("specularColor", "float4");
declareSpec = true;
};

Var *metalness = (Var*)LangElement::find("metalness");
if (!metalness){
metalness = new Var("metalness", "float");
declareMetal = true;
}

Var *smoothness = (Var*)LangElement::find("smoothness");
if (!smoothness){
smoothness = new Var("smoothness", "float");
declareSmooth = true;
}

MultiLine * meta = new MultiLine;
meta->addStatement(new GenOp(" @ = tex2D(@, @);\r\n",
new DecOp(damageComposite), damageCMap, texCoord));
if (declareSmooth)
meta->addStatement(new GenOp(" @ = lerp(0.0,@.r,@);\r\n", new DecOp(smoothness), damageComposite, damage));
else
meta->addStatement(new GenOp(" @ = lerp(@,@.r,@);\r\n", smoothness, smoothness, damageComposite, damage));

if (declareSpec)
meta->addStatement(new GenOp(" @ = lerp(float4(1.0,1.0,1.0,1.0),@.ggga,@);\r\n", new DecOp(specularColor), damageComposite, damage));
else
meta->addStatement(new GenOp(" @ = lerp(@,@.ggga,@);\r\n", specularColor, specularColor, damageComposite, damage));

if (declareMetal)
meta->addStatement(new GenOp(" @ = lerp(0.0,@.b,@);\r\n", new DecOp(metalness), damageComposite, damage));
else
meta->addStatement(new GenOp(" @ = lerp(@,@.b,@);\r\n", metalness, metalness, damageComposite, damage));

if (fd.features.hasFeature(MFT_isDeferred))
{
// search for material var
Var *material = (Var*)LangElement::find(getOutputTargetVarName(ShaderFeature::RenderTarget2));
if (!material)
{
// create material var
material = new Var;
material->setType("fragout");
material->setName(getOutputTargetVarName(ShaderFeature::RenderTarget2));
material->setStructName("OUT");
}

meta->addStatement(new GenOp(" @ = float4(0.0,@,@.g,@);\r\n", material, smoothness, specularColor, metalness));
}
output = meta;
}

ShaderFeature::Resources CompositeDamageFeatHLSL::getResources(const MaterialFeatureData &fd)
{
Resources res;
res.numTex = 1;
res.numTexReg = 1;

return res;
}

void CompositeDamageFeatHLSL::setTexData(Material::StageData &stageDat,
const MaterialFeatureData &fd,
RenderPassData &passData,
U32 &texIndex)
{
GFXTextureObject *tex = stageDat.getTex(MFT_CompositeDamage);
if (tex)
{
passData.mTexType[texIndex] = Material::Standard;
passData.mSamplerNames[texIndex] = "compositeDamageMap";
passData.mTexSlot[texIndex++].texObject = tex;
}
}

void CompositeDamageFeatHLSL::processVert(Vector<ShaderComponent*> &componentList,
const MaterialFeatureData &fd)
{
MultiLine *meta = new MultiLine;
getOutTexCoord("texCoord",
"float2",
true,
fd.features[MFT_TexAnim],
meta,
componentList);
output = meta;
}
@@ -0,0 +1,82 @@
//-----------------------------------------------------------------------------
// 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 _SHADERGEN_HLSL_DAMAGEFEATUREHLSL_H_
#define _SHADERGEN_HLSL_DAMAGEFEATUREHLSL_H_

#ifndef _SHADERGEN_HLSL_SHADERFEATUREHLSL_H_
#include "shaderGen/HLSL/shaderFeatureHLSL.h"
#endif

/// Damage texture - Albedo
class AlbedoDamageFeatHLSL : public ShaderFeatureHLSL
{
public:
virtual void processVert(Vector<ShaderComponent*> &componentList,
const MaterialFeatureData &fd);

virtual void processPix(Vector<ShaderComponent*> &componentList,
const MaterialFeatureData &fd);

virtual U32 getOutputTargets(const MaterialFeatureData &fd) const;
virtual Material::BlendOp getBlendOp(){ return Material::LerpAlpha; }

virtual Resources getResources(const MaterialFeatureData &fd);

// Sets textures and texture flags for current pass
virtual void setTexData(Material::StageData &stageDat,
const MaterialFeatureData &fd,
RenderPassData &passData,
U32 &texIndex);

virtual String getName()
{
return "Damage Albedo Texture";
}
};

/// Damage texture - Composite
class CompositeDamageFeatHLSL : public ShaderFeatureHLSL
{
public:
virtual void processVert(Vector<ShaderComponent*> &componentList,
const MaterialFeatureData &fd);

virtual void processPix(Vector<ShaderComponent*> &componentList,
const MaterialFeatureData &fd);

virtual Material::BlendOp getBlendOp(){ return Material::LerpAlpha; }

virtual Resources getResources(const MaterialFeatureData &fd);

// Sets textures and texture flags for current pass
virtual void setTexData(Material::StageData &stageDat,
const MaterialFeatureData &fd,
RenderPassData &passData,
U32 &texIndex);

virtual String getName()
{
return "Damage Composite Texture";
}
};

#endif
@@ -35,6 +35,7 @@
// Deferred Shading
#include "lighting/advanced/hlsl/deferredShadingFeaturesHLSL.h"
#include "shaderGen/HLSL/accuFeatureHLSL.h"
#include "shaderGen/HLSL/materialDamageHLSL.h"

static ShaderGen::ShaderGenInitDelegate sInitDelegate;

@@ -76,6 +77,12 @@ void _initShaderGenHLSL( ShaderGen *shaderGen )
FEATUREMGR->registerFeature( MFT_RenderTarget2_Zero, new RenderTargetZeroHLSL( ShaderFeature::RenderTarget2 ) );
FEATUREMGR->registerFeature( MFT_RenderTarget3_Zero, new RenderTargetZeroHLSL( ShaderFeature::RenderTarget3 ) );
FEATUREMGR->registerFeature( MFT_Imposter, new NamedFeatureHLSL( "Imposter" ) );

//damage blend
FEATUREMGR->registerFeature(MFT_AlbedoDamage, new AlbedoDamageFeatHLSL);
FEATUREMGR->registerFeature(MFT_NormalDamage, new NamedFeatureHLSL("Damage Normal Map"));
FEATUREMGR->registerFeature(MFT_CompositeDamage, new CompositeDamageFeatHLSL);
FEATUREMGR->registerFeature(MFT_Damage, new NamedFeatureHLSL("materialDamage"));

FEATUREMGR->registerFeature( MFT_DiffuseMapAtlas, new NamedFeatureHLSL( "Diffuse Map Atlas" ) );
FEATUREMGR->registerFeature( MFT_NormalMapAtlas, new NamedFeatureHLSL( "Normal Map Atlas" ) );
@@ -40,6 +40,7 @@ const String ShaderGenVars::nearPlaneWorld("$nearPlaneWorld");
const String ShaderGenVars::fogData("$fogData");
const String ShaderGenVars::fogColor("$fogColor");
const String ShaderGenVars::detailScale("$detailScale");
const String ShaderGenVars::materialDamage("$materialDamage");
const String ShaderGenVars::visibility("$visibility");
const String ShaderGenVars::colorMultiply("$colorMultiply");
const String ShaderGenVars::alphaTestValue("$alphaTestValue");
@@ -83,4 +84,4 @@ const String ShaderGenVars::dLightMask("$dlightMask");
const String ShaderGenVars::toneMap("$toneMap");

// Deferred shading
const String ShaderGenVars::matInfoFlags("$matInfoFlags");
const String ShaderGenVars::matInfoFlags("$matInfoFlags");
@@ -49,6 +49,7 @@ struct ShaderGenVars
const static String fogData;
const static String fogColor;
const static String detailScale;
const static String materialDamage;
const static String visibility;
const static String colorMultiply;
const static String alphaTestValue;
@@ -212,6 +212,7 @@ void TSMesh::innerRender( TSMaterialList *materials, const TSRenderState &rdata,

coreRI->visibility = meshVisibility;
coreRI->cubemap = rdata.getCubemap();
coreRI->mMaterialDamage = rdata.getMaterialDamage();

// NOTICE: SFXBB is removed and refraction is disabled!
//coreRI->backBuffTex = GFX->getSfxBackBuffer();
@@ -34,6 +34,7 @@ TSRenderState::TSRenderState()
mCuller( NULL ),
mLightQuery( NULL ),
mUseOriginSort( false ),
mMaterialDamage(0.0f),
mAccuTex( NULL )
{
}
@@ -47,6 +48,7 @@ TSRenderState::TSRenderState( const TSRenderState &state )
mMaterialHint( state.mMaterialHint ),
mCuller( state.mCuller ),
mLightQuery( state.mLightQuery ),
mUseOriginSort( state.mUseOriginSort )
mUseOriginSort( state.mUseOriginSort ),
mMaterialDamage(state.mMaterialDamage)
{
}
@@ -155,6 +155,13 @@ class TSRenderState
void setLightQuery( LightQuery *query ) { mLightQuery = query; }
LightQuery* getLightQuery() const { return mLightQuery; }

/// @}protected:
F32 mMaterialDamage;
public:
const F32 getMaterialDamage() const
{ return mMaterialDamage; }
void setMaterialDamage(const F32 val)
{ mMaterialDamage = val; }
///@see mAccuTex
void setAccuTex( GFXTextureObject* query ) { mAccuTex = query; }
GFXTextureObject* getAccuTex() const { return mAccuTex; }
@@ -0,0 +1,11 @@

singleton TSShapeConstructor(PBRTEST2Dae)
{
baseShape = "./PBRTEST2.dae";
};

function PBRTEST2Dae::onLoad(%this)
{
%this.addNode("Col-1", "", "0 0 0 0 0 1 0", "0");
%this.addCollisionDetail("-1", "Sphere", "Bounds", "4", "30", "30", "32", "30", "30", "30");
}
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

Large diffs are not rendered by default.

@@ -126,6 +126,7 @@ function destroyMaterialEditor()
lightingPropertiesRollout.Expanded = false;
materialAnimationPropertiesRollout.Expanded = false;
materialAdvancedPropertiesRollout.Expanded = false;
materialDamagePropertiesRollout.Expanded = false;
WorldEditorPlugin.onActivated();

EditorGui-->MatEdPropertiesWindow.setVisible( true );
@@ -896,18 +896,7 @@ singleton Material(notDirtyMaterial)
MaterialEditorPropertiesWindow-->toneMapNameText.setText( (%material).toneMap[%layer] );
MaterialEditorPropertiesWindow-->toneMapDisplayBitmap.setBitmap( (%material).toneMap[%layer] );
}

if((%material).translucencyMap[%layer] $= "")
{
MaterialEditorPropertiesWindow-->translucencyMapNameText.setText( "None" );
MaterialEditorPropertiesWindow-->translucencyMapDisplayBitmap.setBitmap( "tools/materialeditor/gui/unknownImage" );
}
else
{
MaterialEditorPropertiesWindow-->translucencyMapNameText.setText( (%material).translucencyMap[%layer] );
MaterialEditorPropertiesWindow-->translucencyMapDisplayBitmap.setBitmap( (%material).translucencyMap[%layer] );
}


if((%material).specularMap[%layer] $= "")
{
MaterialEditorPropertiesWindow-->specMapNameText.setText( "None" );
@@ -953,6 +942,41 @@ singleton Material(notDirtyMaterial)
MaterialEditorPropertiesWindow-->metalMapDisplayBitmap.setBitmap( (%material).metalMap[%layer] );
}

// material damage

if((%material).albedoDamageMap[%layer] $= "")
{
MaterialEditorPropertiesWindow-->albedoDamageMapNameText.setText( "None" );
MaterialEditorPropertiesWindow-->albedoDamageMapDisplayBitmap.setBitmap( "tools/materialeditor/gui/unknownImage" );
}
else
{
MaterialEditorPropertiesWindow-->albedoDamageMapNameText.setText( (%material).albedoDamageMap[%layer] );
MaterialEditorPropertiesWindow-->albedoDamageMapDisplayBitmap.setBitmap( (%material).albedoDamageMap[%layer] );
}

if((%material).normalDamageMap[%layer] $= "")
{
MaterialEditorPropertiesWindow-->normalDamageMapNameText.setText( "None" );
MaterialEditorPropertiesWindow-->normalDamageMapDisplayBitmap.setBitmap( "tools/materialeditor/gui/unknownImage" );
}
else
{
MaterialEditorPropertiesWindow-->normalDamageMapNameText.setText( (%material).normalDamageMap[%layer] );
MaterialEditorPropertiesWindow-->normalDamageMapDisplayBitmap.setBitmap( (%material).normalDamageMap[%layer] );
}

if((%material).compositeDamageMap[%layer] $= "")
{
MaterialEditorPropertiesWindow-->compositeDamageMapNameText.setText( "None" );
MaterialEditorPropertiesWindow-->compositeDamageMapDisplayBitmap.setBitmap( "tools/materialeditor/gui/unknownImage" );
}
else
{
MaterialEditorPropertiesWindow-->compositeDamageMapNameText.setText( (%material).normalDamageMap[%layer] );
MaterialEditorPropertiesWindow-->compositeDamageMapDisplayBitmap.setBitmap( (%material).compositeDamageMap[%layer] );
}

MaterialEditorPropertiesWindow-->accuScaleTextEdit.setText((%material).accuScale[%layer]);
MaterialEditorPropertiesWindow-->accuScaleTextEdit.setText((%material).accuScale[%layer]);
MaterialEditorPropertiesWindow-->accuDirectionTextEdit.setText((%material).accuDirection[%layer]);
@@ -974,7 +998,6 @@ singleton Material(notDirtyMaterial)
MaterialEditorPropertiesWindow-->SmoothnessSlider.setValue((%material).Smoothness[%layer]);
MaterialEditorPropertiesWindow-->MetalnessTextEdit.setText((%material).Metalness[%layer]);
MaterialEditorPropertiesWindow-->MetalnessSlider.setValue((%material).Metalness[%layer]);
MaterialEditorPropertiesWindow-->pixelSpecularCheckbox.setValue((%material).pixelSpecular[%layer]);
MaterialEditorPropertiesWindow-->glowCheckbox.setValue((%material).glow[%layer]);
MaterialEditorPropertiesWindow-->emissiveCheckbox.setValue((%material).emissive[%layer]);
MaterialEditorPropertiesWindow-->parallaxTextEdit.setText((%material).parallaxScale[%layer]);