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
2
Question by frarees · Aug 20, 2013 at 08:54 PM · shadertexturecgprojectortiling

Projector + custom fragment shader = endless texture draw

Hi there,

What I want to do is, using a Projector, draw a texture several times within a unit, creating a grid / matrix view (i.e. draw a grid of tiles below my player). My shader defines three properties: color, texture and grid size.

Here're the results I'm getting (using grid size = 1 and grid size = 2 respectively):

Using the shader with grid size 1 Using the shader with grid size 2

They repeat properly within a unit (you see the capsule covering a 2x2 grid), but it keeps painting them forever in the direction you see in the screenshots.

Here's the shader I've implemented.

 Shader "Projector/Custom Grid" {
 
     Properties {
         _Color ("Color", Color) = (1,1,1,0)
         _ShadowTex ("Cookie", 2D) = "black" { TexGen ObjectLinear }
         _Size ("Grid Size", Float) = 1
     }
     
     Subshader {
         Tags { "RenderType"="Transparent" "Queue"="Transparent+100" }
         Pass {
             ZWrite Off
             Offset -1, -1
             Fog { Mode Off }
             ColorMask RGBA
             Blend SrcAlpha OneMinusSrcAlpha
             
             CGPROGRAM
             #pragma vertex vert
             #pragma fragment frag
             #pragma fragmentoption ARB_fog_exp2
             #pragma fragmentoption ARB_precision_hint_fastest
             #include "UnityCG.cginc"
              
             struct v2f {
                 float4 pos : SV_POSITION;
                 float2 uv : TEXCOORD0;
             };
              
             sampler2D _ShadowTex;
             float4 _ShadowTex_ST;
             float4 _Color;
             float4x4 _Projector;
             fixed _Size;
              
             v2f vert (appdata_tan v) {
                 v2f o;
                 o.pos = mul (UNITY_MATRIX_MVP, v.vertex);
                 o.uv = TRANSFORM_TEX (mul (_Projector, v.vertex).xy, _ShadowTex);
                 return o;
             }
              
             half4 frag (v2f i) : COLOR {
                 return tex2D (_ShadowTex, fmod (i.uv, 1 / _Size) * _Size) * _Color;
             }
             
             ENDCG
         }
     }
 }

The issue seems to be generated at this line:

 return tex2D (_ShadowTex, fmod (i.uv, 1 / _Size) * _Size) * _Color;

And specifically when applying fmod operation. If I don't use fmod I don't see the texture repeating over and over.

The texture is set to clamp, I've been reading a while about projectors and couldn't find a solution to this. Any ideas? Thanks.

gridsize2.png (34.7 kB)
gridsize1.png (30.9 kB)
Comment
Add comment · Show 1
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 frarees · Aug 21, 2013 at 08:13 PM 0
Share

Here's a top down view of what's happening behind the scenes. Using this frag:

 return half4 (fmod (i.uv, 1 / _Size) * _Size, 0, 1);

Debug

debug.png (123.8 kB)

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by frarees · Aug 24, 2013 at 11:49 AM

Found a way to fix this. Basically I had not to write if I get outside UV (1,1)

 Shader "Projector/Grid Test" {
 
     Properties {
         _Color ("Color", Color) = (1,1,1,0)
         _ShadowTex ("Cookie", 2D) = "black" { TexGen ObjectLinear }
         _Size ("Grid Size", Float) = 1
     }
     
     Subshader {
         Tags { "RenderType"="Transparent" "Queue"="Transparent+100" }
         Pass {
             ZWrite Off
             Offset -1, -1
             Fog { Mode Off }
             ColorMask RGB
             Blend SrcAlpha OneMinusSrcAlpha
             
             CGPROGRAM
             #pragma vertex vert
             #pragma fragment frag
             #pragma fragmentoption ARB_fog_exp2
             #pragma fragmentoption ARB_precision_hint_fastest
             #include "UnityCG.cginc"
              
             struct v2f {
                 float4 pos : SV_POSITION;
                 float2 uv : TEXCOORD0;
             };
              
             uniform sampler2D _ShadowTex;
             float4 _ShadowTex_ST;
             float4 _Color;
             float4x4 _Projector;
             fixed _Size;
              
             v2f vert (appdata_tan v) {
                 v2f o;
                 o.pos = mul (UNITY_MATRIX_MVP, v.vertex);
                 o.uv = TRANSFORM_TEX (mul (_Projector, v.vertex).xy, _ShadowTex);
                 return o;
             }
              
             fixed4 frag (v2f i) : COLOR {
                 if (i.uv.x < 1 && i.uv.y < 1)
                     return tex2D (_ShadowTex, fmod (i.uv, 1 / _Size) * _Size) * _Color;
                 else
                     return fixed4 (0,0,0,0);
             }
             
             ENDCG
         }
     }
 }

Probably not the most optimized and clean way to do this (how could I optimize it?), but it's working now.

Comment
Add comment · Show 7 · 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 mayorc1978 · Jan 04, 2014 at 03:41 AM 0
Share

Texgen ObjectLinear can be omitted because you are using a vertex/fragment shader combo.

Anyway the shader looks great, and you did a good job. But you should avoid if/else statements and your clamp is only partial (you forgot to clamp negative values), you should clamp in the uv range (0,1). You don't need to have an else because you are blending so the texture coordinates you will not touch will remain unchanged. So you can rewrite the whole thing with a one liner:

 return tex2D (_ShadowTex,  fmod (float2(saturate(i.uv.x),saturate(i.uv.y)), 1 / _Size) * _Size) * _Color;


P.S. I was trying to make the same projector texture tiling stuff, tried your code, it works but only on orthographic mode, could you help me making it work in perspective mode, thanks.

avatar image frarees · Jan 04, 2014 at 05:33 PM 0
Share

@mayorc1978 sure. Contact me via twitter P$$anonymous$$ (@frarees).

avatar image frarees · Jan 04, 2014 at 05:54 PM 0
Share

I've created a gist to keep track of changes: https://gist.github.com/frarees/8258128

avatar image mayorc1978 · Jan 04, 2014 at 07:13 PM 0
Share

No problem yesterday i remained awake till 6am and I was able to write that shader from scratch.

avatar image frarees · Jan 04, 2014 at 10:40 PM 0
Share

Nice! Can you share it? Thanks

Show more comments
avatar image
0

Answer by Aras · Aug 22, 2013 at 04:24 AM

Not sure I understood the problem correctly. You do want the texture to repeat forever, or not? If you do, then just remove the fmod() call and set the texture wrap mode to Repeat?

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 frarees · Aug 22, 2013 at 06:55 AM 0
Share

$$anonymous$$y explanation is confusing. What I want is to repeat _ShadowTex _Size times. So, for the first example, I get a tile, and for the second I get four tiles (a 2x2 grid). The area that gets drawn should always be the same (1 unit), so the tiles should get smaller and smaller when _Size grows.

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

18 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 avatar image

Related Questions

How to draw a tiled texture on a post process shader 0 Answers

Why not tiling on Shader (Sprite/Diffuse)? Unity 5.4.1 f1 1 Answer

cg shader blend textures in between two planes/tiles 0 Answers

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

How to clean on RenderTexture to transparent with brush 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