Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
1
Question by Penta- · Aug 06, 2017 at 05:54 PM · shadergrasscggeometry shader

Hi, I'm doing a geometry shader to be able to perform grass. I have a problem (which I think it is) with the transformation of the vertecies.

I'm doing a shader following a tutorial, this is the link

http://ellenack.com/2016/11/05/grass-geometry-shader/

In this, they only do the explanation and development of the geometry shader (which does not bother me). I have been in the middle, since when it is supposed that the first strands of the grass should appear, these are not seen. Playing with the transformations of the vertices that leave the GS I have managed to visualize different things, but I can not do it correctly. Regarding the fragment shader, I have used a lambert shading which is taken from another forum like this.

Enter the code:

 Shader "Custom/Pasto"
 {
     Properties
     {
         _Length ("Cantidad", Float) = 10
         _Width ("Ancho", Float) = 1
         _Gravity ("Gravedad", Float) = 0
         _Steps ("Pasos", Int) = 20
         _shadercolor ("supersimple color", Color) = (1,1,1,1)
         _ObjectSpaceLightPos("Some Vector", Vector) = (0,0,0,0)
 
     }
     SubShader
     {
         Tags { "RenderType"="Opaque" }
         Pass
         {
             CGPROGRAM
             #pragma vertex vert
             #pragma geometry geom
             #pragma fragment frag

             #include "UnityCG.cginc"
 
             //Structura de entrada al vertex
             struct appdata
             {
                 float4 vertex : POSITION;
                 float3 normal : NORMAL;
             };
             //Structura de entra al geometry
             struct geomInput
             {
                 float4 pos : POSITION;
                 float3 normal: NORMAL;
             };
             //Structura de entrada al fragment
             struct v2f
             {
                 float4 vertex : SV_POSITION;
                 float3 normal: NORMAL;
             };
 
             //Vertex shader
             geomInput vert (appdata v)
             {
                 geomInput o;
                 //o.pos = v.vertex;
                 o.pos = UnityObjectToClipPos(v.vertex);
                 o.normal = v.normal;
                 return o;
             }
             float _Length;
             float _Width;
             float _Gravity;
             float _Steps;
 
             //Geometry shader
             [maxvertexcount(80)]
             void geom(triangle geomInput i[3], inout TriangleStream<v2f> stream)
             {
                 float4 P1 = i[0].pos;
                 float4 P2 = i[1].pos;
                 float4 P3 = i[2].pos;
 
                 float3 N1 = i[0].normal;
                 float3 N2 = i[1].normal;
                 float3 N3 = i[2].normal;
 
                 float4 P = (P1 + P2 + P3) / 3.0;
                 float3 N = (N1 + N2 + N3) / 3.0;
                 float4 T = float4 (normalize((P2.xyz-P1.xyz)), 0.0);
 
                 for( int i = 0; i < _Steps; i++ )
                 {
     
                     float t0 = (float)i / _Steps;
                     float t1 = (float)(i+1) / _Steps;
  
                     float4 p0 = normalize(float4(N, 0) - (float4(0, _Length * t0, 0, 0) * _Gravity * t0)) * (_Length * t0);
                     float4 p1 = normalize(float4(N, 0) - (float4(0, _Length * t1, 0, 0) * _Gravity * t1)) * (_Length * t1);
  
 
                     float4 w0 = T * lerp(_Width, 0, t0);
                     float4 w1 = T * lerp(_Width, 0, t1);
 
                     v2f ot;
 
                     float4x4 vp = mul(UNITY_MATRIX_MVP, unity_WorldToObject);
 
                     ot.vertex = p0 - w0;
                     //ot.vertex = mul(UNITY_MATRIX_MVP, p0 - w0);
                     ot.normal = cross(T, p0);
                     stream.Append(ot);
 
                     ot.vertex = p0 + w0;
                     //ot.vertex = mul(UNITY_MATRIX_MVP, p0 + w0);
                     ot.normal = cross(T, p0);
                     stream.Append(ot);
 
                     ot.vertex = p1 - w1;
                     //ot.vertex = mul(UNITY_MATRIX_MVP, p1 - w1);
                     ot.normal = cross(T, p1);
                     stream.Append(ot);
 
                     ot.vertex = p1 + w1;
                     //ot.vertex = mul(UNITY_MATRIX_MVP, p1 + w1);
                     ot.normal = cross(T, p1);
                     stream.Append(ot);
 
                 }        
             }
 
             //Fragment shader
             float4 _shadercolor;
             float4 _ObjectSpaceLightPos; //position vector of light source
 
             fixed4 frag (v2f i) : SV_Target
             {
                 float3 lightdirWRTvertes=((i.vertex)-(_ObjectSpaceLightPos)); 
                 float3 worldnormal = normalize(i.normal);
                 float3 lightdir = normalize(lightdirWRTvertes);
                 float3 lambert = saturate(dot(lightdir,worldnormal));
                 fixed4 col;
                 col.rgb = lambert*_shadercolor;
                         col.a=1;//a for transperency or alpha.
                 return col;
             }
             ENDCG
         }
     }
 }
 

They could help me with this. Sorry for the inconvenience, I'm fairly new to the GS and I have no exact idea how it works. Thank you :)

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by xerneasicle · Dec 01, 2017 at 02:54 AM

I actually just figured this out myself. In the tutorial you and I used, they don't mention anything about using the P variable in the beginning.

I simply added that onto the position that I'm passing into the triStream and that fixed my issue!

Instead of ot.vertex = p0 - w0; it'd be ot.vertex = UnityObjectToClipPos((p0 - w0) + P);

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

148 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 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 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Mirror reflection not working as planned 0 Answers

Shader Clip with Stencil 0 Answers

Unlit unity terrain 1 Answer

Shader is not supported on this GPU.... Grass Shader 0 Answers

Can we make the terrain grass has specular when recieve sun light ? 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