コード例 #1
0
ファイル: MenuManger.cs プロジェクト: toficofi/MetaBalls
    private void InitParticleSystem(GameObject ps)
    {
        Material m = currentMetaBallScript.CurrentMaterial;

        currentMetaBallScript = ps.GetComponent <MetaBalls>();
        currentMetaBallScript.CurrentMaterial = m;
        currentMetaBallScript.ChangeSpeed(currentSpeed);
        currentMetaBallScript.TogglePlay(isNoiseEnabled);
        currentMetaBallScript.TogglePlay(isPlaying);
    }
コード例 #2
0
ファイル: MenuManger.cs プロジェクト: toficofi/MetaBalls
 // Use this for initialization
 void Start()
 {
     if (fallingParticle == null || sphereParticle == null)
     {
         Debug.LogError("Make sure there is a reference to the particle systems in the scene.");
         return;
     }
     // Only one should be active, spherical is by default.
     fallingParticle.SetActive(false);
     sphereParticle.SetActive(true);
     currentMetaBallScript = sphereParticle.GetComponent <MetaBalls>();
 }
コード例 #3
0
    public void ComputeMetaBalls()
    {
        if (vertices == null)
        {
            RegenerateGrid();
        }

        if (metaBalls == null)
        {
            metaBalls = new MetaBalls();
            metaBalls.Initialize();
        }

        metaBalls.isoValue = iso;

        for (int z = 0; z < Steps.z; z++)
        {
            for (int y = 0; y < Steps.y; y++)
            {
                for (int x = 0; x < Steps.x; x++)
                {
                    Vertex v = vertices[GetIndex(x, y, z)];
                    v.flux = metaBalls.GetVertexValue(v);
                    //v.flux = Random.Range(-1.0f, 1.0f);
                    if (capped && (z == 0 || x == 0 || y == 0 || x == Steps.x - 1 || y == Steps.y - 1 || z == Steps.z - 1))
                    {
                        v.flux = 0;
                        if (z == 0)
                        {
                            //float a = metaBalls.GetVertexValue(vertices[x, y, 2]);
                            //float b = metaBalls.GetVertexValue(vertices[x, y, 1]);
                            //v.flux = a;
                        }
                        //v.inside = false;
                    }
                    v.inside = v.flux > iso;

                    vertices[GetIndex(x, y, z)] = v;
                }
            }
        }

        for (int z = 0; z < Steps.z; z++)
        {
            for (int y = 0; y < Steps.y; y++)
            {
                for (int x = 0; x < Steps.x; x++)
                {
                    Vertex  v = vertices[GetIndex(x, y, z)];
                    Vector3 tangent;

                    const float multiplier = 2;
                    if (x == 0)
                    {
                        v.normal.x = (v.flux - vertices[GetIndex(x + 1, y, z)].flux) * multiplier;
                        tangent.x  = (v.pos - vertices[GetIndex(x + 1, y, z)].pos).x * multiplier;
                    }
                    else if (x == Steps.x - 1)
                    {
                        v.normal.x = (vertices[GetIndex(x - 1, y, z)].flux - v.flux) * multiplier;
                        tangent.x  = (vertices[GetIndex(x - 1, y, z)].pos - v.pos).x * multiplier;
                    }
                    else
                    {
                        v.normal.x = vertices[GetIndex(x - 1, y, z)].flux - vertices[GetIndex(x + 1, y, z)].flux;
                        tangent.x  = (vertices[GetIndex(x - 1, y, z)].pos - vertices[GetIndex(x + 1, y, z)].pos).x;
                    }

                    if (y == 0)
                    {
                        v.normal.y = (v.flux - vertices[GetIndex(x, y + 1, z)].flux) * multiplier;
                        tangent.y  = (v.pos - vertices[GetIndex(x, y + 1, z)].pos).y * multiplier;
                    }
                    else if (y == Steps.y - 1)
                    {
                        v.normal.y = (vertices[GetIndex(x, y - 1, z)].flux - v.flux) * multiplier;
                        tangent.y  = (vertices[GetIndex(x, y - 1, z)].pos - v.pos).y * multiplier;
                    }
                    else
                    {
                        v.normal.y = vertices[GetIndex(x, y - 1, z)].flux - vertices[GetIndex(x, y + 1, z)].flux;
                        tangent.y  = (vertices[GetIndex(x, y - 1, z)].pos - vertices[GetIndex(x, y + 1, z)].pos).y;
                    }

                    if (z == 0)
                    {
                        v.normal.z = (v.flux - vertices[GetIndex(x, y, z + 1)].flux) * multiplier;
                        tangent.z  = (v.pos - vertices[GetIndex(x, y, z + 1)].pos).z * multiplier;
                    }
                    else if (z == Steps.z - 1)
                    {
                        v.normal.z = (vertices[GetIndex(x, y, z - 1)].flux - v.flux) * multiplier;
                        tangent.z  = (vertices[GetIndex(x, y, z - 1)].pos - v.pos).z * multiplier;
                    }
                    else
                    {
                        v.normal.z = vertices[GetIndex(x, y, z - 1)].flux - vertices[GetIndex(x, y, z + 1)].flux;
                        tangent.z  = (vertices[GetIndex(x, y, z - 1)].pos - vertices[GetIndex(x, y, z + 1)].pos).z;
                    }

                    v.normal.Normalize();
                    tangent.Normalize();
                    v.tangent = Vector3.Cross(tangent, v.normal);

                    //http://en.wikipedia.org/wiki/UV_mapping#Finding_UV_on_a_sphere
                    v.uv.x = 0.5f - Mathf.Atan2(-v.normal.z, -v.normal.x) / (Mathf.PI * 2);
                    v.uv.y = 0.5f - 2.0f * (Mathf.Asin(-v.normal.y) / (Mathf.PI * 2));

                    vertices[GetIndex(x, y, z)] = v;
                }
            }
        }
    }