-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
164 additions
and
2 deletions.
There are no files selected for viewing
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
118 changes: 118 additions & 0 deletions
118
Mods/TestMod/Source/TestMod/Private/MyGameInstanceSubsystem.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
// Fill out your copyright notice in the Description page of Project Settings. | ||
|
||
|
||
#include "MyGameInstanceSubsystem.h" | ||
|
||
#include "RHICommandList.h" | ||
#include "Rendering/Texture2DResource.h" | ||
|
||
|
||
void UMyGameInstanceSubsystem::Initialize(FSubsystemCollectionBase& Collection) | ||
{ | ||
Super::Initialize(Collection); | ||
|
||
TextureTotalPixels = TextureSideSize * TextureSideSize; | ||
|
||
// Get Total Bytes of Texture - Each pixel has 4 bytes for RGBA | ||
TextureDataSize = TextureTotalPixels * 4; | ||
TextureDataSqrtSize = TextureSideSize * 4; | ||
|
||
// Initialize Texture Data Array | ||
TextureData = new uint8[TextureDataSize]; | ||
|
||
DynamicTexture = UTexture2D::CreateTransient(TextureSideSize, TextureSideSize, EPixelFormat::PF_R8G8B8A8, "DynamicTexture"); | ||
DynamicTexture->CompressionSettings = TextureCompressionSettings::TC_EditorIcon; | ||
DynamicTexture->Filter = TextureFilter::TF_Default; | ||
DynamicTexture->SRGB = 0; | ||
DynamicTexture->AddToRoot(); | ||
DynamicTexture->UpdateResource(); | ||
|
||
TextureRegion = FUpdateTextureRegion2D{ 0, 0, 0, 0, TextureSideSize, TextureSideSize }; | ||
} | ||
|
||
|
||
void UMyGameInstanceSubsystem::Deinitialize() | ||
{ | ||
Super::Deinitialize(); | ||
|
||
DynamicTexture = nullptr; | ||
delete[] TextureData; | ||
} | ||
|
||
|
||
void UMyGameInstanceSubsystem::UpdateTexture() | ||
{ | ||
struct FUpdateTextureRegionsData | ||
{ | ||
FTexture2DResource* Texture2DResource; | ||
FRHITexture2D* TextureRHI; | ||
int32 MipIndex; | ||
uint32 NumRegions; | ||
FUpdateTextureRegion2D* Regions; | ||
uint32 SrcPitch; | ||
uint32 SrcBpp; | ||
uint8* SrcData; | ||
}; | ||
|
||
FTexture2DResource* Resource = static_cast<FTexture2DResource*>(DynamicTexture->GetResource()); | ||
|
||
FUpdateTextureRegionsData RegionData{ | ||
Resource, | ||
Resource->GetTexture2DRHI(), | ||
0, | ||
1, | ||
&TextureRegion, | ||
TextureDataSqrtSize, | ||
4, | ||
TextureData, | ||
}; | ||
|
||
ENQUEUE_RENDER_COMMAND(UpdateTextureRegionsData)( | ||
[RegionData, Texture = DynamicTexture](FRHICommandListImmediate& RHICmdList) | ||
{ | ||
for (uint32 RegionIndex = 0; RegionIndex < RegionData.NumRegions; ++RegionIndex) | ||
{ | ||
int32 CurrentFirstMip = Texture->FirstResourceMemMip; | ||
if (RegionData.TextureRHI && RegionData.MipIndex >= CurrentFirstMip) | ||
{ | ||
RHIUpdateTexture2D( | ||
RegionData.TextureRHI, | ||
RegionData.MipIndex - CurrentFirstMip, | ||
RegionData.Regions[RegionIndex], | ||
RegionData.SrcPitch, | ||
RegionData.SrcData | ||
+ RegionData.Regions[RegionIndex].SrcY * RegionData.SrcPitch | ||
+ RegionData.Regions[RegionIndex].SrcX * RegionData.SrcBpp | ||
); | ||
} | ||
} | ||
}); | ||
} | ||
|
||
|
||
void UMyGameInstanceSubsystem::CreateGrid(int count) | ||
{ | ||
FMemory::Memset(TextureData, 0, TextureDataSize); | ||
|
||
constexpr int dotInterval = 3; | ||
constexpr uint32 color = 0xC87F7F7F; | ||
const int interval = TextureSideSize / count; | ||
|
||
auto* pixelData = reinterpret_cast<uint32*>(TextureData); | ||
|
||
for (int i = 1; i < count; ++i) | ||
{ | ||
uint32 linePos = i * interval; | ||
|
||
for (uint32 offset = 0; offset < TextureSideSize; offset += dotInterval) | ||
{ | ||
// Draw the horizontal dot (y = linePos, x = offset) | ||
pixelData[linePos * TextureSideSize + offset] = color; | ||
|
||
// Draw the vertical dot (y = offset, x = linePos) | ||
pixelData[offset * TextureSideSize + linePos] = color; | ||
} | ||
} | ||
|
||
UpdateTexture(); | ||
} |
44 changes: 44 additions & 0 deletions
44
Mods/TestMod/Source/TestMod/Public/MyGameInstanceSubsystem.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// Fill out your copyright notice in the Description page of Project Settings. | ||
|
||
#pragma once | ||
|
||
#include "CoreMinimal.h" | ||
#include "Subsystems/GameInstanceSubsystem.h" | ||
#include "MyGameInstanceSubsystem.generated.h" | ||
|
||
/** | ||
* | ||
*/ | ||
UCLASS() | ||
class TESTMOD_API UMyGameInstanceSubsystem : public UGameInstanceSubsystem | ||
{ | ||
GENERATED_BODY() | ||
|
||
public: | ||
virtual void Initialize(FSubsystemCollectionBase& Collection) override; | ||
virtual void Deinitialize() override; | ||
|
||
UFUNCTION(BlueprintCallable) | ||
void CreateGrid(int count); | ||
|
||
private: | ||
void UpdateTexture(); | ||
|
||
|
||
protected: | ||
UPROPERTY(EditDefaultsOnly) | ||
uint32 TextureSideSize = 2048; | ||
|
||
UPROPERTY(BlueprintReadWrite, EditAnywhere) | ||
UTexture2D* DynamicTexture = nullptr; | ||
|
||
private: | ||
uint8* TextureData; | ||
|
||
uint32 TextureDataSize; | ||
uint32 TextureDataSqrtSize; | ||
uint32 TextureTotalPixels; | ||
|
||
// Update Region Struct | ||
FUpdateTextureRegion2D TextureRegion; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters