Skip to content

Commit

Permalink
Merge pull request #317 from asus4/bg-texture-builtinrp
Browse files Browse the repository at this point in the history
Support built-in render pipeline in CameraTextureBackgorund component
  • Loading branch information
asus4 committed Sep 11, 2023
2 parents 4881a03 + 9c3958e commit df752d9
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 13 deletions.
70 changes: 57 additions & 13 deletions Assets/Samples/SSD/SSD.unity
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ RenderSettings:
m_ReflectionIntensity: 1
m_CustomReflection: {fileID: 0}
m_Sun: {fileID: 0}
m_IndirectSpecularColor: {r: 0.37311915, g: 0.3807396, b: 0.35872662, a: 1}
m_IndirectSpecularColor: {r: 0.3708708, g: 0.37838137, b: 0.35725543, a: 1}
m_UseRadianceAmbientProbe: 0
--- !u!157 &3
LightmapSettings:
Expand Down Expand Up @@ -104,7 +104,7 @@ NavMeshSettings:
serializedVersion: 2
m_ObjectHideFlags: 0
m_BuildSettings:
serializedVersion: 2
serializedVersion: 3
agentTypeID: 0
agentRadius: 0.5
agentHeight: 2
Expand All @@ -117,7 +117,7 @@ NavMeshSettings:
cellSize: 0.16666667
manualTileSize: 0
tileSize: 256
accuratePlacement: 0
buildHeightMesh: 0
maxJobWorkers: 0
preserveTilesOutsideBounds: 0
debug:
Expand Down Expand Up @@ -165,9 +165,17 @@ Camera:
m_projectionMatrixMode: 1
m_GateFitMode: 2
m_FOVAxisMode: 0
m_Iso: 200
m_ShutterSpeed: 0.005
m_Aperture: 16
m_FocusDistance: 10
m_FocalLength: 50
m_BladeCount: 5
m_Curvature: {x: 2, y: 11}
m_BarrelClipping: 0.25
m_Anamorphism: 0
m_SensorSize: {x: 36, y: 24}
m_LensShift: {x: 0, y: 0}
m_FocalLength: 50
m_NormalizedViewPortRect:
serializedVersion: 2
x: 0
Expand Down Expand Up @@ -201,12 +209,13 @@ Transform:
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 32141214}
serializedVersion: 2
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 1, z: -10}
m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0
m_Children: []
m_Father: {fileID: 0}
m_RootOrder: 0
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!114 &32141218
MonoBehaviour:
Expand Down Expand Up @@ -252,9 +261,20 @@ MonoBehaviour:
m_Dithering: 0
m_ClearDepth: 1
m_AllowXRRendering: 1
m_AllowHDROutput: 1
m_UseScreenCoordOverride: 0
m_ScreenSizeOverride: {x: 0, y: 0, z: 0, w: 0}
m_ScreenCoordScaleBias: {x: 0, y: 0, z: 0, w: 0}
m_RequiresDepthTexture: 0
m_RequiresColorTexture: 0
m_Version: 2
m_TaaSettings:
quality: 3
frameInfluence: 0.1
jitterScale: 1
mipBias: 0
varianceClampScale: 0.9
contrastAdaptiveSharpening: 0
--- !u!1 &780088502
GameObject:
m_ObjectHideFlags: 0
Expand Down Expand Up @@ -285,6 +305,7 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: 4f231c4fb786f3946a6b90b886c48677, type: 3}
m_Name:
m_EditorClassIdentifier:
m_SendPointerHoverToParent: 1
m_HorizontalAxis: Horizontal
m_VerticalAxis: Vertical
m_SubmitButton: Submit
Expand Down Expand Up @@ -314,12 +335,13 @@ Transform:
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 780088502}
serializedVersion: 2
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_ConstrainProportionsScale: 0
m_Children: []
m_Father: {fileID: 0}
m_RootOrder: 2
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!1 &1051232094
GameObject:
Expand Down Expand Up @@ -397,7 +419,9 @@ Canvas:
m_OverrideSorting: 0
m_OverridePixelPerfect: 0
m_SortingBucketNormalizedSize: 0
m_VertexColorAlwaysGammaSpace: 0
m_AdditionalShaderChannelsFlag: 0
m_UpdateRectTransformForStandalone: 0
m_SortingLayerID: 0
m_SortingOrder: 0
m_TargetDisplay: 0
Expand All @@ -411,12 +435,12 @@ RectTransform:
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 0, y: 0, z: 0}
m_ConstrainProportionsScale: 0
m_Children:
- {fileID: 2074584277}
- {fileID: 1121692062}
- {fileID: 1328333883}
m_Father: {fileID: 0}
m_RootOrder: 1
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 0, y: 0}
m_AnchorMax: {x: 0, y: 0}
Expand All @@ -428,6 +452,7 @@ PrefabInstance:
m_ObjectHideFlags: 0
serializedVersion: 2
m_Modification:
serializedVersion: 3
m_TransformParent: {fileID: 1051232098}
m_Modifications:
- target: {fileID: 173531016972836176, guid: a20533af9a9a94af494156c17e85a9e8,
Expand Down Expand Up @@ -551,6 +576,9 @@ PrefabInstance:
value: ToggleCamera
objectReference: {fileID: 0}
m_RemovedComponents: []
m_RemovedGameObjects: []
m_AddedGameObjects: []
m_AddedComponents: []
m_SourcePrefab: {fileID: 100100000, guid: a20533af9a9a94af494156c17e85a9e8, type: 3}
--- !u!224 &1121692062 stripped
RectTransform:
Expand All @@ -565,7 +593,7 @@ LightingSettings:
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_Name: Settings.lighting
serializedVersion: 3
serializedVersion: 6
m_GIWorkflowMode: 1
m_EnableBakedLightmaps: 1
m_EnableRealtimeLightmaps: 1
Expand All @@ -578,7 +606,7 @@ LightingSettings:
m_LightmapMaxSize: 1024
m_BakeResolution: 40
m_Padding: 2
m_TextureCompression: 1
m_LightmapCompression: 2
m_AO: 0
m_AOMaxDistance: 1
m_CompAOExponent: 1
Expand All @@ -605,7 +633,7 @@ LightingSettings:
m_LightProbeSampleCountMultiplier: 4
m_PVRBounces: 2
m_PVRMinBounces: 2
m_PVREnvironmentMIS: 1
m_PVREnvironmentImportanceSampling: 1
m_PVRFilteringMode: 1
m_PVRDenoiserTypeDirect: 1
m_PVRDenoiserTypeIndirect: 1
Expand All @@ -619,6 +647,9 @@ LightingSettings:
m_PVRFilteringAtrousPositionSigmaDirect: 0.5
m_PVRFilteringAtrousPositionSigmaIndirect: 2
m_PVRFilteringAtrousPositionSigmaAO: 1
m_PVRTiledBaking: 0
m_NumRaysToShootPerTexel: -1
m_RespectSceneVisibilityWhenBakingGI: 0
--- !u!224 &1328333883 stripped
RectTransform:
m_CorrespondingSourceObject: {fileID: 7719925103924872094, guid: c3ad31ccc59b04ac782aaabae2b9706b,
Expand Down Expand Up @@ -671,12 +702,13 @@ Transform:
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1453601105}
serializedVersion: 2
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_ConstrainProportionsScale: 0
m_Children: []
m_Father: {fileID: 0}
m_RootOrder: 3
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!114 &1453601108
MonoBehaviour:
Expand All @@ -690,7 +722,7 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: 3f81f0d43e2d14fbbb064a205095507c, type: 3}
m_Name:
m_EditorClassIdentifier:
editorCameraName: FaceTime HD Camera
editorCameraName: HD Pro Webcam C920
preferKind: 1
isFrontFacing: 0
requestSize: {x: 1280, y: 720}
Expand Down Expand Up @@ -738,9 +770,9 @@ RectTransform:
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_ConstrainProportionsScale: 0
m_Children: []
m_Father: {fileID: 1051232098}
m_RootOrder: 0
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 0, y: 0}
m_AnchorMax: {x: 0, y: 0}
Expand Down Expand Up @@ -774,6 +806,7 @@ PrefabInstance:
m_ObjectHideFlags: 0
serializedVersion: 2
m_Modification:
serializedVersion: 3
m_TransformParent: {fileID: 1051232098}
m_Modifications:
- target: {fileID: 7719925103924872094, guid: c3ad31ccc59b04ac782aaabae2b9706b,
Expand Down Expand Up @@ -887,4 +920,15 @@ PrefabInstance:
value: BackToIndexButton
objectReference: {fileID: 0}
m_RemovedComponents: []
m_RemovedGameObjects: []
m_AddedGameObjects: []
m_AddedComponents: []
m_SourcePrefab: {fileID: 100100000, guid: c3ad31ccc59b04ac782aaabae2b9706b, type: 3}
--- !u!1660057539 &9223372036854775807
SceneRoots:
m_ObjectHideFlags: 0
m_Roots:
- {fileID: 32141217}
- {fileID: 1051232098}
- {fileID: 780088505}
- {fileID: 1453601107}
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
namespace TensorFlowLite
{
using UnityEngine;
using UnityEngine.Rendering;

/// <summary>
/// Add this component to a <c>Camera</c> to render the webcam texture (or any arbitrary texture) as a background.
/// </summary>
[DisallowMultipleComponent]
[RequireComponent(typeof(Camera))]
public class CameraTextureBackground : MonoBehaviour
{
[SerializeField]
Expand All @@ -14,16 +20,40 @@ public class CameraTextureBackground : MonoBehaviour

private static readonly int _UVRect = Shader.PropertyToID("_UVRect");

#region Members for built-in render pipeline
private static readonly Matrix4x4 _BackgroundOrthoProjection = Matrix4x4.Ortho(0f, 1f, 0f, 1f, -0.1f, 9.9f);
private CommandBuffer _commandBuffer;
private Mesh _backgroundMesh;
#endregion // Members for built-in render pipeline

protected virtual void Start()
{
Material = useCustomMaterial
? customMaterial
: new Material(Shader.Find("Hidden/TFLite/Resize"));
Material.SetMatrix("_VertTransform", Matrix4x4.identity);

// Setup for built-in render pipeline
if (GraphicsSettings.currentRenderPipeline == null)
{
SetupBuiltInRP();
}
}


protected virtual void OnDestroy()
{
// Cleanup resources for built-in render pipeline
if (_commandBuffer != null && TryGetComponent(out Camera camera))
{
camera.RemoveCommandBuffer(CameraEvent.BeforeForwardOpaque, _commandBuffer);
_commandBuffer = null;

Destroy(_backgroundMesh);
_backgroundMesh = null;
}

// Cleanup resources for all
if (!useCustomMaterial)
{
Destroy(Material);
Expand All @@ -39,6 +69,31 @@ public virtual void SetTexture(Texture texture)
Material.SetVector(_UVRect, uvRect);
}

private void SetupBuiltInRP()
{
if (!TryGetComponent(out Camera camera))
{
Debug.LogError($"{typeof(CameraTextureBackground).Name} requires a Camera component");
return;
}

const string name = "Camera Texture Background Pass (Built-in)";
var cmd = new CommandBuffer()
{
name = name,
};

cmd.BeginSample(name);

cmd.SetInvertCulling(false);
cmd.Blit(null, BuiltinRenderTextureType.CameraTarget, Material);

cmd.EndSample(name);

camera.AddCommandBuffer(CameraEvent.BeforeForwardOpaque, cmd);
_commandBuffer = cmd;
}

private static Vector4 GetDisplayTransform(float srcAspect, float dstAspect)
{
Vector2 scale;
Expand Down

0 comments on commit df752d9

Please sign in to comment.