22
33namespace ReflectionPlugin
44{
5+ public class ProcCubeMap : ProceduralCubemap
6+ {
7+ ProcCubeMap ( )
8+ {
9+ }
10+ }
11+
512 public class ReflectiveScript : MonoBehaviour
613 {
14+ public static int i = 0 ;
715 public int CubemapSize = 128 ;
8- public float FarClipPlane = 100f ;
16+ public Material currentMaterial ;
17+ public float FarClipPlane = float . MaxValue ;
918 public float NearClipPlane = - 1f ;
1019 public Renderer MatRenderer ;
1120 public bool OneFacePerFrame ;
@@ -16,6 +25,7 @@ public class ReflectiveScript : MonoBehaviour
1625
1726 private Camera _cam ;
1827 private RenderTexture _rtex ;
28+ private GameObject _go ;
1929 public string status = "" ;
2030 private string renderResult = "PRE-INITIALIZATION" ;
2131 //private int maskBit = 12;
@@ -25,18 +35,22 @@ public class ReflectiveScript : MonoBehaviour
2535
2636 private void Start ( )
2737 {
28- this . UpdateCubemap ( 63 ) ;
38+ //currentMaterial = renderer.material;
39+ for ( int i = 0 ; i < 31 ; i ++ )
40+ Debug . Log ( " Layer " + i . ToString ( ) + ": " + LayerMask . LayerToName ( i ) ) ;
41+ UpdateCubemap ( 63 ) ;
42+ dirty = 7 ;
2943 }
3044
3145 private bool ShouldUpdate ( )
3246 {
33- if ( realTimeReflection || this . dirty > 0 )
47+ if ( realTimeReflection || dirty > 0 )
3448 {
3549 return true ;
3650 }
37- else if ( Planetarium . GetUniversalTime ( ) >= this . lastUpdate + this . updateRate && ( Time . time > this . lastRealUpdate + 5f ) )
51+ else if ( Planetarium . GetUniversalTime ( ) >= lastUpdate + updateRate && ( Time . time > lastRealUpdate + 5f ) )
3852 {
39- this . dirty = 6 ;
53+ dirty = 6 ;
4054 return true ;
4155 }
4256 else
@@ -48,97 +62,191 @@ private bool ShouldUpdate()
4862 {
4963 scene = FlightGlobals . currentMainBody . name ;
5064 }
51- if ( scene != this . lastScene )
65+ if ( scene != lastScene )
5266 {
53- this . dirty = 7 ;
54- this . lastScene = scene ;
67+ dirty = 7 ;
68+ lastScene = scene ;
5569 return true ;
5670 }
5771 }
5872 return false ;
5973 }
74+
75+ protected void FixedUpdate ( )
76+ {
77+ _cam . transform . position = _go . transform . position = transform . position ;
78+ }
6079
61-
62-
63- private void OnPostRender ( )
80+ protected void LateUpdate ( )
6481 {
6582 if ( ShouldUpdate ( ) )
6683 {
67- if ( OneFacePerFrame && this . dirty < 7 )
84+ if ( OneFacePerFrame && dirty < 7 )
6885 {
69- //this. _cam.cullingMask = maskBit;
70- this . UpdateCubemap ( 1 << Time . frameCount % 6 ) ;
86+ //_cam.cullingMask = maskBit;
87+ UpdateCubemap ( 1 << Time . frameCount % 6 ) ;
7188 }
7289 else
7390 {
74- //this. _cam.cullingMask = maskBit;
75- this . UpdateCubemap ( 63 ) ;
91+ //_cam.cullingMask = maskBit;
92+ UpdateCubemap ( 63 ) ;
7693 }
7794 }
7895 else
79- status = renderResult + ( ( lastUpdate + this . updateRate ) - Planetarium . GetUniversalTime ( ) ) . ToString ( ) ;
96+ status = renderResult + ( ( lastUpdate + updateRate ) - Planetarium . GetUniversalTime ( ) ) . ToString ( ) ;
8097 }
8198
82- private void UpdateCubemap ( int faceMask )
99+ /*
100+ Layer 0: Default
101+ Layer 1: TransparentFX
102+ Layer 2: Ignore Raycast
103+ Layer 3:
104+ Layer 4: Water
105+ Layer 5: UI
106+ Layer 6:
107+ Layer 7:
108+ Layer 8: PartsList_Icons
109+ Layer 9: Atmosphere
110+ Layer 10: Scaled Scenery
111+ Layer 11: UI_Culled
112+ Layer 12: UI_Main
113+ Layer 13: UI_Mask
114+ Layer 14: Screens
115+ Layer 15: Local Scenery
116+ Layer 16: kerbals
117+ Layer 17: Editor_UI
118+ Layer 18: SkySphere
119+ Layer 19: Disconnected Parts
120+ Layer 20: Internal Space
121+ Layer 21: Part Triggers
122+ Layer 22: KerbalInstructors
123+ Layer 23: ScaledSpaceSun
124+ Layer 24: MapFX
125+ Layer 25: EzGUI_UI
126+ Layer 26: WheelCollidersIgnore
127+ Layer 27: WheelColliders
128+ Layer 28: TerrainColliders
129+ Layer 29: DragRender
130+ Layer 30: SurfaceFX
131+ */
132+
133+ protected void UpdateCubemap ( int faceMask )
83134 {
84- if ( ! ( bool ) ( ( UnityEngine . Object ) this . _cam ) )
135+
136+ if ( ( object ) _cam == null )
137+ {
138+ _go = new GameObject ( "CubemapCamera" + i . ToString ( ) ) ;
139+ _go . AddComponent ( typeof ( Camera ) ) ;
140+ _go . hideFlags = HideFlags . HideAndDontSave ;
141+ _go . transform . position = transform . position ;
142+ _go . transform . rotation = Quaternion . identity ;
143+ _go . camera . clearFlags = CameraClearFlags . Skybox ;
144+ _go . camera . renderingPath = RenderingPath . UsePlayerSettings ;
145+ _go . camera . depth = Camera . main . depth + 1 ;
146+ _go . camera . aspect = 1 ;
147+ float [ ] distances = new float [ 32 ] ;
148+
149+ distances [ 10 ] = 3.0E+07F ;
150+ distances [ 15 ] = 3.0E+07F ;
151+
152+ _go . camera . layerCullDistances = distances ;
153+
154+ //_go.camera.cullingMask = LayerMask. //(1 << 0) | (1 << 4) | (1 << 9) | (1 << 10) | (1 << 15) | (1 << 18) | (1 << 23);
155+ _cam = _go . camera ;
156+ _cam . nearClipPlane = NearClipPlane ;
157+ _cam . farClipPlane = FarClipPlane ;
158+ _cam . enabled = false ;
159+ i += 1 ;
160+ }
161+
162+
163+ /*
164+ if (!(bool)((UnityEngine.Object)_cam))
85165 {
86- GameObject gameObject1 = new GameObject ( "ReflectionCamera " , new System . Type [ 1 ] { typeof ( Camera ) } ) ;
166+ GameObject gameObject1 = new GameObject("CubemapCamera ", new System.Type[1] { typeof(Camera) });
87167 gameObject1.hideFlags = HideFlags.HideAndDontSave;
88168 GameObject gameObject2 = gameObject1;
89- gameObject2 . transform . position = this . transform . position ;
169+ gameObject2.transform.position = transform.position;
90170 gameObject2.transform.rotation = Quaternion.identity;
91- this . _cam = gameObject2 . camera ;
92- this . _cam . hdr = true ;
93- this . _cam . depth = Camera . main . depth + 1 ;
94- this . _cam . farClipPlane = this . FarClipPlane ;
95- this . _cam . enabled = false ;
96- this . _cam . cullingMask = ( 1 << 0 ) | ( 1 << 4 ) | ( 1 << 9 ) | ( 1 << 10 ) | ( 1 << 15 ) | ( 1 << 18 ) | ( 1 << 23 ) ;
97- //this. _cam.cullingMask = (1 << maskBit);
98- this . _cam . layerCullSpherical = true ;
99- if ( this . NearClipPlane > 0f )
100- this . _cam . nearClipPlane = this . NearClipPlane ;
171+ _cam = gameObject2.camera;
172+ // _cam.hdr = true;
173+ _cam.depth = Camera.main.depth + 1;// Camera.main.depth + 1;
174+ _cam.farClipPlane = FarClipPlane;
175+ _cam.enabled = false;
176+ _cam.cullingMask = (1 << 0) | (1 << 4) | (1 << 9) | (1 << 10) | (1 << 15) | (1 << 18) | (1 << 23);
177+ //_cam.cullingMask = (1 << maskBit);
178+ // _cam.layerCullSpherical = true;
179+ if (NearClipPlane > 0f)
180+ _cam.nearClipPlane = NearClipPlane;
101181 for (int i = 0; i < 32; i++)
102182 {
103- this . _cam . layerCullDistances [ i ] = int . MaxValue ;
183+ _cam.layerCullDistances[i] = int.MaxValue;
104184 }
105- // -1 -False True
106- //this.status = this._cam.cullingMask.ToString();
107- //this._cam.cullingMask = 32771;
185+ status = _cam.cullingMask.ToString();
108186 }
109- if ( ! ( bool ) ( ( UnityEngine . Object ) this . _rtex ) )
187+ */
188+ if ( ( object ) _rtex == null )
110189 {
111190 ReflectiveScript reflectiveScript = this ;
112- RenderTexture renderTexture1 = new RenderTexture ( this . CubemapSize , this . CubemapSize , 16 ) ;
191+ RenderTexture renderTexture1 = new RenderTexture ( CubemapSize , CubemapSize , 16 ) ;
113192 renderTexture1 . isCubemap = true ;
193+ renderTexture1 . isPowerOfTwo = true ;
114194 renderTexture1 . hideFlags = HideFlags . HideAndDontSave ;
195+ renderTexture1 . useMipMap = true ;
196+ renderTexture1 . wrapMode = TextureWrapMode . Clamp ;
115197 RenderTexture renderTexture2 = renderTexture1 ;
198+ _cam . backgroundColor = Color . black ;
199+
116200 reflectiveScript . _rtex = renderTexture2 ;
117- this . MatRenderer . sharedMaterial . SetTexture ( "_Cube" , ( Texture ) this . _rtex ) ;
201+ //MatRenderer.sharedMaterial.SetTexture("_Cube", (Texture)_rtex);
202+ foreach ( Renderer r in GetComponentsInChildren < Renderer > ( ) )
203+ {
204+ foreach ( Material m in r . sharedMaterials )
205+ {
206+ if ( m . HasProperty ( "_Cube" ) )
207+ {
208+ m . SetTexture ( "_Cube" , _rtex ) ;
209+ }
210+ }
211+ }
118212 }
119- this . _cam . transform . position = this . transform . position ;
120- if ( this . _cam . RenderToCubemap ( this . _rtex , faceMask ) )
213+ if ( ( object ) _cam == null )
214+ Debug . Log ( "_cam null" ) ;
215+ if ( ( object ) _rtex == null )
216+ Debug . Log ( "_rtex null" ) ;
217+ if ( ( object ) _go == null )
218+ Debug . Log ( "_go null" ) ;
219+
220+
221+ _cam . transform . position = transform . position ;
222+ if ( _cam . RenderToCubemap ( _rtex , faceMask ) )
121223 {
122- this . lastUpdate = Planetarium . GetUniversalTime ( ) ;
123- this . lastRealUpdate = Time . time ;
224+ //Debug.Log ("Checking time");
225+ lastUpdate = Planetarium . GetUniversalTime ( ) ;
226+ lastRealUpdate = Time . time ;
227+ //Debug.Log ("Setting material");
228+ //currentMaterial.SetTexture ("_Cube", _rtex);
229+ //renderer.material = currentMaterial;
124230 renderResult = "Success: (next)" ;
125- if ( this . dirty > 0 )
231+ if ( dirty > 0 )
126232 {
127233 if ( faceMask == 63 )
128- this . dirty = 0 ;
234+ dirty = 0 ;
129235 else
130236 {
131- this . dirty -= 1 ;
237+ dirty -= 1 ;
132238 }
133239 }
134240 }
135241 else
136242 renderResult = "Failure: (next)" ;
243+ //Debug.Log ("Result = " + renderResult);
137244 }
138245 private void OnDisable ( )
139246 {
140- UnityEngine . Object . DestroyImmediate ( ( UnityEngine . Object ) this . _cam ) ;
141- UnityEngine . Object . DestroyImmediate ( ( UnityEngine . Object ) this . _rtex ) ;
247+ UnityEngine . Object . DestroyImmediate ( ( UnityEngine . Object ) _cam ) ;
248+ UnityEngine . Object . DestroyImmediate ( ( UnityEngine . Object ) _rtex ) ;
249+ UnityEngine . Object . DestroyImmediate ( ( UnityEngine . Object ) _go ) ;
142250 }
143251 }
144252}
0 commit comments