• 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 Matt-Murch · Apr 06, 2018 at 03:56 PM · animationvertex shaderenvironment

Shader Forge vegetation collision

Hello, I'm working on a foliage shader and I would like it to react to the player as they walk by. HZD and Uncharted both have made really great use fo this and I've seen it in Unity before, but I'm not sure how to do it without collision (collision is to expensive for every pice of vegetation).

Here is what I have so far: https://www.youtube.com/watch?v=BaOKpUQ_TOI

Thanks in advance!

Comment
Add comment · Show 3
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 Matt-Murch · Apr 06, 2018 at 08:21 PM 0
Share

Here it is in unreal, just converting it over now. https://forums.unrealengine.com/development-discussion/content-creation/1914-foliage-collision-foliage-bend-on-collision-in-engine-feature/page2?1795-Foliage-collision-foliage-bend-on-collision-In-engine-feature=&viewfull=1

avatar image Matt-Murch Matt-Murch · Apr 06, 2018 at 08:23 PM 0
Share

Also requires player position, as listed here: https://shaderforge.userecho.com/communities/1/topics/228-would-it-be-possible-to-have-a-player-andor-ai-position-via-an-object-input-node

avatar image Matt-Murch · Apr 07, 2018 at 09:46 PM 0
Share

$$anonymous$$aybe some wind zone inclusion: https://shaderforge.userecho.com/communities/1/topics/1567-wind-zone-value

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Matt-Murch · Apr 08, 2018 at 05:02 PM

Here's how I do it. I'm using ShaderForge, Unity, and Blender.

There are two components to making it work, a script on each foliage asset that gets the player position and sends it to the shader, and a vector in the shader that receives the player. From there it's pretty easy (thats relative).

End result: https://www.youtube.com/watch?v=SBadNrtv6c0

Here is the code I use:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class PlayerToShaderScript : MonoBehaviour
 {
 
     public GameObject player;
 
     // Use this for initialization
     void Start () {
         if (player == null)
             player = GameObject.FindWithTag("Player");
     }
     
     // Update is called once per frame
     void Update () {
         gameObject.GetComponent<Renderer>().materials[0].SetVector("_PlayerPos", player.transform.position);
     }
 }

This gets the player object (tagged) and passes their position to the material every frame..

And here are the shader nodes: Shader Graph from Shader Forge showing the effect nodes The effect has to major node groups, Player reaction (top), and wind animation (bottom). The Top Group takes the player vector (from the script), compares it to world space to find it's distance and applies a vertex offset based on that. The Bottom Group takes time and uses COS to add some wind animation. I have two wind effect, Vertical offset and horizontal offset. I use vertex colours in my model to control all this, R is horizontal (top of tree) and G is Vertical (ends of branches) this gives nice movement fairly cheap. You can also use the alpha value to blend between bark and leaves in your textures (Needs the nightly build of blender for Vertex alpha support).

I hope this helps someone, good luck and happy developing!

Comment
Add comment · 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 AttilaZold · May 23, 2018 at 07:33 PM 0
Share

@$$anonymous$$att-$$anonymous$$urch, I did everything like you said in this comment, and yet the mesh doesn't move when my player brushed by it. Thing is, I only painted the mesh black and red (red supposed to be horizontal, black is no movement). Yes, the player is tagged, the script is attached to the mesh, only difference is my mesh doesn't have branches, it's a simple crossplane with its top vertices painted red in blender. So what am I missing? Please help.

avatar image Matt-Murch AttilaZold · May 24, 2018 at 03:13 AM 0
Share

Hi, you should be getting some 'wind' animation on the top. Only vertices that have some green value in them will react to the player.

If you add the green (you can add green to the red vertex with the add brush) and you still don't have movement check that your object is in positive world space (X Y and Z are all greater than 1), I found a weed glitch in the math for negative space.

Other than that week the values a lot, depending on your scene scale it can be a bit of a hassle but it's with it when you find the right values.

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

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

Can the animation editor create local rotational data? 3 Answers

Create animated scene in Maya and play it out in Unity. 1 Answer

Adding animation clips via script 2 Answers

How to Animate Environment Objects 1 Answer

Can I make animations snap to a frame? 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