Hi guys,
I’m quite new to Unity shaders and would like to ask for your help. I didn’t know where to post my question, either here, or in Image Effects subforum, hope posting here is correct.
The question is the following. For my project I’m planning to project virtual environment onto non-planar surface (cylinder) from three beamers. With the help of additional program I obtain the UV map of the display I’m going to project onto. One map calculated for three projectors. It looks like this:
Based on this UV texture I need to distort the original texture (I believe I should render to texture from main camera), and draw the outcome to main display (or to several displays separately).
As far as I understand, I should use Graphics.Blit function, however all my attempts so far are unsuccessful. And most of the tutorials are about animated UV displacement which is too complicated for my case.
Basically, I ask for any kind of guidance or, best case, example code of how to .Blit Render Texture and specific UV texture and output the final texture either directly on the display or to another texture.
Thank you in advance.
My ugly shader made with ShaderForge
Shader "Shader Forge/Displacement" {
Properties {
_MainTex ("MainTex", 2D) = "white" {}
_Flow ("Flow", 2D) = "white" {}
}
SubShader {
Tags {
"Queue"="Geometry+1"
"RenderType"="Opaque"
}
Pass {
Name "FORWARD"
Tags {
"LightMode"="ForwardBase"
}
ZTest Always
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#define UNITY_PASS_FORWARDBASE
#define _GLOSSYENV 1
#include "UnityCG.cginc"
#include "UnityPBSLighting.cginc"
#include "UnityStandardBRDF.cginc"
#pragma multi_compile_fwdbase_fullshadows
#pragma only_renderers d3d9 d3d11 glcore gles
#pragma target 3.0
uniform sampler2D _MainTex; uniform float4 _MainTex_ST;
uniform sampler2D _Flow; uniform float4 _Flow_ST;
struct VertexInput {
float4 vertex : POSITION;
float3 normal : NORMAL;
float2 texcoord0 : TEXCOORD0;
};
struct VertexOutput {
float4 pos : SV_POSITION;
float2 uv0 : TEXCOORD0;
float4 posWorld : TEXCOORD1;
float3 normalDir : TEXCOORD2;
};
VertexOutput vert (VertexInput v) {
VertexOutput o = (VertexOutput)0;
o.uv0 = v.texcoord0;
o.normalDir = UnityObjectToWorldNormal(v.normal);
v.vertex.xyz = float3((o.uv0*float2(1.0,_ProjectionParams.r)),0.0);
o.posWorld = mul(unity_ObjectToWorld, v.vertex);
o.pos = UnityObjectToClipPos( v.vertex );
return o;
}
float4 frag(VertexOutput i) : COLOR {
i.normalDir = normalize(i.normalDir);
float3 viewDirection = normalize(_WorldSpaceCameraPos.xyz - i.posWorld.xyz);
float3 normalDirection = i.normalDir;
float3 viewReflectDirection = reflect( -viewDirection, normalDirection );
////// Lighting:
////// Emissive:
float4 _Flow_var = tex2D(_Flow,TRANSFORM_TEX(i.uv0, _Flow));
float3 node_3339 = (float3(i.uv0,0.0)+(((float3(_Flow_var.rgb.rg,0.0)*2.0)-1.0)*0.25));
float4 _MainTex_var = tex2D(_MainTex,TRANSFORM_TEX(node_3339, _MainTex));
float3 emissive = _MainTex_var.rgb;
float3 finalColor = emissive;
return fixed4(finalColor,1);
}
ENDCG
}
}
CustomEditor "ShaderForgeMaterialInspector"
}