简体   繁体   English

Unity3D在C#中寻找使用纹理绘制的setPixels示例

[英]Unity3D looking for setPixels example to paint with texture in C#

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 我需要找到一种方法,允许我使用纹理绘制一个gameObject,目标是克隆这个游戏

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 ? 我已经认为我可以通过使用我在这里解释的depthMask解决方案伪造绘画功能,但是,经过多个小时的研究,似乎setPixels,setPixel和setPixels32可以用来创建一个真实的绘画但是由于某种原因所有的setPixels都相关脚本参考的主题是在JavaScript中,我试图将它移植到c#,但我总是有错误,加上它是一种复杂的方法,脚本参考不能很好地解释它,所以我希望有人可以请我写信给我使用setPixels以使用另一个纹理更改对象纹理的一部分的示例? 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 ? 或者我们可以将整个对象alpha设置为0,当我们点击它时,我们将该纹理alpha的一部分更改为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" 现在我认为问题只会是“如何根据鼠标在对象上的位置知道应该替换哪个像素”

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;
    }

}

You need two Asset Kit; 你需要两个资产套件;

1- 1- https://www.assetstore.unity3d.com/#/content/2682 for paint (brush vs) https://www.assetstore.unity3d.com/#/content/2682 for paint(brush vs)
or 要么
https://www.assetstore.unity3d.com/#/content/11735 for paint (airbrush vs) https://www.assetstore.unity3d.com/#/content/11735用于涂料(喷枪vs)

2- https://www.assetstore.unity3d.com/#/content/908 for sprite works (cut,add,delete) 用于精灵工作的2- https://www.assetstore.unity3d.com/#/content/908 (剪切,添加,删除)

Use this to find out which texture pixel are corresponding to the screen's position: 使用它来找出与屏幕位置对应的纹理像素:

http://docs.unity3d.com/ScriptReference/RaycastHit-textureCoord.html 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. 但是,你想要克隆的游戏看起来只是一个2D游戏,似乎没有必要处理3D。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM