/// <summary> /// Returns the mask and trigger assigned for volumes on the camera. /// </summary> /// <param name="camera"></param> /// <param name="cameraData"></param> /// <param name="layerMask"></param> /// <param name="trigger"></param> internal static void GetVolumeLayerMaskAndTrigger(this Camera camera, UniversalAdditionalCameraData cameraData, out LayerMask layerMask, out Transform trigger) { // Default values when there's no additional camera data available layerMask = 1; // "Default" trigger = camera.transform; if (cameraData != null) { layerMask = cameraData.volumeLayerMask; trigger = (cameraData.volumeTrigger != null) ? cameraData.volumeTrigger : trigger; } else if (camera.cameraType == CameraType.SceneView) { // Try to mirror the MainCamera volume layer mask for the scene view - do not mirror the target var mainCamera = Camera.main; UniversalAdditionalCameraData mainAdditionalCameraData = null; if (mainCamera != null && mainCamera.TryGetComponent(out mainAdditionalCameraData)) { layerMask = mainAdditionalCameraData.volumeLayerMask; } trigger = (mainAdditionalCameraData != null && mainAdditionalCameraData.volumeTrigger != null) ? mainAdditionalCameraData.volumeTrigger : trigger; } }
public static void RenderSingleCamera(ScriptableRenderContext context, Camera camera) { if (!camera.TryGetCullingParameters(IsStereoEnabled(camera), out var cullingParameters)) { return; } var settings = asset; UniversalAdditionalCameraData additionalCameraData = null; if (camera.cameraType == CameraType.Game || camera.cameraType == CameraType.VR) #if UNITY_2019_3_OR_NEWER { camera.gameObject.TryGetComponent(out additionalCameraData); } #else { additionalCameraData = camera.gameObject.GetComponent <LWRPAdditionalCameraData>(); } #endif InitializeCameraData(settings, camera, additionalCameraData, out var cameraData); SetupPerCameraShaderConstants(cameraData); ScriptableRenderer renderer = (additionalCameraData != null) ? additionalCameraData.scriptableRenderer : settings.scriptableRenderer; if (renderer == null) { Debug.LogWarning(string.Format("Trying to render {0} with an invalid renderer. Camera rendering will be skipped.", camera.name)); return; } #if UNITY_EDITOR string tag = camera.name; #else string tag = k_RenderCameraTag; #endif CommandBuffer cmd = CommandBufferPool.Get(tag); using (new ProfilingSample(cmd, tag)) { renderer.Clear(); renderer.SetupCullingParameters(ref cullingParameters, ref cameraData); context.ExecuteCommandBuffer(cmd); cmd.Clear(); #if UNITY_EDITOR // Emit scene view UI if (cameraData.isSceneViewCamera) { ScriptableRenderContext.EmitWorldGeometryForSceneView(camera); } #endif var cullResults = context.Cull(ref cullingParameters); InitializeRenderingData(settings, ref cameraData, ref cullResults, out var renderingData); renderer.Setup(context, ref renderingData); renderer.Execute(context, ref renderingData); } context.ExecuteCommandBuffer(cmd); CommandBufferPool.Release(cmd); context.Submit(); }
// Start is called before the first frame update void Start() { cam = gameObject.GetComponent <Camera>(); if (cam == null) { Debug.Log("This script should be attatched to a Camera"); } additionalCameraData = cam.transform.GetComponent <UnityEngine.Rendering.Universal.UniversalAdditionalCameraData>(); additionalCameraData.SetRenderer(0); }
/// <summary> /// Sets the VolumeFrameworkUpdateMode for the camera. /// </summary> /// <param name="camera"></param> /// <param name="mode"></param> public static void SetVolumeFrameworkUpdateMode(this Camera camera, VolumeFrameworkUpdateMode mode) { UniversalAdditionalCameraData cameraData = camera.GetUniversalAdditionalCameraData(); if (cameraData.volumeFrameworkUpdateMode == mode) { return; } cameraData.volumeFrameworkUpdateMode = mode; // We only update the local volume stacks for cameras set to ViaScripting. // Otherwise it will be updated in every frame. if (!cameraData.requiresVolumeFrameworkUpdate) { camera.UpdateVolumeStack(cameraData); } }
void CreateCubemap() { Cubemap cubemap = new Cubemap(size, defaultFormat, textureCreationFlags); Camera cam = this.gameObject.AddComponent <Camera>(); cam.RenderToCubemap(cubemap); string cubemapPath = this.gameObject.scene.path.Replace(".unity", ".cubemap"); AssetDatabase.CreateAsset(cubemap, cubemapPath); AssetImporter.GetAtPath(cubemapPath).SetAssetBundleNameAndVariant(assetBundleName, ""); UnityEngine.Rendering.Universal.UniversalAdditionalCameraData cameraData = cam.GetComponent <UnityEngine.Rendering.Universal.UniversalAdditionalCameraData>(); if (cameraData) { DestroyImmediate(cameraData); } DestroyImmediate(cam); EditorSceneManager.MarkSceneDirty(EditorSceneManager.GetActiveScene()); Debug.Log("Cubemap created in " + cubemapPath); }
/// <summary> /// Updates the volume stack for this camera. /// This function will only update the stack when the camera has ViaScripting selected or if /// the camera is set to UsePipelineSettings and the Render Pipeline Asset is set to ViaScripting. /// </summary> /// <param name="camera"></param> /// <param name="cameraData"></param> public static void UpdateVolumeStack(this Camera camera, UniversalAdditionalCameraData cameraData) { Assert.IsNotNull(cameraData, "cameraData can not be null when updating the volume stack."); // We only update the local volume stacks for cameras set to ViaScripting. // Otherwise it will be updated in the frame. if (cameraData.requiresVolumeFrameworkUpdate) { return; } // Create stack for camera if (cameraData.volumeStack == null) { cameraData.volumeStack = VolumeManager.instance.CreateStack(); } camera.GetVolumeLayerMaskAndTrigger(cameraData, out LayerMask layerMask, out Transform trigger); VolumeManager.instance.Update(cameraData.volumeStack, trigger, layerMask); }
/// <summary> /// Sets the VolumeFrameworkUpdateMode for the camera. /// </summary> /// <param name="camera"></param> /// <param name="mode"></param> public static void SetVolumeFrameworkUpdateMode(this Camera camera, VolumeFrameworkUpdateMode mode) { UniversalAdditionalCameraData cameraData = camera.GetUniversalAdditionalCameraData(); if (cameraData.volumeFrameworkUpdateMode == mode) { return; } bool requiredUpdatePreviously = cameraData.requiresVolumeFrameworkUpdate; cameraData.volumeFrameworkUpdateMode = mode; // We only update the local volume stacks for cameras set to ViaScripting. // Otherwise it will be updated in every frame. // We also check the previous value to make sure we're not updating when // switching between Camera ViaScripting and the URP Asset set to ViaScripting if (requiredUpdatePreviously && !cameraData.requiresVolumeFrameworkUpdate) { camera.UpdateVolumeStack(cameraData); } }
private Camera CreateMirrorObjects(Camera currentCamera) { GameObject go = new GameObject($"Planar Refl Camera id{GetInstanceID().ToString()} for {currentCamera.GetInstanceID().ToString()}", typeof(Camera)); UnityEngine.Rendering.Universal.UniversalAdditionalCameraData lwrpCamData = go.AddComponent(typeof(UnityEngine.Rendering.Universal.UniversalAdditionalCameraData)) as UnityEngine.Rendering.Universal.UniversalAdditionalCameraData; UnityEngine.Rendering.Universal.UniversalAdditionalCameraData lwrpCamDataCurrent = currentCamera.GetComponent <UnityEngine.Rendering.Universal.UniversalAdditionalCameraData>(); lwrpCamData.renderShadows = m_settings.m_shadows; // turn off shadows for the reflection camera lwrpCamData.requiresColorOption = UnityEngine.Rendering.Universal.CameraOverrideOption.Off; lwrpCamData.requiresDepthOption = UnityEngine.Rendering.Universal.CameraOverrideOption.Off; var reflectionCamera = go.GetComponent <Camera>(); reflectionCamera.transform.SetPositionAndRotation(transform.position, transform.rotation); //reflectionCamera.targetTexture = m_ReflectionTexture; reflectionCamera.allowMSAA = currentCamera.allowMSAA; reflectionCamera.depth = -10; reflectionCamera.enabled = false; reflectionCamera.allowHDR = currentCamera.allowHDR; go.hideFlags = HideFlags.HideAndDontSave; return(reflectionCamera); }
private void ChangeRenderView(int state) { UnityEngine.Rendering.Universal.UniversalAdditionalCameraData additionalCameraData = cam.transform.GetComponent <UnityEngine.Rendering.Universal.UniversalAdditionalCameraData>(); additionalCameraData.SetRenderer(state); }
static void InitializeCameraData(UniversalRenderPipelineAsset settings, Camera camera, UniversalAdditionalCameraData additionalCameraData, out CameraData cameraData) { const float kRenderScaleThreshold = 0.05f; cameraData = new CameraData(); cameraData.camera = camera; cameraData.isStereoEnabled = IsStereoEnabled(camera); int msaaSamples = 1; if (camera.allowMSAA && settings.msaaSampleCount > 1) { msaaSamples = (camera.targetTexture != null) ? camera.targetTexture.antiAliasing : settings.msaaSampleCount; } cameraData.isSceneViewCamera = camera.cameraType == CameraType.SceneView; cameraData.isHdrEnabled = camera.allowHDR && settings.supportsHDR; cameraData.postProcessEnabled = CoreUtils.ArePostProcessesEnabled(camera) && camera.cameraType != CameraType.Reflection && camera.cameraType != CameraType.Preview && SystemInfo.graphicsDeviceType != GraphicsDeviceType.OpenGLES2; // Disables postprocessing in mobile VR. It's not stable on mobile yet. // TODO: enable postfx for stereo rendering if (cameraData.isStereoEnabled && Application.isMobilePlatform) { cameraData.postProcessEnabled = false; } Rect cameraRect = camera.rect; cameraData.isDefaultViewport = (!(Math.Abs(cameraRect.x) > 0.0f || Math.Abs(cameraRect.y) > 0.0f || Math.Abs(cameraRect.width) < 1.0f || Math.Abs(cameraRect.height) < 1.0f)); // If XR is enabled, use XR renderScale. // Discard variations lesser than kRenderScaleThreshold. // Scale is only enabled for gameview. float usedRenderScale = XRGraphics.enabled ? XRGraphics.eyeTextureResolutionScale : settings.renderScale; cameraData.renderScale = (Mathf.Abs(1.0f - usedRenderScale) < kRenderScaleThreshold) ? 1.0f : usedRenderScale; cameraData.renderScale = (camera.cameraType == CameraType.Game) ? cameraData.renderScale : 1.0f; bool anyShadowsEnabled = settings.supportsMainLightShadows || settings.supportsAdditionalLightShadows; cameraData.maxShadowDistance = (anyShadowsEnabled) ? settings.shadowDistance : 0.0f; if (additionalCameraData != null) { cameraData.maxShadowDistance = (additionalCameraData.renderShadows) ? cameraData.maxShadowDistance : 0.0f; cameraData.requiresDepthTexture = additionalCameraData.requiresDepthTexture; cameraData.requiresOpaqueTexture = additionalCameraData.requiresColorTexture; cameraData.volumeLayerMask = additionalCameraData.volumeLayerMask; cameraData.volumeTrigger = additionalCameraData.volumeTrigger == null ? camera.transform : additionalCameraData.volumeTrigger; cameraData.postProcessEnabled &= additionalCameraData.renderPostProcessing; cameraData.isStopNaNEnabled = cameraData.postProcessEnabled && additionalCameraData.stopNaN && SystemInfo.graphicsShaderLevel >= 35; cameraData.isDitheringEnabled = cameraData.postProcessEnabled && additionalCameraData.dithering; cameraData.antialiasing = cameraData.postProcessEnabled ? additionalCameraData.antialiasing : AntialiasingMode.None; cameraData.antialiasingQuality = additionalCameraData.antialiasingQuality; } else { cameraData.requiresDepthTexture = settings.supportsCameraDepthTexture; cameraData.requiresOpaqueTexture = settings.supportsCameraOpaqueTexture; cameraData.volumeLayerMask = 1; // "Default" cameraData.volumeTrigger = null; cameraData.postProcessEnabled = false; cameraData.isStopNaNEnabled = false; cameraData.isDitheringEnabled = false; cameraData.antialiasing = AntialiasingMode.None; cameraData.antialiasingQuality = AntialiasingQuality.High; } cameraData.requiresDepthTexture |= cameraData.isSceneViewCamera || cameraData.postProcessEnabled; var commonOpaqueFlags = SortingCriteria.CommonOpaque; var noFrontToBackOpaqueFlags = SortingCriteria.SortingLayer | SortingCriteria.RenderQueue | SortingCriteria.OptimizeStateChanges | SortingCriteria.CanvasOrder; bool hasHSRGPU = SystemInfo.hasHiddenSurfaceRemovalOnGPU; bool canSkipFrontToBackSorting = (camera.opaqueSortMode == OpaqueSortMode.Default && hasHSRGPU) || camera.opaqueSortMode == OpaqueSortMode.NoDistanceSort; cameraData.defaultOpaqueSortFlags = canSkipFrontToBackSorting ? noFrontToBackOpaqueFlags : commonOpaqueFlags; cameraData.captureActions = CameraCaptureBridge.GetCaptureActions(camera); cameraData.cameraTargetDescriptor = CreateRenderTextureDescriptor(camera, cameraData.renderScale, cameraData.isStereoEnabled, cameraData.isHdrEnabled, msaaSamples); }
/// <summary> /// Returns the VolumeFrameworkUpdateMode set on the camera. /// </summary> /// <param name="camera"></param> /// <returns></returns> public static VolumeFrameworkUpdateMode GetVolumeFrameworkUpdateMode(this Camera camera) { UniversalAdditionalCameraData cameraData = camera.GetUniversalAdditionalCameraData(); return(cameraData.volumeFrameworkUpdateMode); }
/// <summary> /// Updates the volume stack for this camera. /// This function will only update the stack when the camera has VolumeFrameworkUpdateMode set to ViaScripting /// or when it set to UsePipelineSettings and the update mode on the Render Pipeline Asset is set to ViaScripting. /// </summary> /// <param name="camera"></param> public static void UpdateVolumeStack(this Camera camera) { UniversalAdditionalCameraData cameraData = camera.GetUniversalAdditionalCameraData(); camera.UpdateVolumeStack(cameraData); }
/// <summary> /// Destroys the volume stack for this camera. /// </summary> /// <param name="camera"></param> /// <param name="cameraData"></param> public static void DestroyVolumeStack(this Camera camera, UniversalAdditionalCameraData cameraData) { cameraData.volumeStack.Dispose(); cameraData.volumeStack = null; }
/// <summary> /// Destroys the volume stack for this camera. /// </summary> /// <param name="camera"></param> public static void DestroyVolumeStack(this Camera camera) { UniversalAdditionalCameraData cameraData = camera.GetUniversalAdditionalCameraData(); camera.DestroyVolumeStack(cameraData); }