a lot of stuff
This commit is contained in:
1 parent
86f54deb01
commit
a3a80dfa16
35 files changed
+407
No files matched your search
Executable
+16
@@ -0,0 +1,16 @@
|
||||
{
|
||||
"FileVersion": 3,
|
||||
"Version": 1,
|
||||
"VersionName": "1.0.0",
|
||||
"FriendlyName": "Demo Render Bridge",
|
||||
"Description": "Minimalny przyklad wywolania C++ z Unreal Python.",
|
||||
"Category": "Examples",
|
||||
"CanContainContent": false,
|
||||
"Modules": [
|
||||
{
|
||||
"Name": "DemoPlugin",
|
||||
"Type": "Editor",
|
||||
"LoadingPhase": "Default"
|
||||
}
|
||||
]
|
||||
}
|
||||
Executable
+48
@@ -0,0 +1,48 @@
|
||||
# Demo Render Bridge: minimalny prawdziwy plugin UE C++ -> Python
|
||||
|
||||
Plugin pokazuje rzeczywisty przeplyw:
|
||||
|
||||
```text
|
||||
TextureRenderTarget2D
|
||||
-> Python wywoluje UFUNCTION
|
||||
-> C++: FImageUtils::GetRenderTargetImage
|
||||
-> C++: FImageUtils::CompressImage(".png")
|
||||
-> TArray<uint8> wraca do Pythona
|
||||
-> Python zapisuje bytes na dysku
|
||||
```
|
||||
|
||||
## Struktura
|
||||
|
||||
```text
|
||||
DemoPlugin.uplugin
|
||||
Source/DemoPlugin/DemoPlugin.Build.cs
|
||||
Source/DemoPlugin/Public/DemoRenderBridge.h
|
||||
Source/DemoPlugin/Private/DemoRenderBridge.cpp
|
||||
Source/DemoPlugin/Private/DemoPlugin.cpp
|
||||
use_plugin.py
|
||||
```
|
||||
|
||||
## Jak uruchomic
|
||||
|
||||
1. Skopiuj caly katalog jako `<Projekt>/Plugins/DemoPlugin`.
|
||||
2. Upewnij sie, ze projekt ma modul C++ i wlaczony `Python Editor Script Plugin`.
|
||||
3. Otworz projekt i zaakceptuj kompilacje pluginu albo zbuduj projekt w IDE.
|
||||
4. Utworz asset `TextureRenderTarget2D` pod `/Game/Demo/RT_Demo` lub zmien
|
||||
sciezke `RENDER_TARGET_ASSET` w `use_plugin.py`.
|
||||
5. W Unreal Editor wybierz `Tools -> Execute Python Script` i uruchom
|
||||
`use_plugin.py`.
|
||||
6. Wynik pojawi sie jako `Saved/demo_from_cpp.png`.
|
||||
|
||||
## Co jest mostem do Pythona
|
||||
|
||||
- `UCLASS()` rejestruje klase w refleksji.
|
||||
- `UFUNCTION(BlueprintCallable)` eksportuje funkcje.
|
||||
- `UDemoRenderBridge` staje sie `unreal.DemoRenderBridge`.
|
||||
- `RenderTargetToPng` staje sie `render_target_to_png`.
|
||||
- `UTextureRenderTarget2D*` przyjmuje obiekt zaladowany w Pythonie.
|
||||
- `TArray<uint8>` wraca jako kolekcja konwertowalna przez `bytes(...)`.
|
||||
|
||||
To jest odchudzona wersja prawdziwego render bridge'a: pomija wybor formatu,
|
||||
luminancje, specjalne kodowanie RGB, transport sieciowy i rozbudowana obsluge
|
||||
bledow. Zachowuje jednak faktyczne wywolania API C++ Unreala i prawdziwy
|
||||
kontrakt widoczny z Unreal Python.
|
||||
@@ -0,0 +1,17 @@
|
||||
using UnrealBuildTool;
|
||||
|
||||
public class DemoPlugin : ModuleRules
|
||||
{
|
||||
public DemoPlugin(ReadOnlyTargetRules Target) : base(Target)
|
||||
{
|
||||
PCHUsage = PCHUsageMode.UseExplicitOrSharedPCHs;
|
||||
|
||||
PublicDependencyModuleNames.AddRange(new[]
|
||||
{
|
||||
"Core",
|
||||
"CoreUObject",
|
||||
"Engine",
|
||||
"ImageCore"
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
#include "Modules/ModuleManager.h"
|
||||
|
||||
IMPLEMENT_MODULE(FDefaultModuleImpl, DemoPlugin)
|
||||
|
||||
//This registers that it is the implementation of DemoPlugin module
|
||||
//that can be loaded by UE
|
||||
|
||||
//Here could be startup scripts or sth
|
||||
@@ -0,0 +1,51 @@
|
||||
#include "DemoRenderBridge.h"
|
||||
|
||||
#include "Engine/TextureRenderTarget2D.h"
|
||||
#include "ImageCore.h"
|
||||
#include "ImageUtils.h"
|
||||
|
||||
bool UDemoRenderBridge::RenderTargetToPng(
|
||||
UTextureRenderTarget2D* RenderTarget,
|
||||
TArray<uint8>& OutPngBytes,
|
||||
FString& OutError)
|
||||
{
|
||||
OutPngBytes.Reset();
|
||||
OutError.Reset();
|
||||
|
||||
if (!IsInGameThread())
|
||||
{
|
||||
OutError = TEXT("Function must be ran on Game Thread");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!IsValid(RenderTarget))
|
||||
{
|
||||
OutError = TEXT("Render Target reference is invalid");
|
||||
return false;
|
||||
}
|
||||
|
||||
// Faktyczne wywolanie API C++ Unreala: odczyt Render Targetu do FImage.
|
||||
FImage Image;
|
||||
if (!FImageUtils::GetRenderTargetImage(RenderTarget, Image))
|
||||
{
|
||||
OutError = TEXT("Couldnt read Render Target");
|
||||
return false;
|
||||
}
|
||||
|
||||
// Faktyczne wywolanie API C++ Unreala: kompresja FImage do PNG w pamieci.
|
||||
TArray64<uint8> Encoded;
|
||||
if (!FImageUtils::CompressImage(Encoded, TEXT(".png"), Image, 0))
|
||||
{
|
||||
OutError = TEXT("Couldnt encode png");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (Encoded.Num() > MAX_int32)
|
||||
{
|
||||
OutError = TEXT("Result too large");
|
||||
return false;
|
||||
}
|
||||
|
||||
OutPngBytes.Append(Encoded.GetData(), static_cast<int32>(Encoded.Num()));
|
||||
return true;
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
#pragma once
|
||||
|
||||
#include "CoreMinimal.h"
|
||||
#include "Kismet/BlueprintFunctionLibrary.h"
|
||||
#include "DemoRenderBridge.generated.h"
|
||||
|
||||
class UTextureRenderTarget2D;
|
||||
|
||||
/** Funkcje C++ dostepne w Blueprintach i Python API */
|
||||
UCLASS()
|
||||
class DEMOPLUGIN_API UDemoRenderBridge : public UBlueprintFunctionLibrary
|
||||
{
|
||||
GENERATED_BODY()
|
||||
|
||||
public:
|
||||
|
||||
/** Odczytuje Render Target z GPU i zwraca zakodowany PNG w pamieci. */
|
||||
UFUNCTION(BlueprintCallable, Category = "Demo|C++ Bridge")
|
||||
static bool RenderTargetToPng(
|
||||
UTextureRenderTarget2D* RenderTarget,
|
||||
TArray<uint8>& OutPngBytes,
|
||||
FString& OutError);
|
||||
};
|
||||
Executable
+19
@@ -0,0 +1,19 @@
|
||||
from pathlib import Path
|
||||
|
||||
import unreal
|
||||
|
||||
RENDER_TARGET_ASSET = "/Game/NewTextureRenderTarget2D"
|
||||
OUTPUT_FILE = Path(unreal.Paths.project_saved_dir()) / "demo_from_cpp.png"
|
||||
|
||||
render_target = unreal.load_asset(RENDER_TARGET_ASSET)
|
||||
|
||||
# Parametry Out funkcji UFUNCTION wracaja w Pythonie jako tuple.
|
||||
png_bytes, error = unreal.DemoRenderBridge.render_target_to_png(
|
||||
render_target
|
||||
)
|
||||
|
||||
if error:
|
||||
raise RuntimeError(error)
|
||||
|
||||
OUTPUT_FILE.write_bytes(bytes(png_bytes))
|
||||
unreal.log(f"preview saved as {OUTPUT_FILE}")
|
||||
Reference in new issue
Block a user