• 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
Question by maccabbe · Aug 09, 2015 at 05:08 PM · shadermeshcgdepthlines

Change Z offset of lines

I am trying to change the z offset of lines in unity so it is possible to cover one line with another in 3D space. For a simple case I am generating a 1x1 square on a 2x2 quad.

The code to generate the square is

 using UnityEngine;
 using System.Collections;
 
 public class Test : MonoBehaviour {
     void Start () {
         MeshFilter filter=gameObject.AddComponent<MeshFilter>();
         Mesh mesh=new Mesh();
         mesh.vertices=new Vector3[] { 
             new Vector3(-0.5f, -0.5f, 0),
             new Vector3(-0.5f, 0.5f, 0),
             new Vector3(0.5f, -0.5f, 0),
             new Vector3(0.5f, 0.5f, 0),
         };
         int[] lines=new int[] { 
             0, 1, 0, 2, 2, 3, 1, 3,
         };
         mesh.SetIndices(lines, MeshTopology.Lines, 0);
         mesh.normals=new Vector3[] {         
             new Vector3(-0.5f, -0.5f, 0),
             new Vector3(-0.5f, 0.5f, 0),
             new Vector3(0.5f, -0.5f, 0),
             new Vector3(0.5f, 0.5f, 0),
         };
         filter.mesh=mesh; 
 
         MeshRenderer renderer=gameObject.AddComponent<MeshRenderer>();
         renderer.sharedMaterial=Resources.Load<Material>("Materials/Flat Offset");
     }    
 }

where the flat offset shader is

 Shader "Custom/Flat Offset"{
    Properties {
      _Color ("Color", Color) = (0.0, 0.0, 0.0, 0.0)
    }
    SubShader {
          Pass {
              Offset -1, -1
              CGPROGRAM
              
              #pragma vertex vert
              #pragma fragment frag
              
              uniform float4 _Color;
               
              float4 vert (float4 position: POSITION) : SV_POSITION
              {
                  return mul(UNITY_MATRIX_MVP, position);
              }
  
              float4 frag () : COLOR
              {
                  return _Color;
              }
              ENDCG
          }
    }
    FallBack "Diffuse"
 }


The shader works to fix z fighting between two quads

alt text

However this does not fix z fighting between the quad/line.

alt text

Is there a way to change the depth offset of lines? I want to make it so one group of lines has a different depth offset from another group of lines (in addition to having a different depth offset than quads).

quad-offset.png (1.6 kB)
squareoffset.png (1.5 kB)
Comment

People who like this

0 Show 0
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

1 Reply

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by zach-r-d · Aug 10, 2015 at 03:09 AM

I can't say for certain without seeing Unity's source code, but I suspect that Offset determines values passed to glPolygonOffset, which does not work with line primitives. It does work with polygons with the draw mode set to lines instead of fill, if there is a way to set that in Unity and the lines being drawn will always be polygons. If not, the solution is to just alter the vertex positions, which wouldn't be too bad in the vertex shader; just subtract a tiny amount in the z axis after the MVP multiplication.

Comment

People who like this

0 Show 2 · 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 maccabbe · Aug 10, 2015 at 10:35 AM 0
Share

@zach.r.d Thanks, I ended up adding 0.0003 to the z value of SV_POSITION for now. However setting the draw mode to lines instead of fill actually seems like a better solution for me. Are there any resources you would recommend to learn how to make a shader do that?

avatar image zach-r-d · Aug 10, 2015 at 10:57 AM 0
Share

Thanks, glad that's working out so far. I'm not sure if switching the polygon fill mode is actually possible at all, since it's an OpenGL thing and Unity would have to support it in a cross-platform way. It's done in OpenGL with glPolygonMode, but there doesn't seem to be an equivalent Pass setting in ShaderLab.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

23 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to determine shader center? 0 Answers

Depth issues with a shader 1 Answer

How to force the compilation of a shader in Unity? 5 Answers

ZBuffer and Object Depth 1 Answer

Thickness Surface Shader 0 Answers


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