예제 #1
0
 /// <summary>
 /// Positive for above water, negative for under. Level = 0
 /// </summary>
 /// <param name="p">Position from which distance to surface is calculated.</param>
 /// <returns></returns>
 public float GetDistanceToSurface(Vector3 p, GetWaterHeightAtPoint waterHeight)
 {
     return(p.y - waterHeight(p.x, p.z));
 }
예제 #2
0
    private void Start()
    {
        sleep = false;

        // Check if the values have alread been set and assign them if not.
        if (originalMesh == null)
        {
            originalMesh = GetComponent <MeshFilter>().sharedMesh;
        }

        if (dummyMeshTriCount == 0)
        {
            dummyMesh = originalMesh;

            // Get mesh vars to prevent garbage
            dummyMeshTris      = dummyMesh.triangles;
            dummyMeshVerts     = dummyMesh.vertices;
            dummyMeshTriCount  = dummyMeshTris.Length;
            dummyMeshVertCount = dummyMeshVerts.Length;
        }

        underwaterTris = new BuoyTri[dummyMeshTriCount];
        for (int i = 0; i < underwaterTris.Length; i++)
        {
            underwaterTris[i] = new BuoyTri();
        }


        if (rb == null)
        {
            rb = GetComponent <Rigidbody>();
        }
        if (t == null)
        {
            t = transform;
        }

        meshSize = GetComponent <MeshRenderer>().bounds.size;

        wgos = GameObject.FindGameObjectsWithTag("Water")
               .OrderBy(x => Vector3.Distance(this.transform.position, x.transform.position)).ToList();

        // No water objects found, dont simulate
        if (wgos.Count == 0)
        {
            simulate = false;
        }
        // Water object found
        else
        {
            water = wgos[0];

            // Try to get Water Interface
            WaterInterface wi = null;
            if (wi = water.GetComponent <WaterInterface>())
            {
                WaterHeightFunction = wi.GetWaterHeightAtLocation;
            }
            // No water interface, use transform position for water height
            else
            {
                WaterHeightFunction = GetWaterHeightFlatSurface;
            }
        }

        // Check for water effects script
        waterEffects = GetComponent <WaterFX>();

        // Initial position
        rbSleepPosition = rb.position;
        rbSleepUp       = rb.transform.up;

        // Check for dummy meshes with too large complexity
        if (dummyMeshVertCount > 1000)
        {
            Debug.LogWarning("No need for this large a dummy mesh (>1000 vertices). Use mesh simplification for better performance. Object: " + gameObject.name);
        }

        // Pre-generate to avoid garbage
        objVerticesGlobal  = new Vector3[dummyMeshVertCount];
        distancesToSurface = new float[dummyMeshVertCount];

        // Avoid get_Item() overhead
        vd0 = new VertexData();
        vd1 = new VertexData();
        vd2 = new VertexData();
    }