Unity3D looking for setPixels example to paint wit

2019-04-02 12:44发布

问题:

I need to find a way that allow me to paint a gameObject using a texture, the goal is to make a clone of this game

I already thought that i can fake the painting feature by using depthMask solution which i explained here, however, after many hours of research it seems that setPixels,setPixel and setPixels32 can be used to create a real painting but For some reason all the setPixels related topics at the Script Reference are in JavaScript, i tried to port it to c# but i always got errors, plus it's kind of a complicated approach and the script reference doesn't explain it very well, So am hoping that anyone can please wrote me an example using setPixels in order to change a part of an object texture using another texture ? or maybe we can set the whole object alpha to 0, and when we click on it, we change a part of that texture alpha to 1 ?

I really hope this topic doesn't get closed because of it's variety, i tried my best to ask a straight forward question while explaining the entire situation.

Thank you

EDIT:

After a couple of hours, i've reached a pretty cool result, and i think that this can be the good way to reach my goal, this script allow to replace a specific pixel in a material texture with another chosen texture (target texture must be in the same size, and readable)

// the texture that i will paint with 
    public Texture2D targetTexture ;
    //temporary texture
    Texture2D tmpTexture;


    void Start ()
    {
            //setting temp texture width and height 
            tmpTexture = new Texture2D (targetTexture.width, targetTexture.height);

            for (int y =0; y<tmpTexture.height; y++) {
                    for (int x = 0; x<tmpTexture.width; x++) {
                            //filling the temporary texture with the target texture
                            tmpTexture.SetPixel (x, y, targetTexture.GetPixel (x, y));
                    }
            }
            //Apply 
            tmpTexture.Apply ();
            //change the object main texture 
            renderer.material.mainTexture = tmpTexture;


    }

Now i think that the problem will only be, "how to know which pixel i should replace based on mouse position over the object"

回答1:

Nailed it !!

This code work perfectly as expected, it's heavily commented and it shows everything i did, it doesn't allow to dynamically paint an object since am still looking for how to get texture coordinate after clicking on the object, however, you can manually enter your coordinate and choose the target texture which with you want to paint your original object, i hope this will be useful for someone else in the future, Code :

using UnityEngine;
using System.Collections;

public class BasicPainting : MonoBehaviour
{

    // the texture that i will paint with and the original texture (for saving)
    public Texture2D targetTexture, originalTexture ;
    //temporary texture
    public Texture2D tmpTexture;

    void Start ()
    {
            //setting temp texture width and height 
            tmpTexture = new Texture2D (originalTexture.width, originalTexture.height);
            //fill the new texture with the original one (to avoid "empty" pixels)
            for (int y =0; y<tmpTexture.height; y++) {
                    for (int x = 0; x<tmpTexture.width; x++) {
                            tmpTexture.SetPixel (x, y, originalTexture.GetPixel (x, y));
                    }
            }
            print (tmpTexture.height);
            //filling a part of the temporary texture with the target texture 
            for (int y =0; y<tmpTexture.height-40; y++) {
                    for (int x = 0; x<tmpTexture.width; x++) {
                            tmpTexture.SetPixel (x, y, targetTexture.GetPixel (x, y));

                    }
            }
            //Apply 
            tmpTexture.Apply ();
            //change the object main texture 
            renderer.material.mainTexture = tmpTexture;
    }

}


回答2:

You need two Asset Kit;

1- https://www.assetstore.unity3d.com/#/content/2682 for paint (brush vs)
or
https://www.assetstore.unity3d.com/#/content/11735 for paint (airbrush vs)

2- https://www.assetstore.unity3d.com/#/content/908
for sprite works (cut,add,delete)



回答3:

Use this to find out which texture pixel are corresponding to the screen's position:

http://docs.unity3d.com/ScriptReference/RaycastHit-textureCoord.html

However,the game you wanna clone look just a 2D GAME,it seem that no need to deal with 3D.



标签: c# unity3d paint