#c #unreal-engine4
#c #unreal-engine4
Вопрос:
Как установить сетку с материалом для actor AMySphere?
void AMyActor::DrawSpheresAtLevel(int level_)
{
// 1. Generate parameters...
if (0 != level_)
{
spheres_count = spheres_count / 100 * factor;
radius = radius / 100 * rad_factor; // radius of a circle with a player placed at the center
}
// 2. Generate spheres.
FVector location;
GetPlayerLocation(location);
float x = location.X radius;
FVector previous_scenter;
for (int j = 0; j <= spheres_count; j)
{
float y = std::sqrt(pow(radius, 2) - pow(x, 2));
FVector sphere_center = { x, y, 100 };
FRotator rot = FRotator::ZeroRotator;
if (j != 0) // calculate distance between previous sphere and current sphere
{ // centers until it becomes less, or equal to 80
float deg = 1.f;
do
{
rot.SetComponentForAxis(EAxis::Type::Z, deg );
sphere_center = rot.RotateVector(previous_scenter);
} while (Distance(previous_scenter, sphere_center) < 80.f);
}
// Draw a sphere.
FActorSpawnParameters SpawnParams;
AMySphere* sphere_ptr = GetWorld()->SpawnActor<AMySphere>(sphere_center, rot, SpawnParams);
if (nullptr == sphere_ptr)
{
std::cout << "nullptrn";
}
else
{
sphere_ptr->SetObserver(this);
sphere_ptr->SetIndex_(spheres.size());
spheres.push_back(sphere_ptr);
}
previous_scenter = sphere_center;
}
}
Ответ №1:
Если вам нужна просто сфера, вы можете добавить подобный компонент в AMySphere::AMySphere
и добавить к этому компоненту любой материал, который вы хотите.
USphereComponent* SphereComponent = CreateDefaultSubobject<USphereComponent>(TEXT("RootComponent"));
RootComponent = SphereComponent;
SphereComponent->InitSphereRadius(40.0f);
Если вы хотите изменить сетку, вы можете вместо этого создать UStaticMeshComponent
и назначить ей ресурс mesh во время выполнения.
Комментарии:
1. Я хочу сделать объект AMySphere видимым в моей сцене. Что для этого требуется?
2. Убедитесь, что сфера имеет правильный размер и появляется в нужном месте, видимый флаг установлен в true, а материал не относится к невидимому виду. Вы можете отладить результат появления при воспроизведении в редакторе, просто задайте какое-либо имя для нового актера и выполните поиск в дереве сцены, чтобы увидеть состояние того, что было создано