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 Nikz_89 · Feb 26, 2015 at 12:01 PM · javascriptlagframeratesetpixelssetpixel

Scratch Card Is not smooth using SetPixel()

Hi,

I want to implement a scratcher where user can scratch the foil to reveal the content on card. For the following I used unity SetPixel(). But it's not smooth when user reveal vigorously.

My code is as follow :

 public class Scratcher : MonoBehaviour
 {
         public UITexture mTex;
 
         private Texture2D texture;
         private float erasedPixels;
 
         private int textureWidth = 28;
         private int textureHieght = 28;
 
         void  Start ()
         {
                 // Create a new texture and assign it to the renderer's material
                 texture = new Texture2D (textureWidth, textureHieght, TextureFormat.RGB24, true);
 
                 GetComponent<UITexture> ().material.SetTexture ("_SliceGuide", texture);
 
                 // Fill the texture with white (you could also paint it black, then draw with white)
                 for (int y = 0; y < texture.height; ++y) {
                         for (int x = 0; x < texture.width; ++x) {
                                 texture.SetPixel (x, y, Color.white);
                         }
                 }
                 texture.Apply ();
         }
 
         void Update ()
         {
                 if (Input.GetMouseButton (0) && UICamera.hoveredObject != null && UICamera.hoveredObject.tag == "ScratchFoil") {
 
                         Vector3 localPos = transform.InverseTransformPoint (UICamera.lastHit.point);
                         Vector2 uv;
 
                         uv.x = localPos.x / (float)mTex.width;
                         uv.y = localPos.y / (float)mTex.height;
 
                         Texture2D tex = texture;
 
                         Vector2 pixelUV = uv;
 
                         pixelUV.x *= tex.width;
                         pixelUV.y *= tex.height;
 
                         // add black spot, which is then transparent in the shader
                         tex.SetPixel ((int)pixelUV.x, (int)pixelUV.y, Color.black);
                         tex.Apply ();
                 }
         }
 }



Now I applied this script on my NGUI UITexture which contains a material with a shader used to clip the current texture RGB value from _MainTex.

Shader As follow :

  Shader "mShaders/ScratchEffect" {
     Properties {
       _MainTex ("Base (RGB), Alpha (A)", 2D) = "black" {}
       _SliceGuide ("Slice Guide (RGB)", 2D) = "white" {}
       _SliceAmount ("Slice Amount", Range(0.0, 1.0)) = 0.9
     }
 
     SubShader {
       Tags {"Queue" = "Transparent" "IgnoreProjector"="True" "RenderType" = "Transparent" }
       Cull Off
 
       CGPROGRAM
       #pragma surface surf Lambert alpha
       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;
           o.Emission = tex2D (_MainTex, IN.uv_MainTex).rgb;
           o.Alpha = tex2D (_MainTex, IN.uv_MainTex).a;
       }
       ENDCG
 
     }
     Fallback "Diffuse"
 
   }



Everything working fine to it just this SetPixel() is sucking a lot of frame per second and not giving smooth scratch effect.

Anybody help me out with this trouble.

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
1

Answer by rageingnonsense · Feb 26, 2015 at 11:39 PM

You should avoid using SetPixel() so often. It is much more efficient to use SetPixels().

Perhaps in Update() you could maintain a list of pixels you need to set. Then, after a certain threshold, apply the pixels you were recording via SetPixels() as a batch. You could play with the threshold such that it is not very noticeable.

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 Nikz_89 · Mar 10, 2015 at 12:35 PM 0
Share

Tried SetPixels() also. Still if I scratch vigorously, it still lagged. No so smooth. will appreciate if you provide any example.

thanks.

avatar image idurvesh · May 25, 2015 at 02:51 PM 0
Share

@nikz_89 have you found workaround?

avatar image nikhil38 · Jul 22, 2015 at 11:26 AM 0
Share

@Nikz_89 : tex.Apply (); is the culprit. if you want to use it in Update( ) ,just call it say , only 30 times or less per second . I hope this shall solve your lagging. You can even try this in retina devices. Just remember , less calls to tex.apply , means less lag. And yes , better to use SetPixels or SetPixels32 ins$$anonymous$$d of setpixel :D

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

Performance issue. Optimize or change SetPixel to allow for painting specific areas of a texture. 1 Answer

Lagspike when using instantiate in coorutine 0 Answers

Reduce Poly Count Within Unity? Higher FPS? 1 Answer

Android lag 1 Answer

Android game lags when using transform.RotateAround to rotate an object with many children 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