private void LateUpdate()
        {
            // find which lod this object is overlapping
            var rect = new Rect(transform.position.x, transform.position.z, 0f, 0f);
            var idx  = WaveDataCam.SuggestCollisionLOD(rect);

            if (idx > -1)
            {
                if (_mpb == null)
                {
                    _mpb = new MaterialPropertyBlock();
                }

                _rend.GetPropertyBlock(_mpb);

                var wdcs = OceanRenderer.Instance.Builder._shapeWDCs;
                wdcs[idx].ApplyMaterialParams(0, _mpb);
                int idx1 = Mathf.Min(idx + 1, wdcs.Length - 1);
                wdcs[idx1].ApplyMaterialParams(1, _mpb);

                // blend LOD 0 shape in/out to avoid pop, if the ocean might scale up later (it is smaller than its maximum scale)
                bool  needToBlendOutShape = idx == 0 && OceanRenderer.Instance.ScaleCouldIncrease;
                float meshScaleLerp       = needToBlendOutShape ? OceanRenderer.Instance.ViewerAltitudeLevelAlpha : 0f;

                // blend furthest normals scale in/out to avoid pop, if scale could reduce
                bool  needToBlendOutNormals = idx == wdcs.Length - 1 && OceanRenderer.Instance.ScaleCouldDecrease;
                float farNormalsWeight      = needToBlendOutNormals ? OceanRenderer.Instance.ViewerAltitudeLevelAlpha : 1f;
                _mpb.SetVector("_InstanceData", new Vector4(meshScaleLerp, farNormalsWeight, idx));

                _rend.SetPropertyBlock(_mpb);
            }
        }
        public bool SampleDisplacement(ref Vector3 worldPos, ref Vector3 displacement)
        {
            int lod = WaveDataCam.SuggestCollisionLOD(new Rect(worldPos.x, worldPos.z, 0f, 0f), 0f);

            if (lod == -1)
            {
                return(false);
            }
            return(OceanRenderer.Instance.Builder._shapeWDCs[lod].CollData.SampleDisplacement(ref worldPos, ref displacement));
        }
        // Called when visible to a camera
        void OnWillRenderObject()
        {
            // per instance data

            // blend LOD 0 shape in/out to avoid pop, if the ocean might scale up later (it is smaller than its maximum scale)
            bool  needToBlendOutShape = _lodIndex == 0 && OceanRenderer.Instance.ScaleCouldIncrease;
            float meshScaleLerp       = needToBlendOutShape ? OceanRenderer.Instance.ViewerAltitudeLevelAlpha : 0f;

            // blend furthest normals scale in/out to avoid pop, if scale could reduce
            bool  needToBlendOutNormals = _lodIndex == _totalLodCount - 1 && OceanRenderer.Instance.ScaleCouldDecrease;
            float farNormalsWeight      = needToBlendOutNormals ? OceanRenderer.Instance.ViewerAltitudeLevelAlpha : 1f;

            _rend.material.SetVector("_InstanceData", new Vector4(meshScaleLerp, farNormalsWeight, _lodIndex));

            // geometry data
            float squareSize         = transform.lossyScale.x / _baseVertDensity;
            float mul                = 1.875f; // fudge 1
            float pow                = 1.4f;   // fudge 2
            float normalScrollSpeed0 = Mathf.Pow(Mathf.Log(1f + 2f * squareSize) * mul, pow);
            float normalScrollSpeed1 = Mathf.Pow(Mathf.Log(1f + 4f * squareSize) * mul, pow);

            _rend.material.SetVector("_GeomData", new Vector4(squareSize, normalScrollSpeed0, normalScrollSpeed1, _baseVertDensity));

            // assign shape textures to shader
            // this relies on the render textures being init'd in CreateAssignRenderTexture::Awake().
            Camera[]    shapeCams = OceanRenderer.Instance.Builder._shapeCameras;
            WaveDataCam wdc0      = shapeCams[_lodIndex].GetComponent <WaveDataCam>();

            wdc0.ApplyMaterialParams(0, _rend.material);
            WaveDataCam wdc1 = (_lodIndex + 1) < shapeCams.Length ? shapeCams[_lodIndex + 1].GetComponent <WaveDataCam>() : null;

            if (wdc1)
            {
                wdc1.ApplyMaterialParams(1, _rend.material);
            }
            else
            {
                _rend.material.SetTexture("_WD_Sampler_1", null);
            }

            // killing this as we use heightmaps now, not displacement textures, and im not sure if/when this will change
#if USING_DISPLACEMENT_TEXTURES
            // expand mesh bounds - bounds need to completely encapsulate verts after any dynamic displacement
            Bounds bounds        = _boundsLocal;
            float  boundsPadding = OceanRenderer.Instance._chop * OceanRenderer.Instance._maxWaveHeight;
            float  expand        = boundsPadding / transform.lossyScale.x;
            bounds.extents += new Vector3(expand, 0f, expand);
            _mesh.bounds    = bounds;
#endif

            if (_drawRenderBounds)
            {
                DebugDrawRendererBounds();
            }
        }
        public bool SampleHeight(ref Vector3 worldPos, ref float height)
        {
            int lod = WaveDataCam.SuggestCollisionLOD(new Rect(worldPos.x, worldPos.z, 0f, 0f), 0f);

            if (lod == -1)
            {
                return(false);
            }
            height = OceanRenderer.Instance.Builder._shapeWDCs[lod].CollData.GetHeight(ref worldPos);
            return(true);
        }
Exemplo n.º 5
0
        // Called when visible to a camera
        void OnWillRenderObject()
        {
            // per instance data

            if (_mpb == null)
            {
                _mpb = new MaterialPropertyBlock();
            }
            _rend.GetPropertyBlock(_mpb);

            // blend LOD 0 shape in/out to avoid pop, if the ocean might scale up later (it is smaller than its maximum scale)
            bool  needToBlendOutShape = _lodIndex == 0 && OceanRenderer.Instance.ScaleCouldIncrease;
            float meshScaleLerp       = needToBlendOutShape ? OceanRenderer.Instance.ViewerAltitudeLevelAlpha : 0f;

            // blend furthest normals scale in/out to avoid pop, if scale could reduce
            bool  needToBlendOutNormals = _lodIndex == _totalLodCount - 1 && OceanRenderer.Instance.ScaleCouldDecrease;
            float farNormalsWeight      = needToBlendOutNormals ? OceanRenderer.Instance.ViewerAltitudeLevelAlpha : 1f;

            _mpb.SetVector("_InstanceData", new Vector4(meshScaleLerp, farNormalsWeight, _lodIndex));

            // geometry data
            float squareSize         = transform.lossyScale.x / _baseVertDensity;
            float mul                = 1.875f; // fudge 1
            float pow                = 1.4f;   // fudge 2
            float normalScrollSpeed0 = Mathf.Pow(Mathf.Log(1f + 2f * squareSize) * mul, pow);
            float normalScrollSpeed1 = Mathf.Pow(Mathf.Log(1f + 4f * squareSize) * mul, pow);

            _mpb.SetVector("_GeomData", new Vector4(squareSize, normalScrollSpeed0, normalScrollSpeed1, _baseVertDensity));

            // assign shape textures to shader
            // this relies on the render textures being init'd in CreateAssignRenderTexture::Awake().
            Camera[]    shapeCams = OceanRenderer.Instance.Builder._shapeCameras;
            WaveDataCam wdc0      = shapeCams[_lodIndex].GetComponent <WaveDataCam>();

            wdc0.ApplyMaterialParams(0, new PropertyWrapperMPB(_mpb));
            WaveDataCam wdc1 = (_lodIndex + 1) < shapeCams.Length ? shapeCams[_lodIndex + 1].GetComponent <WaveDataCam>() : null;

            if (wdc1)
            {
                wdc1.ApplyMaterialParams(1, new PropertyWrapperMPB(_mpb));
            }

            _rend.SetPropertyBlock(_mpb);

            if (_drawRenderBounds)
            {
                DebugDrawRendererBounds();
            }
        }
Exemplo n.º 6
0
        // Called when visible to a camera
        void OnWillRenderObject()
        {
            // per instance data

            // blend closest geometry in/out to avoid pop
            float meshScaleLerp = _lodIndex == 0 ? OceanRenderer.Instance.ViewerAltitudeLevelAlpha : 0f;
            // blend furthest normals scale in/out to avoid pop
            float farNormalsWeight = _lodIndex == _totalLodCount - 1 ? OceanRenderer.Instance.ViewerAltitudeLevelAlpha : 1f;

            _rend.material.SetVector("_InstanceData", new Vector4(meshScaleLerp, farNormalsWeight, _lodIndex));

            // geometry data
            float squareSize         = Mathf.Abs(transform.lossyScale.x) / _baseVertDensity;
            float normalScrollSpeed0 = Mathf.Log(1f + 2f * squareSize) * 1.875f;
            float normalScrollSpeed1 = Mathf.Log(1f + 4f * squareSize) * 1.875f;

            _rend.material.SetVector("_GeomData", new Vector4(squareSize, normalScrollSpeed0, normalScrollSpeed1, _baseVertDensity));

            // assign shape textures to shader
            // this relies on the render textures being init'd in CreateAssignRenderTexture::Awake().
            Camera[]    shapeCams = OceanRenderer.Instance.Builder._shapeCameras;
            WaveDataCam wdc0      = shapeCams[_lodIndex].GetComponent <WaveDataCam>();

            wdc0.ApplyMaterialParams(0, _rend.material);
            WaveDataCam wdc1 = (_lodIndex + 1) < shapeCams.Length ? shapeCams[_lodIndex + 1].GetComponent <WaveDataCam>() : null;

            if (wdc1)
            {
                wdc1.ApplyMaterialParams(1, _rend.material);
            }
            else
            {
                _rend.material.SetTexture("_WD_Sampler_1", null);
            }

            // expand mesh bounds - bounds need to completely encapsulate verts after any dynamic displacement
            Bounds bounds = _boundsLocal;
            float  expand = _boundsPadding / Mathf.Abs(transform.lossyScale.x);

            bounds.extents += new Vector3(expand, 0f, expand);
            _mesh.bounds    = bounds;
            if (_drawRenderBounds)
            {
                DebugDrawRendererBounds();
            }
        }
 public void PrewarmForSamplingArea(Rect areaXZ, float minSpatialLength)
 {
     _areaLod = WaveDataCam.SuggestCollisionLOD(areaXZ, minSpatialLength);
 }
 public void PrewarmForSamplingArea(Rect areaXZ)
 {
     _areaLod = WaveDataCam.SuggestCollisionLOD(areaXZ);
 }