Commit d7390b0d authored by ATREVI Dieudonne's avatar ATREVI Dieudonne
Browse files

Merge dev in local branch

parents aefd08b2 8f4aa4aa
using UnityEngine;
using UnityEngine.Rendering;
using System.Collections;
using System.IO;
public abstract class Filter
{
// configuration
public string name;
public int quality;
public bool supportsAntialiasing;
public bool needsRescale;
public int width=1920;
public int height=1080;
// impl
public Shader filter;
public Camera camera;
public abstract void initParams();
protected void createCam(Camera mainCam)
{
GameObject go = new GameObject(name, typeof(Camera));
//go.hideFlags = HideFlags.HideAndDontSave;
go.transform.parent = mainCam.transform;
camera = go.GetComponent<Camera>();
}
public virtual void onCameraChange(Camera main, int display)
{
// cleanup capturing camera
camera.RemoveAllCommandBuffers();
// copy all "main" camera parameters into capturing camera
camera.CopyFrom(main);
camera.targetDisplay = display;
camera.SetReplacementShader(filter, "");
camera.backgroundColor = Color.black;
camera.clearFlags = CameraClearFlags.SolidColor;
camera.allowHDR = false;
camera.allowMSAA = false;
}
public virtual void OnSceneChange(Renderer r, ref MaterialPropertyBlock mpb)
{
}
}
public class FilterSegmentation : Filter
{
public FilterSegmentation(Camera mainCam)
{
// configuration
name = "Segmentation";
supportsAntialiasing = false;
needsRescale = false;
// impl
filter = Shader.Find("Filter/Filter_Segmentation");
createCam(mainCam);
}
public override void initParams()
{
quality = ConfigReader.imgSegmentation.quality;
width = ConfigReader.imgSegmentation.width;
height = ConfigReader.imgSegmentation.height;
}
public override void OnSceneChange(Renderer r, ref MaterialPropertyBlock mpb)
{
var id = r.gameObject.GetInstanceID();
mpb.SetColor("_ObjectColor", ColorEncoding.EncodeIDAsColor(id));
}
}
public class FilterCategory : Filter
{
public FilterCategory(Camera mainCam)
{
// configuration
name = "Categories";
supportsAntialiasing = false;
needsRescale = false;
// impl
filter = Shader.Find("Filter/Filter_Category");
createCam(mainCam);
}
public override void initParams()
{
quality = ConfigReader.imgCategories.quality;
width = ConfigReader.imgCategories.width;
height = ConfigReader.imgCategories.height;
}
public override void OnSceneChange(Renderer r, ref MaterialPropertyBlock mpb)
{
var layer = r.gameObject.layer;
//var tag = r.gameObject.tag;
mpb.SetColor("_CategoryColor", ColorEncoding.EncodeLayerAsColor(layer));
}
}
public class FilterDepth : Filter
{
public float maxDist = 50;
public float minDist = 0;
public float exponent = 1;
public FilterDepth(Camera mainCam, float maxRepresentedDist=50f)
{
// configuration
name = "Depth";
supportsAntialiasing = true;
needsRescale = false;
maxDist = maxRepresentedDist;
// impl
filter = Shader.Find("Filter/Filter_Depth");
createCam(mainCam);
}
public override void initParams()
{
quality = ConfigReader.imgDepth.quality;
width = ConfigReader.imgDepth.width;
height = ConfigReader.imgDepth.height;
maxDist = ConfigReader.imgDepth.maxDepth;
minDist = ConfigReader.imgDepth.minDepth;
exponent = ConfigReader.imgDepth.exponent;
}
public override void OnSceneChange(Renderer r, ref MaterialPropertyBlock mpb)
{
var layer = r.gameObject.layer;
//var tag = r.gameObject.tag;
mpb.SetFloat("_maxDist", maxDist);
mpb.SetFloat("_minDist", minDist);
mpb.SetFloat("_exponent", exponent);
}
}
public class FilterNormal : Filter
{
public FilterNormal(Camera mainCam)
{
// configuration
name = "Normals";
supportsAntialiasing = true;
needsRescale = false;
// impl
filter = Shader.Find("Filter/Filter_Normal");
createCam(mainCam);
}
public override void initParams()
{
quality = ConfigReader.imgNormals.quality;
width = ConfigReader.imgNormals.width;
height = ConfigReader.imgNormals.height;
}
}
public class FilterOpticalFlow : Filter
{
public bool motionVector;
int ncols = 0;
Color[] colorwheel;
private Material mat;
float sensitivity = 20;
public FilterOpticalFlow(Camera mainCam)
{
// configuration
name = "OpticalFlow";
supportsAntialiasing = false;
needsRescale = true;
// impl
filter = Shader.Find("Filter/Filter_OpticalFlow");
createCam(mainCam);
}
public override void initParams()
{
quality = ConfigReader.imgOpticalFlow.quality;
width = ConfigReader.imgOpticalFlow.width;
height = ConfigReader.imgOpticalFlow.height;
motionVector = ConfigReader.imgOpticalFlow.motionVector;
if (motionVector && !(quality == 16 || quality == 32))
quality = 16;
}
public override void onCameraChange(Camera main, int display)
{
camera.RemoveAllCommandBuffers();
camera.CopyFrom(main);
camera.targetDisplay = display;
camera.backgroundColor = Color.white;
// cache materials and setup material properties
if (!mat)
mat = new Material(filter);
if (ncols==0)
makecolorwheel();
mat.SetFloat("_Sensitivity", sensitivity);
mat.SetColorArray("_ColorWheel", colorwheel);
mat.SetInt("_ncols", ncols);
mat.SetInt("_MotionVector", motionVector ? 1 : 0);
var cb = new CommandBuffer();
cb.Blit(null, BuiltinRenderTextureType.CurrentActive, mat);
camera.AddCommandBuffer(CameraEvent.AfterEverything, cb);
camera.depthTextureMode = DepthTextureMode.Depth | DepthTextureMode.MotionVectors;
}
// Color wheel
void setcols(int r, int g, int b, int k)
{
colorwheel[k] = new Color(r / 255.0f, g / 255.0f, b / 255.0f);
}
void makecolorwheel()
{
// relative lengths of color transitions:
// these are chosen based on perceptual similarity
// (e.g. one can distinguish more shades between red and yellow
// than between yellow and green)
int RY = 15;
int YG = 6;
int GC = 4;
int CB = 11;
int BM = 13;
int MR = 6;
ncols = RY + YG + GC + CB + BM + MR;
//printf("ncols = %d\n", ncols);
colorwheel = new Color[ncols];
int i;
int k = 0;
for (i = 0; i < RY; i++) setcols(255, 255 * i / RY, 0, k++);
for (i = 0; i < YG; i++) setcols(255 - 255 * i / YG, 255, 0, k++);
for (i = 0; i < GC; i++) setcols(0, 255, 255 * i / GC, k++);
for (i = 0; i < CB; i++) setcols(0, 255 - 255 * i / CB, 255, k++);
for (i = 0; i < BM; i++) setcols(255 * i / BM, 0, 255, k++);
for (i = 0; i < MR; i++) setcols(255, 0, 255 - 255 * i / MR, k++);
}
}
fileFormatVersion: 2
guid: 9552886a9081bc94bb817478c87490aa
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
Shader "Filter/Filter_Category"
{
Properties
{
_CategoryColor("Catergory Color", Color) = (0,1,0,1)
}
SubShader{
Tags { "RenderType" = "Opaque" }
Pass {
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct v2f {
float4 pos : SV_POSITION;
UNITY_VERTEX_OUTPUT_STEREO
};
v2f vert(appdata_base v) {
v2f o;
UNITY_SETUP_INSTANCE_ID(v);
UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);
o.pos = UnityObjectToClipPos(v.vertex);
return o;
}
fixed4 _CategoryColor;
float4 frag(v2f i) : SV_Target {
return _CategoryColor;
}
ENDCG
}
}
}
fileFormatVersion: 2
guid: 04526d96f25f26a419a00309ac118f38
guid: 05ae47ba22a73ab419227593e6f001e3
ShaderImporter:
externalObjects: {}
defaultTextures: []
......
Shader "Filter/Filter_Depth"
{
Properties
{
}
SubShader{
Tags { "RenderType" = "Opaque" }
Pass {
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct v2f {
float4 pos : SV_POSITION;
float nz : TEXCOORD0;
float4 worldPos : TEXCOORD2;
UNITY_VERTEX_OUTPUT_STEREO
};
v2f vert(appdata_base v) {
v2f o;
UNITY_SETUP_INSTANCE_ID(v);
UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);
o.pos = UnityObjectToClipPos(v.vertex);
o.nz = -(UnityObjectToViewPos(v.vertex).z);
o.worldPos = mul(unity_ObjectToWorld, v.vertex);
return o;
}
uniform float _maxDist;
uniform float _minDist;
uniform float _exponent;
float4 frag(v2f i) : SV_Target {
float depth = distance(i.worldPos, _WorldSpaceCameraPos) - _minDist;
if (depth < 0)
depth = 0;
float ratio = _maxDist - _minDist;
if (ratio <= 0)
ratio = 1;
depth = pow(depth / ratio, _exponent);
return float4(depth, depth, depth, 1);
}
ENDCG
}
}
}
fileFormatVersion: 2
guid: 2130d252792e06143b4e215535ea2266
guid: f477594abdf5afc4cb3ff367ba41c7e4
ShaderImporter:
externalObjects: {}
defaultTextures: []
......
Shader "Filter/Filter_Normal"
{
Properties
{
_MainTex("", 2D) = "white" {}
}
SubShader{
Tags { "RenderType" = "Opaque" }
Pass {
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct v2f {
float4 pos : SV_POSITION;
float3 nz : TEXCOORD0;
UNITY_VERTEX_OUTPUT_STEREO
};
v2f vert(appdata_base v) {
v2f o;
UNITY_SETUP_INSTANCE_ID(v);
UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);
o.pos = UnityObjectToClipPos(v.vertex);
o.nz = COMPUTE_VIEW_NORMAL;
return o;
}
float4 frag(v2f i) : SV_Target {
float3 c = i.nz * 0.5 + 0.5;
return fixed4(c, 1);
}
ENDCG
}
}
}
fileFormatVersion: 2
guid: 2480f17a0acef3540bfab518b1cbf79a
ShaderImporter:
externalObjects: {}
defaultTextures: []
nonModifiableTextures: []
userData:
assetBundleName:
assetBundleVariant:
Shader "Filter/Filter_OpticalFlow"
{
Properties
{
_Sensitivity("Sensitivity", Float) = 1
}
SubShader
{
// No culling or depth
Cull Off ZWrite Off ZTest Always
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;
};
float4 _CameraMotionVectorsTexture_ST;
v2f vert(appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = TRANSFORM_TEX(v.uv, _CameraMotionVectorsTexture);
return o;
}
sampler2D _CameraMotionVectorsTexture;
uniform float4 _ColorWheel[60];
uniform uint _ncols;
float _Sensitivity;
uniform uint _MotionVector;
float4 frag(v2f i) : SV_Target
{
float2 motion = tex2D(_CameraMotionVectorsTexture, i.uv).rg;
motion.x = motion.x * _ScreenParams.x;
motion.y = motion.y * _ScreenParams.y;
if (_MotionVector>0)
return float4(motion.x, motion.y, 0, 1);
float rad = sqrt(motion.x*motion.x + motion.y*motion.y);
float a = atan2(-motion.y, -motion.x) / UNITY_PI;
float fk = (a + 1.0) / 2.0 * (_ncols - 1);
uint k0 = (int)fk;
uint k1 = (k0 + 1) % _ncols;
float f = fk - k0;
float4 c0 = _ColorWheel[k0];
float4 c1 = _ColorWheel[k1];
float4 col = (1 - f) * c0 + f * c1;
//rad /= 10;
//rad = pow(rad, 0.25);
if (rad <= 1)
col = 1 - rad * (1 - col); // increase saturation with radius
else
col *= .75; // out of range
return float4(col.z, col.y, col.x, 1);
}
ENDCG
}
}
}
fileFormatVersion: 2
guid: e1bc5ae27f9bc20498101412fd0f2563
ShaderImporter:
externalObjects: {}
defaultTextures: []
nonModifiableTextures: []
userData:
assetBundleName:
assetBundleVariant:
Shader "Filter/Filter_Segmentation"
{
Properties
{
_ObjectColor("Object Color", Color) = (1,1,1,1)
}
SubShader{
Tags { "RenderType" = "Opaque" }
Pass {
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct v2f {
float4 pos : SV_POSITION;
UNITY_VERTEX_OUTPUT_STEREO
};
v2f vert(appdata_base v) {
v2f o;
UNITY_SETUP_INSTANCE_ID(v);
UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);
o.pos = UnityObjectToClipPos(v.vertex);
return o;
}
fixed4 _ObjectColor;
float4 frag(v2f i) : SV_Target {
return _ObjectColor;
}
ENDCG
}
}
}
fileFormatVersion: 2
guid: 20082856a641f9641826ad5c55116382
ShaderImporter:
externalObjects: {}
defaultTextures: []
nonModifiableTextures: []
userData:
assetBundleName:
assetBundleVariant:
......@@ -18,10 +18,6 @@ using System.IO;
[RequireComponent(typeof(Camera))]
public class FiltersControl : MonoBehaviour
{
[Header("Shader Setup")]
public Shader learningFiltersShader;
public Shader opticalFlowShader;
public float opticalFlowSensitivity = 1.0f;
[Header("Save Image Capture")]
public bool saveImage = false;
......@@ -36,67 +32,83 @@ public class FiltersControl : MonoBehaviour
private int currentCam = 0;
// pass configuration
private CapturePass[] capturePasses = new CapturePass[] {
new CapturePass() { name = "Images" },
new CapturePass() { name = "Segmentation", supportsAntialiasing = false },
new CapturePass() { name = "Categories", supportsAntialiasing = false },
new CapturePass() { name