So in my initial post i asked for a solution of the bottom image... after testing i came to the conclusion that the examples provided below had some bad side-effects concerning the angle of the camera... The top image is what i am after... the green area representing the visible area... How would i clip/cut an object/mesh/particle/etc as soon as it leaves the green area based on position(not height) ? And if possible in combination to have this transition being done in a fading way? Note that i have no shader coding experience... so i am hoping for a solution, not hints or snippets...
It is possible, there's a shader that cuts object, depending on camera posotion. Although i can't get it to work on samsung devices. Code (CSharp): Shader "PinballShaders/BoundaryGlowShader2" { Properties { _Color ("Main Color", Color) = (1,1,1,1) _Shift ("Shift", Float) = 1.0 _MainTex ("Texture", 2D) = "white" {} } SubShader { Tags {"Queue" = "Transparent" "IgnoreProjector" = "True" "RenderType" = "Transparent"} Lighting Off Blend SrcAlpha OneMinusSrcAlpha Cull Off Pass { CGPROGRAM #pragma vertex vert #pragma fragment frag #include "UnityCG.cginc" float4 _Color; sampler2D _MainTex; float4 _Fade; float _Shift; struct v2f { float4 pos : POSITION; float4 color : COLOR0; float2 uv : TEXCOORD0; float alpha:TEXCOORD1; float4 fragPos : TEXCOORD2; float4 fade : TEXCOORD3; }; float4 _MainTex_ST; v2f vert (appdata_base v) { v2f o; o.color = _Color; o.pos = mul (UNITY_MATRIX_MVP, v.vertex); o.uv = TRANSFORM_TEX (v.texcoord, _MainTex); o.fragPos = mul (UNITY_MATRIX_MV, v.vertex); o.fade = mul (UNITY_MATRIX_MV, _Fade); return o; } half4 frag (v2f i) : COLOR { float4 outColor = i.color; half4 texcol = tex2D (_MainTex, i.uv); float dist = distance(i.fade, i.fragPos); float4 objectOrigin = mul(_Object2World, float4(0.0,0.0,0.0,1.0) ); float dist2 = distance(objectOrigin, _WorldSpaceCameraPos) + _Shift; if (dist < dist2) { discard; } return texcol * outColor; } ENDCG } } FallBack "VertexLit" }
Thank you for replying and offering me a possible solution Vade Mecum! I have tried testing this... but i can't really tell what's going on....it seems to be doing this in reverse? So i drag this shader onto the red cube, then the bear should clip, depending on the current camera position?
This should work for solids: http://wiki.unity3d.com/index.php?title=DepthMask Assign the shader to your red box's material. It writes to depth buffer and thus prevents objects (that are depth tested) from being rendered.
Thanks PixelMind! A very nice solution indeed! Have not come across this page... it has its faults but nice find
I'll see if I can get any traction. This doesn't seem trivial, so trying to solve it might be tough without experience in shader writing.
So, a couple of things: ANYONE: with shader experience, don't be afraid to chip in. We'll appreciate it. Rob: Can you tell us *why* you want this shader? Not the technical images you've supplied, but the final desired output, and what it's being used for? Sometimes it's easier to find a working solution when we know your end goal, rather than chasing the specific issue you think will achieve that end goal. Can you describe what you want to "see" on the screen, and how you want to implement this and why?
So - correct me if I'm wrong. You want to define an area/volume in the game, and if the object leaves the area, you don't want it rendered... ... but more importantly, if you have an object that's partly within this area, only the parts INSIDE the area are rendered and the rest of the object is NOT rendered? Correct?
Yes that is correct, the screenshot i made in my first post should help i hope visualize that... "Anything" showing on the green area is fully visible, until it goes over to the red area, that part should simply clip/cut off!
You may want to try this: 1. Get your bear model vertices position in world space 2. In C#/JS do shader.SetGlobalFloat or SetGlobalMatrix and pass your green box vertices position in world space into it 3. Now all of your shaders can access to the GreenBox vertices positions (with the name of your choice) 4. You now need to get GreenPox matrix/floats into your bear shader, iterpolate this data per-pixel. Also, interpolate Bear WS pos per pixel 5. Do this: Code (CSharp): if (BearWorldSpacePositionPerPixel > GreenBoxWorldSpacePositionPerPixel) discard; So theoretically it should compare distance between GreenBox volume and Bear volume. If any Bear pixel is away from GreenBox volume it should not be rendered.
Ok. I've done it. Project files and a quick tutorial can be found here: http://ethicalmotion.blogspot.com/2015/03/dissolve-box.html
No problemo I'm 100% sure that there is an easier and faster way to do this. Logical comparisons are not the best option for the GPU code. But since I don't know other way... It is an easy task to extend this method for shapes like sphere or hemisphere. For sphere you simply define a sphere radius and position. Then you check if an object's pixels are further away from the point than the distance.