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 RafalDorsz · Sep 10, 2014 at 10:48 AM · shadershader writinggeometry shader

Geometry Shader misses vertices problem.

So I've been working on a Geometry Shader, based on this: http://forum.unity3d.com/threads/geometry-shaders.156553/

Basically, I want it to generate an upright triangle off of every vertex of the base mesh. At this point I've simplified it as much as I could. The first Pass renders the base geometry (the Quad in the image) and the second Pass creates and renders the triangles (the red triangles in the image).

alt text

Here's the problem. The way I understand it my Geometry Shader program should be called for every vertex, no? it takes points as input, and emits a triangle stream. And yet, as you can see it seems to execute only once per base mesh triangle (if that is the case, how do I access all vertices of the triangle,if my input is a single point?)

What am I doing/understanding wrong?

Anyways,here's the code:

 Shader "Custom/Geometry_Shader_Test"
 {
     Properties 
     {
         _MainTex ("Base (RGBA)",2D) ="white"{}
     }
 
     SubShader 
     {
         Pass                ///////////////////////PASS 1
         {
             Tags { "RenderType"="Opaque" }
             LOD 200
             Cull Off
               
             CGPROGRAM
                 #pragma target 4.0
                 #pragma vertex VS_Main
                 #pragma fragment FS_Main
                 #include "UnityCG.cginc" 
 
                 // **************************************************************
                 // Data structures                                                *
                 // **************************************************************
                 struct vertexInput
                 {
                     float4 vertex : POSITION;
                     float2 texcoord :TEXCOORD0;
                 };
                 
                 struct fs_input          // also geometry shader input
                 {
                     float4    pos        : POSITION;
                     float2  tex      : TEXCOORD0;
                 };
 
 
 
                 // **************************************************************
                 // Vars                                                            *
                 // **************************************************************
                 sampler2D _MainTex;
                 
                 
                 
                 // **************************************************************
                 // Shader Programs                                                *
                 // **************************************************************
 
                 // Vertex Shader ------------------------------------------------
                 fs_input VS_Main(vertexInput v)
                 {
                     fs_input output;
                     output.pos = mul(UNITY_MATRIX_MVP,v.vertex);
                     output.tex = v.texcoord;
 
                     return output;
                 }
 
 
                 // Fragment Shader -----------------------------------------------
                 float4 FS_Main(fs_input input) : COLOR
                 {
                     return tex2D(_MainTex, float2(input.tex.xy));
                 }
 
             ENDCG
         }
         
         
                 Pass                  ///////////////////////// PASS 2
         { 
             Tags { "RenderType"="Opaque" }
             LOD 200
             Cull Off
               
             CGPROGRAM
                 #pragma target 4.0
                 #pragma vertex VS_Main
                 #pragma fragment FS_Main
                 #pragma geometry GS_Main
                 #include "UnityCG.cginc" 
 
                 // **************************************************************
                 // Data structures                                                *
                 // **************************************************************
                 
                 struct vertexOutput          // also geometry shader input
                 {
                     float4    pos        : POSITION;
                 };
 
                 struct gs_output
                 {
                     float4    pos        : POSITION;
                 };
 
                 // **************************************************************
                 // Shader Programs                                                *
                 // **************************************************************
 
                 // Vertex Shader ------------------------------------------------
                 vertexOutput VS_Main(float4 vertexPos: POSITION)
                 {
                     vertexOutput output;
                     output.pos=  mul(_Object2World, vertexPos);
                     return output;
                 }
                 // Geometry Shader -----------------------------------------------------
                 [maxvertexcount(3)]
                 void GS_Main(point vertexOutput p[1], inout TriangleStream<gs_output> triStream)
                 {
                       
                         gs_output pIn;
                         
             
                         float4 v[3];   // the triangle to be generated
                         v[0] = float4(p[0].pos.xyz, 1.0f);
                         v[1] = float4(p[0].pos.xyz+float3(0.2,1,0), 1.0f);
                         v[2] = float4(p[0].pos.xyz+float3(-0.2,1,0), 1.0f);
                         
                         
                         // append each vertex into the triangle stram:
                         
                         pIn.pos.w = 1.0f;
                         pIn.pos.xyz = mul(_World2Object,v[0]).xyz * unity_Scale.w;
                         pIn.pos = mul(UNITY_MATRIX_MVP,pIn.pos);
                         triStream.Append(pIn);
                         
                         pIn.pos.w = 1.0f;
                         pIn.pos.xyz = mul(_World2Object,v[1]).xyz * unity_Scale.w;
                         pIn.pos = mul(UNITY_MATRIX_MVP,pIn.pos);
                         triStream.Append(pIn);
                         
                         pIn.pos.w = 1.0f;
                         pIn.pos.xyz = mul(_World2Object,v[2]).xyz * unity_Scale.w;
                         pIn.pos = mul(UNITY_MATRIX_MVP,pIn.pos);
                         triStream.Append(pIn);
 
                 }
                 
                 // Fragment Shader -----------------------------------------------
                 float4 FS_Main(gs_output input) : COLOR
                 {
                     return float4(0.5,0,0,1);
                 }
 
             ENDCG
         }
     } 
 }

gs_problem.png (223.5 kB)
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
0

Answer by unisip · Mar 03, 2015 at 01:02 PM

I'm having similar problems understanding how point geometry shaders are working. From what I gather, it has to do with the triangles you submit in your mesh description. In your case, you have 4 vertices and 2 triangles. The geometry shader seems to be applied once per triangle, on the first vertex of the triangle. That doesn't quite tell you what to do to have the geometry shader run on all 4 vertices (aside from changing the triangles in the mesh), but at least I think that explains it. Haven't found a thorough explaination in unity doc about how to handle that yet

Comment
Add comment · 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
0

Answer by Jeizar · Aug 02, 2017 at 08:12 AM

I resurrect an old topic, but i had some difficulties to find an answer to that problem. If anybody encounter the same issue here is an explanation and a solution :

https://gamedev.stackexchange.com/questions/97009/geometry-shader-not-generating-geometry-for-some-vertices

Comment
Add comment · 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

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

shell fur in HDRP - possible? 0 Answers

Customize Standard Shader Light Rig - Help 1 Answer

CRT shader but NOT for camera 0 Answers

Shader graph is broken pink and after installed packages a lot of errors. How can I resolve all this ? 0 Answers

Shader: Mask Without a Texture? 2 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