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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by rich2701 · May 02, 2015 at 04:27 PM · shaderrendertexturevertexvertex shaderfragment

Vertex shader breaks render to texture - camera preview and render texture not the same

Hi all,

This is my first question here so apologies if the formatting is a bit off. I'm using Unity 5.0.1 Personal.

Issue:

I've written a shader (see below) that should take all fragments within view of the camera and using the associated UV coordinates set the relevant pixels in a render texture to red. When my camera is selected in scene view, the camera preview shows exactly what I would expect - a red patch corresponding to the UV coordinates of fragments within view of the camera. However, the render texture attached to this camera does not contain this red patch, i.e. the camera preview and render texture do not match.

Here is a screenshot from my scene, the camera is rendering into a texture which is applied to the plane, the camera preview is correct however the render texture is not.

Screenshot

I have attached a minimal example project and my shader is below. Does anyone have any ideas why this might be happening?

Shader:

 Shader "Custom/PaintShader" {
     SubShader {
         Pass {
             Lighting Off
             Blend SrcAlpha OneMinusSrcAlpha
                         
             CGPROGRAM
             
             #pragma vertex vert
             #pragma fragment frag
             
             #include "UnityCG.cginc"
 
             struct vertOut {
                 float4 pos:SV_POSITION;
                 float2 uv : TEXCOORD0;
                 float3 norm : TEXCOORD1;
                 float4 scrPos : TEXCOORD2; 
             };
 
             vertOut vert(appdata_base v) {
                 vertOut o;
                 o.pos = mul (UNITY_MATRIX_MVP, v.vertex);
                 o.norm = mul (UNITY_MATRIX_MVP, v.normal);
                 o.scrPos = ComputeScreenPos(o.pos);
                 o.uv = v.texcoord.xy;
                 // Map UV coordinate to clip space and move vertex to this position
                 o.pos.xy = v.texcoord.xy*2.0-1.0;
                 return o;
             }
             
             fixed4 frag(vertOut i) : SV_Target {
                 float2 wcoord = (i.scrPos.xy/i.scrPos.w);
                 // Need to manually do frustum and back face culling
                 if( any(wcoord.xy<0) || any(wcoord.xy>1) || i.norm.z>=-0.01 ){
                     discard; // TODO: Use lerp instead of discard due to expense on mobile
                 }
                 return fixed4(1.0,0.0,0.0,1.0);
             }
             
             ENDCG
         }
     }
 }

Extra info:

I'm trying to create a toolset that will allow the user to interactively paint a mesh. The shader above is the starting point of being able to apply textures and colours to a model in real time. I'm using custom shaders and render textures instead of SetPixels() for speed. Hopefully someone can point me in the right direction.

All the best, Rich

render-texture-fail.png (48.9 kB)
shadererrorminproject.zip (381.9 kB)
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
Best Answer

Answer by rich2701 · May 02, 2015 at 05:42 PM

It turns out that I just needed to disable face culling, thinking about it this makes a lot of sense seeing as I'm messing with vertex coordinates! I'm still a bit puzzled as to why the camera preview should ever be different from the render texture though, are there any render texture gurus with thoughts on this?

Here's the working shader, I've also added the ability to specify a brush texture and colour through the shader properties:

 Shader "Custom/PaintShader" {
 
     Properties {
         _BrushTexture ("Brush Texture", 2D) = "white" {} 
         _BrushColor ("Brush Color", Color) = (1,1,1,1) 
     }
 
     SubShader {
         Pass {
             Lighting Off
             Cull Off
             Blend SrcAlpha OneMinusSrcAlpha
                         
             CGPROGRAM
             
             #pragma vertex vert
             #pragma fragment frag
             
             #include "UnityCG.cginc"
             
             sampler2D _BrushTexture;
             fixed4 _BrushColor;
             
             struct vertOut {
                 float4 pos : POSITION;
                 float3 norm : TEXCOORD1;
                 float4 scrPos : TEXCOORD2; 
             };
 
             vertOut vert(appdata_base v) {
                 vertOut o;
                 o.pos = mul (UNITY_MATRIX_MVP, v.vertex);
                 o.norm = mul (UNITY_MATRIX_MVP, v.normal);
                 o.scrPos = ComputeScreenPos(o.pos);            
                 o.pos.xy = (v.texcoord.xy*2-1)*o.pos.w;
                 return o;
             }
             
             fixed4 frag(vertOut i) : SV_Target {
                 float2 wcoord = (i.scrPos.xy/i.scrPos.w);
                 fixed4 brushTexture = tex2D (_BrushTexture, wcoord.xy);
                 // Need to manually do frustum and back face culling
                 if( any(wcoord.xy<0) || any(wcoord.xy>1) || i.norm.z>=-0.01){
                     discard; // TODO: Use lerp instead of discard due to expense on mobile
                 }
                 return fixed4(_BrushColor.rgb,brushTexture.a);
             }
             
             ENDCG
         }
     }
 }

And here's a screenshot of me using the shader to interactively paint onto a plane:

alt text


texture-paint.png (204.6 kB)
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.

Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

Related Questions

[Shader, sprite] frag shade entire sprite image? 1 Answer

Combine Vertex / Fragment and Surface Shader 0 Answers

Add new vertex attributes for a shader,Create vertex attributes 0 Answers

Vertex/Fragment shaders - transparency ignoring Render Queue 3 Answers

Refraction Shader. Strange Artifacts 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