Hi all! I modified the example code “Generate procedural models” from C# to C++ (see below). But the object that the script generates is not displayed in the game. Can anyone please help me find what I did wrong?
cpp04.cpp
#include "cpp04.h"
cpp04::cpp04(const SpawnParams& params)
: Script(params)
{
// Enable ticking OnUpdate function
_tickUpdate = true;
}
void cpp04::OnEnable()
{
const int32 meshesCountPerLod = 1;
Span<int32> spanMeshesCountPerLod(&meshesCountPerLod, sizeof(int32));
_tplModel = Content::CreateVirtualAsset<Model>();
_tplModel->SetupLODs(spanMeshesCountPerLod);
auto targetActor = GetActor();
String msg = String::Format(
TEXT(" ~~~ Actor Name: {0}"),
targetActor->GetName());
LOG_STR(Info, msg);
auto childModel = targetActor->GetOrAddChild<StaticModel>();
childModel->Model = _tplModel;
childModel->SetStaticFlags(StaticFlags::None);
childModel->SetForcedLOD(0);
childModel->SetLocalScale(Float3(100));
childModel->SetMaterial(0, myMaterial);
UpdateMesh(_tplModel->LODs[0].Meshes[0]);
}
void cpp04::UpdateMesh(Mesh& mesh)
{
const float Width = 1.1f;
const float Depth = 1.1f;
const float Height = 0.8f;
const float X = Width/2;
const float Z = Depth/2;
const float Y = Height/2;
// Normals directions
auto F = Vector3( 0.0f, 0.0f,-1.0f ); // Face
auto T = Vector3( 0.0f, 1.0f, 0.0f ); // Top
auto L = Vector3( 1.0f, 0.0f, 0.0f ); // Left
auto G = Vector3( 0.0f,-1.0f, 0.0f ); // Ground
auto B = Vector3( 0.0f, 0.0f, 1.0f ); // Back
auto R = Vector3(-1.0f, 0.0f, 0.0f ); // Right
// Vertices coordinates
auto V0 = Vector3(-X,-Y,-Z); // 3--------7 y
auto V1 = Vector3(-X,-Y, Z); // /| /| | z
auto V2 = Vector3(-X, Y,-Z); // 2--------6 | |/
auto V3 = Vector3(-X, Y, Z); // | | | | -+--- x
auto V4 = Vector3( X,-Y,-Z); // | 1------|-5 /|
auto V5 = Vector3( X,-Y, Z); // |/ |/
auto V6 = Vector3( X, Y,-Z); // 0--------4
auto V7 = Vector3( X, Y, Z);
Float3 vertices[] = {
V0, V2, V6, V6, V4, V0, // Face
V2, V3, V7, V7, V6, V2, // Top
V4, V6, V5, V6, V7, V5, // Left
V0, V4, V5, V5, V1, V0, // Ground
V1, V5, V7, V7, V3, V1, // Back
V2, V1, V3, V1, V2, V0, // Right
};
Float3 normals[] = {
F,F,F,F,F,F, // Face
T,T,T,T,T,T, // Top
L,L,L,L,L,L, // Left
G,G,G,G,G,G, // Ground
B,B,B,B,B,B, // Back
R,R,R,R,R,R, // Right
};
uint32 triangles[] = {
0,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
};
if (mesh.UpdateMesh(
36, // uint32 vertexCount
12, // uint32 triangleCount
vertices, triangles, normals)) LOG(Warning, " ~~~ Mesh NOT updated.");
else LOG(Info, " ~~~ Mesh updated: OK.");
}
cpp04.h
#pragma once
#include "Engine/Content/Asset.h"
#include "Engine/Content/Assets/Material.h"
#include "Engine/Content/Assets/MaterialBase.h"
#include "Engine/Content/Assets/Model.h"
#include "Engine/Content/Cache/AssetsCache.h"
#include "Engine/Content/Content.h"
#include "Engine/Core/Common.h"
#include "Engine/Core/Log.h"
#include "Engine/Core/Math/Transform.h"
#include "Engine/Core/Math/Vector3.h"
#include "Engine/Engine/Screen.h"
#include "Engine/Engine/Time.h"
#include "Engine/Input/Input.h"
#include "Engine/Level/Actor.h"
#include "Engine/Level/Actors/StaticModel.h"
#include "Engine/Level/Actors/Camera.h"
#include "Engine/Level/Level.h"
#include "Engine/Physics/Actors/RigidBody.h"
#include "Engine/Physics/CollisionData.h"
#include "Engine/Physics/Colliders/CharacterController.h"
#include "Engine/Physics/Colliders/SphereCollider.h"
#include "Engine/Scripting/Script.h"
API_CLASS() class GAME_API cpp04 : public Script
{
API_AUTO_SERIALIZATION();
DECLARE_SCRIPTING_TYPE(cpp04);
private:
AssetReference<Model> _tplModel = nullptr;
public:
API_FIELD() AssetReference<MaterialBase> myMaterial = nullptr;
void OnEnable() override;
void UpdateMesh(Mesh& mesh);
};
Output log
[ 00:02:48.957 ]: [Info] [PlayMode] Start
[ 00:02:48.976 ]: [Info] Changing editor state from FlaxEditor.States.EditingSceneState to FlaxEditor.States.PlayingState
[ 00:02:48.976 ]: [Info] Apply game settings
[ 00:02:48.976 ]: [Info] Collecting scene data
[ 00:02:48.976 ]: [Info] Saving scene Scene to bytes
[ 00:02:48.977 ]: [Info] Scene saved! Time 1 ms
[ 00:02:48.977 ]: [Info] Cleanup graph for scene 'Scene'
[ 00:02:48.977 ]: [Info] Unloding 1 lightmap(s)
[ 00:02:48.977 ]: [Info] Gathered 1 scene(s)!
[ 00:02:48.978 ]: [Info] Creating scenes
[ 00:02:48.978 ]: [Info] Loading scene...
[ 00:02:48.978 ]: [Info] Loading 1 lightmap(s)
[ 00:02:48.987 ]: [Info] ~~~ Actor Name: GenMeshActor
[ 00:02:48.987 ]: [Info] ~~~ Mesh updated: OK.
[ 00:02:48.987 ]: [Info] Created graph for scene 'Scene' in 0 ms
[ 00:02:48.987 ]: [Info] Scene loaded in 9 ms
[ 00:02:48.988 ]: [Info] [PlayMode] Enter
[ 00:02:51.291 ]: [Info] [PlayMode] Stop
[ 00:02:51.308 ]: [Info] Changing editor state from FlaxEditor.States.PlayingState to FlaxEditor.States.EditingSceneState
[ 00:02:51.308 ]: [Info] Restoring scene data
[ 00:02:51.308 ]: [Info] Cleanup graph for scene 'Scene'
[ 00:02:51.308 ]: [Info] Unloding 1 lightmap(s)
[ 00:02:51.308 ]: [Info] Loading scene...
[ 00:02:51.308 ]: [Info] Loading 1 lightmap(s)
[ 00:02:51.308 ]: [Info] Created graph for scene 'Scene' in 0 ms
[ 00:02:51.308 ]: [Info] Scene loaded in 0 ms
[ 00:02:51.308 ]: [Info] Restored previous scenes
[ 00:02:51.309 ]: [Info] [PlayMode] Exit