MaskCamera & MaskUV

- MaskUV shader graph node.
- Mask Camera as controller.
This commit is contained in:
max 2021-03-03 12:51:54 +01:00
parent e400a62ed9
commit a028e600f5
14 changed files with 1329 additions and 340 deletions

View File

@ -1,3 +1,4 @@
using System.Collections.Generic;
using UnityEngine;
namespace TAO.InteractiveMask
@ -5,14 +6,19 @@ namespace TAO.InteractiveMask
[RequireComponent(typeof(MaskRenderer))]
public class MaskCamera : MonoBehaviour
{
public Mode mode = Mode.EachFrame;
[SerializeField]
private Camera maskCamera = null;
private MaskRenderer maskRenderer = null;
public RenderTexture target = null;
private RenderTexture source = null;
public List<Material> materials = new List<Material>();
private int targetWidth = 0;
private void Awake()
{
maskRenderer = GetComponent<MaskRenderer>();
@ -23,11 +29,49 @@ namespace TAO.InteractiveMask
maskRenderer.target = target;
maskRenderer.source = source;
targetWidth = target.width;
maskRenderer.Init();
}
private void Start()
private void Update()
{
maskCamera.enabled = true;
if (mode == Mode.EachFrame)
{
Render();
}
}
public void Render()
{
SnapCameraPosition();
maskCamera.Render();
maskRenderer.Render();
foreach (var m in materials)
{
m.SetVector("_MaskData", new Vector4(maskCamera.transform.position.x, maskCamera.transform.position.z, maskCamera.orthographicSize, 0));
}
}
private void SnapCameraPosition()
{
float pws = (1.0f / targetWidth) * maskCamera.orthographicSize;
// Snap position to pixel.
Vector3 newPos = Vector3.zero;
newPos.x = (Mathf.Floor(maskCamera.transform.position.x / pws) + 0.5f) * pws;
newPos.y = maskCamera.transform.position.y;
newPos.z = (Mathf.Floor(maskCamera.transform.position.z / pws) + 0.5f) * pws;
maskCamera.transform.position = newPos;
}
public enum Mode
{
EachFrame,
Manual
}
}
}

View File

@ -4,8 +4,6 @@ namespace TAO.InteractiveMask
{
public class MaskRenderer : MonoBehaviour
{
public Mode mode = Mode.EachFrame;
public Shader clearBlit = null;
private Material clearBlitMaterial;
// Camera source.
@ -19,7 +17,7 @@ namespace TAO.InteractiveMask
public bool debugGui = false;
public void Awake()
public void Init()
{
clearBlitMaterial = new Material(clearBlit);
@ -29,14 +27,6 @@ namespace TAO.InteractiveMask
}
}
private void Update()
{
if (mode == Mode.EachFrame)
{
Render();
}
}
public void Render()
{
Graphics.Blit(target, target, clearBlitMaterial);
@ -107,12 +97,6 @@ namespace TAO.InteractiveMask
GUILayout.Label(string.Format("{0}\n{1}x{2}\n{3}", texture.name, texture.width, texture.height, texture.graphicsFormat));
}
}
public enum Mode
{
EachFrame,
Manual
}
}
[System.Serializable]

View File

@ -3,7 +3,10 @@ guid: c7ace5587b33c154490f78c11ec9a297
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
defaultReferences:
- clearBlit: {fileID: 4800000, guid: 612652bd41e196b408fe733989b1d223, type: 3}
- source: {instanceID: 0}
- target: {instanceID: 0}
executionOrder: 0
icon: {instanceID: 0}
userData:

View File

@ -1,8 +0,0 @@
fileFormatVersion: 2
guid: 32397a0d798be9c498e9b59fe048be10
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -1,190 +0,0 @@
%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!1 &354132074049328695
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 354132074049328699}
- component: {fileID: 354132074049328698}
- component: {fileID: 354132074049328697}
m_Layer: 6
m_Name: MaskRenderer
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!4 &354132074049328699
Transform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 354132074049328695}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children:
- {fileID: 354132074141824353}
m_Father: {fileID: 0}
m_RootOrder: 0
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!114 &354132074049328698
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 354132074049328695}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 50ab359e83953ab4590f3bdba570d4cf, type: 3}
m_Name:
m_EditorClassIdentifier:
maskCamera: {fileID: 354132074141824159}
target: {fileID: 8400000, guid: 6dbf8770b857bc143a3ebf8ac17ff43a, type: 2}
--- !u!114 &354132074049328697
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 354132074049328695}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: c7ace5587b33c154490f78c11ec9a297, type: 3}
m_Name:
m_EditorClassIdentifier:
mode: 0
clearBlit: {fileID: 4800000, guid: 612652bd41e196b408fe733989b1d223, type: 3}
source: {fileID: 0}
target: {fileID: 8400000, guid: 6dbf8770b857bc143a3ebf8ac17ff43a, type: 2}
layers:
- type: 0
mask:
r: 0
g: 1
b: 0
a: 1
blit: {fileID: 4800000, guid: b56b79d9c9a85fb45bf1a263d73bd938, type: 3}
persistentBlit: {fileID: 4800000, guid: 794c349d1ba10d34c86b6dd5c2b8bb7e, type: 3}
- type: 1
mask:
r: 1
g: 0
b: 0
a: 0
blit: {fileID: 4800000, guid: b56b79d9c9a85fb45bf1a263d73bd938, type: 3}
persistentBlit: {fileID: 4800000, guid: 794c349d1ba10d34c86b6dd5c2b8bb7e, type: 3}
debugGui: 1
--- !u!1 &354132074141824156
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 354132074141824353}
- component: {fileID: 354132074141824159}
- component: {fileID: 354132074141824158}
m_Layer: 6
m_Name: MaskCamera
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!4 &354132074141824353
Transform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 354132074141824156}
m_LocalRotation: {x: 0.7071068, y: 0, z: 0, w: 0.7071068}
m_LocalPosition: {x: 0, y: 100, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 354132074049328699}
m_RootOrder: 0
m_LocalEulerAnglesHint: {x: 90, y: 0, z: 0}
--- !u!20 &354132074141824159
Camera:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 354132074141824156}
m_Enabled: 0
serializedVersion: 2
m_ClearFlags: 2
m_BackGroundColor: {r: 0, g: 0, b: 0, a: 0}
m_projectionMatrixMode: 1
m_GateFitMode: 2
m_FOVAxisMode: 0
m_SensorSize: {x: 36, y: 24}
m_LensShift: {x: 0, y: 0}
m_FocalLength: 50
m_NormalizedViewPortRect:
serializedVersion: 2
x: 0
y: 0
width: 1
height: 1
near clip plane: 0.3
far clip plane: 1000
field of view: 60
orthographic: 1
orthographic size: 10
m_Depth: 0
m_CullingMask:
serializedVersion: 2
m_Bits: 64
m_RenderingPath: -1
m_TargetTexture: {fileID: 0}
m_TargetDisplay: 0
m_TargetEye: 3
m_HDR: 0
m_AllowMSAA: 0
m_AllowDynamicResolution: 0
m_ForceIntoRT: 0
m_OcclusionCulling: 1
m_StereoConvergence: 10
m_StereoSeparation: 0.022
--- !u!114 &354132074141824158
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 354132074141824156}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: a79441f348de89743a2939f4d699eac1, type: 3}
m_Name:
m_EditorClassIdentifier:
m_RenderShadows: 0
m_RequiresDepthTextureOption: 2
m_RequiresOpaqueTextureOption: 2
m_CameraType: 0
m_Cameras: []
m_RendererIndex: 1
m_VolumeLayerMask:
serializedVersion: 2
m_Bits: 64
m_VolumeTrigger: {fileID: 0}
m_RenderPostProcessing: 0
m_Antialiasing: 0
m_AntialiasingQuality: 2
m_StopNaN: 0
m_Dithering: 0
m_ClearDepth: 1
m_AllowXRRendering: 1
m_RequiresDepthTexture: 0
m_RequiresColorTexture: 0
m_Version: 2

View File

@ -1,7 +0,0 @@
fileFormatVersion: 2
guid: 16a45af4d26459143a161c4f419e17b3
PrefabImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -1,8 +0,0 @@
fileFormatVersion: 2
guid: 2efbe7ce63090b44e83e9a0f0f959020
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -1,44 +0,0 @@
%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!114 &11400000
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 0}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: de640fe3d0db1804a85f9fc8f5cadab6, type: 3}
m_Name: MaskRenderer
m_EditorClassIdentifier:
m_RendererFeatures: []
m_RendererFeatureMap:
postProcessData: {fileID: 11400000, guid: 41439944d30ece34e96484bdb6645b55, type: 2}
xrSystemData: {fileID: 11400000, guid: 60e1133243b97e347b653163a8c01b64, type: 2}
shaders:
blitPS: {fileID: 4800000, guid: c17132b1f77d20942aa75f8429c0f8bc, type: 3}
copyDepthPS: {fileID: 4800000, guid: d6dae50ee9e1bfa4db75f19f99355220, type: 3}
screenSpaceShadowPS: {fileID: 4800000, guid: 0f854b35a0cf61a429bd5dcfea30eddd, type: 3}
samplingPS: {fileID: 4800000, guid: 04c410c9937594faa893a11dceb85f7e, type: 3}
tileDepthInfoPS: {fileID: 0}
tileDeferredPS: {fileID: 0}
stencilDeferredPS: {fileID: 4800000, guid: e9155b26e1bc55942a41e518703fe304, type: 3}
fallbackErrorPS: {fileID: 4800000, guid: e6e9a19c3678ded42a3bc431ebef7dbd, type: 3}
materialErrorPS: {fileID: 4800000, guid: 5fd9a8feb75a4b5894c241777f519d4e, type: 3}
m_OpaqueLayerMask:
serializedVersion: 2
m_Bits: 64
m_TransparentLayerMask:
serializedVersion: 2
m_Bits: 64
m_DefaultStencilState:
overrideStencilState: 0
stencilReference: 0
stencilCompareFunction: 8
passOperation: 0
failOperation: 0
zFailOperation: 0
m_ShadowTransparentReceive: 0
m_RenderingMode: 0
m_AccurateGbufferNormals: 0

View File

@ -1,8 +0,0 @@
fileFormatVersion: 2
guid: de7c7563db809844baabc245710725ba
NativeFormatImporter:
externalObjects: {}
mainObjectFileID: 0
userData:
assetBundleName:
assetBundleVariant:

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,10 @@
fileFormatVersion: 2
guid: 1ee90bece743c604ab9cb277623d374e
ScriptedImporter:
internalIDToNameTable: []
externalObjects: {}
serializedVersion: 2
userData:
assetBundleName:
assetBundleVariant:
script: {fileID: 11500000, guid: 60072b568d64c40a485e0fc55012dc9f, type: 3}

View File

@ -1,8 +0,0 @@
fileFormatVersion: 2
guid: 09c1b73271dc0944dad462995cae5ca7
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -1,38 +0,0 @@
%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!84 &8400000
RenderTexture:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_Name: Target
m_ImageContentsHash:
serializedVersion: 2
Hash: 00000000000000000000000000000000
m_ForcedFallbackFormat: 4
m_DownscaleFallback: 0
m_IsAlphaChannelOptional: 0
serializedVersion: 3
m_Width: 256
m_Height: 256
m_AntiAliasing: 1
m_MipCount: -1
m_DepthFormat: 0
m_ColorFormat: 8
m_MipMap: 0
m_GenerateMips: 1
m_SRGB: 0
m_UseDynamicScale: 0
m_BindMS: 0
m_EnableCompatibleFormat: 1
m_TextureSettings:
serializedVersion: 2
m_FilterMode: 1
m_Aniso: 0
m_MipBias: 0
m_WrapU: 1
m_WrapV: 1
m_WrapW: 1
m_Dimension: 2
m_VolumeDepth: 1

View File

@ -1,8 +0,0 @@
fileFormatVersion: 2
guid: 6dbf8770b857bc143a3ebf8ac17ff43a
NativeFormatImporter:
externalObjects: {}
mainObjectFileID: 8400000
userData:
assetBundleName:
assetBundleVariant: