Page 1 of 2

Modify Material opacity via C++ code

Posted: Tue Jul 18, 2017 1:46 pm
by savi
Hey, I'm trying to simply change the opacity of a material runtime. I have looked through examples and found this line:

Code: Select all

GetEntity()->GetMaterial()->SetGetMaterialParamFloat("opacity", m_fTransparentValue, false);
and also

Code: Select all

GetEntity()->SetOpacity(0.2f);
But both of these code snippets don't change anything in the opacity of the target object. I have added a screen of the custom material I created for this object, I guess I forgot to change something to enable the runtime changes? Otherwise I really have no clue what I'm missing.

Thanks in advance,

Savas

Re: Modify Material opacity via C++ code

Posted: Tue Jul 18, 2017 2:12 pm
by mknmknmknjk
check illum.ext illum.cfx

FlowEntityNode.cpp ProcessEvent

Code: Select all

const SShaderItem& shaderItem = pMtl->GetShaderItem(); IRenderShaderResources* pRendRes = shaderItem.m_pShaderResources; DynArrayRef<SShaderParam>& params = pRendRes->GetParameters(); if (pMtl->SetGetMaterialParamFloat(paramNameFloat, floatValue, false) == false) { UParamVal val; val.m_Float = floatValue; SShaderParam::SetParam(paramNameFloat, &params, val); bUpdateShaderConstants = true; } if (bUpdateShaderConstants) { shaderItem.m_pShaderResources->UpdateConstants(shaderItem.m_pShader); } }

Re: Modify Material opacity via C++ code

Posted: Tue Jul 18, 2017 2:32 pm
by mknmknmknjk
and
{ EFTT_OPACITY, "EFTT_OPACITY", true, "Opacity", "" },

and
enum EEfResTextures
{
EFTT_DIFFUSE = 0,
EFTT_NORMALS,
EFTT_SPECULAR,
EFTT_ENV,
EFTT_DETAIL_OVERLAY,
EFTT_SMOOTHNESS,
EFTT_HEIGHT,
EFTT_DECAL_OVERLAY,
EFTT_SUBSURFACE,
EFTT_CUSTOM,
EFTT_CUSTOM_SECONDARY,
EFTT_OPACITY,
EFTT_TRANSLUCENCY,
EFTT_EMITTANCE,

EFTT_MAX,
EFTT_UNKNOWN = EFTT_MAX
};

Do you use OPACITY Texture Slot?


Code: Select all

SComponentParams::MakeMaterial(CParticleComponent* pComponent) m_pMaterial = gEnv->p3DEngine->GetMaterialManager()->CreateMaterial(materialName); if (gEnv->pRenderer) { SInputShaderResourcesPtr pResources = gEnv->pRenderer->EF_CreateInputShaderResource(); pResources->m_Textures[EFTT_DIFFUSE].m_Name = m_diffuseMap; uint32 mask = eGpuParticlesVertexShaderFlags_None; if (params.facingMode == 1) mask |= eGpuParticlesVertexShaderFlags_FacingVelocity; SShaderItem shaderItem = gEnv->pRenderer->EF_LoadShaderItem(shaderName, false, 0, pResources, mask); m_pMaterial->AssignShaderItem(shaderItem); } Vec3 white = Vec3(1.0f, 1.0f, 1.0f); float defaultOpacity = 1.0f; m_pMaterial->SetGetMaterialParamVec3("diffuse", white, false); m_pMaterial->SetGetMaterialParamFloat("opacity", defaultOpacity, false); m_pMaterial->RequestTexturesLoading(0.0f);

Re: Modify Material opacity via C++ code

Posted: Tue Jul 18, 2017 2:38 pm
by savi
What you are sharing does use these lines of code to manipulate the properties of the material. I'm already using these functions but they have no impact.

Code: Select all

Vec3 white = Vec3(1.0f, 1.0f, 1.0f); float defaultOpacity = 1.0f; m_pMaterial->SetGetMaterialParamVec3("diffuse", white, false); m_pMaterial->SetGetMaterialParamFloat("opacity", defaultOpacity, false);
What did you mean with
check illum.ext illum.cfx
I don't find files like these in my project folder and web search is also not one of cryengines strongest features.

[Edit]
Since I give him the parameter "opacity" it should get the Texture Slot "EFTT_OPACITY" according to this function.

Code: Select all

bool MaterialHelpers::SetGetMaterialParamFloat(IRenderShaderResources& pShaderResources, const char* sParamName, float& v, bool bGet) const { EEfResTextures texSlot = EFTT_UNKNOWN; if (!stricmp("emissive_intensity", sParamName)) texSlot = EFTT_EMITTANCE; else if (!stricmp("shininess", sParamName)) texSlot = EFTT_SMOOTHNESS; else if (!stricmp("opacity", sParamName)) texSlot = EFTT_OPACITY; if (!stricmp("alpha", sParamName)) { if (bGet) v = pShaderResources.GetAlphaRef(); else pShaderResources.SetAlphaRef(v); return true; } else if (texSlot != EFTT_UNKNOWN) { if (bGet) v = pShaderResources.GetStrengthValue(texSlot); else pShaderResources.SetStrengthValue(texSlot, v); return true; } return false; }

Re: Modify Material opacity via C++ code

Posted: Tue Jul 18, 2017 2:59 pm
by mknmknmknjk
i guess:
first
the parameter only works when you use related Texture slot , which is the right hand Texture Map.

second

the parameter is not runtime changeable .
only the shader Parameter session is runtime changeable

https://www.youtube.com/watch?v=FuRhCPQar0w&t=230s

use

Code: Select all

DynArrayRef<SShaderParam>& shaderParams = sItem.m_pShaderResources->GetParameters(); for (uint32 i = 0, si = shaderParams.size(); i < si; ++i)
to check if the shader has opacity param.

the illum.ext illum.cfx is in github sourcecode.

could you add breakpoint in SetGetMaterialParamFloat to check where it return;

Re: Modify Material opacity via C++ code

Posted: Tue Jul 18, 2017 3:11 pm
by mknmknmknjk
try

Code: Select all

if (pMtl->SetGetMaterialParamFloat(paramNameFloat, floatValue, false) == false) { UParamVal val; val.m_Float = floatValue; SShaderParam::SetParam(paramNameFloat, &params, val); bUpdateShaderConstants = true; } if (bUpdateShaderConstants) { shaderItem.m_pShaderResources->UpdateConstants(shaderItem.m_pShader); }
or

Code: Select all

m_pMaterial->SetGetMaterialParamFloat("opacity", defaultOpacity, false); m_pMaterial->RequestTexturesLoading(0.0f);
enum EEfResTextures
{
EFTT_DIFFUSE = 0,
EFTT_NORMALS,
EFTT_SPECULAR,
EFTT_ENV,
EFTT_DETAIL_OVERLAY,
EFTT_SMOOTHNESS,
EFTT_HEIGHT,
EFTT_DECAL_OVERLAY,
EFTT_SUBSURFACE,
EFTT_CUSTOM,
EFTT_CUSTOM_SECONDARY,
EFTT_OPACITY,
EFTT_TRANSLUCENCY,
EFTT_EMITTANCE,

EFTT_MAX,
EFTT_UNKNOWN = EFTT_MAX
};
the enum is maped to Texture Map righthand side.

Re: Modify Material opacity via C++ code

Posted: Tue Jul 18, 2017 3:21 pm
by savi
I have tested which parameters are available in the Illum shader and it's these two:
  • EmittanceMapGamma
    SSSIndex

He does return Opacity, I have debugged it. I'll try the latest you have written. When I change the opacity slider within the editor window itself it does get transparent. So since it is missing but the functionality is still given, it doesn't seem to important that it is within this list?

Edit://

I did test your two code snippets and SetGetMaterialParamFloat does return true, but even with the other 2 lines of updating constants and requesting texture loading, it does not change the opacity of this damn cube. >.< I don't get it.. maybe sandbox does switch or expand the shader when it encounters the fact that illum is maybe not made for showing opacity objects?

Re: Modify Material opacity via C++ code

Posted: Thu Jul 20, 2017 7:58 am
by fury22uk
How do you get the material itself? If no custom material assigned then it won't work. Try using

Code: Select all

GetEntity()->GetStatObj()->GetMaterial();

Re: Modify Material opacity via C++ code

Posted: Thu Jul 20, 2017 10:35 am
by savi
I did get it with

Code: Select all

GetEntity()->GetMaterial()
and I got a Material back. I did set a custom material in the editor though.
Using your way of getting a material does also not change the fact that the opacity remains untouched.

I already accepted that I will never find the answer and created a second material with predefined transparency settings.
Who needs dynamic changes anyway? ._.

Re: Modify Material opacity via C++ code

Posted: Thu Jul 20, 2017 10:54 am
by fury22uk
I use dynamic changes for multiple tasks, but i always work this way. I get the material as i posted before and using SetGetMaterialParamFloat or Vec3 i can change my entity's opacity. Although I use GetEntity()->GetStatObj()->SetMaterial() right after LoadMesh() but before physicalizing and that always work. I never assing material inside the editor.