Unity3D寻找的setPixels例如纹理在C#中作画 [英] Unity3D looking for setPixels example to paint with texture in C#

查看:2869
本文介绍了Unity3D寻找的setPixels例如纹理在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

我已经认为我可以伪造使用depthMask的解决方案,我解释的here 的,然而,许多小时的研究之后似乎的setPixels,的setPixel和setPixels32可以被用来创建一个真正的绘画,但出于某种原因,所有的setPixels相关的脚本参考主题是在JavaScript中,我试图将它移植到C#,但我总是错误的,再加上它是一种复杂的方法和脚本参考没有按' ŧ解释得非常好,所以我希望,任何人都可以请给我写的setPixels使用,为了改变使用另一个纹理对象的纹理的一部分的例子吗?
也许我们可以把整个对象设置透明度为0,当我们点击它,我们改变纹理阿尔法的一部分1?

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.

感谢您

编辑:

一两个小时后,我已经达到了一个相当酷的结果,我认为这可能是很好的方法,达到我的目标,这个脚本允许在材料的质感与另一个替换特定的像素选用质地(目标纹理必须是相同的大小,和可读性)

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"

推荐答案

钉它! !

这是预期的代码工作完美,它的大量注释,它显示了一切,我做到了,它不允许动态绘制的对象,因为我仍然寻找如何获得点击的对象后,纹理坐标,但是,您可以手动输入坐标,并选择目标纹理它与你想画你的原始对象,我希望这将是在未来,$别人有用b $ b码:

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

}

这篇关于Unity3D寻找的setPixels例如纹理在C#中作画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆