浏览代码

Update image tracking sample to show how to add an image at runtime

/4.0
Tim Mowrer 5 年前
当前提交
58c82467
共有 7 个文件被更改,包括 2496 次插入2 次删除
  1. 28
      Assets/Scenes/ImageTracking/ImageTracking.unity
  2. 201
      Assets/Scenes/ImageTracking/DynamicLibrary.cs
  3. 11
      Assets/Scenes/ImageTracking/DynamicLibrary.cs.meta
  4. 1001
      Assets/Scenes/ImageTracking/Images/one.png
  5. 128
      Assets/Scenes/ImageTracking/Images/one.png.meta
  6. 1001
      Assets/Scenes/ImageTracking/Images/two.png
  7. 128
      Assets/Scenes/ImageTracking/Images/two.png.meta

28
Assets/Scenes/ImageTracking/ImageTracking.unity


- component: {fileID: 857573723}
- component: {fileID: 857573725}
- component: {fileID: 857573726}
- component: {fileID: 857573727}
m_Layer: 0
m_Name: AR Session Origin
m_TagString: Untagged

m_EditorClassIdentifier:
m_WorldSpaceCanvasCamera: {fileID: 1247506530}
m_DefaultTexture: {fileID: 10305, guid: 0000000000000000f000000000000000, type: 0}
--- !u!114 &857573727
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 857573722}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 92dfcdbbdd0754f9fb947d3639ad91c4, type: 3}
m_Name:
m_EditorClassIdentifier:
m_Images:
- m_Texture: {fileID: 2800000, guid: d1879707d69d24adfb421bde69c92c94, type: 3}
m_Name: One
m_Width: 0.1
- m_Texture: {fileID: 2800000, guid: 038b8a7b4862745aaadfd7c9f14e7bbd, type: 3}
m_Name: Two
m_Width: 0.1
--- !u!1 &1247506529
GameObject:
m_ObjectHideFlags: 0

m_Script: {fileID: 11500000, guid: 4966719baa26e4b0e8231a24d9bd491a, type: 3}
m_Name:
m_EditorClassIdentifier:
m_FocusMode: 1
m_LightEstimationMode: 0
m_FocusMode: -1
m_LightEstimationMode: -1
m_AutoFocus: 1
m_LightEstimation: 0
m_FacingDirection: 1
--- !u!1 &1338354114
GameObject:
m_ObjectHideFlags: 0

m_EditorClassIdentifier:
m_AttemptUpdate: 1
m_MatchFrameRate: 1
m_TrackingMode: 2
--- !u!4 &1338354117
Transform:
m_ObjectHideFlags: 0

201
Assets/Scenes/ImageTracking/DynamicLibrary.cs


using System;
using System.Text;
using Unity.Jobs;
using UnityEngine;
using UnityEngine.XR.ARFoundation;
using UnityEngine.XR.ARSubsystems;
/// <summary>
/// Adds images to the reference library at runtime.
/// </summary>
[RequireComponent(typeof(ARTrackedImageManager))]
public class DynamicLibrary : MonoBehaviour
{
[Serializable]
public class ImageData
{
[SerializeField, Tooltip("The source texture for the image. Must be marked as readable.")]
Texture2D m_Texture;
public Texture2D texture
{
get => m_Texture;
set => m_Texture = value;
}
[SerializeField, Tooltip("The name for this image.")]
string m_Name;
public string name
{
get => m_Name;
set => m_Name = value;
}
[SerializeField, Tooltip("The width, in meters, of the image in the real world.")]
float m_Width;
public float width
{
get => m_Width;
set => m_Width = value;
}
public JobHandle jobHandle { get; set; }
}
[SerializeField, Tooltip("The set of images to add to the image library at runtime")]
ImageData[] m_Images;
/// <summary>
/// The set of images to add to the image library at runtime
/// </summary>
public ImageData[] images
{
get => m_Images;
set => m_Images = value;
}
enum State
{
NoImagesAdded,
AddImagesRequested,
AddingImages,
Done,
Error
}
State m_State;
string m_ErrorMessage = "";
StringBuilder m_StringBuilder = new StringBuilder();
void OnGUI()
{
var fontSize = 30;
GUI.skin.button.fontSize = fontSize;
GUI.skin.label.fontSize = fontSize;
switch (m_State)
{
case State.NoImagesAdded:
{
if (GUILayout.Button("Add images"))
{
m_State = State.AddImagesRequested;
}
break;
}
case State.AddingImages:
{
m_StringBuilder.Clear();
m_StringBuilder.AppendLine("Add image status:");
foreach (var image in m_Images)
{
m_StringBuilder.AppendLine($"\t{image.name}: {(image.jobHandle.IsCompleted ? "done" : "pending")}");
}
GUILayout.Label(m_StringBuilder.ToString());
break;
}
case State.Done:
{
GUILayout.Label("All images added");
break;
}
case State.Error:
{
GUILayout.Label(m_ErrorMessage);
break;
}
}
}
void SetError(string errorMessage)
{
m_State = State.Error;
m_ErrorMessage = $"Error: {errorMessage}";
}
void Update()
{
switch (m_State)
{
case State.AddImagesRequested:
{
if (m_Images == null)
{
SetError("No images to add.");
break;
}
var manager = GetComponent<ARTrackedImageManager>();
if (manager == null)
{
SetError($"No {nameof(ARTrackedImageManager)} available.");
break;
}
// You can either add raw image bytes or use the extension method (used below) which accepts
// a texture. To use a texture, however, its import settings must have enabled read/write
// access to the texture.
foreach (var image in m_Images)
{
if (!image.texture.isReadable)
{
SetError($"Image {image.name} must be readable to be added to the image library.");
break;
}
}
if (manager.referenceLibrary is MutableRuntimeReferenceImageLibrary dynamicLibrary)
{
try
{
foreach (var image in m_Images)
{
// Note: You do not need to do anything with the returned JobHandle, but it can be
// useful if you want to know when the image has been added to the library since it may
// take several frames.
image.jobHandle = dynamicLibrary.ScheduleAddImageJob(image.texture, image.name, image.width);
}
m_State = State.AddingImages;
}
catch (InvalidOperationException e)
{
SetError($"ScheduleAddImageJob threw exception: {e.Message}");
}
}
else
{
SetError($"The reference image library is not mutable.");
}
break;
}
case State.AddingImages:
{
// Check for completion
var done = true;
foreach (var image in m_Images)
{
if (!image.jobHandle.IsCompleted)
{
done = false;
break;
}
}
if (done)
{
m_State = State.Done;
}
break;
}
}
}
}

11
Assets/Scenes/ImageTracking/DynamicLibrary.cs.meta


fileFormatVersion: 2
guid: 92dfcdbbdd0754f9fb947d3639ad91c4
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

1001
Assets/Scenes/ImageTracking/Images/one.png
文件差异内容过多而无法显示
查看文件

128
Assets/Scenes/ImageTracking/Images/one.png.meta


fileFormatVersion: 2
guid: d1879707d69d24adfb421bde69c92c94
TextureImporter:
internalIDToNameTable: []
externalObjects: {}
serializedVersion: 11
mipmaps:
mipMapMode: 0
enableMipMap: 0
sRGBTexture: 1
linearTexture: 0
fadeOut: 0
borderMipMap: 0
mipMapsPreserveCoverage: 0
alphaTestReferenceValue: 0.5
mipMapFadeDistanceStart: 1
mipMapFadeDistanceEnd: 3
bumpmap:
convertToNormalMap: 0
externalNormalMap: 0
heightScale: 0.25
normalMapFilter: 0
isReadable: 1
streamingMipmaps: 0
streamingMipmapsPriority: 0
grayScaleToAlpha: 0
generateCubemap: 6
cubemapConvolution: 0
seamlessCubemap: 0
textureFormat: 1
maxTextureSize: 2048
textureSettings:
serializedVersion: 2
filterMode: 1
aniso: -1
mipBias: -100
wrapU: -1
wrapV: -1
wrapW: -1
nPOTScale: 1
lightmap: 0
compressionQuality: 50
spriteMode: 0
spriteExtrude: 1
spriteMeshType: 1
alignment: 0
spritePivot: {x: 0.5, y: 0.5}
spritePixelsToUnits: 100
spriteBorder: {x: 0, y: 0, z: 0, w: 0}
spriteGenerateFallbackPhysicsShape: 1
alphaUsage: 1
alphaIsTransparency: 0
spriteTessellationDetail: -1
textureType: 0
textureShape: 1
singleChannelComponent: 0
maxTextureSizeSet: 0
compressionQualitySet: 0
textureFormatSet: 0
applyGammaDecoding: 0
platformSettings:
- serializedVersion: 3
buildTarget: DefaultTexturePlatform
maxTextureSize: 2048
resizeAlgorithm: 0
textureFormat: 3
textureCompression: 1
compressionQuality: 50
crunchedCompression: 0
allowsAlphaSplitting: 0
overridden: 0
androidETC2FallbackOverride: 0
forceMaximumCompressionQuality_BC6H_BC7: 0
- serializedVersion: 3
buildTarget: Standalone
maxTextureSize: 2048
resizeAlgorithm: 0
textureFormat: 3
textureCompression: 1
compressionQuality: 50
crunchedCompression: 0
allowsAlphaSplitting: 0
overridden: 0
androidETC2FallbackOverride: 0
forceMaximumCompressionQuality_BC6H_BC7: 0
- serializedVersion: 3
buildTarget: iPhone
maxTextureSize: 2048
resizeAlgorithm: 0
textureFormat: 3
textureCompression: 1
compressionQuality: 50
crunchedCompression: 0
allowsAlphaSplitting: 0
overridden: 0
androidETC2FallbackOverride: 0
forceMaximumCompressionQuality_BC6H_BC7: 0
- serializedVersion: 3
buildTarget: Android
maxTextureSize: 2048
resizeAlgorithm: 0
textureFormat: 3
textureCompression: 1
compressionQuality: 50
crunchedCompression: 0
allowsAlphaSplitting: 0
overridden: 0
androidETC2FallbackOverride: 0
forceMaximumCompressionQuality_BC6H_BC7: 0
spriteSheet:
serializedVersion: 2
sprites: []
outline: []
physicsShape: []
bones: []
spriteID:
internalID: 0
vertices: []
indices:
edges: []
weights: []
secondaryTextures: []
spritePackingTag:
pSDRemoveMatte: 0
pSDShowRemoveMatteOption: 0
userData:
assetBundleName:
assetBundleVariant:

1001
Assets/Scenes/ImageTracking/Images/two.png
文件差异内容过多而无法显示
查看文件

128
Assets/Scenes/ImageTracking/Images/two.png.meta


fileFormatVersion: 2
guid: 038b8a7b4862745aaadfd7c9f14e7bbd
TextureImporter:
internalIDToNameTable: []
externalObjects: {}
serializedVersion: 11
mipmaps:
mipMapMode: 0
enableMipMap: 0
sRGBTexture: 1
linearTexture: 0
fadeOut: 0
borderMipMap: 0
mipMapsPreserveCoverage: 0
alphaTestReferenceValue: 0.5
mipMapFadeDistanceStart: 1
mipMapFadeDistanceEnd: 3
bumpmap:
convertToNormalMap: 0
externalNormalMap: 0
heightScale: 0.25
normalMapFilter: 0
isReadable: 1
streamingMipmaps: 0
streamingMipmapsPriority: 0
grayScaleToAlpha: 0
generateCubemap: 6
cubemapConvolution: 0
seamlessCubemap: 0
textureFormat: 1
maxTextureSize: 2048
textureSettings:
serializedVersion: 2
filterMode: -1
aniso: -1
mipBias: -100
wrapU: -1
wrapV: -1
wrapW: -1
nPOTScale: 1
lightmap: 0
compressionQuality: 50
spriteMode: 0
spriteExtrude: 1
spriteMeshType: 1
alignment: 0
spritePivot: {x: 0.5, y: 0.5}
spritePixelsToUnits: 100
spriteBorder: {x: 0, y: 0, z: 0, w: 0}
spriteGenerateFallbackPhysicsShape: 1
alphaUsage: 1
alphaIsTransparency: 0
spriteTessellationDetail: -1
textureType: 0
textureShape: 1
singleChannelComponent: 0
maxTextureSizeSet: 0
compressionQualitySet: 0
textureFormatSet: 0
applyGammaDecoding: 0
platformSettings:
- serializedVersion: 3
buildTarget: DefaultTexturePlatform
maxTextureSize: 2048
resizeAlgorithm: 0
textureFormat: 3
textureCompression: 1
compressionQuality: 50
crunchedCompression: 0
allowsAlphaSplitting: 0
overridden: 0
androidETC2FallbackOverride: 0
forceMaximumCompressionQuality_BC6H_BC7: 0
- serializedVersion: 3
buildTarget: Standalone
maxTextureSize: 2048
resizeAlgorithm: 0
textureFormat: 3
textureCompression: 1
compressionQuality: 50
crunchedCompression: 0
allowsAlphaSplitting: 0
overridden: 0
androidETC2FallbackOverride: 0
forceMaximumCompressionQuality_BC6H_BC7: 0
- serializedVersion: 3
buildTarget: iPhone
maxTextureSize: 2048
resizeAlgorithm: 0
textureFormat: 3
textureCompression: 1
compressionQuality: 50
crunchedCompression: 0
allowsAlphaSplitting: 0
overridden: 0
androidETC2FallbackOverride: 0
forceMaximumCompressionQuality_BC6H_BC7: 0
- serializedVersion: 3
buildTarget: Android
maxTextureSize: 2048
resizeAlgorithm: 0
textureFormat: 3
textureCompression: 1
compressionQuality: 50
crunchedCompression: 0
allowsAlphaSplitting: 0
overridden: 0
androidETC2FallbackOverride: 0
forceMaximumCompressionQuality_BC6H_BC7: 0
spriteSheet:
serializedVersion: 2
sprites: []
outline: []
physicsShape: []
bones: []
spriteID:
internalID: 0
vertices: []
indices:
edges: []
weights: []
secondaryTextures: []
spritePackingTag:
pSDRemoveMatte: 0
pSDShowRemoveMatteOption: 0
userData:
assetBundleName:
assetBundleVariant:
正在加载...
取消
保存