public FogScope() { fog = RenderSettings.fog; // ... let's remember the current fog setting... // we are technically rendering everything in the scene, so scene fog might affect it... Unsupported.SetRenderSettingsUseFogNoDirty(false); // ... and then temporarily turn it off }
public void Dispose() { Unsupported.SetRenderSettingsUseFogNoDirty(fog); }
public static Texture2D GenerateBillboardNew(GameObject prefab, int width, int height, int gridSizeX, int gridSizeY, Shader replacementShader, Color backgroundColor, Material minPostfilter, int billboardLodIndex, Quaternion rotationOffset) { #if UNITY_EDITOR bool fog = RenderSettings.fog; Unsupported.SetRenderSettingsUseFogNoDirty(false); #endif Vector3 renderPosition = new Vector3(0, 0, 0); const int invisibleLayer = 31; // used for instance and camera cull // atlas size var w = width * gridSizeX; var h = height * gridSizeY; var result = new Texture2D(w, h); var frameBuffer = new RenderTexture(width, height, 24); var filteredFrameBuffer = new RenderTexture(width, height, 24); var camGo = new GameObject("TempCamera"); var cam = camGo.AddComponent <Camera>(); cam.clearFlags = CameraClearFlags.Color; backgroundColor.a = 0; cam.backgroundColor = backgroundColor; cam.renderingPath = RenderingPath.Forward; if (replacementShader != null) { cam.SetReplacementShader(replacementShader, ""); } var go = Instantiate(prefab, renderPosition, rotationOffset); go.hideFlags = HideFlags.DontSave; LODGroup lodGroup = go.GetComponent <LODGroup>(); if (lodGroup) { if (lodGroup.fadeMode == LODFadeMode.SpeedTree) { int lodGroupIndex = 0; switch (billboardLodIndex) { case 0: lodGroupIndex = 2; break; case 1: lodGroupIndex = 1; break; } lodGroup.ForceLOD(lodGroupIndex); } } //MeshFilter meshFilter = go.GetComponent<MeshFilter>(); //RecalculateMeshNormals(meshFilter.mesh,0); var children = go.GetComponentsInChildren <Transform>(); foreach (var t in children) { t.gameObject.layer = invisibleLayer; } var bounds = CalculateBounds(go); float yOffset = FindLowestMeshYposition(go); cam.cullingMask = 1 << invisibleLayer; cam.orthographic = true; //var boundsSize = Mathf.Max(bounds.extents.x, bounds.extents.y, bounds.extents.z) * 2; var boundsSize = Mathf.Max(bounds.extents.x, bounds.extents.y, bounds.extents.z); cam.orthographicSize = boundsSize; cam.nearClipPlane = -boundsSize * 2; cam.farClipPlane = boundsSize * 2; cam.targetTexture = frameBuffer; cam.transform.position = renderPosition + new Vector3(0, bounds.extents.y - yOffset / 2, 0); // + yOffset/2 var xAngleStep = 360f / gridSizeY / 4; var yAngleStep = 360f / gridSizeX; minPostfilter.SetInt("_UseGammaCorrection", 0); #if UNITY_EDITOR #if UNITY_2018_1_OR_NEWER #else if (PlayerSettings.colorSpace == ColorSpace.Linear) { minPostfilter.SetInt("_UseGammaCorrection", 1); } #endif #endif for (int i = 0; i < gridSizeX; i++) { for (int j = 0; j < gridSizeY; j++) { cam.transform.rotation = Quaternion.AngleAxis(yAngleStep * i, Vector3.up) * Quaternion.AngleAxis(xAngleStep * j, Vector3.right); cam.Render(); RenderTexture.active = filteredFrameBuffer; Graphics.Blit(frameBuffer, minPostfilter); result.ReadPixels(new Rect(0, 0, frameBuffer.width, frameBuffer.height), i * width, j * height); RenderTexture.active = null; } } DestroyImmediate(go); DestroyImmediate(camGo); result.Apply(); #if UNITY_EDITOR Unsupported.SetRenderSettingsUseFogNoDirty(fog); #endif return(result); }
private static void TeardownPreviewLightingAndFx(bool oldFog) { Unsupported.SetRenderSettingsUseFogNoDirty(oldFog); InternalEditorUtility.RemoveCustomLighting(); }
public override void OnPreviewGUI(Rect r, GUIStyle background) { //if (!Application.isPlaying) // return; InitPreview(); var asset = Asset; bool fog = RenderSettings.fog; Unsupported.SetRenderSettingsUseFogNoDirty(false); previewRender.BeginPreview(r, background); previewRender.ambientColor = asset.ambientColor; previewRender.lights[0].intensity = asset.lightIntensity; Camera camera = previewRender.camera; if (skeleton) { camera.transform.position = skeleton.transform.position + Quaternion.Euler(angels) * skeleton.transform.rotation * new Vector3(0, height, distance); camera.transform.LookAt(skeleton.transform.position + new Vector3(0, height * 1f, 0), skeleton.transform.up); } camera.Render(); previewRender.EndAndDrawPreview(r); Unsupported.SetRenderSettingsUseFogNoDirty(fog); Rect itemRect = new Rect(r.x, r.y, selectedWidth, selectedHeight); if (avatarRes == null) { GUI.Label(itemRect, "Avatars Empty"); return; } if (GUI.Toggle(new Rect(itemRect.x, itemRect.y, selectedWidth, selectedHeight), combine, "Combine") != combine) { combine = !combine; } itemRect.y += itemRect.height + spaceWidth; itemRect = GUIOptions(itemRect, avatarRes.name, () => { SelectAvatar(selectedAvatarIndex - 1); }, () => { SelectAvatar(selectedAvatarIndex + 1); }); itemRect = GUIOptions(itemRect, avatarRes.animationNames.Count > 0 ? avatarRes.animationNames[avatarRes.selectedAnimationIndex] : "(None)", () => { SelectAnimation(avatarRes.selectedAnimationIndex - 1); }, () => { SelectAnimation(avatarRes.selectedAnimationIndex + 1); }); var parts = avatarRes.awatarParts; for (int i = 0; i < parts.Length; i++) { itemRect = AddCategory(itemRect, i, parts[i].partName); } OnGUIDrag(r); if (Event.current.type == EventType.Repaint) { if (skeleton) { UpdateAnimation(skeleton); Repaint(); } } }
public static Texture2D Bake(MicroSplatTerrain mst, BakingPasses p, int resolution) { Camera cam = new GameObject("cam").AddComponent <Camera>(); cam.orthographic = true; cam.orthographicSize = 0.5f; cam.transform.position = new Vector3(0.5f, 10000.5f, -1); cam.nearClipPlane = 0.1f; cam.farClipPlane = 2.0f; cam.enabled = false; cam.depthTextureMode = DepthTextureMode.None; cam.clearFlags = CameraClearFlags.Color; cam.backgroundColor = Color.grey; var debugOutput = OutputFromPass(p); var readWrite = (debugOutput == MicroSplatBaseFeatures.DebugOutput.Albedo || debugOutput == MicroSplatBaseFeatures.DebugOutput.Emission) ? RenderTextureReadWrite.sRGB : RenderTextureReadWrite.Linear; RenderTexture rt = RenderTexture.GetTemporary(resolution, resolution, 0, RenderTextureFormat.ARGB32, readWrite); RenderTexture.active = rt; cam.targetTexture = rt; GameObject go = GameObject.CreatePrimitive(PrimitiveType.Quad); go.transform.position = new Vector3(0, 10000, 0); cam.transform.position = new Vector3(0, 10000, -1); Material renderMat = SetupMaterial(mst.matInstance, debugOutput); go.GetComponent <MeshRenderer>().sharedMaterial = renderMat; bool fog = RenderSettings.fog; Unsupported.SetRenderSettingsUseFogNoDirty(false); cam.Render(); Unsupported.SetRenderSettingsUseFogNoDirty(fog); Texture2D tex = new Texture2D(resolution, resolution, TextureFormat.ARGB32, false); tex.ReadPixels(new Rect(0, 0, resolution, resolution), 0, 0); RenderTexture.active = null; RenderTexture.ReleaseTemporary(rt); tex.Apply(); MeshRenderer mr = go.GetComponent <MeshRenderer>(); if (mr != null) { if (mr.sharedMaterial != null) { if (mr.sharedMaterial.shader != null) { GameObject.DestroyImmediate(mr.sharedMaterial.shader); } GameObject.DestroyImmediate(mr.sharedMaterial); } } GameObject.DestroyImmediate(go); GameObject.DestroyImmediate(cam.gameObject); return(tex); }
public static Texture2D GenerateBillboardNew(GameObject prefab, int width, int height, int gridSizeX, int gridSizeY, Shader replacementShader, Color backgroundColor, Material minPostfilter, LODLevel billboardSourceLODLevel, Quaternion rotationOffset, bool generateAo, bool recalculateNormals, float normalBlendFactor) { #if UNITY_EDITOR bool fog = RenderSettings.fog; Unsupported.SetRenderSettingsUseFogNoDirty(false); #endif Vector3 renderPosition = new Vector3(0, 0, 0); const int invisibleLayer = 31; // used for instance and camera cull // atlas size var w = width * gridSizeX; var h = height * gridSizeY; var result = new Texture2D(w, h); var frameBuffer = new RenderTexture(width, height, 24); var filteredFrameBuffer = new RenderTexture(width, height, 24); var camGo = new GameObject("TempCamera"); var cam = camGo.AddComponent <Camera>(); //if (generateAo) //{ // ScreenSpaceAmbientOcclusion ao = camGo.AddComponent<ScreenSpaceAmbientOcclusion>(); // ao.m_Radius = 0.874f; // ao.m_SampleCount = ScreenSpaceAmbientOcclusion.SSAOSamples.High; // ao.m_OcclusionIntensity = 4; // ao.m_Blur = 4; // ao.m_Downsampling = 6; // ao.m_OcclusionAttenuation = 2; // ao.m_MinZ = 0.0001f; //} cam.clearFlags = CameraClearFlags.Color; backgroundColor.a = 0; cam.backgroundColor = backgroundColor; cam.renderingPath = RenderingPath.Forward; if (replacementShader != null) { cam.SetReplacementShader(replacementShader, ""); } //cam.renderingPath = RenderingPath.DeferredShading; //cam.gameObject.AddComponent<ReadSpecularOcclusion>(); var go = Instantiate(prefab, renderPosition, rotationOffset); if (recalculateNormals) { RecalculateMeshNormals(go, normalBlendFactor); } go.hideFlags = HideFlags.DontSave; LODGroup lodGroup = go.GetComponent <LODGroup>(); if (lodGroup) { if (lodGroup.fadeMode == LODFadeMode.SpeedTree) { lodGroup.ForceLOD((int)billboardSourceLODLevel); } } //MeshFilter meshFilter = go.GetComponent<MeshFilter>(); //RecalculateMeshNormals(meshFilter.mesh,0); var children = go.GetComponentsInChildren <Transform>(); foreach (var t in children) { t.gameObject.layer = invisibleLayer; } var bounds = CalculateBounds(go); float yOffset = FindLowestMeshYposition(go); cam.cullingMask = 1 << invisibleLayer; cam.orthographic = true; //var boundsSize = Mathf.Max(bounds.extents.x, bounds.extents.y, bounds.extents.z) * 2; var boundsSize = Mathf.Max(bounds.extents.x, bounds.extents.y, bounds.extents.z); cam.orthographicSize = boundsSize; cam.nearClipPlane = -boundsSize * 2; cam.farClipPlane = boundsSize * 2; //cam.renderingPath = RenderingPath.DeferredShading; //float aspect = (float) width/(float)height; //float orthographicSize = boundsSize; //cam.projectionMatrix = Matrix4x4.Ortho(-orthographicSize * aspect, orthographicSize * aspect, -orthographicSize, orthographicSize, 0, cam.farClipPlane); cam.targetTexture = frameBuffer; cam.transform.position = renderPosition + new Vector3(0, bounds.extents.y - yOffset / 2, 0); // + yOffset/2 var xAngleStep = 360f / gridSizeY / 4; var yAngleStep = 360f / gridSizeX; minPostfilter.SetInt("_UseGammaCorrection", 0); #if UNITY_EDITOR #if UNITY_2018_1_OR_NEWER #else if (PlayerSettings.colorSpace == ColorSpace.Linear) { minPostfilter.SetInt("_UseGammaCorrection", 1); } #endif #endif for (int i = 0; i < gridSizeX; i++) { for (int j = 0; j < gridSizeY; j++) { cam.transform.rotation = Quaternion.AngleAxis(yAngleStep * i, Vector3.up) * Quaternion.AngleAxis(xAngleStep * j, Vector3.right); cam.Render(); RenderTexture.active = filteredFrameBuffer; Graphics.Blit(frameBuffer, minPostfilter); result.ReadPixels(new Rect(0, 0, frameBuffer.width, frameBuffer.height), i * width, j * height); RenderTexture.active = null; } } DestroyImmediate(go); DestroyImmediate(camGo); result.Apply(); #if UNITY_EDITOR Unsupported.SetRenderSettingsUseFogNoDirty(fog); #endif return(result); }