var pixelUV = raycastHit.textureCoord; //计算笔刷所覆盖的区域 var PuX = Mathf.FloorToInt(pixelUV.x * controlTex.width); var PuY = Mathf.FloorToInt(pixelUV.y * controlTex.height); var x = Mathf.Clamp(PuX - brushSizeInModel / 2, 0, controlTex.width - 1); var y = Mathf.Clamp(PuY - brushSizeInModel / 2, 0, controlTex.height - 1); var width = Mathf.Clamp((PuX + brushSizeInModel / 2), 0, controlTex.width) - x; var height = Mathf.Clamp((PuY + brushSizeInModel / 2), 0, controlTex.height) - y; Color[] terrainBay = controlTex.GetPixels(x, y, width, height, 0);//获取Control贴图被笔刷所覆盖的区域的颜色 Texture2D TBrush = brushTex[selBrush] as Texture2D;//获取笔刷性状贴图 var brushAlpha = new float[brushSizeInModel * brushSizeInModel];//笔刷透明度 //根据笔刷贴图计算笔刷的透明度 for (var i = 0; i < brushSizeInModel; i++) { for (var j = 0; j < brushSizeInModel; j++) { brushAlpha[j * brushSizeInModel + i] = TBrush.GetPixelBilinear(((float)i) / brushSizeInModel, ((float)j) / brushSizeInModel).a; } } //计算绘制后的颜色 for (var i = 0; i < height; i++) { for (var j = 0; j < width; j++) { var index = (i * width) + j; var Stronger = brushAlpha[Mathf.Clamp((y + i) - (PuY - brushSizeInModel / 2), 0, brushSizeInModel - 1) * brushSizeInModel + Mathf.Clamp((x + j) - (PuX - brushSizeInModel / 2), 0, brushSizeInModel - 1)] * brushStronger; terrainBay[index] = Color.Lerp(terrainBay[index], targetColor, Stronger); } } Undo.RegisterCompleteObjectUndo(controlTex, "meshPaint");//保存历史记录以便撤销 controlTex.SetPixels(x, y, width, height, terrainBay, 0);/这段代码生成的位置是从右上角开始的如何修改成左下角
时间: 2024-03-18 21:40:31 浏览: 91
要将代码生成的位置从右上角改为左下角,需要对获取Control贴图被笔刷所覆盖的区域的颜色和计算绘制后的颜色的部分进行修改。
具体来说,需要将以下代码:
```
var x = Mathf.Clamp(PuX - brushSizeInModel / 2, 0, controlTex.width - 1);
var y = Mathf.Clamp(PuY - brushSizeInModel / 2, 0, controlTex.height - 1);
var width = Mathf.Clamp((PuX + brushSizeInModel / 2), 0, controlTex.width) - x;
var height = Mathf.Clamp((PuY + brushSizeInModel / 2), 0, controlTex.height) - y;
...
var Stronger = brushAlpha[Mathf.Clamp((y + i) - (PuY - brushSizeInModel / 2), 0, brushSizeInModel - 1) * brushSizeInModel + Mathf.Clamp((x + j) - (PuX - brushSizeInModel / 2), 0, brushSizeInModel - 1)] * brushStronger;
terrainBay[index] = Color.Lerp(terrainBay[index], targetColor, Stronger);
```
修改为:
```
var x = Mathf.Clamp(PuX - brushSizeInModel / 2, 0, controlTex.width - 1);
var y = Mathf.Clamp(controlTex.height - (PuY + brushSizeInModel / 2), 0, controlTex.height - 1);
var width = Mathf.Clamp((PuX + brushSizeInModel / 2), 0, controlTex.width) - x;
var height = Mathf.Clamp((PuY + brushSizeInModel / 2), 0, controlTex.height) - y;
...
var Stronger = brushAlpha[Mathf.Clamp((controlTex.height - (y + i)) - (PuY - brushSizeInModel / 2), 0, brushSizeInModel - 1) * brushSizeInModel + Mathf.Clamp((x + j) - (PuX - brushSizeInModel / 2), 0, brushSizeInModel - 1)] * brushStronger;
terrainBay[index] = Color.Lerp(terrainBay[index], targetColor, Stronger);
```
这样就可以将代码生成的位置从右上角改为左下角。
阅读全文