• 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 dsilvavini · Feb 23, 2013 at 04:07 PM · shaderrendertexture

How to use RenderTextures as input textures of a shader?

Search in the forums doesn't return really useful results in this subject. I'm trying to write shaders for cloth simulation in Unity just for portfolio purposes. I have 3 RenderTextures: one with velocities values, one with positions and one with spring connectivity data. I already have done shaders to initialize them all properly. The values were verified debugging the following code in a script attached to the camera:

 //Debug
     public void DebugRenderTexture(ref RenderTexture renderTex)
     {
         Texture2D tex2D = new Texture2D((int)dimensions.x , (int)dimensions.y ,
             TextureFormat.ARGB32 , false);
         tex2D.ReadPixels(new Rect(0 , 0 , dimensions.x , dimensions.y) , 0 , 0);
         Color[] colors = tex2D.GetPixels ();
         int dummy = 0;
     }
     //
     
     void OnRenderImage(RenderTexture src , RenderTexture dst)
     {
         //Debug
         DebugRenderTexture(ref src);
         //
     }

My problem is when I use the generated RenderTextures in the cloth update materials. All textures pixels have the same value, like the RenderTextures were cleaned. The following snippet is the update script of the quad rendered for the update.

 // Renders itself to update position and velocities textures and ping-pongs textures.
     void Update ()
     {
         Camera cam = camObj.camera;
         EffectCamera camScript = camObj.GetComponent("EffectCamera") as EffectCamera;
         
         //TODO: Create a unique shader with velocity and position update as passes.
         
         //Setup cloth velocity update material and render
         camScript.changeRenderTexture(ref velWriteTex);
         clothUpdateVelMaterial.SetTexture("_posRead" , posReadTex);
         clothUpdateVelMaterial.SetTexture("_velRead" , velReadTex);
         renderer.material = clothUpdateVelMaterial;
         cam.Render();
         
         //Setup cloth positions update material and render
         camScript.changeRenderTexture(ref posWriteTex);
         clothUpdatePosMaterial.SetTexture("_posRead" , posReadTex);
         clothUpdatePosMaterial.SetTexture("_velRead" , velReadTex);
         renderer.material = clothUpdatePosMaterial;
         cam.Render();
         
         PingPongTex();
     }
 //Ping-pong position and velocity textures
     public void PingPongTex ()
     {
         if(initialized)
         {
             RenderTexture temp = posReadTex;
             posReadTex = posWriteTex;
             posWriteTex = temp;
             temp = velReadTex;
             velReadTex = velWriteTex;
             velWriteTex = temp;
         }
     }

Now the shader for updating velocities. Currently it is just returning the values of the position texture, which has all pixels set to one color, like it was clean.

 Shader "clothUpdateVel"
 {
     Properties
     {
         _velRead ("Velocity Read Texture" , 2D) = "" {}
         _posRead ("Position Read Texture" , 2D) = "" {}
         _springs ("Spring Texture" , 2D) = "" {}
         _mass("Mass",Float) = 0.05
         _accel("Acceleration",Vector) = (0,-0.01,0)
         _delta("Time Delta",Float) = 0.01
     }
     SubShader
     {
         Tags { "RenderType"="Opaque" }
         
         Pass
         {
             //Compute forces and outputs velocities
             CGPROGRAM
             #pragma vertex vert
             #pragma fragment frag
             
             #include "UnityCG.cginc"
             
             sampler2D _velRead;
             sampler2D _posRead;
             sampler2D _springs;
             
             uniform float _mass;
             uniform float3 _accel;
             uniform float _delta;
             uniform float epsilon = 1e-10f;
             
             struct v2f
             {
                 float4 pos : POSITION;
                 float2 uv  : TEXCOORD0;
             };
             
             v2f vert(v2f IN)
             {
                 v2f toFrag;
                 toFrag.pos = mul(UNITY_MATRIX_MVP, IN.pos);
                 toFrag.uv = IN.uv;
                 
                 return toFrag;
             }
     
             float3 computeForce(float3 pij , float2 uvSpringEnd , float natLenght , float stiffness)
             {
                 float3 pkl = tex2D(_springs , uvSpringEnd);
                 float3 diff = pkl - pij;
                 return stiffness*(diff - natLenght*(normalize(diff)));
             }
             
             float3 computeInternalForces(float2 curr , float3 v , float3 p)
             {
                 float3 ff = float3(0.0,0.0,0.0); 
                 curr *= 0.25; 
                 for (float x=0.0f ; x < 1.0f ; x+=0.25f)  
                 {
                     for (float y=0.0f ; y < 1.0f ; y += 0.25f)
                     { 
                           float4 con = tex2D(_springs, curr);
                           if (abs(con.a) < epsilon) break; 
                           ff += computeForce(p , con.rg , con.b , con.a); 
                     }
                 }
                 
                 return ff;
             }
             
             float4 frag (v2f fromVert) : COLOR
             {
                 float3 v = tex2D(_velRead, fromVert.uv).rgb;  
                 float3 p = tex2D(_posRead, fromVert.uv).rgb;
                 
                 return float4(p , 1);
                 
 //                float3 force = computeInternalForces(fromVert.uv , v , p);
 //                force += _mass*_accel;
 //                float3 velOut = v + _delta*_accel;
 //                
 //                return float4(velOut , 1);
             }
             ENDCG
         }
     } 
     FallBack "Diffuse"
 }

Any help would be very appreciated!

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

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.

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

9 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

how to make shader dont blend color with camera background? 0 Answers

Mesh based volumetric lighting with RenderTexture 1 Answer

Best way to blend two render textures into a final render texture? 1 Answer

Render with replaced shaders and materials to a renderTexture 0 Answers

Manipulating pixels in a texture 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