static bool Prefix(MirrorReflection __instance) { Map Map; if (null != (Map = UnityEngine.Object.FindObjectOfType <Map>())) { Map.GetType().GetField("mirrors", BindingFlags.Instance | BindingFlags.NonPublic).SetValue(Map, null); } MirrorReflectionPlus MirrorPlus; if (null == (MirrorPlus = __instance.GetComponent <MirrorReflectionPlus>())) { MirrorPlus = __instance.gameObject.AddComponent <MirrorReflectionPlus>(); } int texturesize = ModPrefs.GetInt("MirrorHelper", "Resolution", 2048, true); if (texturesize <= 1024) { texturesize = 1024; } else if (texturesize <= 2048) { texturesize = 2048; } else { texturesize = 4096; } MirrorPlus.m_ClipPlaneOffset = ModPrefs.GetFloat("MirrorHelper", "ClipPlaneOffset", 0, true); MirrorPlus.m_ReflectLayers = __instance.m_ReflectLayers; MirrorPlus.m_TextureSize = texturesize; UnityEngine.Object.Destroy(__instance); return(false); }
private static void CalculateObliqueMatrix(ref Matrix4x4 projection, Vector4 clipPlane) { Vector4 b = projection.inverse * new Vector4(MirrorReflection.sgn(clipPlane.x), MirrorReflection.sgn(clipPlane.y), 1f, 1f); Vector4 vector = clipPlane * (2f / Vector4.Dot(clipPlane, b)); projection[2] = vector.x - projection[3]; projection[6] = vector.y - projection[7]; projection[10] = vector.z - projection[11]; projection[14] = vector.w - projection[15]; }
public void OnWillRenderObject() { if (!base.enabled || !base.GetComponent <Renderer>() || !base.GetComponent <Renderer>().sharedMaterial || !base.GetComponent <Renderer>().enabled) { return; } Camera current = Camera.current; if (!current) { return; } if (MirrorReflection.s_InsideRendering) { return; } MirrorReflection.s_InsideRendering = true; Camera camera; this.CreateMirrorObjects(current, out camera); Vector3 position = base.transform.position; Vector3 up = base.transform.up; int pixelLightCount = QualitySettings.pixelLightCount; if (this.m_DisablePixelLights) { QualitySettings.pixelLightCount = 0; } this.UpdateCameraModes(current, camera); float w = -Vector3.Dot(up, position) - this.m_ClipPlaneOffset; Vector4 plane = new Vector4(up.x, up.y, up.z, w); Matrix4x4 zero = Matrix4x4.zero; MirrorReflection.CalculateReflectionMatrix(ref zero, plane); Vector3 position2 = current.transform.position; Vector3 position3 = zero.MultiplyPoint(position2); camera.worldToCameraMatrix = current.worldToCameraMatrix * zero; Vector4 clipPlane = this.CameraSpacePlane(camera, position, up, 1f); Matrix4x4 projectionMatrix = current.projectionMatrix; MirrorReflection.CalculateObliqueMatrix(ref projectionMatrix, clipPlane); camera.projectionMatrix = projectionMatrix; camera.cullingMask = (-17 & this.m_ReflectLayers.value); camera.targetTexture = this.m_ReflectionTexture; GL.invertCulling = true; camera.transform.position = position3; Vector3 eulerAngles = current.transform.eulerAngles; camera.transform.eulerAngles = new Vector3(0f, eulerAngles.y, eulerAngles.z); camera.Render(); camera.transform.position = position2; GL.invertCulling = false; Material[] sharedMaterials = base.GetComponent <Renderer>().sharedMaterials; foreach (Material material in sharedMaterials) { if (material.HasProperty("_ReflectionTex")) { material.SetTexture("_ReflectionTex", this.m_ReflectionTexture); } } Matrix4x4 lhs = Matrix4x4.TRS(new Vector3(0.5f, 0.5f, 0.5f), Quaternion.identity, new Vector3(0.5f, 0.5f, 0.5f)); Vector3 lossyScale = base.transform.lossyScale; Matrix4x4 matrix4x = base.transform.localToWorldMatrix * Matrix4x4.Scale(new Vector3(1f / lossyScale.x, 1f / lossyScale.y, 1f / lossyScale.z)); matrix4x = lhs * current.projectionMatrix * current.worldToCameraMatrix * matrix4x; foreach (Material material2 in sharedMaterials) { material2.SetMatrix("_ProjMatrix", matrix4x); } if (this.m_DisablePixelLights) { QualitySettings.pixelLightCount = pixelLightCount; } MirrorReflection.s_InsideRendering = false; }
void Start() { m = WaterPlane.GetComponent <MirrorReflection> (); }
void Awake() { mirrorReflection = GameObject.FindObjectOfType(typeof(MirrorReflection)) as MirrorReflection; }
public static void MirrorReflection_CreateMirrorObjects_Pre(MirrorReflection __instance) { __instance.m_TextureSize = 2048; }
// Token: 0x0600001C RID: 28 RVA: 0x00002970 File Offset: 0x00000B70 private void RenderReflectionFor(Camera cam, Camera reflectCamera) { if (!reflectCamera) { return; } if (this.m_SharedMaterial && !this.m_SharedMaterial.HasProperty(this.reflectionSampler)) { return; } int pixelLightCount = QualitySettings.pixelLightCount; if (this.m_DisablePixelLights) { QualitySettings.pixelLightCount = 0; } reflectCamera.cullingMask = (this.reflectionMask & ~(1 << LayerMask.NameToLayer("Water"))); this.SaneCameraSettings(reflectCamera); reflectCamera.backgroundColor = this.clearColor; reflectCamera.clearFlags = (this.reflectSkybox ? CameraClearFlags.Skybox : CameraClearFlags.Color); if (this.reflectSkybox && cam.gameObject.GetComponent(typeof(Skybox))) { Skybox skybox = (Skybox)reflectCamera.gameObject.GetComponent(typeof(Skybox)); if (!skybox) { skybox = (Skybox)reflectCamera.gameObject.AddComponent(typeof(Skybox)); } skybox.material = ((Skybox)cam.GetComponent(typeof(Skybox))).material; } GL.invertCulling = true; Transform transform = base.transform; Vector3 eulerAngles = cam.transform.eulerAngles; reflectCamera.transform.eulerAngles = new Vector3(-eulerAngles.x, eulerAngles.y, eulerAngles.z); reflectCamera.transform.position = cam.transform.position; Vector3 position = transform.transform.position; position.y = transform.position.y; Vector3 up = transform.transform.up; float w = -Vector3.Dot(up, position) - this.clipPlaneOffset; Vector4 plane = new Vector4(up.x, up.y, up.z, w); Matrix4x4 matrix4x = Matrix4x4.zero; matrix4x = MirrorReflection.CalculateReflectionMatrix(matrix4x, plane); this.m_Oldpos = cam.transform.position; Vector3 position2 = matrix4x.MultiplyPoint(this.m_Oldpos); reflectCamera.worldToCameraMatrix = cam.worldToCameraMatrix * matrix4x; Vector4 clipPlane = this.CameraSpacePlane(reflectCamera, position, up, 1f); Matrix4x4 matrix4x2 = cam.projectionMatrix; matrix4x2 = MirrorReflection.CalculateObliqueMatrix(matrix4x2, clipPlane); reflectCamera.projectionMatrix = matrix4x2; reflectCamera.transform.position = position2; Vector3 eulerAngles2 = cam.transform.eulerAngles; reflectCamera.transform.eulerAngles = new Vector3(-eulerAngles2.x, eulerAngles2.y, eulerAngles2.z); reflectCamera.Render(); GL.invertCulling = false; if (this.m_DisablePixelLights) { QualitySettings.pixelLightCount = pixelLightCount; } }
public void OnWillRenderObject() { if (!((Behaviour)this).get_enabled() || !Object.op_Implicit((Object)((Component)this).GetComponent <Renderer>()) || (!Object.op_Implicit((Object)((Renderer)((Component)this).GetComponent <Renderer>()).get_sharedMaterial()) || !((Renderer)((Component)this).GetComponent <Renderer>()).get_enabled())) { return; } Camera current = Camera.get_current(); if (!Object.op_Implicit((Object)current) || MirrorReflection.s_InsideRendering) { return; } MirrorReflection.s_InsideRendering = true; Camera reflectionCamera; this.CreateMirrorObjects(current, out reflectionCamera); Vector3 position1 = ((Component)this).get_transform().get_position(); Vector3 forward = ((Component)this).get_transform().get_forward(); int pixelLightCount = QualitySettings.get_pixelLightCount(); if (this.m_DisablePixelLights) { QualitySettings.set_pixelLightCount(0); } this.UpdateCameraModes(current, reflectionCamera); float num = -Vector3.Dot(forward, position1) - this.m_ClipPlaneOffset; Vector4 plane; ((Vector4) ref plane).\u002Ector((float)forward.x, (float)forward.y, (float)forward.z, num); Matrix4x4 zero = Matrix4x4.get_zero(); MirrorReflection.CalculateReflectionMatrix(ref zero, plane); Vector3 position2 = ((Component)current).get_transform().get_position(); Vector3 vector3 = ((Matrix4x4) ref zero).MultiplyPoint(position2); reflectionCamera.set_worldToCameraMatrix(Matrix4x4.op_Multiply(current.get_worldToCameraMatrix(), zero)); Vector4 clipPlane = this.CameraSpacePlane(reflectionCamera, position1, forward, 1f); Matrix4x4 projectionMatrix = current.get_projectionMatrix(); MirrorReflection.CalculateObliqueMatrix(ref projectionMatrix, clipPlane); reflectionCamera.set_projectionMatrix(projectionMatrix); reflectionCamera.set_cullingMask(-17 & ((LayerMask) ref this.m_ReflectLayers).get_value()); reflectionCamera.set_targetTexture(this.m_ReflectionTexture); GL.set_invertCulling(true); ((Component)reflectionCamera).get_transform().set_position(vector3); Vector3 eulerAngles = ((Component)current).get_transform().get_eulerAngles(); ((Component)reflectionCamera).get_transform().set_eulerAngles(new Vector3(0.0f, (float)eulerAngles.y, (float)eulerAngles.z)); reflectionCamera.Render(); ((Component)reflectionCamera).get_transform().set_position(position2); GL.set_invertCulling(false); Material[] sharedMaterials = ((Renderer)((Component)this).GetComponent <Renderer>()).get_sharedMaterials(); foreach (Material material in sharedMaterials) { if (material.HasProperty(this._ReflectionTex)) { material.SetTexture(this._ReflectionTex, (Texture)this.m_ReflectionTexture); } } Matrix4x4 matrix4x4_1 = Matrix4x4.TRS(new Vector3(0.5f, 0.5f, 0.5f), Quaternion.get_identity(), new Vector3(0.5f, 0.5f, 0.5f)); Vector3 lossyScale = ((Component)this).get_transform().get_lossyScale(); Matrix4x4 matrix4x4_2 = Matrix4x4.op_Multiply(((Component)this).get_transform().get_localToWorldMatrix(), Matrix4x4.Scale(new Vector3((float)(1.0 / lossyScale.x), (float)(1.0 / lossyScale.y), (float)(1.0 / lossyScale.z)))); Matrix4x4 matrix4x4_3 = Matrix4x4.op_Multiply(Matrix4x4.op_Multiply(Matrix4x4.op_Multiply(matrix4x4_1, current.get_projectionMatrix()), current.get_worldToCameraMatrix()), matrix4x4_2); foreach (Material material in sharedMaterials) { material.SetMatrix(this._ProjMatrix, matrix4x4_3); } if (this.m_DisablePixelLights) { QualitySettings.set_pixelLightCount(pixelLightCount); } MirrorReflection.s_InsideRendering = false; }
private static void CalculateObliqueMatrix(ref Matrix4x4 projection, Vector4 clipPlane) { Vector4 vector4_1 = Matrix4x4.op_Multiply(((Matrix4x4) ref projection).get_inverse(), new Vector4(MirrorReflection.sgn((float)clipPlane.x), MirrorReflection.sgn((float)clipPlane.y), 1f, 1f)); Vector4 vector4_2 = Vector4.op_Multiply(clipPlane, 2f / Vector4.Dot(clipPlane, vector4_1)); ((Matrix4x4) ref projection).set_Item(2, (float)vector4_2.x - ((Matrix4x4) ref projection).get_Item(3)); ((Matrix4x4) ref projection).set_Item(6, (float)vector4_2.y - ((Matrix4x4) ref projection).get_Item(7)); ((Matrix4x4) ref projection).set_Item(10, (float)vector4_2.z - ((Matrix4x4) ref projection).get_Item(11)); ((Matrix4x4) ref projection).set_Item(14, (float)vector4_2.w - ((Matrix4x4) ref projection).get_Item(15)); }
void OnWizardCreate() { GameObject plane = new GameObject(); plane.name = "Water"; plane.transform.position = Vector3.zero; Vector2 anchorOffset = Vector2.zero;; MeshFilter meshFilter = (MeshFilter)plane.AddComponent(typeof(MeshFilter)); plane.AddComponent(typeof(MeshRenderer)); string planeAssetName = plane.name + "S" + meshWidthSegments + "W" + meshWidth + ".asset"; Mesh m = (Mesh)AssetDatabase.LoadAssetAtPath("Assets/MobileOcean/Meshes/" + planeAssetName, typeof(Mesh)); if (m == null) { m = new Mesh(); m.name = plane.name; int hCount2 = meshWidthSegments + 1; int vCount2 = meshWidthSegments + 1; int numTriangles = meshWidthSegments * meshWidthSegments * 6; int numVertices = hCount2 * vCount2; Vector3[] vertices = new Vector3[numVertices]; Vector2[] uvs = new Vector2[numVertices]; int[] triangles = new int[numTriangles]; int index = 0; float uvFactorX = 1.0f / meshWidthSegments; float uvFactorY = 1.0f / meshWidthSegments; float scaleX = meshWidth / meshWidthSegments; float scaleY = meshWidth / meshWidthSegments; for (float y = 0.0f; y < vCount2; y++) { for (float x = 0.0f; x < hCount2; x++) { vertices[index] = new Vector3(x * scaleX - meshWidth / 2f - anchorOffset.x, 0.0f, y * scaleY - meshWidth / 2f - anchorOffset.y); uvs[index++] = new Vector2(x * uvFactorX, y * uvFactorY); } } index = 0; for (int y = 0; y < meshWidthSegments; y++) { for (int x = 0; x < meshWidthSegments; x++) { triangles[index] = (y * hCount2) + x; triangles[index + 1] = ((y + 1) * hCount2) + x; triangles[index + 2] = (y * hCount2) + x + 1; triangles[index + 3] = ((y + 1) * hCount2) + x; triangles[index + 4] = ((y + 1) * hCount2) + x + 1; triangles[index + 5] = (y * hCount2) + x + 1; index += 6; } } m.vertices = vertices; m.uv = uvs; m.triangles = triangles; m.RecalculateNormals(); AssetDatabase.CreateAsset(m, "Assets/MobileOcean/Meshes/" + planeAssetName); AssetDatabase.SaveAssets(); } meshFilter.sharedMesh = m; m.RecalculateBounds(); Selection.activeObject = plane; GameObject ocean = new GameObject(); ocean.transform.position = Vector3.zero; MirrorReflection mirrorReflection = ocean.AddComponent <MirrorReflection>(); mirrorReflection.enableMirrorReflection = enableMirrorReflect; mirrorReflection.alpha = alpha; Material oceanMaterial; // mirrorReflection.enableShoreLine = enableShoreLine; if (enableShoreLine) { int terrainWidth = (int)meshWidth * meshCountX; int terrainHeight = (int)meshWidth * meshCountZ; int texWidth = 1024; int texHeight = 1024; Material oceanMaterialPrefab = Resources.Load("OceanResources/OceanShoreLine") as Material; oceanMaterial = new Material(oceanMaterialPrefab); string path = "Assets/MobileOcean/Materials/OceanMats/OceanShoreLine_" + System.DateTime.Now.ToString("yyyyMMddHHmmss") + ".mat"; AssetDatabase.CreateAsset(oceanMaterial, path); string texPath = GrayScaleCreator.CreateTexture(texWidth, texHeight, terrainWidth, terrainHeight, shoreLineDetectLayer, shoreLineDepth); Texture2D grayTex = (Texture2D)(AssetDatabase.LoadAssetAtPath(texPath, typeof(Texture2D))); oceanMaterial.SetTexture("_ShoreGray", grayTex); oceanMaterial.SetFloat("_OceanWidth", 1f / terrainWidth); oceanMaterial.SetFloat("_OceanHeight", 1f / terrainHeight); AssetDatabase.SaveAssets(); AssetDatabase.Refresh(); ocean.name = "OceanHigh"; } else { oceanMaterial = Resources.Load("OceanResources/OceanLow") as Material; // mirrorReflection.enableShoreLine = false; ocean.name = "OceanLow"; } ocean.layer = LayerMask.NameToLayer("Water"); plane.GetComponent <Renderer>().material = oceanMaterial; mirrorReflection.oceanMaterial = oceanMaterial; if (enableMirrorReflect) { mirrorReflection.enableMirrorReflection = true; } for (int i = 0; i < meshCountX; i++) { for (int j = 0; j < meshCountZ; j++) { GameObject newPlane = Instantiate(plane) as GameObject; newPlane.transform.parent = ocean.transform; newPlane.name = "Water" + i + j; float posX = -meshWidth * meshCountX * 0.5f + 0.5f * meshWidth + i * meshWidth; float posZ = -meshWidth * meshCountZ * 0.5f + 0.5f * meshWidth + j * meshWidth; newPlane.transform.localPosition = new Vector3(posX, 0, posZ); newPlane.layer = LayerMask.NameToLayer("Water"); } } DestroyImmediate(plane); SavePlayerPrefs(); }