# How can I assign my own dynamic generated texture in C++ to an object in the scene?

**URL:** <https://forums.unrealengine.com/t/how-can-i-assign-my-own-dynamic-generated-texture-in-c-to-an-object-in-the-scene/391854>\
**Category:** Rendering\
**Tags:** kinect, unreal-engine, dynamic, CPP, question\
**Created:** [April 25, 2017, 10:06am UTC](https://forums.unrealengine.com/t/how-can-i-assign-my-own-dynamic-generated-texture-in-c-to-an-object-in-the-scene/391854 "2017-04-25T10:06:38Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![BigThink](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@BigThink](https://forums.unrealengine.com/u/BigThink)\
**Post date:** [April 25, 2017, 10:06am UTC](https://forums.unrealengine.com/t/how-can-i-assign-my-own-dynamic-generated-texture-in-c-to-an-object-in-the-scene/391854/1 "2017-04-25T10:06:38Z")

</div>

I am trying to assign my own generated texture in C++ in an object in the scene in Unreal e.g. a cube.  
The code with which I generate the texture is outlined below. I am using data from a Kinect device to create a color texture - just like a normal camera.

```
// @BeginPlay
    ColorTexture = UTexture2D::CreateTransient(1920, 1080);
    ColorTexture->UpdateResource();
    ColorTexture->SRGB = 1;
    ColorQuad = new RGBQUAD[1920 * 1080];
    // @Tick
    	pColorBuffer = ColorQuad;
    	nColorBufferSize = 1920 * 1080 * sizeof(RGBQUAD);
    	HRESULT hr = ColorFrameReader->AcquireLatestFrame(&ColorFrame); // ColorFrameReader and ColorFrame are from Kinect library.
    	hr = ColorFrame->CopyConvertedFrameDataToArray(nColorBufferSize, reinterpret_cast<BYTE*>(pColorBuffer), ColorImageFormat_Bgra);
    	const size_t SizeColorQuad = 1920 * 1080 * sizeof(RGBQUAD);
    	uint8* Destination = (uint8*)ColorTexture->PlatformData->Mips[0].BulkData.Lock(LOCK_READ_WRITE);
    	FMemory::Memcpy(Destination, (uint8*)ColorQuad, SizeColorQuad);
    	ColorTexture->PlatformData->Mips[0].BulkData.Unlock();
    	ColorTexture->UpdateResource();

```

These are the objects in my scene with which I am experimenting:

[https://i.imgur.com/q9IUd5p.png](https://i.imgur.com/q9IUd5p.png)

How can I assign my own dynamic generated texture in C++ to an object in the scene?

---

<div class="post-metadata">

**Author:** ![plangton](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@plangton](https://forums.unrealengine.com/u/plangton)\
**Post date:** [April 25, 2017, 10:16am UTC](https://forums.unrealengine.com/t/how-can-i-assign-my-own-dynamic-generated-texture-in-c-to-an-object-in-the-scene/391854/2 "2017-04-25T10:16:19Z")

</div>

the safest way is that you create another UTexture2D pointer and copy your generated texture to it when it is ready.  
this way you are sure even if you are working on a separate thread there won’t be any conflict over accessing a locked texture.

after that you need to make a blueprint function that simply return a pointer to UTexture2D you have your texture stored in

---

<div class="post-metadata">

**Author:** ![BigThink](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@BigThink](https://forums.unrealengine.com/u/BigThink)\
**Post date:** [April 25, 2017, 11:18am UTC](https://forums.unrealengine.com/t/how-can-i-assign-my-own-dynamic-generated-texture-in-c-to-an-object-in-the-scene/391854/3 "2017-04-25T11:18:49Z")

</div>

Thank you!  
I exposed the UTexture2D to blueprint and used it there to set the texture for the material of my object in the scene and it works!
