Skip to content
This repository has been archived by the owner on Mar 26, 2024. It is now read-only.

Need coarse location permission together with the fine one #14

Open
wants to merge 10 commits into
base: step-place-anchor
Choose a base branch
from
22 changes: 11 additions & 11 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@ apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'

android {
compileSdkVersion 30
compileSdkVersion 33
defaultConfig {
applicationId "com.google.ar.core.codelabs.hellogeospatial"

// AR Optional apps must declare minSdkVersion >= 14.
// AR Required apps must declare minSdkVersion >= 24.
minSdkVersion 24
targetSdkVersion 30
versionCode 1
versionName '1.0'
targetSdkVersion 33
versionCode 2
versionName '1.1'
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
Expand All @@ -30,21 +30,21 @@ android {

dependencies {
// ARCore (Google Play Services for AR) library.
implementation 'com.google.ar:core:1.31.0'
implementation 'com.google.ar:core:1.34.0'

// Obj - a simple Wavefront OBJ file loader
// https://github.com/javagl/Obj
implementation 'de.javagl:obj:0.2.1'

implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.lifecycle:lifecycle-common-java8:2.2.0'
implementation 'androidx.appcompat:appcompat:1.5.1'
implementation 'androidx.lifecycle:lifecycle-common-java8:2.5.1'

implementation 'com.google.android.material:material:1.1.0'
implementation 'com.google.android.material:material:1.7.0'

implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
implementation 'com.google.android.gms:play-services-auth:20+'
implementation 'com.google.android.gms:play-services-location:19+'
implementation 'com.google.android.gms:play-services-maps:18.0.2'
implementation 'com.google.android.gms:play-services-auth:20.3.0'
implementation 'com.google.android.gms:play-services-location:21.0.1'
implementation 'com.google.android.gms:play-services-maps:18.1.0'

}
2 changes: 1 addition & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>


Expand All @@ -29,7 +30,6 @@
<uses-feature android:glEsVersion="0x00020000" android:required="true" />

<application
android:allowBackup="false"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* Copyright 2020 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

precision mediump float;

uniform sampler2D u_DepthTexture;
uniform sampler2D u_ColorMap;

varying vec2 v_TexCoord;

const float kMidDepthMeters = 8.0;
const float kMaxDepthMeters = 30.0;

float DepthGetMillimeters(in sampler2D depth_texture, in vec2 depth_uv) {
// Depth is packed into the red and green components of its texture.
// The texture is a normalized format, storing millimeters.
vec3 packedDepthAndVisibility = texture2D(depth_texture, depth_uv).xyz;
return dot(packedDepthAndVisibility.xy, vec2(255.0, 256.0 * 255.0));
}

// Returns linear interpolation position of value between min and max bounds.
// E.g. InverseLerp(1100, 1000, 2000) returns 0.1.
float InverseLerp(float value, float min_bound, float max_bound) {
return clamp((value - min_bound) / (max_bound - min_bound), 0.0, 1.0);
}

// Returns a color corresponding to the depth passed in.
// The input x is normalized in range 0 to 1.
vec3 DepthGetColorVisualization(in float x) {
return texture2D(u_ColorMap, vec2(x, 0.5)).rgb;
}

void main() {
// Interpolating in units of meters is more stable, due to limited floating
// point precision on GPU.
float depth_mm = DepthGetMillimeters(u_DepthTexture, v_TexCoord.xy);
float depth_meters = depth_mm * 0.001;

// Selects the portion of the color palette to use.
float normalized_depth = 0.0;
if (depth_meters < kMidDepthMeters) {
// Short-range depth (0m to 8m) maps to first half of the color palette.
normalized_depth = InverseLerp(depth_meters, 0.0, kMidDepthMeters) * 0.5;
} else {
// Long-range depth (8m to 30m) maps to second half of the color palette.
normalized_depth =
InverseLerp(depth_meters, kMidDepthMeters, kMaxDepthMeters) * 0.5 + 0.5;
}

// Converts depth to color by with the selected value in the color map.
vec4 depth_color = vec4(DepthGetColorVisualization(normalized_depth), 1.0);

// Invalid depth (pixels with value 0) mapped to black.
depth_color.rgb *= sign(depth_meters);
gl_FragColor = depth_color;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Copyright 2020 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

attribute vec4 a_Position;
attribute vec2 a_TexCoord;

varying vec2 v_TexCoord;

void main() {
v_TexCoord = a_TexCoord;
gl_Position = a_Position;
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ class HelloGeoRenderer(val activity: HelloGeoActivity) :
companion object {
val TAG = "HelloGeoRenderer"

private val Z_NEAR = 0.1f
private val Z_FAR = 1000f
private const val Z_NEAR = 0.1f
private const val Z_FAR = 1000f
}

lateinit var backgroundRenderer: BackgroundRenderer
Expand Down Expand Up @@ -92,7 +92,7 @@ class HelloGeoRenderer(val activity: HelloGeoActivity) :
Texture.ColorFormat.SRGB
)

virtualObjectMesh = Mesh.createFromAsset(render, "models/geospatial_marker.obj");
virtualObjectMesh = Mesh.createFromAsset(render, "models/geospatial_marker.obj")
virtualObjectShader =
Shader.createFromAssets(
render,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ class ARCoreSessionLifecycleHelper(

return try {
// Request installation if necessary.
when (ArCoreApk.getInstance().requestInstall(activity, !installRequested)!!) {
when (ArCoreApk.getInstance().requestInstall(activity, !installRequested)) {
ArCoreApk.InstallStatus.INSTALL_REQUESTED -> {
installRequested = true
// tryCreateSession will be called again, so we return null for now.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@ import androidx.core.content.ContextCompat

/** Helper to ask camera permission. */
object GeoPermissionsHelper {
private val PERMISSIONS = arrayOf(Manifest.permission.CAMERA, Manifest.permission.ACCESS_FINE_LOCATION)
private val PERMISSIONS = arrayOf(
Manifest.permission.CAMERA,
Manifest.permission.ACCESS_COARSE_LOCATION,
Manifest.permission.ACCESS_FINE_LOCATION
)

/** Check to see we have the necessary permissions for this app. */
fun hasGeoPermissions(activity: Activity): Boolean {
Expand All @@ -53,4 +57,4 @@ object GeoPermissionsHelper {
intent.data = Uri.fromParts("package", activity.packageName, null)
activity.startActivity(intent)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ class MapView(val activity: HelloGeoActivity, val googleMap: GoogleMap) {
color: Int,
): Marker {
val markersOptions = MarkerOptions()
.position(LatLng(0.0,0.0))
.position(LatLng(0.0, 0.0))
.draggable(false)
.anchor(0.5f, 0.5f)
.flat(true)
Expand All @@ -110,4 +110,4 @@ class MapView(val activity: HelloGeoActivity, val googleMap: GoogleMap) {
canvas.drawBitmap(navigationIcon, /* left= */0f, /* top= */0f, p)
return navigationIcon
}
}
}
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
ext.kotlin_version = '1.4.20'
ext.kotlin_version = '1.7.20'
repositories {
google()
mavenCentral()
}
dependencies {
classpath "com.android.tools.build:gradle:4.1.1"
classpath 'com.android.tools.build:gradle:7.3.1'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"

// NOTE: Do not place your application dependencies here; they belong
Expand Down
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-6.5.1-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-7.4-all.zip