private void OnWillRenderObject() { AntiPortalCuller culler = Camera.current.GetComponent <AntiPortalCuller>(); if (culler != null && culler.enabled) { if (this.isStatic == false) { //a GameObject maybe render multiple times in one frame, just update bounds once if (this.lastFrame != Time.frameCount) { this.bounds = this.renderer.bounds; this.lastFrame = Time.frameCount; } } culler.AddOccludee(this); } }
void OnGUI() { GUIStyle style = new GUIStyle(); style.fontSize = 26; GUI.Label(new Rect(0, 0, 100, 100), " " + fps.ToString("f2"), style); if (GUI.Button(new Rect(0, 100, 100, 100), "")) { AntiPortalCuller culler = Camera.main.GetComponent <AntiPortalCuller>(); culler.enabled = !culler.enabled; Camera.main.useOcclusionCulling = !culler.enabled; } //if (GUI.Button(new Rect(0, 100, 100, 100), "")) //{ // Bounds[] b = new Bounds[10]; // bool[] v = new bool[10]; // NativeCull.Cull(b, v); //} }
private void OnWillRenderObject() { Camera camera = Camera.current; AntiPortalCuller culler = camera.GetComponent <AntiPortalCuller>(); if (culler != null && culler.enabled) { Vector2 min = Vector2.positiveInfinity; Vector2 max = Vector2.negativeInfinity; for (int i = 0; i < 8; i++) { Vector3 corner = this.corners[i]; Vector3 screenPos = camera.WorldToViewportPoint(corner); //if occluder cross with camera's near plane, ignore this occluder if (screenPos.z < camera.nearClipPlane) { return; } //if occluder cross with camera's far plane, this occluder maybe too far and no renderer in frustum will be occluded if (screenPos.z > camera.farClipPlane) { return; } bool visible = true; if (screenPos.x < -1.0f) { screenPos.x = -1.0f; visible = false; } else if (screenPos.x > 1.0f) { screenPos.x = 1.0f; visible = false; } if (screenPos.y < -1.0f) { screenPos.y = -1.0f; visible = false; } else if (screenPos.y > 1.0f) { screenPos.y = 1.0f; visible = false; } this.cornerVisible[i] = visible; if (screenPos.x < min.x) { min.x = screenPos.x; } else if (screenPos.x > max.x) { max.x = screenPos.x; } if (screenPos.y < min.y) { min.y = screenPos.y; } else if (screenPos.y > max.y) { max.y = screenPos.y; } } this.screenArea = (max.x - min.x) * (max.y - min.y); culler.AddOccluder(this); } }