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
0
Question by badescuga9 · Aug 07, 2013 at 12:39 PM · shadershadersmasking

eraser effect in plane using shader

i am trying to produce an eraser for a material on a plane. The way i am thinking of doing this is by passing an array to the shader telling the shader where the material should be transparent; if the value from the array is 0, i return no color for the material (i.e. tranparent). i have 2 problems:

1) how do i declare and pass an array in CG?

2) does this way work and if so, is this the best way to do this? I am thinking that it might be very demanding..

UPDATE

ok, so i have this shader:

 Shader "mShaders/Holes1" {
     Properties {
       _MainTex ("Texture (RGB)", 2D) = "white" {}
       _SliceGuide ("Slice Guide (RGB)", 2D) = "white" {}
       _SliceAmount ("Slice Amount", Range(0.0, 1.0)) = 0.9
     }
     SubShader {
       Tags { "RenderType" = "Opaque" }
       Cull Off
       CGPROGRAM
       //if you're not planning on using shadows, remove "addshadow" for better performance
       #pragma surface surf Lambert 
       //addshadow
       struct Input {
           float2 uv_MainTex;
           float2 uv_SliceGuide;
           float _SliceAmount;
       };
       sampler2D _MainTex;
       sampler2D _SliceGuide;
       float _SliceAmount;
       void surf (Input IN, inout SurfaceOutput o) {
           clip(tex2D (_SliceGuide, IN.uv_SliceGuide).rgb - _SliceAmount);
           o.Albedo = tex2D (_MainTex, IN.uv_MainTex).rgb;
       }
       ENDCG
     }
     Fallback "Diffuse"
   }

In my touch script i have the start function containing:

 texture = new Texture2D(32, 16);
     // set texture in the inspector slot
     renderer.material.SetTexture("_SliceGuide", texture);
     
     // Fill the texture with white (you could also paint it black, then draw with white)
     for (var y : int = 0; y < texture.height; ++y) 
     {
         for (var x : int = 0; x < texture.width; ++x) 
         {
             texture.SetPixel (x, y, Color.white);
         }
     }
     // Apply all SetPixel calls
     texture.Apply();



and in Update i have:

 if (Input.touchCount ==0  && !Input.anyKey) return;
     
     // Only if we hit something, do we continue
     var hit : RaycastHit;
     if (!Physics.Raycast (Camera.main.ScreenPointToRay(Input.mousePosition), hit)) return;
     
     // Just in case, also make sure the collider also has a renderer
     // material and texture. Also we should ignore primitive colliders.
     var renderer : Renderer = hit.collider.renderer;
     var meshCollider = hit.collider as MeshCollider;
     if (renderer == null || renderer.sharedMaterial == null ||texture == null || meshCollider == null) return;
     
     // Now draw a pixel where we hit the object
     var tex : Texture2D = texture;
     var pixelUV = hit.textureCoord;
     pixelUV.x *= tex.width;
     pixelUV.y *= tex.height;
     
     // add black spot, which is then transparent in the shader
     tex.SetPixel(pixelUV.x, pixelUV.y, Color.black);
     tex.Apply();



All seems to work on the computer, but on my iphone the object (here a mapped sphere) is rendered white with black spots after first touch (before first touch it's rendered ok with the texture on) -- the black spots are from where i touch the object. What am i doing wrong?

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 Owen-Reynolds · Aug 07, 2013 at 01:04 PM

Nor difficult at all. Use your own texture2D as the array. Shaders enjoy reading from textures. In script, works like a 2D-array except you use SetPixel(x,y) to change color. Also some rules about writing it back to the graphics card after changes, and using SetPixels if you have lots of changes.

Values are from 0-1 (normal color range.) Pass it to the shader just like a "real" texture. The shader can and use whatever value (RGBA) for alpha -- like o.col.a = col2.r;

Normal texture rules say the texture size won't matter. It will stretch to cover the plane. Larger just means less pixelated. The initial math to figure out which pixels to change will be standard "screen percent" math -- pixel = percentOver*textureWidth.

Comment
Add comment · Show 2 · 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 badescuga9 · Aug 07, 2013 at 01:42 PM 0
Share

@Owen_Reynolds

i've updated my code above. The solution works but get this: only if i have another object behind -- for some reason. If i don't, the code doesn't seem to work. very weird behaviour

avatar image Owen-Reynolds · Aug 07, 2013 at 03:15 PM 0
Share

Seems more of an iPhone-specific question. But, I've had trouble with clip (really, discard) on some cards, and it doesn't seem the right operation, anyway.

Clip tosses, or not, an entire pixel, which makes it look pixelated. If you ins$$anonymous$$d do another tex2D on the SlideGuide texture, it will lookup using a smooth fade.

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

15 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

Related Questions

How to make effect between scenes with UI Mask or Shader 0 Answers

Can you mask a shader effect with a stencil 0 Answers

Merge 2 shaders 0 Answers

Image effect shader that masks spritelayer 1 Answer

Mask Shader and Shadow 2 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