Unity中图片挖洞

游戏中常见的对图片挖洞方法的一种实现

直接上原理:
用一张与目标图大小一样的贴图,填充其alpha通道,将需要挖洞的区域的alpha值设置为>0的值,在shader里处理alpha>0的区域将目标图的alpha设置为0即可,比较简单吧
shader代码:
(1).在Properties中加一个_AlphaTex (“Alpha Texture”, 2D) = “white” {}

// 加一个alpha贴图
Properties
{
	[PerRendererData] _MainTex ("Sprite Texture", 2D) = "white" {}
	_AlphaTex ("Alpha Texture", 2D) = "white" {}
}

(2).frag着色器中的处理

// A code block
fixed4 frag(v2f IN) : SV_Target
{
	fixed4 texcol   = (tex2D(_MainTex, IN.texcoord) + _TextureSampleAdd) * IN.color;
	fixed4 alphacol = (tex2D(_AlphaTex, IN.texcoord) + _TextureSampleAdd) * IN.color;
	fixed4 color = texcol;
	color.a *= step(alphacol.a,0);   //即当alphacol.a <= 0的时候返回1 当alphacol.a > 0的时候返回0
	return color;
}

逻辑代码:
(1).三角形洞

--作用:图片挖洞(三角形洞)
--参数说明:
--pt1,pt2,pt3三角形的三个顶点
function UIHoleUtil.setTriangleHole(image,pt1,pt2,pt3)
	if goutil.isNil(image) then return end
	if not image.material or image.material.shader ~= UIHoleUtil.Shader then
		local size = image.transform.sizeDelta 
		local newMat = UnityEngine.Material.New(UIHoleUtil.Shader)
		local alphaTex = UnityEngine.Texture2D.New(size.x,size.y) --UnityEngine.TextureFormat.Alpha8,false
		for i = 0,size.x - 1 do
			for j = 0,size.y - 1 do
				local alpha = 0
				if isInTriangle(pt1,pt2,pt3,Vector2(i,j)) then
					alpha = 1
				end
				alphaTex:SetPixel(i,j,Color.New(0,0,0,alpha))
			end
		end
		alphaTex:Apply()
		newMat:SetTexture("_AlphaTex",alphaTex)
		image.material = newMat
	end
end

(2).圆形洞

--作用:图片挖洞(圆形洞)
--参数说明:
--pt1,pt2,pt3三角形的三个顶点
function UIHoleUtil.setTriangleHole(image,pt1,pt2,pt3)
	if goutil.isNil(image) then return end
	if not image.material or image.material.shader ~= UIHoleUtil.Shader then
		local size = image.transform.sizeDelta 
		local newMat = UnityEngine.Material.New(UIHoleUtil.Shader)
		local alphaTex = UnityEngine.Texture2D.New(size.x,size.y) --UnityEngine.TextureFormat.Alpha8,false
		for i = 0,size.x - 1 do
			for j = 0,size.y - 1 do
				local alpha = 0
				if (i-offsetx)*(i-offsetx) + (j-offsety)*(j-offsety) <= radius * radius then
					alpha = 1
				end
				alphaTex:SetPixel(i,j,Color.New(0,0,0,alpha))
			end
		end
		alphaTex:Apply()
		newMat:SetTexture("_AlphaTex",alphaTex)
		image.material = newMat
	end
end

(3).矩形洞

--作用:图片挖洞(矩形洞)
--参数说明:
--pt1,pt2,pt3三角形的三个顶点
function UIHoleUtil.setTriangleHole(image,pt1,pt2,pt3)
	if goutil.isNil(image) then return end
	if not image.material or image.material.shader ~= UIHoleUtil.Shader then
		local size = image.transform.sizeDelta
		local newMat = UnityEngine.Material.New(UIHoleUtil.Shader)
		local alphaTex = UnityEngine.Texture2D.New(size.x,size.y) --UnityEngine.TextureFormat.Alpha8,false
		for i = 0,size.x - 1 do
			for j = 0,size.y - 1 do
				local alpha = 0
				if i >= offsetx - width and i <= offsetx + width and j >= offsety - height and j <= offsety + height then
					alpha = 1
				end
				alphaTex:SetPixel(i,j,Color.New(0,0,0,alpha))
			end
		end
		alphaTex:Apply()
		newMat:SetTexture("_AlphaTex",alphaTex)
		image.material = newMat
	end
end

(4).自定义贴图

--作用:图片挖洞(自定义的alpha单通道贴图形状)
--参数说明:
--alphaTex2d:该贴图只需要写入alpha通道的值,alpha > 0的区域将会被挖空
function UIHoleUtil.setHoleByTex2D(image,alphaTex2d)
	if goutil.isNil(image) then return end
	if not image.material or image.material.shader ~= UIHoleUtil.Shader then   
		newMat:SetTexture("_AlphaTex",alphaTex2d)
		image.material = newMat
	end
end

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Unity物理挖洞是指在Unity游戏引擎,通过使用物理引擎来实现角色或物体在地面上挖洞的效果。 在Unity物理挖洞,通常使用的是物理引擎组件,如Mesh Collider和Rigidbody。首先,我们需要给需要挖洞的地面添加Mesh Collider组件,这样地面就具备了物理碰撞功能。然后,给挖洞的工具(例如玩家手的镐子)添加Rigidbody组件,以便让挖洞工具受到物理引擎的影响。 接下来,我们可以使用Raycast或者Collider.Raycast来检测挖洞的位置。当挖洞工具与地面发生碰撞时,我们可以通过修改地面的网格顶点来模拟挖洞的效果。具体地,我们可以根据挖洞工具的位置和半径,找到所有在工具半径内的顶点,并将它们的高度调整为地面下沉的深度值。 为了实现连续挖洞的效果,我们可以在每次挖洞前将新的地面拷贝一份,并在新的地面上进行挖洞操作。这样,我们就可以实现实时更新地面和连续挖洞的效果。 需要注意的是,Unity物理挖洞需要合理设置物理材质、碰撞体积和刚体参数,以达到符合游戏设计的物理表现效果。此外,挖洞操作也可能会对游戏性能造成一定影响,因此在实现挖洞功能时需要进行适当的优化。 总的来说,Unity物理挖洞是利用物理引擎和碰撞检测技术实现的一种在游戏让角色或物体能够实时挖掘地面的效果,通过调整地面网格顶点的位置来模拟挖洞的视觉效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值