【问题标题】:Unity3D looking for setPixels example to paint with texture in C#Unity3D 寻找 setPixels 示例以在 C# 中使用纹理进行绘制
【发布时间】:2014-12-26 20:26:56
【问题描述】:

我需要找到一种方法,允许我使用纹理绘制游戏对象,目标是克隆this game

我已经认为我可以通过使用 depthMask 解决方案来伪造绘画功能,我解释了 here,但是,经过数小时的研究,似乎 setPixels、setPixel 和 setPixels32 可以用来创建真实的绘画,但对于某些人来说因为脚本参考中所有与 setPixels 相关的主题都在 JavaScript 中,我试图将它移植到 c# 但我总是遇到错误,而且这是一种复杂的方法,脚本参考并没有很好地解释它,所以我希望任何人都可以给我写一个使用 setPixels 的示例,以便使用另一个纹理更改对象纹理的一部分? 或者我们可以将整个对象的 alpha 设置为 0,当我们点击它时,我们将部分纹理 alpha 更改为 1?

我真的希望这个话题不要因为它的多样性而被关闭,我在解释整个情况时尽力问一个直截了当的问题。

谢谢

编辑:

几个小时后,我得到了一个很酷的结果,我认为这是实现目标的好方法,这个脚本允许用另一个选定的纹理替换材质纹理中的特定像素(目标纹理必须具有相同的大小,并且可读)

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


    }

现在我认为问题只是“如何根据鼠标在对象上的位置知道我应该替换哪个像素”

【问题讨论】:

  • 我建议您发布您目前生成的代码:您对 C# API 究竟有什么不了解?你试过什么?
  • 我不能说我没有尝试过,但这远不是一个好的结果,我会继续尝试,如果我至少达到一个起点,我会在这里发布它,同时,我希望一些编码忍者会来结束我的痛苦:-)
  • 好的,我取得了一些进步,请再次检查问题

标签: c# unity3d paint


【解决方案1】:

成功了!!

此代码按预期完美运行,它被大量注释并显示了我所做的一切,它不允许动态绘制对象,因为我仍在寻找单击对象后如何获取纹理坐标,但是,您可以手动输入您的坐标并选择您想要绘制原始对象的目标纹理,我希望这对其未来的其他人有用, 代码:

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

}

【讨论】:

  • 那是 setpixel 单数。
【解决方案2】:

您需要两个 Asset Kit;

1- https://www.assetstore.unity3d.com/#/content/2682 用于油漆(画笔与)

https://www.assetstore.unity3d.com/#/content/11735 用于油漆(喷枪与)

2- https://www.assetstore.unity3d.com/#/content/908 用于精灵作品(剪切、添加、删除)

【讨论】:

  • 这正是我想知道如何制作这个的原因,如果有人用它制作了资产,那么我们可以在没有资产的情况下创建它,我们需要的只是耐心^_^
  • 时间就是金钱,朋友:)
  • 知识是无价的:)
【解决方案3】:

用它来找出哪个纹理像素对应于屏幕的位置:

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

但是,你要克隆的游戏看起来只是一个2D游戏,似乎不需要处理3D。

【讨论】:

    猜你喜欢
    • 2016-07-19
    • 1970-01-01
    • 2014-11-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多