Unity4.5 热扭曲Shaderunity5.3 heatdistortt报错如何解决?

unity3d shader编程中GrabPass 在某些android手机上失效的解决方案 - geoffyange - 博客园
随笔 - 6, 文章 - 0, 评论 - 0, 引用 - 0
有个著名的扭曲效果的SHADER如下所示
// Upgrade NOTE: replaced 'glstate.matrix.modelview[0]' with 'UNITY_MATRIX_MV'// Upgrade NOTE: replaced 'glstate.matrix.mvp' with 'UNITY_MATRIX_MVP'
Shader "HeatDistortion" {Properties {
_NoiseTex ("Noise Texture (RG)", 2D) = "white" {}
strength("strength", Range(0.1, 0.3)) = 0.2
transparency("transparency", Range(0.01, 0.1)) = 0.05}
Category {
Tags { "Queue" = "Transparent+10" }
SubShader {
GrabPass {
Name "BASE"
Tags { "LightMode" = "Always" }
Name "BASE"
Tags { "LightMode" = "Always" }
Fog { Color (0,0,0,0) }
Lighting Off
ZTest LEqual
Blend SrcAlpha OneMinusSrcAlpha
AlphaTest Greater 0
CGPROGRAM// Upgrade NOTE: excluded shader from DX11 and Xbox360; has structs without semantics (struct v2f members distortion)#pragma exclude_renderers d3d11 xbox360// Upgrade NOTE: excluded shader from Xbox360; has structs without semantics (struct v2f members distortion)#pragma exclude_renderers xbox360#pragma vertex vert#pragma fragment frag#pragma fragmentoption ARB_precision_hint_fastest#pragma fragmentoption ARB_fog_exp2#include "UnityCG.cginc"
sampler2D _GrabTexture : register(s0);float4 _NoiseTex_ST;sampler2D _NoiseT
struct data {
float4 vertex : POSITION;
float3 normal : NORMAL;
float4 texcoord : TEXCOORD0;};
struct v2f {
float4 position : POSITION;
float4 screenPos : TEXCOORD0;
float2 uvmain : TEXCOORD2; };
v2f vert(data i){
o.position = mul(UNITY_MATRIX_MVP, i.vertex);
// compute transformed vertex position
o.uvmain = TRANSFORM_TEX(i.texcoord, _NoiseTex);
// compute the texcoords of the noise
//float viewAngle = dot(normalize(mul((float3x3)glstate.matrix.invtrans.modelview[0], i.normal)), //
float3(0,0,1)); float viewAngle = dot(normalize(ObjSpaceViewDir(i.vertex)),
i.normal); o.distortion = viewAngle * viewA // square viewAngle to make the effect fall off stronger float depth = -mul( UNITY_MATRIX_MV, i.vertex ).z; // compute vertex depth o.distortion /= 1+
// scale effect with vertex depth o.distortion *= // multiply with user controlled strength o.screenPos = o.
// pass the position to the pixel shader }
half4 frag( v2f i ) : COLOR{
// compute the texture coordinates
float2 screenPos = i.screenPos.xy / i.screenPos.w;
// screenpos ranges from -1 to 1
screenPos.x = (screenPos.x + 1) * 0.5;
// I need 0 to 1
screenPos.y = (screenPos.y + 1) * 0.5;
// I need 0 to 1
if (_ProjectionParams.x & 0)
screenPos.y = 1 - screenPos.y;
// get two offset values by looking up the noise texture shifted in different directions
half4 offsetColor1 = tex2D(_NoiseTex, i.uvmain + _Time.xz);
half4 offsetColor2 = tex2D(_NoiseTex, i.uvmain - _Time.yx);
// use the r values from the noise texture lookups and combine them for x offset
// use the g values from the noise texture lookups and combine them for y offset
// use minus one to shift the texture back to the center
// scale with distortion amount
screenPos.x += ((offsetColor1.r + offsetColor2.r) - 1) * i.
screenPos.y += ((offsetColor1.g + offsetColor2.g) - 1) * i.
half4 col = tex2D( _GrabTexture, screenPos );
col.a = i.distortion/ }
这个SHADER是一个很好的SHADER,它可以用来做火焰的扭曲模仿效果也可以用来做地震波的效果。
做地震波的效果的方法是先做一个环形的MESH,然后将这个SHADER给MESH材质使用,然后
通过脚本改变MESH的半径从而实现地震波使地形扭曲的效果。
在IOS上当然是没有问题了,可是在某些android机上发现这个效果竟然实现不了,跟踪程序发现,原来是
在这些Aandroid机上SHADER里的GrabPass方法失效了。这可如何是好呢?
经过分析发现GrabPass无非就是采集了当前的texture而已,所以试着用CAMERA的render texture功能,结果轻松的
实现了GrabPass的功能。
首先将上面的SHADER改为如下:(具体改的地方请自己比对)
// Upgrade NOTE: replaced 'glstate.matrix.modelview[0]' with 'UNITY_MATRIX_MV'// Upgrade NOTE: replaced 'glstate.matrix.mvp' with 'UNITY_MATRIX_MVP'
Shader "HeatDistortionNew" {Properties { _MainTex ("Main Texture (RG)", 2D) = "white" {}
_NoiseTex ("Noise Texture (RG)", 2D) = "white" {}
strength("strength", Range(0.01, 0.1)) = 0.04
transparency("transparency", Range(0.01, 0.1)) = 0.01}
Category {
//Tags { "Queue" = "Transparent+10" }
Tags {"Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Opaque"}
SubShader {
// GrabPass {
Name "BASE"
Tags { "LightMode" = "Always" }
Name "BASE"
Tags { "LightMode" = "Always" }
Fog { Color (0,0,0,0) }
Lighting Off
ZTest LEqual
Blend SrcAlpha OneMinusSrcAlpha
AlphaTest Greater 0
CGPROGRAM// Upgrade NOTE: excluded shader from DX11 and Xbox360; has structs without semantics (struct v2f members distortion)#pragma exclude_renderers d3d11 xbox360// Upgrade NOTE: excluded shader from Xbox360; has structs without semantics (struct v2f members distortion)#pragma exclude_renderers xbox360#pragma vertex vert#pragma fragment frag#pragma fragmentoption ARB_precision_hint_fastest#pragma fragmentoption ARB_fog_exp2#include "UnityCG.cginc"
sampler2D _MainTex : register(s0);float4 _NoiseTex_ST;sampler2D _NoiseTuniform float _BumpAfloat4 _MainTex_TexelS
struct data {
float4 vertex : POSITION;
float3 normal : NORMAL;
float4 texcoord : TEXCOORD0;};
struct v2f {
float4 position : POSITION;
float4 screenPos : TEXCOORD0;
float2 uvmain : TEXCOORD2; };
v2f vert(data i){
o.position = mul(UNITY_MATRIX_MVP, i.vertex);
// compute transformed vertex position
o.uvmain = TRANSFORM_TEX(i.texcoord, _NoiseTex);
// compute the texcoords of the noise
//float viewAngle = dot(normalize(mul((float3x3)glstate.matrix.invtrans.modelview[0], i.normal)), //
float3(0,0,1)); float viewAngle = dot(normalize(ObjSpaceViewDir(i.vertex)),
i.normal); o.distortion = viewAngle * viewA // square viewAngle to make the effect fall off stronger float depth = -mul( UNITY_MATRIX_MV, i.vertex ).z; // compute vertex depth o.distortion /= 1+
// scale effect with vertex depth o.distortion *= // multiply with user controlled strength o.screenPos = o.
// pass the position to the pixel shader }
half4 frag( v2f i ) : COLOR{
// compute the texture coordinates
float2 screenPos = i.screenPos.xy / i.screenPos.w;
// screenpos ranges from -1 to 1
screenPos.x = (screenPos.x + 1) * 0.5;
// I need 0 to 1
screenPos.y = (screenPos.y + 1) * 0.5;
// I need 0 to 1
if (_ProjectionParams.x & 0)
screenPos.y = 1 - screenPos.y;
// get two offset values by looking up the noise texture shifted in different directions
half4 offsetColor1 = tex2D(_NoiseTex, i.uvmain + _Time.xz);
half4 offsetColor2 = tex2D(_NoiseTex, i.uvmain - _Time.yx);
// use the r values from the noise texture lookups and combine them for x offset
// use the g values from the noise texture lookups and combine them for y offset
// use minus one to shift the texture back to the center
// scale with distortion amount
screenPos.x += ((offsetColor1.r + offsetColor2.r) - 1) * i.
screenPos.y += ((offsetColor1.g + offsetColor2.g) - 1) * i.
half4 col = tex2D( _MainTex, screenPos );
col.a = i.distortion/ }
然后再写一个脚本如下:
using UnityEusing System.C
public class Heat : MonoBehaviour {
public C M public RenderT // Use this for initialization void Start () {
m = GetComponent&MeshRenderer&().
Instantiate(c, Camera.mainCamera.transform.position, Camera.mainCamera.transform.rotation); }
// Update is called once per frame void Update () {
m.SetTexture("_MainTex", t); }
注意的地方是:CAMERA当然是你的相机了,RenderTexture t 是你要绑上去的。
这个问题就得到了解决。。。。。后使用快捷导航没有帐号?
只需一步,快速开始
查看: 777|回复: 2
HeatDistort导不进法线贴图
TA的其他好贴
马上注册,加入CGJOY,享用更多功能,让你轻松玩转CGJOY。
才可以下载或查看,没有帐号?
未标题-1.jpg (55.33 KB, 下载次数: 9)
23:24 上传
版本455,已关闭DX11,法线贴图应该没问题,不知道是不是操作有误。
法线贴图放到normalmap里,如果不行应该是版本的事,见过其他同学也遇到过这个情况,也是因为4.5+版本
4.5以上版本
用某些SHADER
要关闭DX11
然后重新拖进去
法线贴图放到normalmap里,如果不行应该是版本的事,见过其他同学也遇到过这个情况,也是因为4.5+版本
4.5以上版本&&用某些SHADER& && &要关闭DX11& && && & 然后重新拖进去&&就可解决&&
感谢回答,很精彩
次时代游戏角色模型分析和欣赏
Powered by

我要回帖

更多关于 heatdistort shader 的文章

 

随机推荐