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 danilaaka · Dec 13, 2019 at 12:56 PM · texture2dpaintpaintingcomparisonpainter

How can I paint a picture and then compare it to another?

I want to make the game similar to ink.inc . Right now I'm trying to make the core gameplay of the game. There is an example image and I should try to paint as precise as I can and then compare them. Right now I found 2 separate solutions on the internet: One for painting and saving the texture and one for comparing textures. The first one is from a youtube tutorial: https://www.youtube.com/watch?v=DCD8Pt1zf4c The code from this video:


 using System.Collections;
 using System.IO;
 using UnityEngine;
 
 public class Paintable : MonoBehaviour {
 
     public GameObject Brush;
     public float BrushSize = 0.1f;
     public RenderTexture RTexture;
 
     // Use this for initialization
     void Start () {
         
     }
     
     // Update is called once per frame
     void Update () {
 
         if (Input.GetMouseButton(0))
         {
             //cast a ray to the plane
             var Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
             RaycastHit hit;
             if(Physics.Raycast(Ray, out hit))
             {
                 //instanciate a brush
                 var go = Instantiate(Brush, hit.point + Vector3.up * 0.1f, Quaternion.identity, transform);
                 go.transform.localScale = Vector3.one * BrushSize;
             }
 
         }
     }
 
     public void Save()
     {
         StartCoroutine(CoSave());
     }
 
     private IEnumerator CoSave()
     {
         //wait for rendering
         yield return new WaitForEndOfFrame();
         Debug.Log(Application.dataPath + "/savedImage.png");
 
         //set active texture
         RenderTexture.active = RTexture;
 
         //convert rendering texture to texture2D
         var texture2D = new Texture2D(RTexture.width, RTexture.height);
         texture2D.ReadPixels(new Rect(0, 0, RTexture.width, RTexture.height), 0, 0);
         texture2D.Apply();
 
         //write data to file
         var data = texture2D.EncodeToPNG();
         File.WriteAllBytes(Application.dataPath + "/savedImage.png", data);
 
 
     }
 }



The second with this code from StackOverflow answer:


 private bool CompareTexture (Texture2D first, Texture2D second)
 {
     Color[] firstPix = first.GetPixels();
     Color[] secondPix = second.GetPixels();
     if (firstPix.Length!= secondPix.Length)
     {
         return false;
     }
     for (int i= 0;i < firstPix.Length;i++)
     {
         if (firstPix[i] != secondPix[i])
         {
             return false;
         }
     }
 
     return true;
 }


The problems that I currently have are the following:

1) While painting the tutorial way the output looks weird and colors are not exact as in a play mode.

In unity the painted picture looks like this: https://prnt.sc/qacd16

If you open it in finder it looks like this: https://prnt.sc/qacel5

How can I make it look the same as in Unity?

2) Because of the lighting and other stuff, the colors are obscured ob the painted image and as a result, I can't compare these images with the original one.

Would be grateful for any help :)

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 Felipe669 · Dec 20, 2019 at 03:11 PM

To compare 2 textures you could:


  • Loop through every pixel of each texture, compare their colors and count the matching pixels;

  • Then you could divide this value from the total amount of pixels (of one texture);

  • With that, you should get a value from 0 to 1, that corresponds to how much the textures match with each other.


(This is assuming that you're working with flat colored images, like simple drawings)

Hope this helps!

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

122 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 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 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 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 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 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 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

Painting issue, drawing is not consistent, how to fix? 1 Answer

Cannot place trees on terrain 0 Answers

Sprite/Texture Eraser and Painter 0 Answers

Drawn lines are dotted and incomplete if drawn too quickly!? 1 Answer

RaycastHit2D hit to texture coordinate 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