Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Xeal · Jul 10, 2012 at 03:52 PM · shaderlinerenderer

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 !

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1

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);
Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Xeal · Jul 16, 2012 at 09:56 AM 0
Share

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...

avatar image ScroodgeM · Jul 16, 2012 at 10:01 AM 1
Share

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)

avatar image Xeal · Jul 16, 2012 at 11:03 AM 0
Share

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 ?

avatar image ScroodgeM · Jul 16, 2012 at 11:13 AM 0
Share

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

avatar image ScroodgeM · Jul 16, 2012 at 11:14 AM 0
Share

PS did you think about texture coordinates? it can contains information you can use for your task

Show more comments
avatar image
0

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 !

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ScroodgeM · Jul 16, 2012 at 06:50 PM 0
Share

http://docs.unity3d.com/Documentation/Components/SL-BuiltinValues.html

_ScreenParams

may be it will help? 8)

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

LineRenderer unchanging texture rotation 0 Answers

Preventing a LineRenderer from drawing over itself 0 Answers

Change linerenderer color to custom shader's color 1 Answer

Line Renderer Ilumination 1 Answer

Low quality on Sprite and Line Renderer - not sharp enough 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges