The Mixed Reality Forums here are no longer being used or maintained.
There are a few other places we would like to direct you to for support, both from Microsoft and from the community.
The first way we want to connect with you is our mixed reality developer program, which you can sign up for at https://aka.ms/IWantMR.
For technical questions, please use Stack Overflow, and tag your questions using either hololens or windows-mixed-reality.
If you want to join in discussions, please do so in the HoloDevelopers Slack, which you can join by going to https://aka.ms/holodevelopers, or in our Microsoft Tech Communities forums at https://techcommunity.microsoft.com/t5/mixed-reality/ct-p/MicrosoftMixedReality.
And always feel free to hit us up on Twitter @MxdRealityDev.
Avoidance of passing holograms through the user. The smooth changing of the level of "transperency".
Hello guys!
While I was reading Hologram
article (https://developer.microsoft.com/en-us/windows/mixed-reality/hologram), I've found something really exciting about how we can avoid the degradation of user's experience. Here is the bulk logic:
Two meters is the most optimal, and the experience will degrade the closer you get from one meter. At distances nearer than one meter, holograms that regularly move in depth are more likely to be problematic than stationary holograms. Consider gracefully clipping or fading out your content when it gets too close so as not to jar the user into an unexpected experience.
In terms of the last sentence, I don't really know to realize smooth fading as we get closer and closer to the hologram. I personally think that this functionality works as follows:
The
distance
between user and hologram is 0.5m, so than the variable which will be calledfadingFactor = 0.5f
, if thedistance
will equal 0.2m, so thanfadingFactor = 0.8f
and so on. Consequently, if the distance between user and hologram is critical, so than that hologram should be fully transparent.
So, those who is already worked with something like this, please have a feedback.
All the best,
Andrew
Best Answer
-
Optionsthebanjomatic ✭✭✭
1) Truth is, it doesn't really matter where you attach this script it just needs to be attached to a game object so that the code runs and sets the appropriate global values so that they are accessible to reference from the shaders. Since its setting a global value it doesn't really make sense to have multiple instances of it, therefore I use the camera for this because you are likely to only have one and is kind of like an extension to the existing near-plane setting on the camera.
2) This part is a more complicated answer, but .cginc files are basically just shader code that can be referenced from multiple shaders. Unfortunately, to get SpeedTree to work with near plane fade, I believe you will need to copy the shader and modify it to work. If you didn't know, you can download the source for the built-in shaders here: https://unity3d.com/unity/whats-new/unity-2017.1.0
As a disclaimer, I haven't tested this at all, but you need to do something like the following:
a) Add
#pragma multi_compile __ _NEAR_PLANE_FADE_ON
to the shader to enable the option
b) Add #include "..\relativePathTo\HoloToolkitCommon.cginc" or alternatively we can copy that function to our new file
c) modify the v2f structure to pass along the fade amount
d) modify the vertex shader to calculate fade amount
e) modify the fragment shader to apply the fade amount to the output colorAll together you might get something like this:
Pass { Tags { "LightMode" = "Vertex" } CGPROGRAM #pragma vertex vert #pragma fragment frag #pragma target 3.0 #pragma multi_compile_fog #pragma multi_compile_vertex __ LOD_FADE_PERCENTAGE LOD_FADE_CROSSFADE #pragma multi_compile_fragment __ LOD_FADE_CROSSFADE #pragma multi_compile_instancing #pragma multi_compile __ _NEAR_PLANE_FADE_ON #pragma instancing_options assumeuniformscaling lodfade maxcount:50 #pragma shader_feature GEOM_TYPE_BRANCH GEOM_TYPE_BRANCH_DETAIL GEOM_TYPE_FROND GEOM_TYPE_LEAF GEOM_TYPE_MESH #pragma shader_feature EFFECT_HUE_VARIATION #define ENABLE_WIND #include "SpeedTreeCommon.cginc" struct v2f { UNITY_POSITION(vertex); UNITY_FOG_COORDS(0) Input data : TEXCOORD1; #if _NEAR_PLANE_FADE_ON float fade : TEXCOORD2; #endif UNITY_VERTEX_INPUT_INSTANCE_ID UNITY_VERTEX_OUTPUT_STEREO }; float4 _NearPlaneFadeDistance; // Helper function for focal plane fading // Could instead be done non-linear in projected space for speed inline float ComputeNearPlaneFadeLinear(float4 vertex) { float distToCamera = -(UnityObjectToViewPos(vertex).z); return saturate(mad(distToCamera, _NearPlaneFadeDistance.y, _NearPlaneFadeDistance.x)); } v2f vert(SpeedTreeVB v) { v2f o; UNITY_SETUP_INSTANCE_ID(v); UNITY_TRANSFER_INSTANCE_ID(v, o); UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o); SpeedTreeVert(v, o.data); o.data.color.rgb *= ShadeVertexLightsFull(v.vertex, v.normal, 4, true); o.vertex = UnityObjectToClipPos(v.vertex); #if _NEAR_PLANE_FADE_ON o.fade = ComputeNearPlaneFadeLinear(v.vertex); #endif UNITY_TRANSFER_FOG(o,o.vertex); return o; } fixed4 frag(v2f i) : SV_Target { UNITY_SETUP_INSTANCE_ID(i); SpeedTreeFragOut o; SpeedTreeFrag(i.data, o); UNITY_APPLY_DITHER_CROSSFADE(i.vertex.xy); fixed4 c = fixed4(o.Albedo, o.Alpha); UNITY_APPLY_FOG(i.fogCoord, c); #if _NEAR_PLANE_FADE_ON c.rgb *= i.fade; #endif return c; } ENDCG } }
That being said, I don't know how well speedtree will perform on device. I seem to remember people saying it was too expensive to hit the framerate targets, but I haven't ever used it myself.
5
Answers
Take a look at the shaders in the MixedRealityToolkit-Unity project. They use a NearPlaneFadeDistance to achieve this.
I'll check this out. Thank you. If it works, I'll vote up
I'm a beginner and can't really understand how to add this property to my object's shader?! Please, help)
@AndrewMilko The easiest thing to do is to use one of the built-in shaders from the MixedRealityToolkit (such as FastConfigurable) and then to add the NearPlaneFade script to you camera.
If you need to convert an existing shader of yours to work with the NearPlaneFade script, I would suggest looking at one of the simpler shaders in the toolkit to see how it handles things there:
https://github.com/Microsoft/MixedRealityToolkit-Unity/blob/master/Assets/HoloToolkit/Utilities/Shaders/Obsolete/UnlitConfigurable.cginc
Its pretty simple, but in the vertex shader, you figure out the fade amount:
And then in the fragment shader you multiply the final output color by the fade amount:
First of all, thank you for contacting! It does your credit
1) If I'm using built-in shaders from "MixedRealityToolkit" everything works great but in terms of where I need to add that
NearPlaneFade
script, I can't really understand why we were need to add that thing to the camera. I have everything working when that script is attached only to the hologram which should be transparent sometimes. So, what's the difference if I add this script only to the hologram or only to the camera?2) As described later, I've used only built-in shaders for creating this effect but to convert an existing shader of mine to work with the NearPlaneFade script - that is not really clear for me. I wish you gave me some advices how to work with
.cginc
files. I don't know for what we are using them. Do I need to use it to create changes in my own shader or not? For instance, I'm usingSpeedTree
shader fromNature
folder in the shaders' hierarchy. If it's possible could you direct me what I need to do to have my shader that functionality? I just can't clearly see what lines of code I need to add to my object's shader.Thank you so mush, that I just can't imagine. You are out of this world!
@AndrewMilko
1) Truth is, it doesn't really matter where you attach this script it just needs to be attached to a game object so that the code runs and sets the appropriate global values so that they are accessible to reference from the shaders. Since its setting a global value it doesn't really make sense to have multiple instances of it, therefore I use the camera for this because you are likely to only have one and is kind of like an extension to the existing near-plane setting on the camera.
2) This part is a more complicated answer, but .cginc files are basically just shader code that can be referenced from multiple shaders. Unfortunately, to get SpeedTree to work with near plane fade, I believe you will need to copy the shader and modify it to work. If you didn't know, you can download the source for the built-in shaders here: https://unity3d.com/unity/whats-new/unity-2017.1.0
As a disclaimer, I haven't tested this at all, but you need to do something like the following:
a) Add
#pragma multi_compile __ _NEAR_PLANE_FADE_ON
to the shader to enable the optionb) Add #include "..\relativePathTo\HoloToolkitCommon.cginc" or alternatively we can copy that function to our new file
c) modify the v2f structure to pass along the fade amount
d) modify the vertex shader to calculate fade amount
e) modify the fragment shader to apply the fade amount to the output color
All together you might get something like this:
That being said, I don't know how well speedtree will perform on device. I seem to remember people saying it was too expensive to hit the framerate targets, but I haven't ever used it myself.