Unityshader实例01:冰块材质
简单版本
效果如下
原理
使用法线贴图扭曲透明颜色贴图的uv值
Shader 代码1
Shader"PengLu/normal/iceTrans" {
Properties {_Color ("Main Color", Color) = (1,1,1,1)_MainTex ("BaseTex ", 2D) = "white" {}_BumpMap ("Normalmap", 2D) = "bump" {}_BumpAmt ("Distortion", range (0,2)) = 0.1}SubShader {Tags { "Queue"="Transparent""RenderType"="Opaque" }ZWrite offCGPROGRAM
#pragma surface surfLambert nolightmap nodirlightmap alpha:blendsampler2D _BumpMap;sampler2D _MainTex;float4 _Color;float _BumpAmt;struct Input {float2 uv_MainTex;float2 uv_BumpMap;
};void surf (Input IN,inout SurfaceOutput o) {fixed3 nor = UnpackNormal (tex2D(_BumpMap, IN.uv_BumpMap));fixed4 trans =tex2D(_MainTex,IN.uv_MainTex+nor.xy*_BumpAmt)*_Color;o.Albedo = trans.rgb;o.Alpha =trans.a;o.Emission = trans;
}
ENDCG
}FallBack"Transparent/VertexLit"
}
升级版本
效果如下
原理
使用grabtexture 截取屏幕并扭曲uv坐标,以达到透明折射的效果。
一些热浪扭曲效果也可用此shader引申出来
shader 代码2
Shader "PengLu/Custom/iceRefrationVF" {
Properties {_Color ("Main Color", Color) = (1,1,1,1)_MainTex ("BaseTex", 2D) = "white" {}_BumpMap ("Normalmap", 2D) = "bump" {}_BumpAmt ("Distortion", range (0,1)) = 0.12}Category {// We must be transparent, so other objects are drawn before this one.Tags { "Queue"="Transparent" "RenderType"="Opaque" }zwrite offSubShader {// This pass grabs the screen behind the object into a texture.// We can access the result in the next pass as _GrabTextureGrabPass {Name "BASE"Tags { "LightMode" = "Always" }}// Main pass: Take the texture grabbed above and use the bumpmap to perturb it// on to the screenPass {Name "BASE"Tags { "LightMode" = "Always" }CGPROGRAM#pragma vertex vert#pragma fragment frag#pragma multi_compile_fog#include "UnityCG.cginc"struct appdata_t {float4 vertex : POSITION;float2 texcoord: TEXCOORD0;};struct v2f {float4 vertex : SV_POSITION;float4 uvgrab : TEXCOORD0;float2 uvbump : TEXCOORD1;float2 uvmain : TEXCOORD2;UNITY_FOG_COORDS(3)};float _BumpAmt;float4 _BumpMap_ST;float4 _MainTex_ST;v2f vert (appdata_t v){v2f o;o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);#if UNITY_UV_STARTS_AT_TOPfloat scale = -1.0;#elsefloat scale = 1.0;#endifo.uvgrab.xy = (float2(o.vertex.x, o.vertex.y*scale) + o.vertex.w) * 0.5;o.uvgrab.zw = o.vertex.zw;//以上7行可以用o.uvgrab = ComputeGrabScreenPos(o.vertex);代替o.uvbump = TRANSFORM_TEX( v.texcoord, _BumpMap );o.uvmain = TRANSFORM_TEX( v.texcoord, _MainTex );UNITY_TRANSFER_FOG(o,o.vertex);return o;}sampler2D _GrabTexture;
// float4 _GrabTexture_TexelSize;float4 _Color;sampler2D _BumpMap;sampler2D _MainTex;half4 frag (v2f i) : SV_Target{// calculate perturbed coordinateshalf2 bump = UnpackNormal(tex2D( _BumpMap, i.uvbump )).rg; // we could optimize this by just reading the x & y without reconstructing the Zfloat2 offset = bump * _BumpAmt ;i.uvgrab.xy = offset * i.uvgrab.z + i.uvgrab.xy;half4 col = tex2Dproj( _GrabTexture, UNITY_PROJ_COORD(i.uvgrab));half4 tint = tex2D(_MainTex, i.uvmain)*_Color*2;col *= tint;UNITY_APPLY_FOG(i.fogCoord, col);return col;}ENDCG}}// ------------------------------------------------------------------// Fallback for older cards and Unity non-ProSubShader {Blend DstColor ZeroPass {Name "BASE"SetTexture [_MainTex] { combine texture }}}}}
surface版本 shader代码:
Shader "PengLu/Custom/iceRefrationSurf" {
Properties {_Color ("Main Color", Color) = (1,1,1,1)_MainTex ("BaseTex", 2D) = "white" {}_BumpMap ("Normalmap", 2D) = "bump" {}_BumpAmt ("Distortion", range (0,1)) = 0.12
}SubShader {Tags { "Queue"="Transparent" "RenderType"="Opaque" }ZWrite offLighting offGrabPass { Name "BASE"Tags { "LightMode" = "Always" }}CGPROGRAM
#pragma surface surf Lambert nolightmap nodirlightmap
#pragma target 3.0
#pragma debugfloat4 _Color;
sampler2D _MainTex;
sampler2D _BumpMap;
sampler2D _GrabTexture;
float _BumpAmt;struct Input {float2 uv_MainTex;float2 uv_BumpMap;float4 screenPos;
};void surf (Input IN, inout SurfaceOutput o) {fixed3 nor = UnpackNormal (tex2D(_BumpMap, IN.uv_BumpMap));fixed4 col = tex2D(_MainTex,IN.uv_MainTex);float4 screenUV2 = IN.screenPos;screenUV2.xy = screenUV2.xy / screenUV2.w;screenUV2.xy += nor.xy * _BumpAmt;fixed4 trans = tex2D(_GrabTexture,screenUV2.xy)*_Color;trans*=col; o.Albedo = trans.rgb;o.Emission = trans.rgb;
;
}
ENDCG
}FallBack "Transparent/VertexLit"
}
surface版本目前还有问题,在编辑视图里面结果是对的,但是再camera显示不正确。暂时能力不够没找到方法修改
Unityshader实例01:冰块材质相关推荐
- 冰shader_干货 | UnityShader Demo01之冰块材质
原标题:干货 | UnityShader Demo01之冰块材质 原理--使用法线贴图扭曲透明颜色贴图的uv值. 代码如下: Shader"PengLu/normal/iceTrans&qu ...
- maya 阿诺德水晶材质_MAYA阿诺德基础材质(冰块材质)MAYA学习笔记
冰块材质是透明的,有很高的高光.有折射.有一定的焦散. 创建一个阿诺德基础材质球. 冰块材质是透明的.没有颜色,所以没有表面颜色.,在漫反射(diffuse)里把颜色(color)设置成黑色. 在高光 ...
- UnityShader实例17:屏幕特效之碎屏特效
碎屏特效 概述 在前公司,由于工作项目的原因,需要在unity实现一个类似狂野飙车8 ,撞车翻车后的碎屏效果(如下图),从图可以看出,该特效除了碎屏的效果外还有个降低饱和度的操作,接下来 ...
- maya 阿诺德水晶材质_阿诺德基础材质(冰块材质)
环境球背景取消:primary visibility 着色器:shader 环境球亮度调节:intensity FEVTE编注:更多MAYA渲染教程讨论交流和MAYA相关知识学习请进入飞特网MAYA教 ...
- UnityShader实例12:屏幕特效之马赛克(Mosaic)材质
马赛克(Mosaic)材质 概述 马赛克(Mosaic),估计是大伙平时很常见最讨厌的图片处理手段,嘿嘿,没错我说的就是"打码".好了,正经点,马赛克指现行广为使用的一种图像(视频 ...
- UnityShader实例08:溶解消融(Dissolve)材质
溶解消融(Dissolve)材质 在不少3D游戏中,角色死亡后会有一个溶解消融的特效,这个除了粒子特效以外还需要shader的配合.下面就是本例实现的一个效果,当然没有例子配合看起来是搓搓的,不过效果 ...
- UnityShader实例10:广告牌(Billboard)材质
原文链接: http://blog.csdn.net/u011047171/article/details/47255233 广告牌(Billboard)材质 Billboard概述 Billboar ...
- UnityShader实例05:Toon(卡通)材质
卡通材质 卡通着色也叫Non-photorealisticrendering非真实渲染,通常一些3D游戏用来做一些卡通风格的游戏,一般来说特点主要有两点,一是描边,二是风格化着色,表现为明暗渐变过渡为 ...
- 如何用UE4制作假透明冰块材质
冰块是大家生活中必不可少的东西,但是要在引擎中还原出冰块的质感和物理属性却是比较困难的,我这边也只是用了些简单的方法进行模拟,下面就给大家讲下制作流程.先给大家看几张近距离的图片. 较为透明的冰 透明 ...
最新文章
- 使用余弦相似度算法计算文本相似度-数学
- SQLite第八课 auth.c授权文件解析
- 虚拟机的网络连接三种形式的说明
- C# selecd,new,virtual,abstract与override
- [Emgu]判断一张图片是否在另一张图片中
- Flask-RESTful 安装
- spingboot 集成swagger2
- 计算机软考网络管理员题,2020年计算机软考网络管理员考前测试题及答案
- java购物车商品排序_Java购物车
- mysql hzpy_MySQL 实现查询汉字的拼音首字母 的字拼音
- 4、elasticsearch安装head插件
- html5 svg编辑器,HTML5 之 SVG
- 【论文阅读】Tightly Coupled 3D Lidar Inertial Odometry and Mapping
- Windows7操作系统下的修改屏幕旋转快捷键
- 计算机桌面上的声音图标没了怎么办,右下角小喇叭不见了-电脑桌面右下角有一个调整声音的小喇叭图标没有了,怎么能调出来,电? 爱问知识人...
- 华为21级程序员月薪27万,你怎么看?
- nodejs 将对象转化为query(URLSearchParams)
- excel 修改设置(将excel修改后缀名,解压缩方式)
- 解决edge浏览器无法打开pdf文件问题
- DOS窗口(控制台程序)禁用鼠标左键选择(暂停程序的功能)
热门文章
- 程序员用10分钟写了个旅游管家APP,女友用了直呼贴心
- Linux操作系统管理
- 新概念英语精讲 钟平 pdf_新概念英语500核心词汇详解(14)well
- 员工工号问题-华为OD
- 特斯拉要对车内数据服务收费了:年费100美元;百度14.43亿投资东软,又在长沙投入45辆自动驾驶出租车;途歌倒闭消失了......
- 1099:第n小的质数(信奥)
- 数据包经由路由转发时源、目的IP地址及MAC地址变化情况
- 肖申克的救赎——我们每个人都需要这样的救赎
- 路由守卫的简单使用-登录功能
- 自制腾讯视频去除水印Chrome插件