6
2012
Camera Shaders
yay..took few hours just to get this working: Render a whole camera view with one shader.
It should be simple, but the documents wont give any examples: Camera.RenderWithShader
Main image is showing vertex-depth-shader seen with camera (its modified version from one of these unity replacement shaders)
Next..
– How to use camera shaders to make some more fake shadows? or ambient occlusion? or this kind of screen water effect? (with unity indie..)
(I dont know..going to play around with the camera shader a bit..)
—
Update#1
Well this is interesting, the fake river shader works quite nicely as camera shader.
And a problem..fragment shaders wont work with camera??
image#2: without camera shader & with shader
—
CameraShader.js:
#pragma strict private var cam:Camera; public var repl:Shader; function Start () { //repl = Shader.Find( "Custom/Render Depth" ); cam = Camera.main; cam.RenderWithShader(repl, "RenderType"); } function OnPostRender () { cam.RenderWithShader(repl, "RenderType"); }
Depth shader test:
// Upgrade NOTE: replaced 'glstate.matrix.mvp' with 'UNITY_MATRIX_MVP' Shader "Custom/DepthCamSurf2" { Properties { _MainTex ("Texture", 2D) = "white" {} _Amount ("Extrusion Amount", Range(-1,1)) = 0.5 } SubShader { Tags { "RenderType" = "Opaque" } Lighting Off CGPROGRAM // Upgrade NOTE: excluded shader from OpenGL ES 2.0 because it uses non-square matrices #pragma exclude_renderers gles #pragma surface surf Lambert vertex:vert #include "UnityCG.cginc" struct Input { // float2 uv_MainTex; float2 uv_MainTex; // float3 depth; float4 pos; // float3 viewDir; float3 color; float3 worldPos; // float3 worldRefl; // float3 worldNormal; // float4 screenPos; INTERNAL_DATA }; // struct v2f { // float4 pos : POSITION; // float4 color : COLOR; // }; float _Amount; void vert (inout appdata_full v, out Input o) { // v.vertex.xyz += v.normal * _Amount; o.pos = mul(UNITY_MATRIX_MVP, v.vertex); // float3 viewNormal = mul((float3x3)glstate.matrix.invtrans.modelview[0], v.normal); float z = (mul((float3x4)UNITY_MATRIX_MV, v.vertex).z+9); o.color.rgb = float3(z,z,z); //viewNormal * 0.5 + 0.5; //o.color.a = 1; //-z / _ProjectionParams.z; } sampler2D _MainTex; void surf (Input IN, inout SurfaceOutput o) { half3 col = IN.color; // could use as "scanner effect" (laser line moves across screen) if (IN.worldPos.x >= 2 && IN.worldPos.x <= 2.1) { col= float3(1,0,0); } o.Albedo = col; //tex2D (_MainTex, IN.uv_MainTex).rgb; } ENDCG } Fallback "Diffuse" }
One other test shader:
// Upgrade NOTE: replaced 'glstate.matrix.mvp' with 'UNITY_MATRIX_MVP' Shader "Custom/DepthCamSurf3" { Properties { _MainTex ("Texture", 2D) = "white" {} _Amount ("Extrusion Amount", Range(-1,1)) = 0.5 _ObjPos ("ObjPos", Vector) = (1,1,1,1) } SubShader { Tags { "RenderType" = "Opaque" } Lighting Off CGPROGRAM // Upgrade NOTE: excluded shader from OpenGL ES 2.0 because it uses non-square matrices #pragma exclude_renderers gles #pragma surface surf Lambert vertex:vert #include "UnityCG.cginc" struct Input { // float2 uv_MainTex; float2 uv_MainTex; // float3 depth; float4 pos; // float3 viewDir; float3 color; float3 worldPos; // float3 worldRefl; // float3 worldNormal; // float4 screenPos; INTERNAL_DATA }; // struct v2f { // float4 pos : POSITION; // float4 color : COLOR; // }; float _Amount; void vert (inout appdata_full v, out Input o) { // v.vertex.xyz += v.normal * _Amount; o.pos = mul(UNITY_MATRIX_MVP, v.vertex); // float3 viewNormal = mul((float3x3)glstate.matrix.invtrans.modelview[0], v.normal); float z = (mul((float3x4)UNITY_MATRIX_MV, v.vertex).z+9); o.color.rgb = float3(z,z,z); //viewNormal * 0.5 + 0.5; //o.color.a = 1; //-z / _ProjectionParams.z; // do scanning here, with screenpos? } sampler2D _MainTex; uniform float4 _ObjPos; // sampler2D input : register(s0); // ? void surf (Input IN, inout SurfaceOutput o) { half3 col = IN.color; float x1 = IN.worldPos.x; float y1 = IN.worldPos.y; float x2 = _ObjPos.x; float y2 = _ObjPos.y; int steps = 15; float dx = x2-x1; float dy = y2-y1; float xs = dx/steps; float ys = dy/steps; float x = x1; float y = y1; //half3 origcol = tex2D (_MainTex, IN.uv_MainTex).rgb*3; float pixel=0; half c = 0; // float2 screenUV = IN.screenPos.xy / IN.screenPos.w; // screenUV *= float2(8,6); for (float n= 0; n <= 15; n++) { //pixel = tex2D (_WallTex, float2(x,y)*0.1).r; pixel = IN.color.r; if (pixel>0) { c++; } x+=xs; y+=ys; } if (c>0) // we hit something { col = float3(1,0,0); }else{ col = float3(0,0,1); } // col = tex2D (input, IN.uv_MainTex).rgb; // could use as "scanner effect" (laser line moves across screen) if (IN.worldPos.x >= 2 && IN.worldPos.x <= 2.1) { col= float3(1,0,0); } o.Albedo = col; //tex2D (_MainTex, IN.uv_MainTex).rgb; } ENDCG } Fallback "Diffuse" }
Related Posts
7 Comments + Add Comment
Leave a comment
Recent posts
- [GreaseMonkey] Unity Forum Fixer
- UnityHub: Make Hub application background Translucent
- Customize SpriteShapeRenderer quality (but has issues)
- Editor tool: Copy selected gameobject’s names into clipboard as rows (for Excel)
- Editor tool: Replace string in selected gameobject’s names
- UnityHub: Enable built-in Login Dialog (no more browser login/logout issues!)
- Use TikTok-TTS in Unity (with WebRequest)
- Create Scene Thumbnail Image using OnSceneSaved & OnPreviewGUI
- webgl+javascript TTS
- Using Moonsharp (LUA) + Unity Webgl
- Using 3D gameobject prefabs with Unity Tilemap + NavMesh Surface
- Custom Unity Hub Project Template Preview Image/Video (using HTML+CSS in package description)
Recent Comments
- Vector3 maths for dummies! on
- UnityHub: Make Hub application background Translucent on
- UnityHub: Make Hub application background Translucent on
- Install Android SDK+JDK+NDK for Unity (without AndroidStudio or Unity Hub) on
- Install Android SDK+JDK+NDK for Unity (without AndroidStudio or Unity Hub) on
- [Asset Store] Point Cloud Viewer & Tools on
- [Asset Store] Point Cloud Viewer & Tools on
- ffmpeg stream raw video into Unity Texture2D on
^ 2 test depth shader sources added.
Any chance you could post how you got RenderWithShader working in the first place? I’m having a heck of a time with it.
old camera script source added, but its giving some error on 4.5..
Thanks! I’ll check it out. I ended up using a camera script from the pro package that gives a similar look to what I was going for, but I’ll check it out anyways, since I do want to figure out how to do this replace thing.
depth shader:
http://forum.unity3d.com/threads/render-depth-distance.272979/#post-1813478
[…] Render With Shader […]
[…] Render With Shader […]