MCPcopy Create free account
hub / github.com/defold/defold / SetMaterialTexture

Function SetMaterialTexture

engine/gamesys/src/gamesys/scripts/script_material.cpp:898–952  ·  view source on GitHub ↗

sets textures in a material * Sets textures in a material, if the samplers exist. * * @name material.set_textures * * @param path [type:hash|string] The path to the resource * @param textures [type:table] A table keyed by sampler name with texture resources as values. * * @examples * Set a texture in a material from a resource * * ```lua

Source from the content-addressed store, hash-verified

896 * ```
897 */
898 static int SetMaterialTexture(lua_State* L, MaterialResource* material_res, dmhash_t name_hash, int texture_index)
899 {
900 texture_index = texture_index < 0 ? lua_gettop(L) + texture_index + 1 : texture_index;
901
902 uint32_t unit = dmRender::GetMaterialSamplerUnit(material_res->m_Material, name_hash);
903 if (unit == dmRender::INVALID_SAMPLER_UNIT)
904 {
905 return luaL_error(L, "Material sampler '%s' not found", dmHashReverseSafe64(name_hash));
906 }
907
908 dmhash_t texture_path = dmScript::CheckHashOrString(L, texture_index);
909 TextureResource* texture_res = (TextureResource*) CheckResource(L, g_MaterialModule.m_Factory, texture_path, "texturec");
910
911 dmGraphics::HContext graphics_context = dmRender::GetGraphicsContext(dmRender::GetMaterialRenderContext(material_res->m_Material));
912 dmGraphics::TextureType texture_type_in = dmGraphics::GetTextureType(graphics_context, texture_res->m_Texture);
913 dmRender::HSampler sampler = dmRender::GetMaterialSampler(material_res->m_Material, unit);
914
915 dmRender::SamplerInfo sampler_info = {};
916 dmRender::GetSamplerInfo(sampler, &sampler_info);
917
918 dmGraphics::TextureType normalized_sampler_type = NormalizeBindableTextureType(sampler_info.m_TextureType);
919
920 if (normalized_sampler_type != texture_type_in)
921 {
922 return luaL_error(L, "Texture type mismatch. Can't bind a %s texture to a %s sampler",
923 dmGraphics::GetTextureTypeLiteral(texture_type_in),
924 dmGraphics::GetTextureTypeLiteral(normalized_sampler_type));
925 }
926
927 material_res->m_TextureResourcePaths[unit] = texture_path;
928 material_res->m_SamplerNames[unit] = name_hash;
929
930 if (material_res->m_Textures[unit] != texture_res)
931 {
932 if (material_res->m_Textures[unit])
933 {
934 dmResource::Release(g_MaterialModule.m_Factory, material_res->m_Textures[unit]);
935 }
936 dmResource::IncRef(g_MaterialModule.m_Factory, texture_res);
937 material_res->m_Textures[unit] = texture_res;
938 }
939
940 // Recalculate number of textures (similar to res_material.cpp) by checking which samplers exist
941 material_res->m_NumTextures = 0;
942 for (uint32_t i = 0; i < dmRender::RenderObject::MAX_TEXTURE_COUNT; ++i)
943 {
944 uint32_t unit = dmRender::GetMaterialSamplerUnit(material_res->m_Material, material_res->m_SamplerNames[i]);
945 if (unit == dmRender::INVALID_SAMPLER_UNIT)
946 {
947 continue;
948 }
949 material_res->m_NumTextures++;
950 }
951 return 0;
952 }
953
954 static int Material_SetTextures(lua_State* L)
955 {

Callers 1

Material_SetTexturesFunction · 0.85

Calls 15

lua_gettopFunction · 0.85
GetMaterialSamplerUnitFunction · 0.85
luaL_errorFunction · 0.85
dmHashReverseSafe64Function · 0.85
CheckHashOrStringFunction · 0.85
CheckResourceFunction · 0.85
GetGraphicsContextFunction · 0.85
GetMaterialRenderContextFunction · 0.85
GetTextureTypeFunction · 0.85
GetMaterialSamplerFunction · 0.85
GetSamplerInfoFunction · 0.85
GetTextureTypeLiteralFunction · 0.85

Tested by

no test coverage detected