slitscan with 3d texture
using System.Collections; using System.Collections.Generic; using UnityEngine; public class slitscan3d : MonoBehaviour { public RenderTexture _rt3d; public Material _mat; private WebCamTexture _tex; private int _index=0; void Start() { Application.targetFrameRate=60; WebCamDevice[] devices = WebCamTexture.devices; _tex = new WebCamTexture(devices[1].name, 960, 540, 30); _tex.Play(); var info = new RenderTextureDescriptor(); info.dimension=UnityEngine.Rendering.TextureDimension.Tex3D; info.colorFormat = RenderTextureFormat.ARGB32; info.width = 512; info.height=256; info.volumeDepth=90; info.depthBufferBits = 0; info.msaaSamples = 1; info.mipCount=0; _rt3d=new RenderTexture(info); _rt3d.name="TEST3DTEX"; _rt3d.Create(); } // Update is called once per frame void Update() { for(int i=_rt3d.volumeDepth-1;i>=1;i--){ Graphics.CopyTexture(_rt3d,i-1,_rt3d,i); } Graphics.Blit(_tex,_rt3d,0,0); _mat.SetTexture("_Volume",_rt3d); } }
Shader "VolumeRender/Tex3d" { Properties { _Volume("Volume", 3D) = "" {} _Depth("_Depth",Range(0,10)) = 0 _Brightness("_Brightness",Range(0,1)) = 1 } CGINCLUDE #include "UnityCG.cginc" struct appdata { float4 vertex : POSITION; }; struct v2f { float4 vertex : SV_POSITION; float3 uv : TEXCOORD0; }; sampler3D _Volume; float _Depth; float _GlobalBrightness; float _Brightness; v2f vert(appdata v) { v2f o; o.vertex = UnityObjectToClipPos(v.vertex); float4 wpos = mul(unity_ObjectToWorld, v.vertex); //-0 to 1 o.uv.x = wpos.x * 0.5 + 0.5; o.uv.y = 1-(wpos.y * 0.5 + 0.5); o.uv.z = wpos.z * 0.5 + 0.5; //o.uv.z = wpos.z * 0.5 + 0.5; return o; } fixed4 frag(v2f i) : SV_Target { //i.uv.z *= _Depth; //i.uv.z = saturate( i.uv.z )*0.99; return tex3D(_Volume, (i.uv) )*_Brightness; } ENDCG SubShader { Tags { "RenderType"="Opaque" } Pass { CGPROGRAM #pragma vertex vert #pragma fragment frag ENDCG } } }