initPlatformState method
Initializes the renderer with the given viewport size and device pixel ratio.
Implementation
Future<void> initPlatformState(Size validSize, double dpr) async {
if (_initStarted || _didInit) return;
_initStarted = true;
_initError = null;
if (validSize.width <= 0 || validSize.height <= 0) {
debugPrint('Invalid size for initialization: $validSize');
_initStarted = false;
return;
}
debugPrint(
'Initializing with size: ${validSize.width}x${validSize.height} @ $dpr',
);
_logicalSize = _snap(validSize);
_lastDpr = dpr;
final renderSize = _physicalSize(validSize, dpr);
final camera = Camera.createDefault(
width: renderSize.width,
height: renderSize.height,
ndcYSign: _renderer.ndcYSign,
);
// Initialize spherical coordinates relative to the orbit origin so the
// first frame is posed with the same math as orbit interaction (otherwise
// the camera jumps on the first drag, since createDefault orbits the
// world origin while _applyCameraFromSpherical orbits _orbitOrigin).
final rel = camera.position - _orbitOrigin;
_orbitDistance = rel.length;
_theta = math.atan2(rel.x, rel.z);
_phi = math.acos(rel.y / _orbitDistance);
try {
await _renderer.initialize();
await _renderer.setup(
width: renderSize.width.toInt(),
height: renderSize.height.toInt(),
enableProfiling: widget.enableProfiling,
);
_renderer.camera = camera;
_renderer.highQualitySH = widget.highQualitySH;
// Re-pose through the orbit path so frame 0 matches post-drag math.
_applyCameraFromSpherical();
if (widget.backgroundAssetPath != null) {
await _renderer.enableBackgroundFromAsset(widget.backgroundAssetPath!);
// Apply the shared default orientation so backends can't drift.
_renderer.setBackgroundRotation(
defaultBackgroundYawDegrees,
defaultBackgroundPitchDegrees,
);
}
await _loadSplatDataFromAsset(widget.assetPath);
if (!mounted) return;
setState(() {
_isReady = true;
});
// Initial render after setup
_requestRender();
} catch (e) {
debugPrint('Failed to initialize renderer: $e');
if (!mounted) return;
setState(() {
_isReady = false;
_initError = e;
});
}
}