Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Venat · Mar 16, 2017 at 08:28 PM · graphicscgdrawtexture

Modify some texture pixels in fragment function

Greetings,

Now I have a camera moving over a mesh and what I'm trying to do is painting the area covered by this camera (between the 4 frustrum corners).

I've been able to create a runtime Texture2D in a projector, get that texture, get the UV coordinates corresponding to the four frustrum corners hit points and modifying all pixels of my texture with SetPixels32(). This works "fine", I move the camera and it leaves a rectangular spot on the mesh according to the area covered which I can show/hide just by activating the projector. The problem is that this code makes graphics performance to fall every time it executes texture.Apply().

What I'm looking for is the SetPixels32() sentence needed to modify these same texture pixels inside a fragment shader. The CG code needed to take this sampler2D _ProjectedTexture and modify its pixels in the fragment function of a shader.

Thanks in advance.

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
0

Answer by Cherno · Mar 16, 2017 at 10:35 PM

I've taken the basic Unlit shader from this page: Vertex and fragment shader examples

and added some functionality to it. Namely, you specify min and max uv values and a color and if a pixel is inside the uv values, it is rendered in the specified color. You could possibly optimize this by combing the uv properties into float2 or float4, but I'm not sure how that would work with the PropertyBlock code below.

Disclaimer: I didn't test this.

 Shader "Unlit/NewUnlitShader"
 {
     Properties
     {
         _MainTex ("Texture", 2D) = "white" {}
         _UVMin_x("UV Min_x", float) = 0
         _UVMin_y("UV Min_y", float) = 0
         _UVMax_x("UV Max_x", float) = 0
         _UVMax_y("UV Max_y", float) = 0
         _ProjectorColor("Projector Color", Color) = (1,1,1,1)
     }
     SubShader
     {
         Tags { "RenderType"="Opaque" }
         LOD 100
 
         Pass
         {
             CGPROGRAM
             #pragma vertex vert
             #pragma fragment frag
             // make fog work
             #pragma multi_compile_fog
             
             #include "UnityCG.cginc"
 
             struct appdata
             {
                 float4 vertex : POSITION;
                 float2 uv : TEXCOORD0;
             };
 
             struct v2f
             {
                 float2 uv : TEXCOORD0;
                 UNITY_FOG_COORDS(1)
                 float4 vertex : SV_POSITION;
             };
 
             sampler2D _MainTex;
             float4 _MainTex_ST;
             float _UVMin_x;
             float _UVMin_y;
             float _UVMax_x;
             float _UVMax_y;
             fixed4 _ProjectorColor;
             
             v2f vert (appdata v)
             {
                 v2f o;
                 o.vertex = UnityObjectToClipPos(v.vertex);
                 o.uv = TRANSFORM_TEX(v.uv, _MainTex);
                 UNITY_TRANSFER_FOG(o,o.vertex);
                 return o;
             }
             
             fixed4 frag (v2f i) : SV_Target
             {
                 // sample the texture
                 fixed4 col = tex2D(_MainTex, i.uv);
                 if(i.uv.x >= _UVMin_x && i.uv.y >= _UVMin_y && i.uv.y <= _UVMax_x && i.uv.y <= _UVMax_y) 
                 {
                        col = _ProjectorColor;
                 }
                 // apply fog
                 UNITY_APPLY_FOG(i.fogCoord, col);
                 return col;
             }
             ENDCG
         }
     }
 }


To access these added variables, you need to include a function like this in a script:

 Renderer myRenderer
 
 void UpdateOutline(Color color, Vector2 uv_min, Vector2 uv_max, ) {
         MaterialPropertyBlock mpb = new MaterialPropertyBlock();
         if (myRenderer!= null) {
             myRenderer.GetPropertyBlock(mpb);
             mpb.SetFloat("_UVMin_x", uv_min.x);
             mpb.SetFloat("_UVMin_y", uv_min.y);
             mpb.SetFloat("_UVMax_x", uv_max.x);
             mpb.SetFloat("_UVMax_y", uv_max.y);
             mpb.SetColor("_ProjectorColor", color);
             myRenderer.SetPropertyBlock(mpb);
         }
     }

Comment
Add comment · Show 1 · 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 Venat · Mar 17, 2017 at 08:49 AM 1
Share

Thanks for your answer Cherno,

Your code surely works but made me realize that I've probably made the wrong question. I know how to modify texture pixels every frame, but what I'm trying to do is leaving a scar in that texture. I mean modifying it persistently.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Overlay Effect Shader - Very slow in mobile device 1 Answer

Do TEXCOORDS need to be in sequence? 1 Answer

RenderTexture and DrawTexture using material is not working 0 Answers

Graphics.DrawTexture vs GUI.DrawTexture vs Graphics.Blit 0 Answers

How to implement scaling without scaling the texture. 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