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

Function SetTexture

engine/gamesys/src/gamesys/scripts/script_resource.cpp:1479–1556  ·  view source on GitHub ↗

set a texture * Sets the pixel data for a specific texture. * * @name resource.set_texture * * @param path [type:hash|string] The path to the resource * @param table [type:table] A table containing info about the texture. Supported entries: * * `type` * : [type:number] The texture type. Supported values: * * - `graphics.TEXTURE_TYPE_2D` * - `graphics.TEXTURE_TYPE_IMAGE_2D` * - `graphi

Source from the content-addressed store, hash-verified

1477 * ```
1478 */
1479static int SetTexture(lua_State* L)
1480{
1481 const int32_t DEFAULT_INT_NOT_SET = -1;
1482
1483 int top = lua_gettop(L);
1484
1485 dmhash_t path_hash = dmScript::CheckHashOrString(L, 1);
1486
1487 luaL_checktype(L, 2, LUA_TTABLE);
1488 dmGraphics::TextureType type = (dmGraphics::TextureType) CheckTableInteger(L, 2, "type");
1489 dmGraphics::TextureFormat format = (dmGraphics::TextureFormat) CheckTableInteger(L, 2, "format");
1490 uint32_t width = (uint32_t) CheckTableInteger(L, 2, "width");
1491 uint32_t height = (uint32_t) CheckTableInteger(L, 2, "height");
1492 uint32_t depth = (uint32_t) CheckTableInteger(L, 2, "depth", 1);
1493 uint32_t mipmap = (uint32_t) CheckTableInteger(L, 2, "mipmap", 0);
1494 int32_t page = (int32_t) CheckTableInteger(L, 2, "page", DEFAULT_INT_NOT_SET);
1495 int32_t x = (int32_t) CheckTableInteger(L, 2, "x", DEFAULT_INT_NOT_SET);
1496 int32_t y = (int32_t) CheckTableInteger(L, 2, "y", DEFAULT_INT_NOT_SET);
1497 int32_t z = (int32_t) CheckTableInteger(L, 2, "z", DEFAULT_INT_NOT_SET);
1498
1499 if (!dmGraphics::IsTextureFormatSupportedForType(g_ResourceModule.m_GraphicsContext, type, format))
1500 {
1501 return luaL_error(L, "Unable to set texture, unsupported texture format '%s'.", dmGraphics::GetTextureFormatLiteral(format));
1502 }
1503
1504 if (!IsTextureFormatSupported(type))
1505 {
1506 return luaL_error(L, "Unable to set texture, unsupported texture type '%s'.", dmGraphics::GetTextureTypeLiteral(type));
1507 }
1508
1509 if (dmGraphics::IsTextureType3D(type) && !dmGraphics::IsContextFeatureSupported(g_ResourceModule.m_GraphicsContext, dmGraphics::CONTEXT_FEATURE_3D_TEXTURES))
1510 {
1511 return luaL_error(L, "Unable to create texture, 3D textures are not supported on this graphics context.");
1512 }
1513
1514 dmGraphics::TextureImage::CompressionType compression_type = (dmGraphics::TextureImage::CompressionType) CheckTableInteger(L, 2, "compression_type", (int) dmGraphics::TextureImage::COMPRESSION_TYPE_DEFAULT);
1515
1516 bool sub_update = x != DEFAULT_INT_NOT_SET || y != DEFAULT_INT_NOT_SET || z != DEFAULT_INT_NOT_SET || page != DEFAULT_INT_NOT_SET;
1517 x = dmMath::Max(0, x);
1518 y = dmMath::Max(0, y);
1519 z = dmMath::Max(0, z);
1520 page = dmMath::Max(0, page);
1521
1522 dmScript::LuaHBuffer* lua_buffer = dmScript::CheckBuffer(L, 3);
1523 dmBuffer::HBuffer buffer_handle = dmGameSystem::UnpackLuaBuffer(lua_buffer);
1524
1525 uint8_t* data = 0;
1526 uint32_t datasize = 0;
1527 dmBuffer::GetBytes(buffer_handle, (void**)&data, &datasize);
1528
1529 SetTextureResourceParams params = {};
1530 params.m_PathHash = path_hash;
1531 params.m_TextureType = type;
1532 params.m_TextureFormat = format;
1533 params.m_CompressionType = compression_type;
1534 params.m_Data = data;
1535 params.m_DataSize = datasize;
1536 params.m_Width = width;

Callers 4

CompGuiNewWorldFunction · 0.50
CompModelNewWorldFunction · 0.50
CreateMorphTargetTextureFunction · 0.50

Calls 15

lua_gettopFunction · 0.85
CheckHashOrStringFunction · 0.85
luaL_checktypeFunction · 0.85
CheckTableIntegerFunction · 0.85
luaL_errorFunction · 0.85
GetTextureFormatLiteralFunction · 0.85
GetTextureTypeLiteralFunction · 0.85
IsTextureType3DFunction · 0.85
CheckBufferFunction · 0.85
UnpackLuaBufferFunction · 0.85

Tested by

no test coverage detected