• 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 gangafinti · Jul 06, 2015 at 06:25 PM · cg

How can I collide with my water

Hi, I am working on my water shader but I have run into some problems. The first problem is that I dont know how to collide with my water because my shaders doesnt work on a collider. The second problem is that the normals dont get the same offset as the water mesh, even tho I do wrote the code to do so. In the picture you can see that I put a plane under the water mesh(the lowest object) to show you that there is somthing under the water that goes over the shadow of the plane. Thanks in advance!

alt text

This is my code:

 Shader "Custom/NoobShader_04" {
     Properties {
         _Color ("Color", Color) = (0,0.55,0.83,1)
         _Diffuse ("Diffuse Map", 2D) = "white" {}
         _Displacement ("Displacement Map", 2D) = "white" {}
     
         _Scale ("Wave Scale", float) = 0.7
         _Frequency ("Frequency", float) = 0.6
         _Speed ("Speed", float) = 0.5
 
     }
     SubShader {
         Pass{
         Tags { "LightMode" = "ForwardBase"}
         CGPROGRAM
         #pragma vertex vert
         #pragma fragment frag
         
         float4 _Color;
         sampler2D _Displacement;
         sampler2D _Diffuse;
         
         float _Scale;
         float _Frequency;
         float _Speed;
         
         float4 _LightColor0;
             
         struct VertexOutput
         {
             float4 pos : SV_POSITION;
             float3 nor : NORMAL;
             float4 col : COLOR;
             float4 tex : TEXCOORD0;
         };
         
         struct VertexInput
         {
             float4 vertex : POSITION;
             float3 normal : NORMAL;
             float4 texcoord : TEXCOORD0;
         };
         
         struct FragmentOutput
         {
             float4 color : COLOR;
         };
 
         VertexOutput vert (VertexInput i)
         {
             VertexOutput VOUT;
             
             float4 disp = tex2Dlod(_Displacement, float4(i.texcoord.x * _Frequency + (_Time.x * _Speed), i.texcoord.y * _Frequency + (_Time.x * _Speed),0.0,0.0));
             float4 newPos = i.vertex;
             float3 newNor = i.normal;
             newPos.y += _Scale * disp.y; 
             newNor.y += _Scale * disp.y;
             
             VOUT.nor = newNor;
             VOUT.pos = mul(UNITY_MATRIX_MVP,newPos);
             VOUT.tex = i.texcoord;
             
             float3 normalDirection = normalize( mul(float4(newNor,0.0),_World2Object).xyz);
             float3 lightDirection = normalize(_WorldSpaceLightPos0.xyz);
             float atten = 1.0;
             
             float3 diffuseRefflection = atten * _LightColor0.xyz * _Color.rgb *  max( 0.0, dot(normalDirection, lightDirection));
             
             VOUT.col = float4(diffuseRefflection, 1.0);
             
             return VOUT;
         }
         
         FragmentOutput frag(VertexOutput v) 
         {
             FragmentOutput FOUT;
             float4 tex = tex2D(_Diffuse,float4(v.tex.x * _Frequency + (_Time.x * _Speed), v.tex.y * _Frequency + (_Time.x * _Speed),0.0,0.0));
             FOUT.color = tex * v.col + UNITY_LIGHTMODEL_AMBIENT.xyzw;
             return FOUT;
         }
         ENDCG
         }
     } 
     FallBack "Diffuse"
 }

ref.png (167.9 kB)
Comment
Add comment · Show 2
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 Calum1015 · Jul 06, 2015 at 06:54 PM 0
Share

You could scale a cube down so its very thin, remove the mesh renderer, increase the size of it so that it is the same size as your water, then place it under the very lowest part of the water shader, then you add a collider to that scaled cube and displace the water mesh through this.

avatar image gangafinti · Jul 06, 2015 at 07:00 PM 0
Share

I am really going to need a lot of accuracy so I think that won't be the method I am going to use but thanks for helping out :) I am going to need somthing like the terrain collider! :)

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by fantasticdev · Jan 08, 2016 at 08:36 AM

I've been working on some water effects that include a reactive body of water having more detailed collision than a single box or plane. Have a look and see if it gives you any ideas for your situation: https://www.youtube.com/watch?v=74V3dBNLkDc&list=PL-fhOOPYcZLLfr9geNSiKxgD2pQchhp4C∈dex=4

There are more videos in that playlist and the water is currently more advanced than the latest video but if you have any questions about specific features I can describe how I went about achieving them.

It's among several other gizmos I'm thinking of putting up on the asset store when they're finished but I don't know if there would be interest in many of them as they seem like they might be too specific. I might just put them on for free as a form of advertising my dev services.

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.

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

CGPROGRAM supported #pragma keywords 1 Answer

Depth issues with a shader 1 Answer

Accessing shadow texture in shader 2 Answers

How to specify my CGIncludes directories? 3 Answers

Texture not mapped to UV CG shader 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