LateInitializationError: Field 'ref' has not been initialized
@wolfenrain I'm seeing this error when running the example
C:\tools\dart-sdk-2.14.4\bin\dart.exe .\example\lib\models\mesh_generation.dart
INFO: Initializing raylib 4.2
INFO: Supported raylib modules:
INFO: > rcore:..... loaded (mandatory)
INFO: > rlgl:...... loaded (mandatory)
INFO: > rshapes:... loaded (optional)
INFO: > rtextures:. loaded (optional)
INFO: > rtext:..... loaded (optional)
INFO: > rmodels:... loaded (optional)
INFO: > raudio:.... loaded (optional)
INFO: DISPLAY: Device initialized successfully
INFO: > Display size: 1920 x 1080
INFO: > Screen size: 800 x 450
INFO: > Render size: 800 x 450
INFO: > Viewport offsets: 0, 0
INFO: GLAD: OpenGL extensions loaded successfully
INFO: GL: Supported extensions count: 187
INFO: GL: OpenGL device information:
INFO: > Vendor: Intel
INFO: > Renderer: Intel(R) HD Graphics 4000
INFO: > Version: 3.3.0 - Build 10.18.10.5161
INFO: > GLSL: 3.30 - Build 10.18.10.5161
INFO: GL: DXT compressed textures supported
INFO: GL: ETC2/EAC compressed textures supported
INFO: TEXTURE: [ID 1] Texture loaded successfully (1x1 | R8G8B8A8 | 1 mipmaps)
INFO: TEXTURE: [ID 1] Default texture loaded successfully
INFO: SHADER: [ID 1] Vertex shader compiled successfully
INFO: SHADER: [ID 2] Fragment shader compiled successfully
INFO: SHADER: [ID 3] Program shader loaded successfully
INFO: SHADER: [ID 3] Default shader loaded successfully
INFO: RLGL: Render batch vertex buffers loaded successfully in RAM (CPU)
INFO: RLGL: Render batch vertex buffers loaded successfully in VRAM (GPU)
INFO: RLGL: Default OpenGL state initialized successfully
INFO: TEXTURE: [ID 2] Texture loaded successfully (128x128 | GRAY_ALPHA | 1 mipmaps)
INFO: FONT: Default font loaded successfully (224 glyphs)
Image.fromRef Instance of 'Image'
Unhandled exception:
LateInitializationError: Field 'ref' has not been initialized.
#0 NativeClass.ref (package:raylib/src/utils/native_type.dart)
#1 main (file:///C:/MyProjectsDart/dart-raylib/example/lib/models/mesh_generation.dart:59:26)
#2 _delayEntrypointInvocation.<anonymous closure> (dart:isolate-patch/isolate_patch.dart:283:19)
#3 _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:184:12)
import 'package:raylib/raylib.dart';
/// generate a simple triangle mesh from code
Mesh makeMesh() {
final mesh = Mesh(1);
// vertex at the origin
mesh.vertices[0] = 0;
mesh.vertices[1] = 0;
mesh.vertices[2] = 0;
mesh.normals[0] = 0;
mesh.normals[1] = 1;
mesh.normals[2] = 0;
mesh.texcoords[0] = 0;
mesh.texcoords[1] = 0;
// vertex at 1,0,2
mesh.vertices[3] = 1;
mesh.vertices[4] = 0;
mesh.vertices[5] = 2;
mesh.normals[3] = 0;
mesh.normals[4] = 1;
mesh.normals[5] = 0;
mesh.texcoords[2] = 0.5;
mesh.texcoords[3] = 1.0;
// vertex at 2,0,0
mesh.vertices[6] = 2;
mesh.vertices[7] = 0;
mesh.vertices[8] = 0;
mesh.normals[6] = 0;
mesh.normals[7] = 1;
mesh.normals[8] = 0;
mesh.texcoords[4] = 1;
mesh.texcoords[5] = 0;
uploadMesh(mesh);
return mesh;
}
void main() {
initLibrary(
linux: './include/libraylib.so',
windows: 'C:/MyProjectsDart/dart-raylib/example/raylib_4.2.0_msvc16.dll',
);
const screenWidth = 800;
const screenHeight = 450;
initWindow(
screenWidth,
screenHeight,
'dart-raylib [models] example - mesh generation',
);
// We generate a checked image for texturing
final checked = genImageChecked(2, 2, 1, 1, Color.red, Color.green);
print(' main ${checked.ref}');
final texture = loadTextureFromImage(checked);
// unloadImage(checked);
final models = <Model>[
loadModelFromMesh(genMeshPlane(2, 2, 5, 5)),
loadModelFromMesh(genMeshCube(2, 1, 2)),
loadModelFromMesh(genMeshSphere(2, 32, 32)),
loadModelFromMesh(genMeshHemiSphere(2, 16, 16)),
loadModelFromMesh(genMeshCylinder(1, 2, 16)),
loadModelFromMesh(genMeshTorus(0.25, 4, 16, 32)),
loadModelFromMesh(genMeshKnot(1, 2, 16, 128)),
loadModelFromMesh(genMeshPoly(5, 2)),
loadModelFromMesh(makeMesh()),
];
// Set checked texture as default diffuse component for all models material
// for (final model in models) {
// model.materials[0].maps[materialMapDiffuse].texture = texture;
// }
// Define the camera to look into our 3d world
final camera = Camera3D(
position: Vector3.all(5),
target: Vector3.zero(),
up: Vector3(0, 1, 0),
fovy: 45,
);
// Model drawing position
final position = Vector3.zero();
var currentModel = 0;
setCameraMode(camera, CameraMode.orbital);
setTargetFPS(60);
while (!windowShouldClose()) {
updateCamera(camera);
if (isMouseButtonPressed(MouseButton.left)) {
// Cycle between the textures
currentModel = (currentModel + 1) % models.length;
}
if (isKeyPressed(KeyboardKey.right)) {
currentModel++;
if (currentModel >= models.length) currentModel = 0;
} else if (isKeyPressed(KeyboardKey.left)) {
currentModel--;
if (currentModel < 0) currentModel = models.length - 1;
}
beginDrawing();
clearBackground(Color.rayWhite);
beginMode3D(camera);
drawModel(models[currentModel], position, 1, Color.white);
drawGrid(10, 1);
endMode3D();
drawRectangle(30, 400, 310, 30, fade(Color.skyBlue, .5));
drawRectangleLines(30, 400, 310, 30, fade(Color.darkBlue, .5));
drawText(
'MOUSE LEFT BUTTON to CYCLE PROCEDURAL MODELS',
40,
410,
10,
Color.blue,
);
switch (currentModel) {
case 0:
drawText('PLANE', 680, 10, 20, Color.darkBlue);
break;
case 1:
drawText('CUBE', 680, 10, 20, Color.darkBlue);
break;
case 2:
drawText('SPHERE', 680, 10, 20, Color.darkBlue);
break;
case 3:
drawText('HEMISPHERE', 640, 10, 20, Color.darkBlue);
break;
case 4:
drawText('CYLINDER', 680, 10, 20, Color.darkBlue);
break;
case 5:
drawText('TORUS', 680, 10, 20, Color.darkBlue);
break;
case 6:
drawText('KNOT', 680, 10, 20, Color.darkBlue);
break;
case 7:
drawText('POLY', 680, 10, 20, Color.darkBlue);
break;
case 8:
drawText('Parametric(custom)', 580, 10, 20, Color.darkBlue);
break;
default:
break;
}
endDrawing();
}
//unloadTexture(texture); // Unload texture
// Unload models data (GPU VRAM)
for (final model in models) {
unloadModel(model);
}
closeWindow();
}
Edited by Isaque Neves Sant Ana