Obi Official Forum
Help Counting Fluid & Android Export - Printable Version

+- Obi Official Forum (https://obi.virtualmethodstudio.com/forum)
+-- Forum: Obi Users Category (https://obi.virtualmethodstudio.com/forum/forum-1.html)
+--- Forum: Obi Fluid (https://obi.virtualmethodstudio.com/forum/forum-3.html)
+--- Thread: Help Counting Fluid & Android Export (/thread-750.html)

Pages: 1 2


Counting Fluid & Android Export - Tuna.Y - 15-11-2018

Hi All,

I bought this asset a few days ago and everything was going well until I tried to count particles Sonrisa

I know this page: http://obi.virtualmethodstudio.com/tutorials/scriptingcollisions.html and spent time there but I couldn't manage to "count" particles. It would be amazing if someone give hand about that.

I have my box collider and script on my Solver object. There is a counter in the script, but it counts as soon as 1 collision happens BETWEEN the fluid particles (not the collider) and it never stops increasing.

Here is my code;

Code:
using Obi;
using System.Collections.Generic;
using UnityEngine;

[RequireComponent(typeof(ObiSolver))]

public class Fill : MonoBehaviour
{
   ObiSolver solver;
   public int counter = 0;
   public Collider2D targetCollider = null;

   ObiSolver.ObiCollisionEventArgs collisionEvent;

   void Awake()
   {
       solver = GetComponent<ObiSolver>();

   }

   void OnEnable()
   {
       solver.OnCollision += Solver_OnCollision;
   }

   void OnDisable()
   {
       solver.OnCollision -= Solver_OnCollision;
   }

   void Solver_OnCollision(object sender, ObiSolver.ObiCollisionEventArgs e)
   {
       foreach (Oni.Contact contact in e.contacts)
       {
           // this one is an actual collision:
           if (contact.distance < 0.01)
           {
               Component collider;
               if (ObiCollider.idToCollider.TryGetValue(contact.other, out collider))
               {
                   counter++;
               }
           }
       }
   }
}


Second thing is, I only see pink screen when I export my game to the Android. I found a similar issue on the forum, did the suggestion (It was something like changing current shader to simple shader) and it didn't work.

Thanks in advance.


RE: Counting Fluid & Android Export - josemendez - 15-11-2018

(15-11-2018, 06:39 PM)Tuna.Y Wrote: Hi All,

I bought this asset a few days ago and everything was going well until I tried to count particles Sonrisa

I know this page: http://obi.virtualmethodstudio.com/tutorials/scriptingcollisions.html and spent time there but I couldn't manage to "count" particles. It would be amazing if someone give hand about that.

I have my box collider and script on my Solver object. There is a counter in the script, but it counts as soon as 1 collision happens BETWEEN the fluid particles (not the collider) and it never stops increasing.

Here is my code;

Code:
using Obi;
using System.Collections.Generic;
using UnityEngine;

[RequireComponent(typeof(ObiSolver))]

public class Fill : MonoBehaviour
{
   ObiSolver solver;
   public int counter = 0;
   public Collider2D targetCollider = null;

   ObiSolver.ObiCollisionEventArgs collisionEvent;

   void Awake()
   {
       solver = GetComponent<ObiSolver>();

   }

   void OnEnable()
   {
       solver.OnCollision += Solver_OnCollision;
   }

   void OnDisable()
   {
       solver.OnCollision -= Solver_OnCollision;
   }

   void Solver_OnCollision(object sender, ObiSolver.ObiCollisionEventArgs e)
   {
       foreach (Oni.Contact contact in e.contacts)
       {
           // this one is an actual collision:
           if (contact.distance < 0.01)
           {
               Component collider;
               if (ObiCollider.idToCollider.TryGetValue(contact.other, out collider))
               {
                   counter++;
               }
           }
       }
   }
}


Second thing is, I only see pink screen when I export my game to the Android. I found a similar issue on the forum, did the suggestion (It was something like changing current shader to simple shader) and it didn't work.

Thanks in advance.

Hi,

You're counting how many particles are there in the collider every frame, completely disregarding particle IDs when counting. So if a particle was already inside the collider the previous frame, it will be counted again next frame. That's why the counter will never stop increasing as long as there's any particle inside of it.

There's several solutions for counting particles in the forum, here's one:
http://obi.virtualmethodstudio.com/forum/showthread.php?tid=708&highlight=collision

As for the pink screen in Android, check what the ADB log says. Your particular device isn't capable of running one or more of the default fluid shaders, so you might have to modify them.


RE: Counting Fluid & Android Export - Tuna.Y - 15-11-2018

(15-11-2018, 06:44 PM)josemendez Wrote: Hi,

You're counting how many particles are there in the collider every frame, completely disregarding particle IDs when counting. So if a particle was already inside the collider the previous frame, it will be counted again next frame. That's why the counter will never stop increasing as long as there's any particle inside of it.

There's several solutions for counting particles in the forum, here's one:
http://obi.virtualmethodstudio.com/forum/showthread.php?tid=708&highlight=collision

As for the pink screen in Android, check what the ADB log says. Your particular device isn't capable of running one or more of the default fluid shaders, so you might have to modify them.

Thank you very much for your fast reply Jose.

Let's put aside the Android issue since I haven't spent much time on that issue, yet.

About counting particles. I have already found that topic and tried the same sample code, but couldn't get it worked. I set the collider but counter is not increasing when particles collide with collider that I've set up.

The only change I made was changing public Collider to public Collider2D, since my game is 2D.

Which part do you think should I edit on the sample code that you've provided to make it work on 2D game?


Thank you.


RE: Counting Fluid & Android Export - josemendez - 15-11-2018

(15-11-2018, 08:12 PM)Tuna.Y Wrote: Thank you very much for your fast reply Jose.

Let's put aside the Android issue since I haven't spent much time on that issue, yet.

About counting particles. I have already found that topic and tried the same sample code, but couldn't get it worked. I set the collider but counter is not increasing when particles collide with collider that I've set up.

The only change I made was changing public Collider to public Collider2D, since my game is 2D.

Which part do you think should I edit on the sample code that you've provided to make it work on 2D game?


Thank you.

Hi,

It works fine both in 2D and 3D for me. Which kind of 2D collider are you using?


RE: Counting Fluid & Android Export - Tuna.Y - 15-11-2018

(15-11-2018, 08:36 PM)josemendez Wrote: Hi,

It works fine both in 2D and 3D for me. Which kind of 2D collider are you using?

Hi,

It was a standard Box Collider 2D. I have an object which includes obi solver, Collider script and box collider 2d.


RE: Counting Fluid & Android Export - josemendez - 15-11-2018

(15-11-2018, 08:43 PM)Tuna.Y Wrote: Hi,

It was a standard Box Collider 2D. I have an object which includes obi solver, Collider script and box collider 2d.

Works for me. Make sure you're indexing the correct collider map in the for loop, ie ObiCollider2D instead of ObiCollider. I'm pasting the 2D version just in case:

Code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Obi;

[RequireComponent(typeof(ObiSolver))]
public class CollisionCounter : MonoBehaviour {

    ObiSolver solver;
    public int counter = 0;
    public Collider2D targetCollider = null;
    
    Obi.ObiSolver.ObiCollisionEventArgs frame;
    HashSet<int> particles = new HashSet<int>();

    void Awake(){
        solver = GetComponent<Obi.ObiSolver>();
    }

    void OnEnable () {
        solver.OnCollision += Solver_OnCollision;
    }

    void OnDisable(){
        solver.OnCollision -= Solver_OnCollision;
    }
    
    void Solver_OnCollision (object sender, Obi.ObiSolver.ObiCollisionEventArgs e)
    {
        HashSet<int> currentParticles = new HashSet<int>();
        
        for(int i = 0;  i < e.contacts.Count; ++i)
        {
            if (e.contacts.Data[i].distance < 0.001f)
            {

                Component collider;
                if (ObiCollider2D.idToCollider.TryGetValue(e.contacts.Data[i].other,out collider)){

                    if (collider == targetCollider)
                        currentParticles.Add(e.contacts.Data[i].particle);

                }
            }
        }

        particles.ExceptWith(currentParticles);
        counter += particles.Count;
        particles = currentParticles;Debug.Log(counter);
    }

}



RE: Counting Fluid & Android Export - Tuna.Y - 16-11-2018

(15-11-2018, 09:40 PM)josemendez Wrote: Works for me. Make sure you're indexing the correct collider map in the for loop, ie ObiCollider2D instead of ObiCollider. I'm pasting the 2D version just in case:

Code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Obi;

[RequireComponent(typeof(ObiSolver))]
public class CollisionCounter : MonoBehaviour {

   ObiSolver solver;
   public int counter = 0;
   public Collider2D targetCollider = null;
   
   Obi.ObiSolver.ObiCollisionEventArgs frame;
   HashSet<int> particles = new HashSet<int>();

   void Awake(){
       solver = GetComponent<Obi.ObiSolver>();
   }

   void OnEnable () {
       solver.OnCollision += Solver_OnCollision;
   }

   void OnDisable(){
       solver.OnCollision -= Solver_OnCollision;
   }
   
   void Solver_OnCollision (object sender, Obi.ObiSolver.ObiCollisionEventArgs e)
   {
       HashSet<int> currentParticles = new HashSet<int>();
       
       for(int i = 0;  i < e.contacts.Count; ++i)
       {
           if (e.contacts.Data[i].distance < 0.001f)
           {

               Component collider;
               if (ObiCollider2D.idToCollider.TryGetValue(e.contacts.Data[i].other,out collider)){

                   if (collider == targetCollider)
                       currentParticles.Add(e.contacts.Data[i].particle);

               }
           }
       }

       particles.ExceptWith(currentParticles);
       counter += particles.Count;
        particles = currentParticles;Debug.Log(counter);
   }

}

Ahhh! I was not using Obi Collider at all as a component... I just added the Obi Collider 2D and it works. Thank you very much Jose!


RE: Counting Fluid & Android Export - Tuna.Y - 16-11-2018

(15-11-2018, 09:40 PM)josemendez Wrote: Works for me. Make sure you're indexing the correct collider map in the for loop, ie ObiCollider2D instead of ObiCollider. I'm pasting the 2D version just in case:

Code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Obi;

[RequireComponent(typeof(ObiSolver))]
public class CollisionCounter : MonoBehaviour {

   ObiSolver solver;
   public int counter = 0;
   public Collider2D targetCollider = null;
   
   Obi.ObiSolver.ObiCollisionEventArgs frame;
   HashSet<int> particles = new HashSet<int>();

   void Awake(){
       solver = GetComponent<Obi.ObiSolver>();
   }

   void OnEnable () {
       solver.OnCollision += Solver_OnCollision;
   }

   void OnDisable(){
       solver.OnCollision -= Solver_OnCollision;
   }
   
   void Solver_OnCollision (object sender, Obi.ObiSolver.ObiCollisionEventArgs e)
   {
       HashSet<int> currentParticles = new HashSet<int>();
       
       for(int i = 0;  i < e.contacts.Count; ++i)
       {
           if (e.contacts.Data[i].distance < 0.001f)
           {

               Component collider;
               if (ObiCollider2D.idToCollider.TryGetValue(e.contacts.Data[i].other,out collider)){

                   if (collider == targetCollider)
                       currentParticles.Add(e.contacts.Data[i].particle);

               }
           }
       }

       particles.ExceptWith(currentParticles);
       counter += particles.Count;
        particles = currentParticles;Debug.Log(counter);
   }

}

Hi Jose,

I have checked the ADB logs. This is the error that I'm getting when I open the scene.

CommandBuffer: temporary render texture _FluidDepthTexture not found while executing Render fluid (SetGlobalTexture)

Tried with 2 different shaders. Obi/ParticleShader and Obi/FluidColorsOpaque

Thanks


RE: Counting Fluid & Android Export - josemendez - 16-11-2018

(16-11-2018, 09:15 AM)Tuna.Y Wrote: Hi Jose,

I have checked the ADB logs. This is the error that I'm getting when I open the scene.

CommandBuffer: temporary render texture _FluidDepthTexture not found while executing Render fluid (SetGlobalTexture)

Tried with 2 different shaders. Obi/ParticleShader and Obi/FluidColorsOpaque

Thanks

This means your device does not support floating point render textures.

Try using the SimpleFluidRenderer instead of the default FluidRenderer. This one is optimized for mobile and does not perform depth testing.

See:http://obi.virtualmethodstudio.com/tutorials/customparticlerendering.html


RE: Counting Fluid & Android Export - Tuna.Y - 16-11-2018

(16-11-2018, 11:55 AM)josemendez Wrote: This means your device does not support floating point render textures.

Try using the SimpleFluidRenderer instead of the default FluidRenderer. This one is optimized for mobile and does not perform depth testing.

See:http://obi.virtualmethodstudio.com/tutorials/customparticlerendering.html

Thank you Jose.

I have already tried to change Simple Fluid Renderer, and just tried again but nothing changed.

Is there anything that I should change except Obi Particle Renderer component's Shader in the Emitter object?