void OnDestroy() { if (_shadowTexture != null) { DestroyImmediate(_shadowTexture); } targetCamera = null; targetList.Clear(); _instance = null; }
public override void OnInspectorGUI() { DrawDefaultInspector(); RenderShadow self = (RenderShadow)target; if (Application.isPlaying) { EditorGUILayout.BeginHorizontal(); bool oldValue = self.hasInit; bool newValue = EditorGUILayout.Toggle(self.hasInit, GUILayout.Width(15)); EditorGUILayout.LabelField("Do Init Function", GUILayout.Width(100)); EditorGUILayout.EndHorizontal(); if (oldValue == false && newValue == true) { self.Init(); } } }
public void Init() { if (_hasInit) { return; } _shadowCamera = gameObject.GetComponent <Camera>(); //_shadowCamera.hideFlags = HideFlags.HideAndDontSave; _shadowCamera.enabled = false; _shadowCamera.nearClipPlane = -15; _shadowCamera.farClipPlane = 15; for (int i = 0; i < targetLayer.Count; i++) { _shadowCamera.cullingMask |= (1 << (int)targetLayer[i]); } int textureSize = renderTextureSize; _shadowTexture = new RenderTexture(textureSize, textureSize, 0, RenderTextureFormat.ARGB32); _shadowTexture.name = "RenderShadowTexture"; _shadowTexture.isPowerOfTwo = true; _shadowTexture.hideFlags = HideFlags.DontSave; _shadowCamera.targetTexture = _shadowTexture; _projector = gameObject.GetComponent <Projector>(); _projector.nearClipPlane = targetCamera.nearClipPlane; _projector.farClipPlane = targetCamera.farClipPlane; _projector.fieldOfView = targetCamera.fieldOfView; //设置 ignoreLayers _projector.ignoreLayers = ~(1 << (int)LayerEnum.RenderShadowReciver); matVP = GL.GetGPUProjectionMatrix(_shadowCamera.projectionMatrix, true) * _shadowCamera.worldToCameraMatrix; _projector.material.SetMatrix("ShadowMatrix", matVP); //_shadowCamera.SetReplacementShader (Shader.Find ("depthShader"), ""); _instance = this; _hasInit = true; }