initPlatformState method

Future<void> initPlatformState(
  1. Size validSize,
  2. double dpr
)

Initializes the renderer with the given viewport size and device pixel ratio.

Implementation

Future<void> initPlatformState(Size validSize, double dpr) async {
  if (_didInit) return;

  if (validSize.width <= 0 || validSize.height <= 0) {
    debugPrint('Invalid size for initialization: $validSize');
    return;
  }

  debugPrint(
    'Initializing with size: ${validSize.width}x${validSize.height}',
  );

  final camera = Camera.createDefault(
    width: validSize.width,
    height: validSize.height,
    ndcYSign: Platform.isAndroid ? 1 : -1,
  );

  // Initialize spherical coordinates from initial camera position
  final pos = camera.position;
  _orbitDistance = pos.length;
  _theta = math.atan2(pos.x, pos.z);
  _phi = math.acos(pos.y / _orbitDistance);

  try {
    await _renderer.initialize();

    await _renderer.setupTexture(
      width: validSize.width,
      height: validSize.height,
      enableProfiling: widget.enableProfiling,
    );

    _renderer.camera = camera;
    if (widget.backgroundAssetPath != null) {
      await _renderer.enableBackgroundFromAsset(widget.backgroundAssetPath!);
    }

    texture = _renderer.targetTexture;

    if (texture == null) {
      throw Exception('Failed to create texture: texture is null');
    }

    textureId = texture!.textureId;

    if (textureId < 0) {
      throw Exception(
        'Failed to create texture: invalid texture ID ($textureId)',
      );
    }

    debugPrint('Successfully created texture with ID: $textureId');

    await _loadSplatDataFromAsset(widget.assetPath);

    if (!mounted) return;
    setState(() {});

    // Initial render after setup
    _requestRender();
  } catch (e) {
    debugPrint('Failed to initialize renderer: $e');
    rethrow;
  }
}