-
Notifications
You must be signed in to change notification settings - Fork 1
/
SceneCaptureActor.cpp
188 lines (155 loc) · 5.04 KB
/
SceneCaptureActor.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
// Fill out your copyright notice in the Description page of Project Settings.
#include "MyProject2.h" //Fix Your Project Name
#include "SceneCaptureActor.h"
#include "rgbe.h"
#include "Engine/Canvas.h"
#include "Engine/TextureRenderTarget2D.h"
#include <string>
#include <vector>
#include <algorithm>
// Sets default values
ASceneCaptureActor::ASceneCaptureActor()
{
// 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 ASceneCaptureActor::BeginPlay()
{
Super::BeginPlay();
//for fixed step
//FApp::SetBenchmarking(true);
//FApp::SetFixedDeltaTime(1/60);
}
// Capture One Frame
void ASceneCaptureActor::capture(char *name,FFloat16Color *FormatedImageData,int width,int height,int face_n)
{
//例外処理
if(width<=0 || height<=0){
return;
}
//画素を取得
const int RGB_UNIT=3;
int stride=width*face_n;
std::vector<float> for_rgbe(stride*height*RGB_UNIT);
for (int f = 0; f < face_n ; f++){
for (int y = 0; y < height ; y++){
for (int x = 0; x < width; x++){
FFloat16Color c = FormatedImageData[y*width+x+f*width*height];
int adr=(y*stride+x+width*f)*RGB_UNIT;
if(capture_alpha){
for_rgbe[adr + 0] = c.A.GetFloat();
for_rgbe[adr + 1] = c.A.GetFloat();
for_rgbe[adr + 2] = c.A.GetFloat();
}else{
for_rgbe[adr + 0] = c.R.GetFloat();
for_rgbe[adr + 1] = c.G.GetFloat();
for_rgbe[adr + 2] = c.B.GetFloat();
}
}
}
}
//サイズ補正
width*=face_n;
//クロップ
int crop_width=width;
int crop_height=height;
float *p_rgbe=&for_rgbe[0];
std::vector<float> crop_rgbe;
if(face_n==1){
crop_width=width-crop_left-crop_right;
crop_height=height-crop_top-crop_bottom;
if(crop_width<0 || crop_height<0){
return;
}
crop_rgbe.resize(crop_width*crop_height * RGB_UNIT);
for(int y=0;y<crop_height;y++){
for(int x=0;x<crop_width;x++){
int dst_adr=(crop_width*y+x)*RGB_UNIT;
int src_adr=(stride*(y+crop_top)+(x+crop_left))*RGB_UNIT;
crop_rgbe[dst_adr + 0] = for_rgbe[src_adr + 0];
crop_rgbe[dst_adr + 1] = for_rgbe[src_adr + 1];
crop_rgbe[dst_adr + 2] = for_rgbe[src_adr + 2];
}
}
p_rgbe=&crop_rgbe[0];
}
//.hdr出力
rgbe_header_info info;
info.valid = 1;
strcpy(info.programtype,"RADIANCE");
info.gamma = 1.0f;
info.exposure = 1.0f;
FILE *f = fopen(name, "wb");
if(f==NULL){
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Blue, FString::Printf(TEXT("Output file open failed %s"), name));
return;
}
RGBE_WriteHeader(f, crop_width, crop_height, &info);
RGBE_WritePixels_RLE(f, p_rgbe, crop_width, crop_height);
fclose(f);
}
// Called every frame
void ASceneCaptureActor::Tick( float DeltaTime )
{
Super::Tick( DeltaTime );
//録画をポーズする
if(pause){
return;
}
//2DとCubeを順次書き出し
for(int is_cube=0;is_cube<2;is_cube++){
FFloat16Color* FormatedImageData = NULL;
int width,height,face_n;
char name[2048];
if(is_cube==0){
//RenderTextureからUTexture2Dを生成
//テクスチャサイズは2の乗数かつ正方形である必要がある
if(!render_tex){
continue;
}
UTexture2D* Aux2DTex = render_tex->ConstructTexture2D(this, "new_texture_name_2d", EObjectFlags::RF_NoFlags, CTF_DeferCompression);
if(!Aux2DTex){
continue;
}
//テクスチャ圧縮を無効に設定
Aux2DTex->CompressionSettings = TextureCompressionSettings::TC_VectorDisplacementmap;
#if WITH_EDITORONLY_DATA
Aux2DTex->MipGenSettings = TextureMipGenSettings::TMGS_NoMipmaps;
#endif
Aux2DTex->SRGB = 0;
Aux2DTex->UpdateResource();
//テクスチャからRAWデータを取得
Aux2DTex->PlatformData->Mips[0].BulkData.GetCopy((void**)&FormatedImageData);
//テクスチャのサイズを取得
width = Aux2DTex->GetSurfaceWidth();
height = Aux2DTex->GetSurfaceHeight();
face_n = 1;
//One shot
sprintf(name, "%s%08d.hdr", TCHAR_TO_ANSI(*path),frame);
capture(name,FormatedImageData,width,height,face_n);
}else{
//RenderTextureCubeからテクスチャの生成
if(!render_tex_cube){
continue;
}
UTextureCube* AuxCubeTex = render_tex_cube->ConstructTextureCube(this, "new_texture_name_cube", EObjectFlags::RF_NoFlags);
if(!AuxCubeTex){
continue;
}
//テクスチャからRAWデータを取得
AuxCubeTex->PlatformData->Mips[0].BulkData.GetCopy((void**)&FormatedImageData);
//テクスチャのサイズを取得
width = AuxCubeTex->GetSurfaceWidth();
height = AuxCubeTex->GetSurfaceHeight();
face_n = 6;
//One shot
sprintf(name, "%s%08d.hdr", TCHAR_TO_ANSI(*path_cube),frame);
capture(name,FormatedImageData,width,height,face_n);
}
}
//ログ出力
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Blue, FString::Printf(TEXT("Output frame %d"), frame));
//フレームカウントを進める
frame++;
}