- Home /
How to cut a LineRenderer ?
Hi,
I would like to cut a line displayed by a LineRenderer in a view-independant way.
I have this shader working with meshes. (here it colors in red the part of the object with x < 0 and black for x > 0)
With a LineRenderer, the cut is always view-dependant... And useWorldSpace does not change anything.
Shader "Custom/LineShader" {
Properties {
}
SubShader {
Tags { "Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent-1" }
LOD 200
Pass{
Blend SrcAlpha OneMinusSrcAlpha
CGPROGRAM
#include "UnityCG.cginc"
// Setup
#pragma target 3.0
#pragma vertex vert
#pragma fragment frag
#pragma multi_compile_builtin
struct v2p {
float4 pos : SV_POSITION;
float4 lPos : TEXCOORD0;
};
struct frag_out {
float4 Color : COLOR0;
};
float4x4 _localMat;
v2p vert(appdata_full v) {
v2p o;
o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
o.lPos = v.vertex;
return o;
}
frag_out frag(v2p i){
frag_out o;
float4 c = float4(0.0,0.0,0.0,1.0);
if(i.lPos.x <0)
c.r = 1.0;
o.Color = c;
return o;
}
ENDCG
}
}
FallBack "Diffuse"
}
A hint anyone ?
Thank you in advance !
Answer by ScroodgeM · Jul 14, 2012 at 09:15 AM
if you want to color line based on it's world position, first you need to change
o.wPos = v.vertex;
to
o.wPos = mul(_Object2World, v.vertex);
Oh, the name of the variable is misleading (I'm going to edit this). Actually I would like to define a geometrical area in the referential of the line. It does work for a mesh but not with a LineRenderer...
try to assign output color using local coordinate directly - this will open the renderer's secret in which coordinates does it comes to shader.
float4 c = float4(i.lPos.xyz, 1.0);
if it's still left unclear, return here and tell us what you see 8)
Thanks for your response. It seems that the coordinates are always define in screen space. The colors change according to the position in the XY screen coordinates. Is it a property of the linerenderer ?
if v.vertex already contains coordinates in screen-space then you can't handle local coordinates in shader. i think even it hasn't local coordinates at all - it calculates them 'on the fly' and puts already calculated coordinates to shader directly. so if you still want to use local coordinates - search them before shader drawing
PS did you think about texture coordinates? it can contains information you can use for your task
Answer by Xeal · Jul 16, 2012 at 06:34 PM
Thanks to ScroodgeM, I realized that the v.vertex values of a LineRenderer are the screen-space coordinates. But in this case I need the world positions.
With the texture coordinates, we can get the position along the line. So the solution I found is to pass the shader a texture containing the world positions of each vertex and use the texCoords to retrieve them.
One problem is that you have to convert XYZ positions into colors (in the range [0,1]) and pass the min and max values to the shader to retrieve the original values.
Real to color : colVal = (realVal - minVal) / (maxVal-minVal)
Color to real : realVal = (colVal * (maxVal-minVal)) + minVal
I'm happy because it works, but if someone have a more elegant solution, please share it !
http://docs.unity3d.com/Documentation/Components/SL-BuiltinValues.html
_ScreenParams
may be it will help? 8)