• 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
1
Question by Daniel-Strayer · Oct 30, 2013 at 09:35 AM · shader

How to write to RWTexture3D in Pixel Shader

It seems like writing to a RWTexture2D in a pixel shader is possible; I have succeeded in doing this. But when it comes to writing to a RWTexture3D, it doesn't seem to work at all.

 Shader "Custom/RenderToVolume"
 {
     Properties 
     {
         _MainTex ("Diffuse (RGBA)", 2D) = "white" {}
     }
 
     SubShader 
     {
         Pass 
         {
             Tags { "RenderType"="Opaque" }
             Cull Off ZWrite Off ZTest Always Fog { Mode Off }
 
             CGPROGRAM
             #pragma target 5.0
             #pragma vertex vert
             #pragma fragment frag
             #pragma exclude_renderers flash gles opengl
 
             #include "UnityCG.cginc"
 
 
             sampler2D _MainTex;
 
             RWTexture3D<float4> volumeTex;
             float volumeResolution;
             float4 volumeParams;
 
             struct ApplicationToVertex 
             {
                 float4 vertex : POSITION;
                 float4 texcoord : TEXCOORD0;
             };
 
             struct VertexToFragment 
             {
                 float4 pos : SV_POSITION;
                 float4 wPos : TEXCOORD0;
                 float2 uv : TEXCOORD1;
             };
 
             void vert(ApplicationToVertex input, out VertexToFragment output)
             {
                 output.pos = mul (UNITY_MATRIX_MVP, input.vertex);
                 output.wPos = mul(_Object2World, input.vertex);
                 output.uv = input.texcoord.xy;
             }
 
             void frag(VertexToFragment input)
             {
                 float4 color = tex2D(_MainTex, input.uv);
                 
                 float3 volumePos = ((input.wPos.xyz - volumeParams.xyz) / volumeParams.w) * volumeResolution;
 
                 int3 volumeCoords;
                 volumeCoords.x = int(volumePos.x); 
                 volumeCoords.y = int(volumePos.y); 
                 volumeCoords.z = int(volumePos.z); 
                 
                 volumeTex[volumeCoords] = color;
             }
             ENDCG
         }
     }
     Fallback Off
 }

This is what I am trying to do, and it doesn't seem to work, but if I try and write to a 2D texture, it does. Not sure where I am messing it up.

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
1
Best Answer

Answer by Daniel-Strayer · Nov 04, 2013 at 08:21 PM

EDIT:

I found that I needed to clear the random write targets AFTER doing my rendering:

 Graphics.ClearRandomWriteTargets();
 Graphics.SetRandomWriteTarget(1, volumeTexture);
             
 orthoCamera.Render();
 
 Graphics.ClearRandomWriteTargets();

This has worked from Unity 4.3

Comment
Add comment · Show 3 · 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 raja-bala · Sep 09, 2014 at 01:35 AM 0
Share

has support for writing to it in the pixel shader been added?

avatar image fasdfas · Oct 21, 2014 at 07:24 AM 0
Share

Writing to a RWTexture3D from a fragment shader works for me in unity 4.6, but only using Graphics.Blit. It fails when rendering any other way (for example: Graphics.Draw$$anonymous$$eshNow).

avatar image Boltman · Aug 05, 2021 at 03:36 AM 0
Share

Commenting many years later because this is SO IMPORTANT.

I knew this was possible as I did it ages ago, but I followed multiple guides, NONE of which mention to clear targets AFTER rendering. As soon as I saw this I knew it was the issue because it made so much sense as other things will mess up your texture if you don't unbind it from the register, but still it's one of those things you can't really just know to do without seeing it. I had looked at forums galore with everyone on their own wild goose chase for what the problem is, nobody mentioning anything about it there either. Until finally stumbling on this obscure post with no upvotes or anything with my guy Daniel just putting it right there. Like why is this stuff so poorly thought out and ill documented. Graphics development is always shoved aside for those who like to be tortured.

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

17 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

Related Questions

How to force the compilation of a shader in Unity? 5 Answers

Why does specular highlight ignore vertex position? 1 Answer

Shader vprog texture error on sm2 0 Answers

Problem with shadow casting on transparent material 0 Answers

Can i trust the Unity shader compiler more than i can trust myself? 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