- Home /
This question was
closed Dec 27, 2017 at 12:47 PM by
aditya for the following reason:
No one is answering it
Question by
aditya · Nov 28, 2017 at 01:33 PM ·
c#androidshadershader programmingimage effect
This shader (or Image Effect) is not working on Android
The below script/shader is not working on android device but working perfectly in editor and im not a shader expert so i couldn't even find any clue what's going wrong. Please help
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DisplacementBehaviour : MonoBehaviour
{
private RenderTexture _screenTex;
private RenderTexture _waterMaskTex;
public Material _mat;
public Texture _displacementTex;
public Texture _waterTex;
public float _turbulence = 1f;
public float _scrollOffset = 0f;
private GameObject _postRenderCamObj;
private Camera _postRenderCam;
private Camera _screenCam;
void Awake()
{
_screenTex = new RenderTexture(Screen.width, Screen.height, 0, RenderTextureFormat.Default);
//_screenTex.wrapMode = TextureWrapMode.Repeat;
_waterMaskTex = new RenderTexture(Screen.width / 4, Screen.height / 4, 0, RenderTextureFormat.Default);
//_waterMaskTex.wrapMode = TextureWrapMode.Repeat;
_screenCam = GetComponent<Camera>();
_screenCam.SetTargetBuffers(_screenTex.colorBuffer, _screenTex.depthBuffer);
CreatePostRenderCam();
}
void OnPostRender()
{
_postRenderCam.CopyFrom(_screenCam);
_postRenderCam.clearFlags = CameraClearFlags.SolidColor;
_postRenderCam.backgroundColor = Color.black;
_postRenderCam.cullingMask = 1 << LayerMask.NameToLayer("Water");
_postRenderCam.targetTexture = _waterMaskTex;
_postRenderCam.Render();
_mat.SetTexture("_MaskTex", _waterMaskTex);
_mat.SetTexture("_DisplacementTex", _displacementTex);
_mat.SetTexture("_WaterTex", _waterTex);
_mat.SetFloat("_Turbulence", _turbulence);
_mat.SetFloat("_ScrollOffset", _scrollOffset);
Graphics.Blit(_screenTex, null, _mat);
}
private void CreatePostRenderCam()
{
_postRenderCamObj = new GameObject("PostRenderCam");
_postRenderCamObj.transform.position = transform.position;
_postRenderCamObj.transform.rotation = transform.rotation;
_postRenderCam = _postRenderCamObj.AddComponent<Camera>();
_postRenderCam.enabled = false;
}
}
And here is the shader
Shader "Unlit/DisplacementShader"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
_DisplacementTex ("DisplacementTex", 2D) = "white" {}
_WaterTex ("WaterTex", 2D) = "white" {}
_MaskTex ("MaskTex", 2D) = "white" {}
_BaseHeight ("BaseHeight", float) = 0.4
_Turbulence ("Turbulence", float) = 1
_ScrollOffset ("ScrollOffset", float) = 0
}
SubShader
{
Tags { "RenderType"="Transparent" }
LOD 100
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};
struct v2f
{
float2 uv : TEXCOORD0;
float4 vertex : SV_POSITION;
};
sampler2D _MainTex;
sampler2D _DisplacementTex;
sampler2D _WaterTex;
sampler2D _MaskTex;
float4 _MainTex_ST;
fixed4 _MainTex_TexelSize;
float _BaseHeight;
fixed _Turbulence;
fixed _ScrollOffset;
v2f vert (appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = TRANSFORM_TEX(v.uv, _MainTex);
return o;
}
float wave(float x) {
fixed waveOffset = cos((x - _Time + _ScrollOffset) * 60) * 0.004
+ cos((x - 2 * _Time + _ScrollOffset) * 20) * 0.008
+ sin((x + 2 * _Time + _ScrollOffset) * 35) * 0.01
+ cos((x + 4 * _Time + _ScrollOffset) * 70) * 0.001;
return _BaseHeight * _Turbulence;
}
fixed4 frag (v2f i) : SV_Target
{
fixed4 waterCol = tex2D(_WaterTex, i.uv);
fixed maskValue = tex2D(_MaskTex, i.uv).r;
float waveHeight = wave(i.uv.x);
fixed isTexelAbove = step(waveHeight, i.uv.y);
fixed isTexelBelow = 1 - isTexelAbove;
fixed2 disPos = i.uv;
disPos.x += (_Time[0]) % 2;
disPos.y += (_Time[0]) % 2;
fixed4 dis = tex2D(_DisplacementTex, disPos);
i.uv.x += dis * 0.006 * isTexelBelow * maskValue * _Turbulence;
i.uv.y += dis * 0.006 * isTexelBelow * maskValue * _Turbulence;
fixed4 col = tex2D(_MainTex, fixed2(i.uv.x, i.uv.y));
fixed waterColBlendFac = maskValue * isTexelBelow * 0.5;
col.r = lerp(col.r, waterCol.r, waterColBlendFac);
col.g = lerp(col.g, waterCol.g, waterColBlendFac);
col.b = lerp(col.b, waterCol.b, waterColBlendFac);
float topDist = abs(i.uv.y - waveHeight);
fixed isNearTop = 1 - step(abs(_MainTex_TexelSize.y * 6), topDist);
fixed isVeryNearTop = 1 - step(abs(_MainTex_TexelSize.y * 3), topDist);
fixed topColorBlendFac = isNearTop * isTexelBelow * maskValue;
col.r = lerp(col.r, waterCol.r * 0.83, topColorBlendFac);
col.g = lerp(col.g, waterCol.g * 0.83, topColorBlendFac);
col.b = lerp(col.b, waterCol.b * 0.83, topColorBlendFac);
col.r += 0.2 * isVeryNearTop * isTexelBelow * maskValue;
col.g += 0.2 * isVeryNearTop * isTexelBelow * maskValue;
col.b += 0.2 * isVeryNearTop * isTexelBelow * maskValue;
return col;
}
ENDCG
}
}
}
Comment
Follow this Question
Related Questions
fog shader not working in android platform 0 Answers
How to detect if the Device can run the shader ? 0 Answers
Multiple Cars not working 1 Answer
Multiple Vectors in shader/CGINC 0 Answers