protected virtual void PrepareForUpdate() { // Set our main thread stuff numVertices = originalVertices.Length; numInnerBubbles = InnerBubbles.Count; time = Time.time; deltaTime = time - timeLastUpdated; timeLastUpdated = time; adjustedRadius = radius * (1 + radiusMultiplier); bubbleWorldPos = transform.position; for (int i = 0; i < innerBubbles.Count; i++) { InnerBubble b = innerBubbles[i]; b.Active = b.Transform != null && b.Transform.gameObject.activeSelf; b.InnerPos = transform.InverseTransformPoint(b.Transform.position); b.DistToCenter = Vector3.Distance(b.Transform.position, transform.position); if (b.DistToCenter < radius - b.Radius) { if (!b.InsideBubble) { OnEnterBubble?.Invoke(i); } b.InsideBubble = true; } else { if (b.InsideBubble) { OnExitBubble?.Invoke(i); } b.InsideBubble = false; } innerBubbles[i] = b; } }
protected void CalculateInternalBubbleForces(Vector3 currentVertex, int vertexIndex) { Vector3 innerBubblePos = zero; //Vector3.zero; Vector3 vertexDistVector = zero; //Vector3.zero; Vector3 currentInnerBubbleForce = innerBubbleForces[vertexIndex]; Vector3 newInnerBubbleForce = zero;//Vector3.zero; for (int i = 0; i < numInnerBubbles; i++) { InnerBubble innerBubble = innerBubbles[i]; if (!innerBubble.Active) { continue; } innerBubblePos = innerBubble.InnerPos; vertexDistVector.x = currentVertex.x - innerBubblePos.x; vertexDistVector.y = currentVertex.y - innerBubblePos.y; vertexDistVector.z = currentVertex.z - innerBubblePos.z; float innerBubbleRadius = Mathf.Max(minInnerBubbleRadius * adjustedRadius, innerBubble.Radius); float sqrDistToInnerBubble = (vertexDistVector.x * vertexDistVector.x + vertexDistVector.y * vertexDistVector.y + vertexDistVector.z * vertexDistVector.z); float normalizedDistToInnerBubble = sqrDistToInnerBubble / (innerBubbleRadius * innerBubbleRadius); if (normalizedDistToInnerBubble >= 1f) { continue; } if (normalizedDistToInnerBubble < 0) { normalizedDistToInnerBubble = 0; } // Make the force direction come from the inner-most point of the inner bubble innerBubblePos -= (innerBubblePos.normalized * innerBubbleRadius); // Then re-calculate the direction from the bubble pos to the vertex vertexDistVector.x = currentVertex.x - innerBubblePos.x; vertexDistVector.y = currentVertex.y - innerBubblePos.y; vertexDistVector.z = currentVertex.z - innerBubblePos.z; // If the inner bubble is outside the radius, push the surface in // Otherwise, push the surface out if (innerBubble.DistToCenter > (radius - innerBubble.Radius)) { vertexDistVector.x = -vertexDistVector.x; vertexDistVector.y = -vertexDistVector.y; vertexDistVector.z = -vertexDistVector.z; } float forceToInnerBubble = innerBubbleForceTable[(int)(normalizedDistToInnerBubble * (tableResolution - 1))] * innerBubbleForceMultiplier; //innerBubbleForceCurve.Evaluate(normalizedDistToInnerBubble) * innerBubbleForceMultiplier; newInnerBubbleForce.x += (vertexDistVector.x * forceToInnerBubble); newInnerBubbleForce.y += (vertexDistVector.y * forceToInnerBubble); newInnerBubbleForce.z += (vertexDistVector.z * forceToInnerBubble); if (useVertexColors && innerBubble.UseColor) { vertexColor = currentColors[vertexIndex]; float alpha = Mathf.Max(vertexColor.a, normalizedDistToInnerBubble * normalizedDistToInnerBubble); vertexColor = Color.Lerp(vertexColor, innerBubble.Color, normalizedDistToInnerBubble); vertexColor.a = alpha; currentColors[vertexIndex] = vertexColor; } } newInnerBubbleForce = Vector3.Lerp(currentInnerBubbleForce, newInnerBubbleForce, deltaTime * innerBubbleForceInertia); innerBubbleForces[vertexIndex] = newInnerBubbleForce; }