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

Commit

Permalink
Dev lumin spatial understanding (#171)
Browse files Browse the repository at this point in the history
* changes for Lumin Spatial Understanding

* updated wmr checkout

* Final touched on Lumin Spatial Observer implementation and fixed wireframe shader to work on OpenGL

* updated lumin checkout
  • Loading branch information
StephenHodgson authored May 10, 2019
1 parent 68ca427 commit 73bdb37
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 64 deletions.
3 changes: 1 addition & 2 deletions Definitions/SpatialAwarenessSystem/SpatialMeshObject.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.

using XRTK.Extensions;
using UnityEngine;

namespace XRTK.Definitions.SpatialAwarenessSystem
Expand Down Expand Up @@ -50,7 +49,7 @@ public Mesh Mesh
get => Filter.sharedMesh;
internal set
{
// Reset the surface mesh collider to fit the updated mesh.
// Reset the surface mesh collider to fit the updated mesh.
// Unity tribal knowledge indicates that to change the mesh assigned to a
// mesh collider and mesh filter, the mesh must first be set to null. Presumably there
// is a side effect in the setter when setting the shared mesh to null.
Expand Down
5 changes: 5 additions & 0 deletions Inspectors/Utilities/SymbolicLinks/SymbolicLinker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,11 @@ private static async Task<bool> VerifySymbolicLink(string targetAbsolutePath, st
if (!isValid &&
Directory.Exists(targetAbsolutePath))
{
if (DebugEnabled)
{
Debug.Log($"Removing invalid link for {targetAbsolutePath}");
}

DeleteSymbolicLink(targetAbsolutePath);
}

Expand Down
19 changes: 7 additions & 12 deletions Providers/SpatialObservers/BaseMixedRealitySpatialMeshObserver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ protected BaseMixedRealitySpatialMeshObserver(string name, uint priority, BaseMi
meshObjectPrefab = profile.MeshObjectPrefab;
spatialMeshObjectPool = new Stack<SpatialMeshObject>();


if (additionalComponents != null)
{
requiredMeshComponents = new Type[additionalComponents.Length + 3];
Expand Down Expand Up @@ -80,7 +79,6 @@ public override void Initialize()
{
base.Initialize();

// Only update the observer if it is running.
if (!Application.isPlaying) { return; }

for (int i = 0; i < 10; i++)
Expand All @@ -94,7 +92,6 @@ public override void Enable()
{
base.Enable();

// Only update the observer if it is running.
if (!Application.isPlaying) { return; }

// If we've got some spatial meshes and were disabled previously, turn them back on.
Expand All @@ -109,7 +106,6 @@ public override void Update()
{
base.Update();

// Only update the observer if it is running.
if (!Application.isPlaying || !IsRunning) { return; }

lock (spatialMeshObjectPool)
Expand All @@ -127,11 +123,10 @@ public override void Disable()
{
base.Disable();

// Only update the observer if it is running.
if (!Application.isPlaying) { return; }

// Disable any spatial meshes we might have.
foreach (SpatialMeshObject meshObject in spatialMeshObjects.Values)
foreach (var meshObject in spatialMeshObjects.Values)
{
if (meshObject.GameObject != null)
{
Expand Down Expand Up @@ -233,7 +228,7 @@ public SpatialAwarenessMeshLevelOfDetail MeshLevelOfDetail
public bool IsStationaryObserver { get; }

/// <inheritdoc />
public Vector3 ObserverOrigin { get; protected set; }
public Vector3 ObserverOrigin { get; protected set; } = Vector3.zero;

/// <inheritdoc />
public Quaternion ObserverOrientation { get; protected set; } = Quaternion.identity;
Expand Down Expand Up @@ -264,7 +259,7 @@ public virtual void RaiseMeshUpdated(SpatialMeshObject spatialMeshObject)
/// <inheritdoc />
public virtual void RaiseMeshRemoved(SpatialMeshObject spatialMeshObject)
{
if (spatialMeshObjects.TryGetValue(spatialMeshObject.Id, out SpatialMeshObject spatialMesh))
if (spatialMeshObjects.TryGetValue(spatialMeshObject.Id, out var spatialMesh))
{
spatialMeshObjects.Remove(spatialMesh.Id);

Expand Down Expand Up @@ -300,7 +295,7 @@ public virtual void RaiseMeshRemoved(SpatialMeshObject spatialMeshObject)
/// <returns>A <see cref="SpatialMeshObject"/></returns>
protected async Task<SpatialMeshObject> RequestSpatialMeshObject(int meshId)
{
if (spatialMeshObjects.TryGetValue(meshId, out SpatialMeshObject spatialMesh))
if (spatialMeshObjects.TryGetValue(meshId, out var spatialMesh))
{
return spatialMesh;
}
Expand Down Expand Up @@ -328,7 +323,7 @@ private GameObject CreateBlankSpatialMeshGameObject()

if (meshObjectPrefab == null)
{
newGameObject = new GameObject($"Blank Spatial Mesh GameObject", requiredMeshComponents)
newGameObject = new GameObject("Blank Spatial Mesh GameObject", requiredMeshComponents)
{
layer = layer
};
Expand All @@ -337,9 +332,9 @@ private GameObject CreateBlankSpatialMeshGameObject()
{
newGameObject = UnityEngine.Object.Instantiate(meshObjectPrefab);

foreach (var requiredComponent in requiredMeshComponents)
for (var i = 0; i < requiredMeshComponents.Length; i++)
{
newGameObject.EnsureComponent(requiredComponent);
newGameObject.EnsureComponent(requiredMeshComponents[i]);
}

newGameObject.layer = layer;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System;
using System.Collections;
using UnityEngine;
using XRTK.Definitions.Utilities;
using XRTK.Interfaces.Providers.SpatialObservers;
using XRTK.Services;
Expand All @@ -20,18 +21,14 @@ public abstract class BaseMixedRealitySpatialObserverDataProvider : BaseDataProv
/// <param name="name"></param>
/// <param name="priority"></param>
/// <param name="profile"></param>
protected BaseMixedRealitySpatialObserverDataProvider(string name, uint priority, BaseMixedRealitySpatialObserverProfile profile) : base(name, priority)
protected BaseMixedRealitySpatialObserverDataProvider(string name, uint priority, BaseMixedRealitySpatialObserverProfile profile)
: base(name, priority)
{
if (profile == null)
{
throw new ArgumentNullException($"Missing profile for {name}");
}

if (MixedRealityToolkit.SpatialAwarenessSystem == null)
{
throw new Exception("Missing Spatial Awareness System!");
}

SourceId = MixedRealityToolkit.SpatialAwarenessSystem.GenerateNewObserverId();
StartupBehavior = profile.StartupBehavior;
UpdateInterval = profile.UpdateInterval;
Expand Down Expand Up @@ -82,12 +79,14 @@ public override void Disable()
/// <inheritdoc />
public virtual void StartObserving()
{
if (!Application.isPlaying) { return; }
IsRunning = true;
}

/// <inheritdoc />
public virtual void StopObserving()
{
if (!Application.isPlaying) { return; }
IsRunning = false;
}

Expand Down
9 changes: 2 additions & 7 deletions Providers/SpatialObservers/BaseMixedRealitySurfaceObserver.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.

using System;
using System.Collections.Generic;
using UnityEngine;
using XRTK.Interfaces.Providers.SpatialObservers;
Expand All @@ -19,13 +18,9 @@ public abstract class BaseMixedRealitySurfaceObserver : BaseMixedRealitySpatialO
/// <param name="name"></param>
/// <param name="priority"></param>
/// <param name="profile"></param>
protected BaseMixedRealitySurfaceObserver(string name, uint priority, BaseMixedRealitySurfaceObserverProfile profile) : base(name, priority, profile)
protected BaseMixedRealitySurfaceObserver(string name, uint priority, BaseMixedRealitySurfaceObserverProfile profile)
: base(name, priority, profile)
{
if (profile == null)
{
throw new Exception($"Missing profile for {name}");
}

SurfacePhysicsLayerOverride = profile.SurfacePhysicsLayerOverride;
SurfaceFindingMinimumArea = profile.SurfaceFindingMinimumArea;
DisplayFloorSurfaces = profile.DisplayFloorSurfaces;
Expand Down
81 changes: 44 additions & 37 deletions StandardAssets/Shaders/XRTK_Wireframe.shader
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Shader "Mixed Reality Toolkit/Wireframe"
_WireColor("Wire color", Color) = (1.0, 1.0, 1.0, 1.0)
_WireThickness("Wire thickness", Range(0, 800)) = 100
}
SubShader
SubShader
{
Tags { "RenderType" = "Opaque" }

Expand All @@ -25,10 +25,6 @@ Shader "Mixed Reality Toolkit/Wireframe"
#pragma geometry geom
#pragma fragment frag

// We only target the HoloLens (and the Unity editor), so take advantage of shader model 5.
#pragma target 5.0
#pragma only_renderers d3d11

#include "UnityCG.cginc"

float4 _BaseColor;
Expand All @@ -38,38 +34,49 @@ Shader "Mixed Reality Toolkit/Wireframe"
// Based on approach described in Shader-Based Wireframe Drawing (2008)
// http://orbit.dtu.dk/en/publications/id(13e2122d-bec7-48de-beca-03ce6ea1c3f1).html

struct v2g
struct appdata
{
float4 viewPos : SV_POSITION;
UNITY_VERTEX_OUTPUT_STEREO
float4 vertex : POSITION;
UNITY_VERTEX_INPUT_INSTANCE_ID
};

v2g vert(appdata_base v)
struct v2g
{
UNITY_SETUP_INSTANCE_ID(v);
v2g o;
o.viewPos = UnityObjectToClipPos(v.vertex);
UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);
return o;
}
float4 projectionSpaceVertex : SV_POSITION;
float4 worldSpacePosition : TEXCOORD1;
UNITY_VERTEX_OUTPUT_STEREO_EYE_INDEX
};

// inverseW is to counteract the effect of perspective-correct interpolation so that the lines
// worldSpacePosition is to counteract the effect of perspective-correct interpolation so that the lines
// look the same thickness regardless of their depth in the scene.
struct g2f
{
float4 viewPos : SV_POSITION;
float inverseW : TEXCOORD0;
float4 projectionSpaceVertex : SV_POSITION;
float worldSpacePosition : TEXCOORD0;
float3 dist : TEXCOORD1;
UNITY_VERTEX_OUTPUT_STEREO
};

v2g vert(appdata v)
{
v2g o;
UNITY_SETUP_INSTANCE_ID(v);
UNITY_INITIALIZE_OUTPUT_STEREO_EYE_INDEX(o);

o.projectionSpaceVertex = UnityObjectToClipPos(v.vertex);
o.worldSpacePosition = mul(unity_ObjectToWorld, v.vertex);
return o;
}

[maxvertexcount(3)]
void geom(triangle v2g i[3], inout TriangleStream<g2f> triStream)
{
UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(i[0]);

// Calculate the vectors that define the triangle from the input points.
float2 point0 = i[0].viewPos.xy / i[0].viewPos.w;
float2 point1 = i[1].viewPos.xy / i[1].viewPos.w;
float2 point2 = i[2].viewPos.xy / i[2].viewPos.w;
float2 point0 = i[0].projectionSpaceVertex.xy / i[0].projectionSpaceVertex.w;
float2 point1 = i[1].projectionSpaceVertex.xy / i[1].projectionSpaceVertex.w;
float2 point2 = i[2].projectionSpaceVertex.xy / i[2].projectionSpaceVertex.w;

// Calculate the area of the triangle.
float2 vector0 = point2 - point1;
Expand All @@ -87,14 +94,14 @@ Shader "Mixed Reality Toolkit/Wireframe"
// Output each original vertex with its distance to the opposing line defined
// by the other two vertices.
g2f o;
UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);

[unroll]
for (uint idx = 0; idx < 3; ++idx)
{
o.viewPos = i[idx].viewPos;
o.inverseW = 1.0 / o.viewPos.w;
o.dist = distScale[idx] * o.viewPos.w * wireScale;
UNITY_TRANSFER_VERTEX_OUTPUT_STEREO(i[idx], o);
o.projectionSpaceVertex = i[idx].projectionSpaceVertex;
o.worldSpacePosition = 1.0 / o.projectionSpaceVertex.w;
o.dist = distScale[idx] * o.projectionSpaceVertex.w * wireScale;
triStream.Append(o);
}
}
Expand All @@ -103,20 +110,20 @@ Shader "Mixed Reality Toolkit/Wireframe"
{
// Calculate minimum distance to one of the triangle lines, making sure to correct
// for perspective-correct interpolation.
float dist = min(i.dist[0], min(i.dist[1], i.dist[2])) * i.inverseW;
float dist = min(i.dist[0], min(i.dist[1], i.dist[2])) * i.worldSpacePosition;

// Make the intensity of the line very bright along the triangle edges but fall-off very
// quickly.
float I = exp2(-2 * dist * dist);
// Make the intensity of the line very bright along the triangle edges but fall-off very
// quickly.
float I = exp2(-2 * dist * dist);

// Fade out the alpha but not the color so we don't get any weird halo effects from
// a fade to a different color.
float4 color = I * _WireColor + (1 - I) * _BaseColor;
color.a = I;
return color;
}
ENDCG
// Fade out the alpha but not the color so we don't get any weird halo effects from
// a fade to a different color.
float4 color = I * _WireColor + (1 - I) * _BaseColor;
color.a = I;
return color;
}
ENDCG
}
}
FallBack "Diffuse"
FallBack "Diffuse"
}

0 comments on commit 73bdb37

Please sign in to comment.