文章转自游戏蛮牛:http://www.manew.com/forum.php?mod=viewthread&tid=103087&_dsign=ec6d9a3d

偶然在国外网站上看到的一个脚本,通过纯色背景抠像的方法,把windows的窗体透明化,可以用来做无背景的小头像、桌面精灵等。
首先新建一个叫TransparentWindow的CS脚本,输入如下代码:
using System;
using System.Runtime.InteropServices;
using UnityEngine;public class TransparentWindow : MonoBehaviour
{[SerializeField]private Material m_Material;private struct MARGINS{public int cxLeftWidth;public int cxRightWidth;public int cyTopHeight;public int cyBottomHeight;}// Define function signatures to import from Windows APIs[DllImport("user32.dll")]private static extern IntPtr GetActiveWindow();[DllImport("user32.dll")]private static extern int SetWindowLong(IntPtr hWnd, int nIndex, uint dwNewLong);[DllImport("Dwmapi.dll")]private static extern uint DwmExtendFrameIntoClientArea(IntPtr hWnd, ref MARGINS margins);// Definitions of window stylesconst int GWL_STYLE = -16;const uint WS_POPUP = 0x80000000;const uint WS_VISIBLE = 0x10000000;void Start(){
#if !UNITY_EDITORvar margins = new MARGINS() { cxLeftWidth = -1 };// Get a handle to the windowvar hwnd = GetActiveWindow();// Set properties of the window// See: https://msdn.microsoft.com/en-us/library/windows/desktop/ms633591%28v=vs.85%29.aspxSetWindowLong(hwnd, GWL_STYLE, WS_POPUP | WS_VISIBLE);// Extend the window into the client area//See: https://msdn.microsoft.com/en-us/library/windows/desktop/aa969512%28v=vs.85%29.aspx DwmExtendFrameIntoClientArea(hwnd, ref margins);
#endif}// Pass the output of the camera to the custom material// for chroma replacementvoid OnRenderImage(RenderTexture from, RenderTexture to){Graphics.Blit(from, to, m_Material);}
}
然后新建一个叫ChromakeyTransparent的Shader:
Shader "Custom/ChromakeyTransparent" {Properties{_MainTex("Base (RGB)", 2D) = "white" {}_TransparentColourKey("Transparent Colour Key", Color) = (0,0,0,1)_TransparencyTolerance("Transparency Tolerance", Float) = 0.01}SubShader{Pass{Tags{ "RenderType" = "Opaque" }LOD 200CGPROGRAM#pragma vertex vert#pragma fragment frag#include "UnityCG.cginc"struct a2v{float4 pos : POSITION;float2 uv : TEXCOORD0;};struct v2f{float4 pos : SV_POSITION;float2 uv : TEXCOORD0;};v2f vert(a2v input){v2f output;output.pos = UnityObjectToClipPos(input.pos);output.uv = input.uv;return output;}sampler2D _MainTex;float3 _TransparentColourKey;float _TransparencyTolerance;float4 frag(v2f input) : SV_Target{// What is the colour that *would* be rendered here?float4 colour = tex2D(_MainTex, input.uv);// Calculate the different in each component from the chosen transparency colourfloat deltaR = abs(colour.r - _TransparentColourKey.r);float deltaG = abs(colour.g - _TransparentColourKey.g);float deltaB = abs(colour.b - _TransparentColourKey.b);// If colour is within tolerance, write a transparent pixelif (deltaR < _TransparencyTolerance && deltaG < _TransparencyTolerance && deltaB < _TransparencyTolerance){return float4(0.0f, 0.0f, 0.0f, 0.0f);}// Otherwise, return the regular colourreturn colour;}ENDCG}}
}
新建一个材质,选择我们刚刚写好的Shader,将TransparentWindow挂载到摄像机上,摄像机的Clear Flags选择Solid Color,Background选择和材质的Transparent Color Key相同的颜色(建议选择与模型边缘颜色相近的颜色,不然会出现较明显的毛边),将材质拖拽给TransparentWindow的Material变量。
运行之后可以看到背景颜色已经被扣掉(黑色其实是透明):
Build一下之后我们就可以看到类似桌面精灵的一个程序了:

Unity 窗体透明相关推荐

  1. delphi 窗体透明详解TransparentColorValue,窗体透明控件不透明

    关于窗体透明的做法 来自:http://blog.csdn.net/shuaihj/article/details/8610343 关于窗体透明的做法 1.在Delphi中,设置窗体的AlphaBle ...

  2. unity自发光透明shader

    unity自发光透明shader 这本来是一个glass的shader,但是我发现也可以用到自发光+透明,这两个脚本都是unity文档里面的.呵呵~~~ [html] view plaincopypr ...

  3. xp下编程实现窗体透明特效

    本文主要介绍了Windows 2000下新增的API函数SetLayeredWindowAttributes()以及通过该函数实现窗体透明特效的一般方法 摘要:本文主要介绍了Windows 2000下 ...

  4. Unity3D下实现Linux平台RTMP推流(以采集Unity窗体和声音为例)

    技术背景 随着物联网等行业的崛起,越来越多的传统行业如虚拟仿真.航天工业.工业仿真.城市规划等,对Linux下的生态构建,有了更大的期望,Linux平台下,可选的直播推拉流解决方案相对Windows和 ...

  5. au3 窗体透明及半透明

    透明效果技术要点: A: _winapi_SetLayeredWindowAttributes ()这个函数+ $WS_EX_LAYERED +$LWA_COLORKEY 实现透明效果的效果 B:_w ...

  6. 【转载】Layered Window(分层窗体,透明窗体)

    本文转载自花间醉卧<Layered Window(分层窗体,透明窗体)> // 为窗体添加 WS_EX_LAYERED 属性 , 该属性使窗体支持透明 ModifyStyleEx (0,  ...

  7. Unity 设置窗体透明

    设置窗口透明.窗口置顶.鼠标穿透    方法一. 缺点:边缘不平滑,有毛边 参考博客: 1.https://alastaira.wordpress.com/2015/06/15/creating-wi ...

  8. android 窗体透明的,黑暗度等的设置技巧

    设置透明度(这是窗体本身的透明度,非背景) 1 WindowManager.LayoutParams lp=getWindow().getAttributes(); 2 lp.alpha=0.3f; ...

  9. Unity Shader透明效果

    在实时渲染中要实现透明效果,通常会在渲染模型时控制他的透明通道.当开启透明混合后,当一个物体被渲染到屏幕上时,每个片元除了颜色值和深度值之外,它还有另一个属性--透明度.当透明度为1时表示该像素是完全 ...

最新文章

  1. 自制清理电脑里的垃圾软件
  2. Android之解析XML
  3. css3 自定义滚动条样式
  4. libevent -简单的异步IO介绍
  5. flask的ajax的csrf代码
  6. 计算机的前端总线又称为,《计算机组成原理》复习资料
  7. C++ multimap的插入
  8. python图像质量评价_OpenCV图像质量评价的SSIM算法(图像相似度)
  9. Spark-TaskSchedule和TaskScheduleImpl解释和过程
  10. 期末C语言45分能过的吗,C语言—期末小黄衫获奖感言
  11. 2017年英国机器人周:身体有问题 就找机器人
  12. Linux中vi的退出命令
  13. 升级ios13后,iPhone手机新增了截长屏功能,实用又方便
  14. 电脑未安装任何音频设备解决办法
  15. Power Designer创建概念数据模型
  16. 听说你的淘客群又做死了?来学学群维护这几招!
  17. 使用对比学习处理大规模多模态单细胞数据
  18. Cacti--cacti数据库详解
  19. canonical是什么意思
  20. CMS垃圾回收器详解

热门文章

  1. 一年工作生活总结(转)
  2. 迅雷下载百度云中的内容_在云中加速Web内容交付的最佳实践
  3. Stata:定制论文中表1-table1
  4. 在云计算时代,如何保护个人隐私?
  5. 彩虹云商城QQ快捷登录设置教程,附QQ互联分发api平台地址
  6. “扯皮”终结者,区块链帮农民工计薪水
  7. 万字详解数据治理自动化体系化实践
  8. linux 挂载nfts U盘或着移动硬盘
  9. WinRAR v5.60
  10. CVAT学习笔记 - 连续标注