Commit a666b0f7 authored by Jan Kremer's avatar Jan Kremer

Working on 2D plots.

parent fd5eedd7
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
import unreal_engine as ue
# EPixelFormat defines the various pixel formats for a texture/image, we will use RGBA with 8bit per channel
from unreal_engine.enums import EPixelFormat
import matplotlib
# set the Agg renderer as we do not need any toolkit
matplotlib.use('Agg')
import matplotlib.pyplot as plt
# set texture/plot dimensions and dpi, ensure dpi is a float !
width = 1024
height = 1024
dpi = 72.0
# create a new figure with the specified sizes
fig = plt.figure(1)
fig.set_dpi(dpi)
fig.set_figwidth(width/dpi)
fig.set_figheight(height/dpi)
# plot a simple graph with a label on the y axis
plt.plot([1, 2, 3, 4])
plt.ylabel('some numbers')
# draw the graph (in memory)
fig.canvas.draw()
# create a texture in memory (will be saved later)
texture = ue.create_transient_texture(width, height, EPixelFormat.PF_R8G8B8A8)
# copy pixels from matplotlib canvas to the texture as RGBA
texture.texture_set_data(fig.canvas.buffer_rgba())
# save the texture
texture.save_package('/Game/GraphTexture')
# open its editor
ue.open_editor_for_asset(texture)
\ No newline at end of file
import unreal_engine as ue
from unreal_engine.enums import EPixelFormat
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import numpy as np
class PlotComponent:
def begin_play(self):
class Hero:
# this is called on game start
def begin_play(self):
width = 1024
height = 1024
dpi = 72.0
matplotlib.rcParams.update({'font.size': 32})
matplotlib.rcParams.update({'axes.linewidth': 6})
self.texture = ue.create_transient_texture(width, height, EPixelFormat.PF_R8G8B8A8)
# self.uobject.get_owner().StaticMeshComponent.getMaterial(0).set_material_texture_parameter('Graph', self.texture)
# self.uobject.get_owner().StaticMeshComponent.createDynamicMaterialInstance()
self.uobject.get_owner().StaticMeshComponent.OverrideMaterials[0].set_material_texture_parameter('Graph', self.texture)
# matinst = self.uobject.get_owner().StaticMeshComponent.CreateAndSetMaterialInstanceDynamic(0)
# matinst.set_material_texture_parameter('Graph', self.texture)
......@@ -23,14 +23,27 @@ class PlotComponent:
self.fig.set_dpi(dpi)
self.fig.set_figwidth(width/dpi)
self.fig.set_figheight(height/dpi)
self.uobject.get_owner().bind_event('OnRedraw', self.redraw)
self.uobject.get_owner().bind_event('OnDrawPoint', self.draw_point)
self.uobject.get_owner().bind_event('OnGraphDataUpdated', self.update_graph)
def redraw(self, x, y, c):
# clear the current plot data
plt.clf()
plt.scatter(x, y, marker='o', c=c)
def update_graph(self, platform):
# clear the current plot data
plt.clf()
# draw a pie chart
plt.pie([5, 7, 12], colors=['r', 'g', 'b'], labels=['RedCube', 'GreenCube', 'BlueCube'], shadow=True)
bbox = plt.gca().get_position()
[xmin, xmax] = plt.gca().get_xlim()
[ymin, ymax] = plt.gca().get_ylim()
[[x0, y0], [x1, y1]] = bbox.get_points()
self.uobject.get_owner().call_function('UpdatePlotBounds', x0, y0, x1, y1, xmin, xmax, ymin, ymax)
self.fig.canvas.draw()
self.texture.texture_set_data(self.fig.canvas.buffer_rgba())
def draw_point(self, x, y):
ue.log('plotter draw point')
#plt.clf()
plt.plot(x, y, 'bo')
self.fig.canvas.draw()
self.texture.texture_set_data(self.fig.canvas.buffer_rgba())
\ No newline at end of file
No preview for this file type
This diff is collapsed.
......@@ -19,7 +19,7 @@ public class ImmersiveAnalytics : ModuleRules
{
PCHUsage = PCHUsageMode.UseExplicitOrSharedPCHs;
PublicDependencyModuleNames.AddRange(new string[] { "Core", "CoreUObject", "Engine", "InputCore" });
PublicDependencyModuleNames.AddRange(new string[] { "Core", "CoreUObject", "Engine", "InputCore", "PointCloudRuntime", "PointCloudEditor" });
// TODO: remove. was used when HDF5 was added via plugin
//PrivateDependencyModuleNames.AddRange(new string[] { "HDF5" });
......
// Fill out your copyright notice in the Description page of Project Settings.
#include "Plot3D.h"
// Sets default values
APlot3D::APlot3D()
{
// Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = true;
}
// Called when the game starts or when spawned
void APlot3D::BeginPlay()
{
Super::BeginPlay();
InitPoints();
InitCloud();
}
void APlot3D::InitPoints()
{
// Populate the MyPoints array with 1000 randomly positioned, red points
for (int32 i = 0; i < 1000; i++)
{
// See FPointCloudPoint for more constructor versions
MyPoints.Emplace(FMath::VRand(), FColor::Red, true);
}
//
}
void APlot3D::InitCloud()
{
// Construct an empty Point Cloud asset
UPointCloud *MyPointCloud = NewObject<UPointCloud>();
//
// Assign the generated points to the cloud and automatically rebuild the data
MyPointCloud->SetPointCloudData(MyPoints);
// Alternatively, do not auto-build the data by calling
// MyPointCloud->SetPointCloudData(MyPoints, false);
// Get the target Point Cloud Actor
//APointCloudActor *MyPointCloudActor = MyActorGettingFunction();
// Assign the constructed Cloud
//MyPointCloudActor->GetPointCloudComponent()->SetPointCloud(MyPointCloud);
//
}
// Called every frame
void APlot3D::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "PointCloud.h"
#include "PointCloudActor.h"
#include "Plot3D.generated.h"
UCLASS()
class IMMERSIVEANALYTICS_API APlot3D : public AActor
{
GENERATED_BODY()
public:
// Sets default values for this actor's properties
APlot3D();
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
private:
void InitPoints();
void InitCloud();
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
UPROPERTY(Category = Plot3D, VisibleAnywhere, BlueprintReadOnly)
TArray<FPointCloudPoint> MyPoints;
};
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment