Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable updatePosition, updateRotation and updateScale of existing Nodes #125

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,15 @@ import com.difrancescogianmarco.arcore_flutter_plugin.flutter_models.FlutterArCo
import com.difrancescogianmarco.arcore_flutter_plugin.flutter_models.FlutterArCorePose
import com.difrancescogianmarco.arcore_flutter_plugin.models.RotatingNode
import com.difrancescogianmarco.arcore_flutter_plugin.utils.ArCoreUtils
import com.difrancescogianmarco.arcore_flutter_plugin.utils.DecodableUtils.Companion.parseVector3
import com.difrancescogianmarco.arcore_flutter_plugin.utils.DecodableUtils.Companion.parseQuaternion
import com.google.ar.core.*
import com.google.ar.core.exceptions.CameraNotAvailableException
import com.google.ar.core.exceptions.UnavailableException
import com.google.ar.core.exceptions.UnavailableUserDeclinedInstallationException
import com.google.ar.sceneform.*
import com.google.ar.sceneform.math.Quaternion
import com.google.ar.sceneform.math.Vector3
import com.google.ar.sceneform.rendering.ModelRenderable
import com.google.ar.sceneform.rendering.Texture
import com.google.ar.sceneform.ux.AugmentedFaceNode
Expand All @@ -30,6 +34,7 @@ import io.flutter.plugin.common.MethodCall
import io.flutter.plugin.common.MethodChannel
import io.flutter.plugin.platform.PlatformView


class ArCoreView(val activity: Activity, context: Context, messenger: BinaryMessenger, id: Int, private val isAugmentedFaces: Boolean, private val debug: Boolean) : PlatformView, MethodChannel.MethodCallHandler {
private val methodChannel: MethodChannel = MethodChannel(messenger, "arcore_flutter_plugin_$id")
// private val activity: Activity = (context.applicationContext as FlutterApplication).currentActivity
Expand Down Expand Up @@ -181,6 +186,17 @@ class ArCoreView(val activity: Activity, context: Context, messenger: BinaryMess
}
"positionChanged" -> {
debugLog(" positionChanged")
updatePosition(call, result)

}
"scaleChanged" -> {
debugLog(" scaleChanged")
updateScale(call, result)
}

"nodeRotationChanged" -> {
debugLog(" nodeRotationChanged")
updateNodeRotation(call, result)

}
"rotationChanged" -> {
Expand Down Expand Up @@ -221,6 +237,26 @@ class ArCoreView(val activity: Activity, context: Context, messenger: BinaryMess
}
}
}
fun updateScale(call: MethodCall, result: MethodChannel.Result) {
val name = call.argument<String>("name")
val node = arSceneView?.scene?.findByName(name)
node?.localScale = parseVector3(call.arguments as HashMap<String, Any>) ?: Vector3(1.0F, 1.0F, 1.0F)
result.success(null)
}

fun updatePosition(call: MethodCall, result: MethodChannel.Result) {
val name = call.argument<String>("name")
val node = arSceneView?.scene?.findByName(name)
node?.localPosition = parseVector3(call.arguments as HashMap<String, Any>)?: Vector3()
result.success(null)
}

fun updateNodeRotation(call: MethodCall, result: MethodChannel.Result) {
val name = call.argument<String>("name")
val node = arSceneView?.scene?.findByName(name)
node?.localRotation = parseQuaternion(call.arguments as? HashMap<String, Double>) ?: Quaternion()
result.success(null)
}

/* fun maybeEnableArButton() {
Log.i(TAG,"maybeEnableArButton" )
Expand Down Expand Up @@ -556,11 +592,4 @@ class ArCoreView(val activity: Activity, context: Context, messenger: BinaryMess
}

}*/

/* fun updatePosition(call: MethodCall, result: MethodChannel.Result) {
val name = call.argument<String>("name")
val node = arSceneView?.scene?.findByName(name)
node?.localPosition = parseVector3(call.arguments as HashMap<String, Any>)
result.success(null)
}*/
}
1 change: 1 addition & 0 deletions example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -166,3 +166,4 @@ packages:
sdks:
dart: ">=2.12.0-0.0 <3.0.0"
flutter: ">=1.10.0"

34 changes: 23 additions & 11 deletions lib/src/arcore_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,19 @@ class ArCoreController {
return arcoreInstalled;
}

ArCoreController(
{int id,
this.enableTapRecognizer,
this.enablePlaneRenderer,
this.enableUpdateListener,
this.debug = false
ArCoreController({this.id,
this.enableTapRecognizer,
this.enablePlaneRenderer,
this.enableUpdateListener,
this.debug = false
// @required this.onUnsupported,
}) {
}) {
_channel = MethodChannel('arcore_flutter_plugin_$id');
_channel.setMethodCallHandler(_handleMethodCalls);
init();
}

final int id;
final bool enableUpdateListener;
final bool enableTapRecognizer;
final bool enablePlaneRenderer;
Expand Down Expand Up @@ -105,7 +105,7 @@ class ArCoreController {
}
break;
case 'getTrackingState':
// TRACKING, PAUSED or STOPPED
// TRACKING, PAUSED or STOPPED
trackingState = call.arguments;
if (debug) {
print('Latest tracking state received is: $trackingState');
Expand All @@ -116,7 +116,7 @@ class ArCoreController {
print('flutter onTrackingImage');
}
final arCoreAugmentedImage =
ArCoreAugmentedImage.fromMap(call.arguments);
ArCoreAugmentedImage.fromMap(call.arguments);
onTrackingImage(arCoreAugmentedImage);
break;
case 'togglePlaneRenderer':
Expand Down Expand Up @@ -190,13 +190,25 @@ class ArCoreController {

void _addListeners(ArCoreNode node) {
node.position.addListener(() => _handlePositionChanged(node));
node.rotation.addListener(() => _handleNodeRotationChanged(node));
node.scale.addListener(() => _handleScaleChanged(node));
node?.shape?.materials?.addListener(() => _updateMaterials(node));

if (node is ArCoreRotatingNode) {
node.degreesPerSecond.addListener(() => _handleRotationChanged(node));
}
}

void _handleNodeRotationChanged(ArCoreNode node) {
_channel.invokeMethod<void>('nodeRotationChanged',
_getHandlerParams(node, convertVector4ToMap(node.rotation.value)));
}

void _handleScaleChanged(ArCoreNode node) {
_channel.invokeMethod<void>('scaleChanged',
_getHandlerParams(node, convertVector3ToMap(node.scale.value)));
}

void _handlePositionChanged(ArCoreNode node) {
_channel.invokeMethod<void>('positionChanged',
_getHandlerParams(node, convertVector3ToMap(node.position.value)));
Expand All @@ -212,8 +224,8 @@ class ArCoreController {
'updateMaterials', _getHandlerParams(node, node.shape.toMap()));
}

Map<String, dynamic> _getHandlerParams(
ArCoreNode node, Map<String, dynamic> params) {
Map<String, dynamic> _getHandlerParams(ArCoreNode node,
Map<String, dynamic> params) {
final Map<String, dynamic> values = <String, dynamic>{'name': node.name}
..addAll(params);
return values;
Expand Down