Tim Mowrer
5 年前
当前提交
58c82467
共有 7 个文件被更改,包括 2496 次插入 和 2 次删除
-
28Assets/Scenes/ImageTracking/ImageTracking.unity
-
201Assets/Scenes/ImageTracking/DynamicLibrary.cs
-
11Assets/Scenes/ImageTracking/DynamicLibrary.cs.meta
-
1001Assets/Scenes/ImageTracking/Images/one.png
-
128Assets/Scenes/ImageTracking/Images/one.png.meta
-
1001Assets/Scenes/ImageTracking/Images/two.png
-
128Assets/Scenes/ImageTracking/Images/two.png.meta
|
|||
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; |
|||
} |
|||
} |
|||
} |
|||
} |
|
|||
fileFormatVersion: 2 |
|||
guid: 92dfcdbbdd0754f9fb947d3639ad91c4 |
|||
MonoImporter: |
|||
externalObjects: {} |
|||
serializedVersion: 2 |
|||
defaultReferences: [] |
|||
executionOrder: 0 |
|||
icon: {instanceID: 0} |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
1001
Assets/Scenes/ImageTracking/Images/one.png
文件差异内容过多而无法显示
查看文件
文件差异内容过多而无法显示
查看文件
|
|||
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
文件差异内容过多而无法显示
查看文件
文件差异内容过多而无法显示
查看文件
|
|||
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: |
撰写
预览
正在加载...
取消
保存
Reference in new issue