private void OnEnable() { script = (StylizedGrassRenderer)target; #if URP if (script.bendRenderer == null) { script.bendRenderer = PipelineUtilities.GetRenderer(DrawGrassBenders.AssetName); PipelineUtilities.ValidatePipelineRenderers(script.bendRenderer); script.OnEnable(); script.OnDisable(); } #endif renderExtends = serializedObject.FindProperty("renderExtends"); followSceneCamera = serializedObject.FindProperty("followSceneCamera"); followTarget = serializedObject.FindProperty("followTarget"); renderLayer = serializedObject.FindProperty("renderLayer"); maskEdges = serializedObject.FindProperty("maskEdges"); colorMap = serializedObject.FindProperty("colorMap"); listenToWindZone = serializedObject.FindProperty("listenToWindZone"); windZone = serializedObject.FindProperty("windZone"); layerStr = UnityEditorInternal.InternalEditorUtility.layers; }
private Camera CreateCamera() { Camera cam = new GameObject().AddComponent <Camera>(); cam.gameObject.name = "GrassBendCamera " + GetInstanceID(); cam.transform.localEulerAngles = new Vector3(90f, 0f, 0f); cam.gameObject.hideFlags = HideFlags.HideAndDontSave; if (actualFollowTarget) { cam.gameObject.transform.position = new Vector3(actualFollowTarget.transform.position.x, actualFollowTarget.transform.position.y + renderExtends, actualFollowTarget.transform.position.z); } cam.orthographic = true; cam.depth = -100f; cam.allowHDR = false; cam.allowMSAA = false; cam.clearFlags = CameraClearFlags.SolidColor; cam.cullingMask = 0; //Neutral bend direction and zero strength/mask cam.backgroundColor = neutralVector; cam.useOcclusionCulling = false; cam.allowHDR = true; cam.allowMSAA = false; cam.forceIntoRenderTexture = true; #if URP UniversalAdditionalCameraData camData = cam.gameObject.AddComponent <UniversalAdditionalCameraData>(); camData.renderShadows = false; camData.renderPostProcessing = false; camData.antialiasing = AntialiasingMode.None; camData.requiresColorOption = CameraOverrideOption.Off; camData.requiresDepthOption = CameraOverrideOption.Off; camData.requiresColorTexture = false; camData.requiresDepthTexture = false; if (UniversalRenderPipeline.asset) { #if UNITY_EDITOR //Only runs in editor, but will be referenced in instance from there on if (!bendRenderer) { bendRenderer = PipelineUtilities.GetRenderer((DrawGrassBenders.AssetName)); } PipelineUtilities.ValidatePipelineRenderers(bendRenderer); #endif PipelineUtilities.AssignRendererToCamera(camData, bendRenderer); } else { Debug.LogError("[StylizedGrassRenderer] No Universal Render Pipeline is currently active."); } #endif return(cam); }
public static void SetupRenderer(GrassColorMapRenderer renderer) { if (!renderer.renderCam) { renderer.renderCam = new GameObject().AddComponent <Camera>(); } renderer.renderCam.name = "Grass color map renderCam"; renderer.renderCam.enabled = false; //Camera set up renderer.renderCam.orthographic = true; renderer.renderCam.orthographicSize = (renderer.colorMap.bounds.size.x / 2); renderer.renderCam.farClipPlane = renderer.colorMap.bounds.size.y + CLIP_PADDING; renderer.renderCam.clearFlags = CameraClearFlags.Color; renderer.renderCam.backgroundColor = Color.red; renderer.renderCam.cullingMask = renderer.useLayers ? (int)renderer.renderLayer : -1; //Position cam in given center of terrain(s) renderer.renderCam.transform.position = new Vector3( renderer.colorMap.bounds.center.x, renderer.colorMap.bounds.center.y + renderer.colorMap.bounds.extents.y + CLIP_PADDING + (renderer.useLayers ? 0f : HEIGHT_OFFSET), renderer.colorMap.bounds.center.z ); renderer.renderCam.transform.localEulerAngles = new Vector3(90, 0, 0); #if URP UniversalAdditionalCameraData camData = renderer.renderCam.gameObject.AddComponent <UniversalAdditionalCameraData>(); camData.renderShadows = false; camData.renderPostProcessing = false; camData.antialiasing = AntialiasingMode.None; camData.requiresColorOption = CameraOverrideOption.Off; camData.requiresDepthOption = CameraOverrideOption.Off; camData.requiresColorTexture = false; camData.requiresDepthTexture = false; if (UniversalRenderPipeline.asset) { #if UNITY_EDITOR //Only runs in editor, but will be referenced in instance from there on if (!renderer.renderData) { renderer.renderData = PipelineUtilities.GetRenderer(("ColormapRenderer")); } #endif PipelineUtilities.ValidatePipelineRenderers(renderer.renderData); PipelineUtilities.AssignRendererToCamera(camData, renderer.renderData); } else { Debug.LogError("[StylizedGrassRenderer] No Universal Render Pipeline is currently active."); } #endif }
private static void RenderToTexture(GrassColorMapRenderer renderer) { if (!renderer.renderCam) { Debug.LogError("Renderer does not have a render cam set up"); return; } bool isTerrain = false; //Get all terrains List <Terrain> terrains = new List <Terrain>(); foreach (GameObject item in renderer.terrainObjects) { if (item == null) { continue; } Terrain t = item.GetComponent <Terrain>(); if (t) { isTerrain = true; terrains.Add(t); } } if (isTerrain) { foreach (Terrain t in terrains) { t.drawTreesAndFoliage = false; } } //Temporarly move terrains up 1000 units if (renderer.terrainObjects != null || renderer.terrainObjects.Count != 0) { if (renderer.terrainObjects[0]) { originalTerrainHeight = renderer.terrainObjects[0].transform.position.y; } foreach (GameObject item in renderer.terrainObjects) { if (item == null) { continue; } if (renderer.useLayers == false && renderer.terrainObjects[0]) { item.transform.position = new Vector3(item.transform.position.x, item.transform.position.y + HEIGHT_OFFSET, item.transform.position.z); } } } //Set up render texture RenderTexture rt = new RenderTexture(renderer.resolution, renderer.resolution, 0, RenderTextureFormat.ARGB32, RenderTextureReadWrite.Linear); renderer.renderCam.targetTexture = rt; RenderTexture.active = rt; //Render camera into a texture renderer.renderCam.Render(); //Generate heightmap from terrain layers if (isTerrain) { GenerateScalemap(terrains, renderer, rt); } Graphics.SetRenderTarget(rt); Texture2D render = new Texture2D(renderer.resolution, renderer.resolution, TextureFormat.ARGB32, false, true); render.ReadPixels(new Rect(0, 0, renderer.resolution, renderer.resolution), 0, 0); render.Apply(); render.Compress(false); render.name = renderer.colorMap.name; if (!renderer.colorMap.texture) { renderer.colorMap.texture = new Texture2D(renderer.colorMap.resolution, renderer.colorMap.resolution); } //Saving texture if (EditorUtility.IsPersistent(renderer.colorMap)) { //string texPath = AssetDatabase.GetAssetPath(renderer.colorMap.texture); //byte[] bytes = render.EncodeToPNG(); //System.IO.File.WriteAllBytes(texPath, bytes); //AssetDatabase.ImportAsset(texPath, ImportAssetOptions.Default); //SaveTexture(render, renderer.colorMap); EditorUtility.CopySerialized(render, renderer.colorMap.texture); DestroyImmediate(render); } else { renderer.colorMap.texture = render; } SetName(renderer.colorMap); EditorUtility.SetDirty(renderer.colorMap); //Cleanup renderer.renderCam.targetTexture = null; RenderTexture.active = null; DestroyImmediate(rt); DestroyImmediate(renderer.renderCam.gameObject); renderer.renderCam = null; #if URP PipelineUtilities.RemoveRendererFromPipeline(renderer.renderData); #endif if (isTerrain) { //Restore materials foreach (Terrain t in terrains) { t.drawTreesAndFoliage = true; } } //Restore terrains to original position height foreach (var item in renderer.terrainObjects) { if (item == null) { continue; } if (renderer.useLayers == false && renderer.terrainObjects[0]) { item.transform.position = new Vector3(item.transform.position.x, originalTerrainHeight, item.transform.position.z); } } }