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
0
Question by infilrtrator_55 · Jan 03, 2017 at 08:03 PM · animationshadervertex

Random rotation in a shader?

Hi, I'm trying to create a basic grass sway shader (using shader forge, I'm using a low poly bush type of mesh to test it on) but my grass seems sway in only one way (left/right) and it doesn't change directions. I want it to sway in every direction randomly so, I'm thinking maybe multiplying direction vector to a random vector or something can fix the issue. I'm very new to shaders and stuff please suggest a way to achieve this. Heres my shader code:

 // Upgrade NOTE: replaced '_Object2World' with 'unity_ObjectToWorld'
 
 // Shader created with Shader Forge v1.27 
 // Shader Forge (c) Neat Corporation / Joachim Holmer - http://www.acegikmo.com/shaderforge/
 // Note: Manually altering this data may prevent you from opening it in Shader Forge
 /*SF_DATA;ver:1.27;sub:START;pass:START;ps:flbk:,iptp:0,cusa:False,bamd:0,lico:1,lgpr:1,limd:1,spmd:1,trmd:0,grmd:0,uamb:True,mssp:True,bkdf:False,hqlp:False,rprd:False,enco:False,rmgx:True,rpth:0,vtps:0,hqsc:True,nrmq:1,nrsp:0,vomd:0,spxs:False,tesm:0,olmd:1,culm:0,bsrc:0,bdst:1,dpts:2,wrdp:True,dith:0,rfrpo:True,rfrpn:Refraction,coma:15,ufog:False,aust:True,igpj:False,qofs:0,qpre:2,rntp:3,fgom:False,fgoc:False,fgod:False,fgor:False,fgmd:0,fgcr:0.5,fgcg:0.5,fgcb:0.5,fgca:1,fgde:0.01,fgrn:0,fgrf:300,stcl:False,stva:128,stmr:255,stmw:255,stcp:6,stps:0,stfa:0,stfz:0,ofsf:0,ofsu:0,f2p0:False,fnsp:False,fnfb:False;n:type:ShaderForge.SFN_Final,id:3138,x:33682,y:32625,varname:node_3138,prsc:2|diff-8765-RGB,clip-8765-A,voffset-7127-OUT;n:type:ShaderForge.SFN_Tex2d,id:8765,x:33306,y:32368,ptovrint:False,ptlb:MainTex,ptin:_MainTex,varname:node_8765,prsc:2,glob:False,taghide:False,taghdr:False,tagprd:False,tagnsco:False,tagnrm:False,tex:7f0fb83ce3c774c44847a5f5ba3f57c6,ntxv:0,isnm:False;n:type:ShaderForge.SFN_Slider,id:745,x:33149,y:32545,ptovrint:False,ptlb:OutlineWidth,ptin:_OutlineWidth,varname:node_745,prsc:2,glob:False,taghide:False,taghdr:False,tagprd:False,tagnsco:False,tagnrm:False,min:0,cur:0,max:1;n:type:ShaderForge.SFN_Color,id:699,x:33306,y:32632,ptovrint:False,ptlb:OutlineColor,ptin:_OutlineColor,varname:node_699,prsc:2,glob:False,taghide:False,taghdr:False,tagprd:False,tagnsco:False,tagnrm:False,c1:1,c2:1,c3:1,c4:1;n:type:ShaderForge.SFN_Multiply,id:7127,x:33371,y:32980,varname:node_7127,prsc:2|A-4785-OUT,B-5955-R,C-8958-OUT,D-9384-OUT;n:type:ShaderForge.SFN_VertexColor,id:5955,x:32360,y:32989,varname:node_5955,prsc:2;n:type:ShaderForge.SFN_Vector3,id:4111,x:32360,y:32709,cmnt:Wind dir,varname:node_4111,prsc:2,v1:0.8,v2:0,v3:0.8;n:type:ShaderForge.SFN_Add,id:7313,x:32775,y:32825,varname:node_7313,prsc:2|A-4111-OUT,B-7183-OUT,C-3954-OUT;n:type:ShaderForge.SFN_Normalize,id:4785,x:32970,y:32825,varname:node_4785,prsc:2|IN-7313-OUT;n:type:ShaderForge.SFN_Multiply,id:7571,x:32637,y:33143,varname:node_7571,prsc:2|A-5955-R,B-1445-OUT;n:type:ShaderForge.SFN_Pi,id:1445,x:32394,y:33126,varname:node_1445,prsc:2;n:type:ShaderForge.SFN_Time,id:8819,x:32361,y:33253,varname:node_8819,prsc:2;n:type:ShaderForge.SFN_ValueProperty,id:7973,x:32361,y:33406,ptovrint:False,ptlb:TimeMul,ptin:_TimeMul,varname:node_7973,prsc:2,glob:False,taghide:False,taghdr:False,tagprd:False,tagnsco:False,tagnrm:False,v1:1;n:type:ShaderForge.SFN_Multiply,id:4210,x:32637,y:33265,varname:node_4210,prsc:2|A-8819-T,B-7973-OUT;n:type:ShaderForge.SFN_Add,id:769,x:32880,y:33201,varname:node_769,prsc:2|A-7571-OUT,B-4210-OUT;n:type:ShaderForge.SFN_Sin,id:8958,x:33068,y:33183,varname:node_8958,prsc:2|IN-769-OUT;n:type:ShaderForge.SFN_ValueProperty,id:9384,x:32918,y:33110,ptovrint:False,ptlb:Intensity,ptin:_Intensity,varname:node_9384,prsc:2,glob:False,taghide:False,taghdr:False,tagprd:False,tagnsco:False,tagnrm:False,v1:0.6;n:type:ShaderForge.SFN_FragmentPosition,id:6285,x:32360,y:32816,varname:node_6285,prsc:2;n:type:ShaderForge.SFN_Sin,id:3954,x:32555,y:32944,varname:node_3954,prsc:2|IN-6285-XYZ;n:type:ShaderForge.SFN_ArcTan,id:7183,x:32555,y:32794,varname:node_7183,prsc:2|IN-6285-XYZ;proporder:8765-745-699-7973-9384;pass:END;sub:END;*/
 
 Shader "Shader Forge/VertexMovementUnlit" {
     Properties {
         _MainTex ("MainTex", 2D) = "white" {}
         _OutlineWidth ("OutlineWidth", Range(0, 1)) = 0
         _OutlineColor ("OutlineColor", Color) = (1,1,1,1)
         _TimeMul ("TimeMul", Float ) = 1
         _Intensity ("Intensity", Float ) = 0.6
         [HideInInspector]_Cutoff ("Alpha cutoff", Range(0,1)) = 0.5
     }
     SubShader {
         Tags {
             "Queue"="AlphaTest"
             "RenderType"="TransparentCutout"
         }
         Pass {
             Name "FORWARD"
             Tags {
                 "LightMode"="ForwardBase"
             }
             
             
             CGPROGRAM
             #pragma vertex vert
             #pragma fragment frag
             #define UNITY_PASS_FORWARDBASE
             #include "UnityCG.cginc"
             #include "AutoLight.cginc"
             #pragma multi_compile_fwdbase_fullshadows
             #pragma exclude_renderers gles3 metal d3d11_9x xbox360 xboxone ps3 ps4 psp2 
             #pragma target 3.0
             uniform float4 _LightColor0;
             uniform float4 _TimeEditor;
             uniform sampler2D _MainTex; uniform float4 _MainTex_ST;
             uniform float _TimeMul;
             uniform float _Intensity;
             struct VertexInput {
                 float4 vertex : POSITION;
                 float3 normal : NORMAL;
                 float2 texcoord0 : TEXCOORD0;
                 float4 vertexColor : COLOR;
             };
             struct VertexOutput {
                 float4 pos : SV_POSITION;
                 float2 uv0 : TEXCOORD0;
                 float4 posWorld : TEXCOORD1;
                 float3 normalDir : TEXCOORD2;
                 float4 vertexColor : COLOR;
                 LIGHTING_COORDS(3,4)
             };
             VertexOutput vert (VertexInput v) {
                 VertexOutput o = (VertexOutput)0;
                 o.uv0 = v.texcoord0;
                 o.vertexColor = v.vertexColor;
                 o.normalDir = UnityObjectToWorldNormal(v.normal);
                 float4 node_8819 = _Time + _TimeEditor;
                 v.vertex.xyz += (normalize((float3(0.8,0,0.8)+atan(mul(unity_ObjectToWorld, v.vertex).rgb)+sin(mul(unity_ObjectToWorld, v.vertex).rgb)))*o.vertexColor.r*sin(((o.vertexColor.r*3.141592654)+(node_8819.g*_TimeMul)))*_Intensity);
                 o.posWorld = mul(unity_ObjectToWorld, v.vertex);
                 float3 lightColor = _LightColor0.rgb;
                 o.pos = mul(UNITY_MATRIX_MVP, v.vertex );
                 TRANSFER_VERTEX_TO_FRAGMENT(o)
                 return o;
             }
             float4 frag(VertexOutput i) : COLOR {
                 i.normalDir = normalize(i.normalDir);
                 float3 normalDirection = i.normalDir;
                 float4 _MainTex_var = tex2D(_MainTex,TRANSFORM_TEX(i.uv0, _MainTex));
                 clip(_MainTex_var.a - 0.5);
                 float3 lightDirection = normalize(_WorldSpaceLightPos0.xyz);
                 float3 lightColor = _LightColor0.rgb;
 ////// Lighting:
                 float attenuation = LIGHT_ATTENUATION(i);
                 float3 attenColor = attenuation * _LightColor0.xyz;
 /////// Diffuse:
                 float NdotL = max(0.0,dot( normalDirection, lightDirection ));
                 float3 directDiffuse = max( 0.0, NdotL) * attenColor;
                 float3 indirectDiffuse = float3(0,0,0);
                 indirectDiffuse += UNITY_LIGHTMODEL_AMBIENT.rgb; // Ambient Light
                 float3 diffuseColor = _MainTex_var.rgb;
                 float3 diffuse = (directDiffuse + indirectDiffuse) * diffuseColor;
 /// Final Color:
                 float3 finalColor = diffuse;
                 return fixed4(finalColor,1);
             }
             ENDCG
         }
         Pass {
             Name "FORWARD_DELTA"
             Tags {
                 "LightMode"="ForwardAdd"
             }
             Blend One One
             
             
             CGPROGRAM
             #pragma vertex vert
             #pragma fragment frag
             #define UNITY_PASS_FORWARDADD
             #include "UnityCG.cginc"
             #include "AutoLight.cginc"
             #pragma multi_compile_fwdadd_fullshadows
             #pragma exclude_renderers gles3 metal d3d11_9x xbox360 xboxone ps3 ps4 psp2 
             #pragma target 3.0
             uniform float4 _LightColor0;
             uniform float4 _TimeEditor;
             uniform sampler2D _MainTex; uniform float4 _MainTex_ST;
             uniform float _TimeMul;
             uniform float _Intensity;
             struct VertexInput {
                 float4 vertex : POSITION;
                 float3 normal : NORMAL;
                 float2 texcoord0 : TEXCOORD0;
                 float4 vertexColor : COLOR;
             };
             struct VertexOutput {
                 float4 pos : SV_POSITION;
                 float2 uv0 : TEXCOORD0;
                 float4 posWorld : TEXCOORD1;
                 float3 normalDir : TEXCOORD2;
                 float4 vertexColor : COLOR;
                 LIGHTING_COORDS(3,4)
             };
             VertexOutput vert (VertexInput v) {
                 VertexOutput o = (VertexOutput)0;
                 o.uv0 = v.texcoord0;
                 o.vertexColor = v.vertexColor;
                 o.normalDir = UnityObjectToWorldNormal(v.normal);
                 float4 node_8819 = _Time + _TimeEditor;
                 v.vertex.xyz += (normalize((float3(0.8,0,0.8)+atan(mul(unity_ObjectToWorld, v.vertex).rgb)+sin(mul(unity_ObjectToWorld, v.vertex).rgb)))*o.vertexColor.r*sin(((o.vertexColor.r*3.141592654)+(node_8819.g*_TimeMul)))*_Intensity);
                 o.posWorld = mul(unity_ObjectToWorld, v.vertex);
                 float3 lightColor = _LightColor0.rgb;
                 o.pos = mul(UNITY_MATRIX_MVP, v.vertex );
                 TRANSFER_VERTEX_TO_FRAGMENT(o)
                 return o;
             }
             float4 frag(VertexOutput i) : COLOR {
                 i.normalDir = normalize(i.normalDir);
                 float3 normalDirection = i.normalDir;
                 float4 _MainTex_var = tex2D(_MainTex,TRANSFORM_TEX(i.uv0, _MainTex));
                 clip(_MainTex_var.a - 0.5);
                 float3 lightDirection = normalize(lerp(_WorldSpaceLightPos0.xyz, _WorldSpaceLightPos0.xyz - i.posWorld.xyz,_WorldSpaceLightPos0.w));
                 float3 lightColor = _LightColor0.rgb;
 ////// Lighting:
                 float attenuation = LIGHT_ATTENUATION(i);
                 float3 attenColor = attenuation * _LightColor0.xyz;
 /////// Diffuse:
                 float NdotL = max(0.0,dot( normalDirection, lightDirection ));
                 float3 directDiffuse = max( 0.0, NdotL) * attenColor;
                 float3 diffuseColor = _MainTex_var.rgb;
                 float3 diffuse = directDiffuse * diffuseColor;
 /// Final Color:
                 float3 finalColor = diffuse;
                 return fixed4(finalColor * 1,0);
             }
             ENDCG
         }
         Pass {
             Name "ShadowCaster"
             Tags {
                 "LightMode"="ShadowCaster"
             }
             Offset 1, 1
             
             CGPROGRAM
             #pragma vertex vert
             #pragma fragment frag
             #define UNITY_PASS_SHADOWCASTER
             #include "UnityCG.cginc"
             #include "Lighting.cginc"
             #pragma fragmentoption ARB_precision_hint_fastest
             #pragma multi_compile_shadowcaster
             #pragma exclude_renderers gles3 metal d3d11_9x xbox360 xboxone ps3 ps4 psp2 
             #pragma target 3.0
             uniform float4 _TimeEditor;
             uniform sampler2D _MainTex; uniform float4 _MainTex_ST;
             uniform float _TimeMul;
             uniform float _Intensity;
             struct VertexInput {
                 float4 vertex : POSITION;
                 float2 texcoord0 : TEXCOORD0;
                 float4 vertexColor : COLOR;
             };
             struct VertexOutput {
                 V2F_SHADOW_CASTER;
                 float2 uv0 : TEXCOORD1;
                 float4 posWorld : TEXCOORD2;
                 float4 vertexColor : COLOR;
             };
             VertexOutput vert (VertexInput v) {
                 VertexOutput o = (VertexOutput)0;
                 o.uv0 = v.texcoord0;
                 o.vertexColor = v.vertexColor;
                 float4 node_8819 = _Time + _TimeEditor;
                 v.vertex.xyz += (normalize((float3(0.8,0,0.8)+atan(mul(unity_ObjectToWorld, v.vertex).rgb)+sin(mul(unity_ObjectToWorld, v.vertex).rgb)))*o.vertexColor.r*sin(((o.vertexColor.r*3.141592654)+(node_8819.g*_TimeMul)))*_Intensity);
                 o.posWorld = mul(unity_ObjectToWorld, v.vertex);
                 o.pos = mul(UNITY_MATRIX_MVP, v.vertex );
                 TRANSFER_SHADOW_CASTER(o)
                 return o;
             }
             float4 frag(VertexOutput i) : COLOR {
                 float4 _MainTex_var = tex2D(_MainTex,TRANSFORM_TEX(i.uv0, _MainTex));
                 clip(_MainTex_var.a - 0.5);
                 SHADOW_CASTER_FRAGMENT(i)
             }
             ENDCG
         }
     }
     FallBack "Diffuse"
     CustomEditor "ShaderForgeMaterialInspector"
 }
 

Here's my node setup if anyone wants:

alt text

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 Feartheway · May 13, 2020 at 09:36 AM

I am interested but cant get the picture to load

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

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

how to create a beating heart / organs 1 Answer

maze game trim stroked path 0 Answers

Why Won't my 2D Animation's Normals Render Unless I Have an Animator 1 Answer

how can i reproduce the 'Unity_Matrix_VP' value? 0 Answers

Writing with prefabs instead of letters 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