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 Danimita92 · Apr 20, 2013 at 02:48 PM · shaderwatercubemapdistortiondistort

How can I distort a cubemap in a shader?

Ok, so I'm new to programming shaders, but am learning quickly (I think) and I've managed to make this shader that alternates between a Cubemap and a texture based on the color of each vertex, to create puddles on a floor: alt text

The thing is, I would like the puddles to be distorted with time, to give it more of a sense of water. Something like this: alt text

I assume I have to distort it with a noise texture map, but I can't figure out how to do it. Any help??

Here's the shader so far:

 Shader "Wet Vertex Blend"
 {
     Properties 
      {
          _MainTex ("Main Texture", 2D) = "white" {}
          _BumpMap ("Normal Texture", 2D) = "bump" {}
          _Cube ("Cube Map", Cube) = "" {}
          _NoiseTex ("Noise (Cloud) Texture", 2D) = "white" {}
      }
 SubShader 
 {
     Pass 
     {
             CGPROGRAM
             #pragma vertex vert
             #pragma fragment frag
             
             //user defined variables
             samplerCUBE _Cube;
             
             //Base Input Structs
             struct vertexInput {
                 float4 vertex : POSITION;
                 float3 normal : NORMAL;
             };
             struct vertexOutput {
                 float4 pos : SV_POSITION;
                 float3 normalDir : TEXCOORD0;
                 float3 viewDir : TEXCOORD1;
             };
             //vertex function
             vertexOutput vert(vertexInput v){
                 vertexOutput o;
                 
                 o.normalDir = normalize( mul(float4(v.normal, 0.0), _World2Object).xyz );
                 o.viewDir = float3(mul(_Object2World, v.vertex) - _WorldSpaceCameraPos).xyz;
                 
                 o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
                 return o;
             }
             //fragment function
             float4 frag(vertexOutput i) : COLOR{
                 
                 //reflect the ray based on the normals to get the cube coordinates
                 float3 reflectDir = reflect(i.viewDir, i.normalDir);
                 
                 //texture maps
                 float4 texC = texCUBE(_Cube, reflectDir);
                 
                 return texC;
             }
             ENDCG
         }
     Pass 
     {
     Blend SrcAlpha OneMinusSrcAlpha
         Fog { Mode Off }
         CGPROGRAM
         #pragma vertex vert
         #pragma fragment frag
         
         sampler2D _MainTex;
         float4 _MainTex_ST;
         
         sampler2D _NoiseTex;
         float4 _NoiseTex_ST;
         
         sampler2D _BumpMap;
         
         // vertex input: position, color
         struct appdata 
         {
             float4 vertex : POSITION;
             fixed4 color : COLOR;
             float4 texcoord : TEXCOORD0;
             float3 normal : NORMAL;            
         };
         
         //vertex output
         struct v2f 
         {
             float4 pos : SV_POSITION;
             fixed4 color : COLOR;
             float4 tex : TEXCOORD0;
         };
         
         v2f vert (appdata v) 
         {
             v2f o;
             o.tex = v.texcoord;
             o.pos = mul( UNITY_MATRIX_MVP, v.vertex );
             o.color = v.color;
             return o;
         }
         fixed4 frag (v2f i) : COLOR0 
         { 
             float4 tex = float4(0.0,0.0,0.0,0.0);
             float4 noiseTex = tex2D(_NoiseTex, i.tex.xy * _NoiseTex_ST.xy + _NoiseTex_ST.zw);
             //Texture Maps
             if(i.color.r >= 0.5)
             {
                 tex = tex2D(_MainTex, i.tex.xy * _MainTex_ST.xy + _MainTex_ST.zw)*i.color.r;
             }
             if(i.color.r < 0.5)
             {
                 tex = tex2D(_MainTex, i.tex.xy * _MainTex_ST.xy + _MainTex_ST.zw)*i.color.r*noiseTex.r;
             }
             return tex;
             //return i.color; 
         }
         ENDCG
         }
 }
 }


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 whydoidoit · Apr 20, 2013 at 04:22 PM 1
Share

I'd be using the noise map to modify the reflection vector and then have an offset into the noise map (so you update the uv lookup into the noise by the offset) that you use to simulate it over time.

avatar image Danimita92 · Apr 20, 2013 at 04:24 PM 0
Share

And how could I modify the vector with a noise texture exactly?

avatar image whydoidoit · Apr 20, 2013 at 04:32 PM 1
Share

Well you make a vector out of whatever's in the noise texture and modify the existing reflection vector by doing something like sum$$anonymous$$g them and normalizing the result.

0 Replies

· Add your reply
  • Sort: 

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

12 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

Related Questions

How I do a Pixel Distort Shader 0 Answers

Rendering a G-Buffer for a cubemap 0 Answers

Shader add vertex displacement 0 Answers

Cubemap on Android 1 Answer

Why is my Pro Water Material Grey? 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