diff --git a/Assets/Doug.prefab b/Assets/Doug.prefab index e325af8..abaa477 100644 --- a/Assets/Doug.prefab +++ b/Assets/Doug.prefab @@ -1545,14 +1545,14 @@ Transform: m_PrefabAsset: {fileID: 0} m_GameObject: {fileID: 4310657189086097289} serializedVersion: 2 - m_LocalRotation: {x: 0.30070576, y: -0, z: -0, w: 0.953717} - m_LocalPosition: {x: -0.35901004, y: 47.8, z: -70.2} + m_LocalRotation: {x: 0.1947668, y: 0.05645799, z: -0.01123019, w: 0.979159} + m_LocalPosition: {x: -2.5, y: 11.5, z: -21} m_LocalScale: {x: 1, y: 1, z: 1} m_ConstrainProportionsScale: 0 m_Children: - {fileID: 6872729306491234319} m_Father: {fileID: 5526037850913171920} - m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} + m_LocalEulerAnglesHint: {x: 22.5, y: 6.6, z: 0} --- !u!1 &4324082620616441630 GameObject: m_ObjectHideFlags: 0 @@ -2188,7 +2188,7 @@ Camera: near clip plane: 0.3 far clip plane: 1000 field of view: 60 - orthographic: 1 + orthographic: 0 orthographic size: 2.3 m_Depth: -1 m_CullingMask: @@ -3159,11 +3159,17 @@ MonoBehaviour: mouseSensitivity: 1000 minPitch: 10 maxPitch: 60 - distance: 6 + distance: 4 heightOffset: -0.5 followSpeed: 1000 - maxZoom: 6 - minZoom: 1 + maxDistance: 6 + minDistance: 1 + zoomSpeed: 5 + obstructionMask: + serializedVersion: 2 + m_Bits: 4294967295 + fadedAlpha: 0.25 + fadeSpeed: 5 playerCamHome: {fileID: 2119885955527004040} --- !u!114 &5412473048133157928 MonoBehaviour: diff --git a/Assets/OccludeTest.mat b/Assets/OccludeTest.mat new file mode 100644 index 0000000..70ab193 --- /dev/null +++ b/Assets/OccludeTest.mat @@ -0,0 +1,141 @@ +%YAML 1.1 +%TAG !u! tag:unity3d.com,2011: +--- !u!21 &2100000 +Material: + serializedVersion: 8 + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_Name: OccludeTest + m_Shader: {fileID: 4800000, guid: 933532a4fcc9baf4fa0491de14d08ed7, type: 3} + m_Parent: {fileID: 0} + m_ModifiedSerializedProperties: 0 + m_ValidKeywords: + - _ALPHAPREMULTIPLY_ON + - _SURFACE_TYPE_TRANSPARENT + m_InvalidKeywords: [] + m_LightmapFlags: 4 + m_EnableInstancingVariants: 0 + m_DoubleSidedGI: 0 + m_CustomRenderQueue: 3000 + stringTagMap: + RenderType: Transparent + disabledShaderPasses: + - MOTIONVECTORS + - DepthOnly + - SHADOWCASTER + m_LockedProperties: + m_SavedProperties: + serializedVersion: 3 + m_TexEnvs: + - _BaseMap: + m_Texture: {fileID: 0} + m_Scale: {x: 1, y: 1} + m_Offset: {x: 0, y: 0} + - _BumpMap: + m_Texture: {fileID: 0} + m_Scale: {x: 1, y: 1} + m_Offset: {x: 0, y: 0} + - _DetailAlbedoMap: + m_Texture: {fileID: 0} + m_Scale: {x: 1, y: 1} + m_Offset: {x: 0, y: 0} + - _DetailMask: + m_Texture: {fileID: 0} + m_Scale: {x: 1, y: 1} + m_Offset: {x: 0, y: 0} + - _DetailNormalMap: + m_Texture: {fileID: 0} + m_Scale: {x: 1, y: 1} + m_Offset: {x: 0, y: 0} + - _EmissionMap: + m_Texture: {fileID: 0} + m_Scale: {x: 1, y: 1} + m_Offset: {x: 0, y: 0} + - _MainTex: + m_Texture: {fileID: 0} + m_Scale: {x: 1, y: 1} + m_Offset: {x: 0, y: 0} + - _MetallicGlossMap: + m_Texture: {fileID: 0} + m_Scale: {x: 1, y: 1} + m_Offset: {x: 0, y: 0} + - _OcclusionMap: + m_Texture: {fileID: 0} + m_Scale: {x: 1, y: 1} + m_Offset: {x: 0, y: 0} + - _ParallaxMap: + m_Texture: {fileID: 0} + m_Scale: {x: 1, y: 1} + m_Offset: {x: 0, y: 0} + - _SpecGlossMap: + m_Texture: {fileID: 0} + m_Scale: {x: 1, y: 1} + m_Offset: {x: 0, y: 0} + - unity_Lightmaps: + m_Texture: {fileID: 0} + m_Scale: {x: 1, y: 1} + m_Offset: {x: 0, y: 0} + - unity_LightmapsInd: + m_Texture: {fileID: 0} + m_Scale: {x: 1, y: 1} + m_Offset: {x: 0, y: 0} + - unity_ShadowMasks: + m_Texture: {fileID: 0} + m_Scale: {x: 1, y: 1} + m_Offset: {x: 0, y: 0} + m_Ints: [] + m_Floats: + - _AddPrecomputedVelocity: 0 + - _AlphaClip: 0 + - _AlphaToMask: 0 + - _Blend: 0 + - _BlendModePreserveSpecular: 1 + - _BumpScale: 1 + - _ClearCoatMask: 0 + - _ClearCoatSmoothness: 0 + - _Cull: 2 + - _Cutoff: 0.5 + - _DetailAlbedoMapScale: 1 + - _DetailNormalMapScale: 1 + - _DstBlend: 10 + - _DstBlendAlpha: 10 + - _EnvironmentReflections: 1 + - _GlossMapScale: 0 + - _Glossiness: 0 + - _GlossyReflections: 0 + - _Metallic: 0 + - _OcclusionStrength: 1 + - _Parallax: 0.005 + - _QueueOffset: 0 + - _ReceiveShadows: 1 + - _Smoothness: 0.5 + - _SmoothnessTextureChannel: 0 + - _SpecularHighlights: 1 + - _SrcBlend: 1 + - _SrcBlendAlpha: 1 + - _Surface: 1 + - _WorkflowMode: 1 + - _XRMotionVectorsPass: 1 + - _ZWrite: 0 + m_Colors: + - _BaseColor: {r: 0.509434, g: 0.509434, b: 0.509434, a: 1} + - _Color: {r: 0.509434, g: 0.509434, b: 0.509434, a: 1} + - _EmissionColor: {r: 0, g: 0, b: 0, a: 1} + - _SpecColor: {r: 0.19999996, g: 0.19999996, b: 0.19999996, a: 1} + m_BuildTextureStacks: [] + m_AllowLocking: 1 +--- !u!114 &3531991361068812674 +MonoBehaviour: + m_ObjectHideFlags: 11 + 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: d0353a89b1f911e48b9e16bdc9f2e058, type: 3} + m_Name: + m_EditorClassIdentifier: Unity.RenderPipelines.Universal.Editor::UnityEditor.Rendering.Universal.AssetVersion + version: 10 diff --git a/Assets/OccludeTest.mat.meta b/Assets/OccludeTest.mat.meta new file mode 100644 index 0000000..57ef789 --- /dev/null +++ b/Assets/OccludeTest.mat.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 78be8f8bd251f244ab82d30f189c8824 +NativeFormatImporter: + externalObjects: {} + mainObjectFileID: 2100000 + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scenes/SampleScene.unity b/Assets/Scenes/SampleScene.unity index c940591..56287ca 100644 --- a/Assets/Scenes/SampleScene.unity +++ b/Assets/Scenes/SampleScene.unity @@ -3695,7 +3695,7 @@ MeshRenderer: m_RenderingLayerMask: 1 m_RendererPriority: 0 m_Materials: - - {fileID: 2100000, guid: 31321ba15b8f8eb4c954353edc038b1d, type: 2} + - {fileID: 2100000, guid: 78be8f8bd251f244ab82d30f189c8824, type: 2} m_StaticBatchInfo: firstSubMesh: 0 subMeshCount: 0 @@ -4471,6 +4471,10 @@ PrefabInstance: propertyPath: minPitch value: 10 objectReference: {fileID: 0} + - target: {fileID: 5478143440182735446, guid: 0fd9b22e9158e474a96c42de5ee0d85f, type: 3} + propertyPath: zoomSpeed + value: 5 + objectReference: {fileID: 0} - target: {fileID: 5478143440182735446, guid: 0fd9b22e9158e474a96c42de5ee0d85f, type: 3} propertyPath: MainCamera value: @@ -4483,6 +4487,10 @@ PrefabInstance: propertyPath: mouseSensitivity value: 1000 objectReference: {fileID: 0} + - target: {fileID: 5478143440182735446, guid: 0fd9b22e9158e474a96c42de5ee0d85f, type: 3} + propertyPath: obstructionMask.m_Bits + value: 4294967295 + objectReference: {fileID: 0} - target: {fileID: 5526037850913171920, guid: 0fd9b22e9158e474a96c42de5ee0d85f, type: 3} propertyPath: m_LocalPosition.x value: -0.60945 @@ -4539,6 +4547,10 @@ PrefabInstance: propertyPath: m_LocalEulerAnglesHint.x value: 0 objectReference: {fileID: 0} + - target: {fileID: 7814557473141052265, guid: 0fd9b22e9158e474a96c42de5ee0d85f, type: 3} + propertyPath: orthographic + value: 0 + objectReference: {fileID: 0} - target: {fileID: 9054701043111961555, guid: 0fd9b22e9158e474a96c42de5ee0d85f, type: 3} propertyPath: m_Name value: Doug diff --git a/Assets/Scripts/Management/CameraController.cs b/Assets/Scripts/Management/CameraController.cs index e56ce75..4bc24c0 100644 --- a/Assets/Scripts/Management/CameraController.cs +++ b/Assets/Scripts/Management/CameraController.cs @@ -2,6 +2,7 @@ using System.Linq; using Unity.VisualScripting; using UnityEngine; +using static Unity.Cinemachine.CinemachineTargetGroup; using static UnityEngine.GraphicsBuffer; public class CameraController : MonoBehaviour @@ -16,20 +17,36 @@ public class CameraController : MonoBehaviour public float mouseSensitivity = 200f; public float minPitch = -30f; public float maxPitch = 60f; - public float distance = 6f; + public float distance = 5f; public float heightOffset = -0.5f; public float followSpeed = 10f; - public float maxZoom = 6f; - public float minZoom = 1f; - private float zoom = 2.3f; + public float maxDistance = 6f; + public float minDistance = 1f; + public float zoomSpeed = 5f; private float yaw; private float pitch; + private float currentDistance; + private float distanceVelocity; + + [Header("Occlusion Settings")] + public LayerMask obstructionMask; + public float fadedAlpha = 0.25f; + public float fadeSpeed = 5f; + + private Dictionary currentAlpha = + new Dictionary(); + + private Dictionary targetAlpha = + new Dictionary(); public Transform playerCamHome; Transform dougBody; private void Update() { + UpdateOcclusion(); + UpdateFade(); + if (Input.GetMouseButton(1)) { HandleFreeCamRotation(); @@ -42,6 +59,7 @@ void Start() yaw = angles.y; pitch = angles.x; + currentDistance = distance; } /// @@ -102,6 +120,9 @@ void HandleFreeCamRotation() { if (dougBody == null) { + if (GameManager.Instance.PlayerController.DougBody == null) + return; + dougBody = GameManager.Instance.PlayerController.DougBody.transform; } @@ -119,15 +140,23 @@ void HandleFreeCamRotation() // Zoom float scroll = Input.GetAxis("Mouse ScrollWheel"); - zoom -= scroll * 2f; - zoom = Mathf.Clamp(zoom, minZoom, maxZoom); - MainCamera.orthographicSize = zoom; + if (Mathf.Abs(scroll) > 0.0001f) + { + distance -= scroll * zoomSpeed; + distance = Mathf.Clamp(distance, minDistance, maxDistance); + } + + currentDistance = Mathf.SmoothDamp( + currentDistance, + distance, + ref distanceVelocity, + 0.05f); // Rotation Quaternion rotation = Quaternion.Euler(pitch, yaw, 0); // Desired Position - Vector3 offset = rotation * Vector3.forward * -distance; + Vector3 offset = rotation * Vector3.forward * -currentDistance; Vector3 desiredPosition = target + offset; // Smooth Follow @@ -140,6 +169,93 @@ void HandleFreeCamRotation() // Look at player playerCamHome.transform.LookAt(target + Vector3.up); } + + void UpdateOcclusion() + { + if (dougBody == null) + { + if (GameManager.Instance.PlayerController.DougBody == null) + return; + + dougBody = GameManager.Instance.PlayerController.DougBody.transform; + } + + if (dougBody == null) return; + + // Reset all renderers to visible + foreach (Renderer r in new List(targetAlpha.Keys)) + { + targetAlpha[r] = 1f; + } + + Vector3 dir = + MainCamera.transform.position - dougBody.position; + + float dist = dir.magnitude; + + RaycastHit[] hits = Physics.RaycastAll( + dougBody.position, + dir.normalized, + dist, + obstructionMask, + QueryTriggerInteraction.Ignore); + + foreach (RaycastHit hit in hits) + { + Renderer r = hit.collider.GetComponent(); + + if (r == null) + continue; + + if (!currentAlpha.ContainsKey(r)) + { + currentAlpha[r] = 1f; + targetAlpha[r] = 1f; + } + + targetAlpha[r] = fadedAlpha; + } + } + + void UpdateFade() + { + List renderers = + new List(currentAlpha.Keys); + + foreach (Renderer r in renderers) + { + float current = currentAlpha[r]; + float target = targetAlpha[r]; + + current = Mathf.Lerp( + current, + target, + Time.deltaTime * fadeSpeed); + + currentAlpha[r] = current; + + foreach (Material mat in r.materials) + { + Color c = mat.color; + c.a = current; + + if (Mathf.Abs(current - targetAlpha[r]) < 0.05f) + { + c.a = targetAlpha[r]; + } + + mat.color = c; + } + + // Cleanup once fully restored + if (Mathf.Abs(current - 1f) < 0.01f && + Mathf.Abs(target - 1f) < 0.01f) + { + currentAlpha.Remove(r); + targetAlpha.Remove(r); + } + } + } } public enum CameraControlMode