/// <summary>This will return a list of all paintables that overlap the specified bounds</summary> public static List <P3dModel> FindOverlap(Vector3 position, float radius, int layerMask) { tempModels.Clear(); foreach (var instance in instances) { if (P3dHelper.IndexInMask(instance.CachedGameObject.layer, layerMask) == true && instance.Paintable != null) { var bounds = instance.CachedRenderer.bounds; var sqrRadius = radius + bounds.extents.magnitude; sqrRadius *= sqrRadius; if (Vector3.SqrMagnitude(position - bounds.center) < sqrRadius) { tempModels.Add(instance); if (instance.paintable.Activated == false) { instance.paintable.Activate(); } } } } return(tempModels); }
/// <summary>This will return a list of all paintables that overlap the specified bounds</summary> public static List <P3dModel> FindOverlap(Vector3 position, float radius, int layerMask) { tempModels.Clear(); var model = FirstInstance; for (var i = 0; i < InstanceCount; i++) { if (P3dHelper.IndexInMask(model.CachedGameObject.layer, layerMask) == true && model.Paintable != null) { var bounds = model.CachedRenderer.bounds; var sqrRadius = radius + bounds.extents.magnitude; sqrRadius *= sqrRadius; if (Vector3.SqrMagnitude(position - bounds.center) < sqrRadius) { tempModels.Add(model); if (model.paintable.Activated == false) { model.paintable.Activate(); } } } model = model.NextInstance; } return(tempModels); }
private void CheckCollision(Collision collision) { if (cooldown > 0.0f) { return; } var impulse = collision.impulse.magnitude / Time.fixedDeltaTime; // Only handle the collision if the impact was strong enough if (impulse >= impactMin) { cooldown = delay; // Calculate up vector ahead of time var finalUp = orientation == OrientationType.CameraUp ? P3dHelper.GetCameraUp(_camera) : Vector3.up; var contacts = collision.contacts; var pressure = Mathf.InverseLerp(impactMin, impactPressure, impulse); var finalRoot = root != null ? root : gameObject; for (var i = contacts.Length - 1; i >= 0; i--) { var contact = contacts[i]; if (P3dHelper.IndexInMask(contact.otherCollider.gameObject.layer, layers) == true) { var finalPosition = contact.point + contact.normal * offset; var finalRotation = Quaternion.LookRotation(-contact.normal, finalUp); hitCache.InvokePoint(finalRoot, preview, priority, pressure, finalPosition, finalRotation); if (raycastDistance > 0.0f) { var ray = new Ray(contact.point + contact.normal * raycastDistance, -contact.normal); var hit = default(RaycastHit); if (contact.otherCollider.Raycast(ray, out hit, raycastDistance * 2.0f) == true) { hitCache.InvokeRaycast(finalRoot, preview, priority, pressure, hit, finalRotation); } } if (onlyUseFirstContact == true) { break; } } } } }
public static P3dMask Find(Vector3 position, LayerMask layers) { var bestMask = default(P3dMask); var bestDistance = float.PositiveInfinity; foreach (var instance in instances) { if (P3dHelper.IndexInMask(instance.gameObject.layer, layers) == true) { var distance = Vector3.SqrMagnitude(position - instance.transform.position); if (distance < bestDistance) { bestMask = instance; } } } return(bestMask); }
/// <summary>This allows you to get a list of all paintable textures on a P3dModel/P3dPaintable within the specified group mask.</summary> public static List <P3dPaintableTexture> Filter(P3dModel model, int groupMask) { tempPaintableTextures.Clear(); if (model.Paintable != null) { var paintableTextures = model.Paintable.PaintableTextures; for (var i = paintableTextures.Count - 1; i >= 0; i--) { var paintableTexture = paintableTextures[i]; if (P3dHelper.IndexInMask(paintableTexture.group, groupMask) == true) { tempPaintableTextures.Add(paintableTexture); } } } return(tempPaintableTextures); }
public static P3dMask Find(Vector3 position, LayerMask layers) { var mask = FirstInstance; var bestMask = default(P3dMask); var bestDistance = float.PositiveInfinity; for (var i = 0; i < InstanceCount; i++) { if (P3dHelper.IndexInMask(mask.gameObject.layer, layers) == true) { var distance = Vector3.SqrMagnitude(position - mask.transform.position); if (distance < bestDistance) { bestMask = mask; } } mask = mask.NextInstance; } return(bestMask); }
public void Paint(P3dCommand command, List <P3dWindowPaintable> paintables) { var commandMaterial = command.Material; //if (bounds.Intersects(lastBounds) == true) { for (var i = PaintableTextures.Count - 1; i >= 0; i--) { var paintableTexture = PaintableTextures[i]; var renderTexture = paintableTexture.PreparePaint(); // Prepare the paint regardless, to sync undo states if (P3dHelper.IndexInMask(paintableTexture.Group, command.Groups) == true) { var oldActive = RenderTexture.active; var swap = default(RenderTexture); RenderTexture.active = renderTexture; if (true) // TODO { swap = P3dHelper.GetRenderTexture(renderTexture.width, renderTexture.height, renderTexture.depth, renderTexture.format); P3dHelper.Blit(swap, renderTexture); commandMaterial.SetTexture(P3dShader._Buffer, swap); } command.Apply(); if (command.RequireMesh == true) { for (var j = paintables.Count - 1; j >= 0; j--) { var otherP = paintables[j]; for (var k = otherP.PaintableTextures.Count - 1; k >= 0; k--) { var otherT = otherP.PaintableTextures[k]; if (otherT.OldTexture == paintableTexture.OldTexture) { switch (otherT.Channel) { case P3dChannel.UV: commandMaterial.SetVector(P3dShader._Channel, new Vector4(1.0f, 0.0f, 0.0f, 0.0f)); break; case P3dChannel.UV2: commandMaterial.SetVector(P3dShader._Channel, new Vector4(0.0f, 1.0f, 0.0f, 0.0f)); break; case P3dChannel.UV3: commandMaterial.SetVector(P3dShader._Channel, new Vector4(0.0f, 0.0f, 1.0f, 0.0f)); break; case P3dChannel.UV4: commandMaterial.SetVector(P3dShader._Channel, new Vector4(0.0f, 0.0f, 0.0f, 1.0f)); break; } commandMaterial.SetPass(0); Graphics.DrawMeshNow(otherP.lastMesh, otherP.lastMatrix, otherT.MaterialIndex); } } } } else { Graphics.Blit(default(Texture), renderTexture, commandMaterial); } RenderTexture.active = oldActive; if (swap != null) { P3dHelper.ReleaseRenderTexture(swap); } } } } }
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) { var sObj = property.serializedObject; var sPro = property.FindPropertyRelative("mask"); var right = position; right.xMin += EditorGUIUtility.labelWidth; var handle = default(string); EditorGUI.LabelField(position, property.displayName); if (sPro.intValue == 0) { handle = "Nothing"; } else if (sPro.intValue == -1) { handle = "Everything"; } else { //handle = "Mixed ..."; var total = 0; for (var i = 0; i < 32; i++) { var on = P3dHelper.IndexInMask(i, sPro.intValue); if (on == true) { if (total == 0) { handle = "Group " + i; } else { handle += ", Group " + i; } total += 1; } } } if (GUI.Button(right, handle, EditorStyles.popup) == true) { var menu = new GenericMenu(); menu.AddItem(new GUIContent("Nothing"), sPro.intValue == 0, () => { sPro.intValue = 0; sObj.ApplyModifiedProperties(); }); menu.AddItem(new GUIContent("Everything"), sPro.intValue == -1, () => { sPro.intValue = -1; sObj.ApplyModifiedProperties(); }); for (var i = 0; i < 32; i++) { var index = i; var content = new GUIContent("Group " + index); var on = P3dHelper.IndexInMask(index, sPro.intValue); menu.AddItem(content, on, () => { sPro.intValue ^= 1 << index; sObj.ApplyModifiedProperties(); }); } menu.DropDown(right); } }
private void CheckCollision(Collision collision) { if (cooldown > 0.0f) { return; } var impulse = collision.impulse.magnitude / Time.fixedDeltaTime; // Only handle the collision if the impact was strong enough if (impulse >= pressureMin) { cooldown = delay; // Calculate up vector ahead of time var finalUp = orientation == OrientationType.CameraUp ? P3dHelper.GetCameraUp(_camera) : Vector3.up; var contacts = collision.contacts; var finalPressure = pressureMultiplier; var finalRoot = root != null ? root : gameObject; switch (pressureMode) { case PressureType.Constant: { finalPressure *= pressureConstant; } break; case PressureType.ImpactSpeed: { finalPressure *= Mathf.InverseLerp(pressureMin, pressureMax, impulse); } break; } for (var i = contacts.Length - 1; i >= 0; i--) { var contact = contacts[i]; if (P3dHelper.IndexInMask(contact.otherCollider.gameObject.layer, layers) == true) { var finalPosition = contact.point + contact.normal * offset; var finalRotation = Quaternion.LookRotation(-contact.normal, finalUp); switch (emit) { case EmitType.PointsIn3D: { hitCache.InvokePoint(finalRoot, preview, priority, finalPressure, finalPosition, finalRotation); } break; case EmitType.PointsOnUV: { var hit = default(RaycastHit); if (TryGetRaycastHit(contact, ref hit) == true) { hitCache.InvokeCoord(finalRoot, preview, priority, finalPressure, new P3dHit(hit), finalRotation); } } break; case EmitType.TrianglesIn3D: { var hit = default(RaycastHit); if (TryGetRaycastHit(contact, ref hit) == true) { hitCache.InvokeTriangle(gameObject, preview, priority, finalPressure, hit, finalRotation); } } break; } if (onlyUseFirstContact == true) { break; } } } } }
protected virtual void OnParticleCollision(GameObject hitGameObject) { if (cachedParticleSystemSet == false) { cachedParticleSystem = GetComponent <ParticleSystem>(); cachedParticleSystemSet = true; } // Get the collision events array var count = cachedParticleSystem.GetSafeCollisionEventSize(); // Expand collisionEvents list to fit all particles for (var i = particleCollisionEvents.Count; i < count; i++) { particleCollisionEvents.Add(new ParticleCollisionEvent()); } count = cachedParticleSystem.GetCollisionEvents(hitGameObject, particleCollisionEvents); // Calculate up vector ahead of time var finalUp = orientation == OrientationType.CameraUp ? P3dHelper.GetCameraUp(_camera) : Vector3.up; var finalRoot = root != null ? root : gameObject; // Paint all locations for (var i = 0; i < count; i++) { var collision = particleCollisionEvents[i]; if (P3dHelper.IndexInMask(collision.colliderComponent.gameObject.layer, layers) == false) { continue; } if (skip > 0) { if (skipCounter++ > skip) { skipCounter = 0; } else { continue; } } var finalPosition = collision.intersection + collision.normal * offset; var finalNormal = normal == NormalType.CollisionNormal ? collision.normal : -collision.velocity; var finalRotation = finalNormal != Vector3.zero ? Quaternion.LookRotation(-finalNormal, finalUp) : Quaternion.identity; var finalPressure = pressureMultiplier; switch (pressureMode) { case PressureType.Constant: { finalPressure *= pressureConstant; } break; case PressureType.Distance: { var distance = Vector3.Distance(transform.position, collision.intersection); finalPressure *= Mathf.InverseLerp(pressureMin, pressureMax, distance); } break; case PressureType.Speed: { var speed = Vector3.SqrMagnitude(collision.velocity); if (speed > 0.0f) { speed = Mathf.Sqrt(speed); } finalPressure *= Mathf.InverseLerp(pressureMin, pressureMax, speed); } break; } switch (emit) { case EmitType.PointsIn3D: { hitCache.InvokePoint(finalRoot, preview, priority, finalPressure, finalPosition, finalRotation); } break; case EmitType.PointsOnUV: { var hit = default(RaycastHit); if (TryGetRaycastHit(collision, ref hit) == true) { hitCache.InvokeCoord(finalRoot, preview, priority, finalPressure, new P3dHit(hit), finalRotation); } } break; case EmitType.TrianglesIn3D: { var hit = default(RaycastHit); if (TryGetRaycastHit(collision, ref hit) == true) { hitCache.InvokeTriangle(gameObject, preview, priority, finalPressure, hit, finalRotation); } } break; } } }