Exemple #1
0
    void Update()
    {
        MeshHeight = mMeshFilter.sharedMesh.bounds.size.y;

        WindDirection.Normalize();
        var perpendicularWindDirection = new Vector3(WindDirection.z, 0.0f, -WindDirection.x);
        var modulation = WindDirectionModulationStrength * Mathf.Sin(Time.time * WindDirectionModulationTimeScale);

        WindDirectionModulated = WindDirection + perpendicularWindDirection * modulation;
        WindDirectionModulated.Normalize();

        CurTime             += Time.deltaTime;
        RollingWindOffset.x -= WindDirectionModulated.x * (WindScrollSpeed * Time.deltaTime);
        RollingWindOffset.y -= WindDirectionModulated.z * (WindScrollSpeed * Time.deltaTime);

        for (int i = 0; i < Buckets.Count; ++i)
        {
            BatchBucket bucket = Buckets[i];

            propertyBlock.SetVectorArray("_Color", bucket.ColorArray);
            propertyBlock.SetVectorArray("_InstancePosition", bucket.PositionArray);

            propertyBlock.SetFloat("_CurTime", CurTime);
            propertyBlock.SetFloat("_DisplacementStrength", DisplacementStrength);
            propertyBlock.SetFloat("_Flexibility", Flexibility);
            propertyBlock.SetFloat("_WindStrength", WindStrength);
            propertyBlock.SetVector("_WindDirection", WindDirectionModulated);
            propertyBlock.SetFloat("_RollingWindPositionScale", RollingWindPositionScale);
            propertyBlock.SetTexture("_RollingWindTex", RollingWindTex);
            propertyBlock.SetVector("_RollingWindOffset", RollingWindOffset);
            propertyBlock.SetFloat("_MeshHeight", MeshHeight);
            propertyBlock.SetVector("_Scale", Scale);
            propertyBlock.SetTexture("_Displacement", DisplacementMap.Get().rt);
            propertyBlock.SetColor("_FreezeColor", FreezeColor);
            propertyBlock.SetFloat("_FreezeFactor", FreezeFactor);

            var grassBounds = new Vector4();
            grassBounds.x = targetBounds.center.x - targetBounds.extents.x;
            grassBounds.y = targetBounds.center.z - targetBounds.extents.z;
            grassBounds.z = targetBounds.size.x;
            grassBounds.w = targetBounds.size.z;
            propertyBlock.SetVector("_Bounds", grassBounds);

            Graphics.DrawMeshInstanced(
                mMeshFilter.sharedMesh,
                0,
                meshMaterial,
                bucket.MatrixArray,
                bucket.NumInstances,
                propertyBlock,
                UnityEngine.Rendering.ShadowCastingMode.On,
                true,
                GRASS_GEOMETRY_LAYER_MASK
                );
        }
    }
Exemple #2
0
        /// <summary>
        /// Releases the unmanaged resources used by an instance of the <see cref="WaterWaves"/> class
        /// and optionally releases the managed resources.
        /// </summary>
        /// <param name="disposing">
        /// <see langword="true"/> to release both managed and unmanaged resources;
        /// <see langword="false"/> to release only unmanaged resources.
        /// </param>
        protected virtual void Dispose(bool disposing)
        {
            if (disposing)
            {
                DisplacementMap.SafeDispose();
                DisplacementMap = null;

                NormalMap.SafeDispose();
                NormalMap = null;
            }
        }
Exemple #3
0
    private void Awake()
    {
        Instance = this;

        targetBounds = target.GetComponent <MeshRenderer>().bounds;
        var w = Mathf.RoundToInt(targetBounds.size.x * pixelsPerGameUnit);
        var h = Mathf.RoundToInt(targetBounds.size.z * pixelsPerGameUnit);

        rt            = new RenderTexture(w, h, 0, RenderTextureFormat.ARGB32);
        rt.filterMode = FilterMode.Bilinear;
        rt.wrapModeU  = TextureWrapMode.Clamp;
        rt.wrapModeV  = TextureWrapMode.Clamp;
        rt.Create();

        prevrt            = new RenderTexture(w, h, 0, RenderTextureFormat.ARGB32);
        prevrt.filterMode = FilterMode.Bilinear;
        prevrt.wrapModeU  = TextureWrapMode.Clamp;
        prevrt.wrapModeV  = TextureWrapMode.Clamp;
        prevrt.Create();

        cam.orthographic    = true;
        cam.cullingMask     = 1 << GRASS_DISPLACEMENT_LAYER;
        cam.targetTexture   = rt;
        cam.clearFlags      = CameraClearFlags.SolidColor;
        cam.backgroundColor = new Color(0.0f, 0.0f, 0.0f, 0.0f);

        cam.transform.rotation = Quaternion.AngleAxis(90.0f, Vector3.right);
        cam.transform.position = targetBounds.center + Vector3.up * 100.0f;
        cam.orthographicSize   = targetBounds.extents.z;
        cam.aspect             = targetBounds.size.x / targetBounds.size.z;

        // Preview mode
        if (previewMode)
        {
            // Top-down view to use as a reference for grass planting: hide grass geometry and displacement layers
            int toHide = (1 << GRASS_DISPLACEMENT_LAYER) | (1 << Grass.GRASS_GEOMETRY_LAYER_MASK);
            cam.cullingMask = ~(toHide);
        }
    }
Exemple #4
0
    void OnRenderObject()
    {
        var camera = Camera.current;

        if (RenderMotionVectors && (camera.depthTextureMode & DepthTextureMode.MotionVectors) != 0)
        {
            var nonJitteredVP = GL.GetGPUProjectionMatrix(camera.projectionMatrix, true) * camera.worldToCameraMatrix;

            for (int i = 0; i < Buckets.Count; ++i)
            {
                BatchBucket bucket = Buckets[i];
                var         props  = bucket.MotionVectorsPropertyBlock;
                var         pass   = bucket.MotionVectorsPass;

                // Build and execute the motion vector rendering pass.
                pass.Clear();
                if (camera.allowMSAA && camera.actualRenderingPath == RenderingPath.Forward)
                {
                    pass.SetRenderTarget(BuiltinRenderTextureType.MotionVectors);
                }
                else
                {
                    pass.SetRenderTarget(BuiltinRenderTextureType.MotionVectors, BuiltinRenderTextureType.CameraTarget);
                }

                // Set the per-camera properties.
                props.SetMatrix("_PreviousVP", camera.previousViewProjectionMatrix);
                props.SetMatrix("_NonJitteredVP", nonJitteredVP);

                // The usual
                props.SetVectorArray("_InstancePosition", bucket.PositionArray);
                props.SetFloat("_CurTime", CurTime);
                props.SetFloat("_DisplacementStrength", DisplacementStrength);
                props.SetFloat("_Flexibility", Flexibility);
                props.SetFloat("_WindStrength", WindStrength);
                props.SetVector("_WindDirection", WindDirectionModulated);
                props.SetFloat("_RollingWindPositionScale", RollingWindPositionScale);
                props.SetTexture("_RollingWindTex", RollingWindTex);
                props.SetVector("_RollingWindOffset", RollingWindOffset);
                props.SetFloat("_MeshHeight", MeshHeight);
                props.SetVector("_Scale", Scale);
                props.SetTexture("_Displacement", DisplacementMap.Get().rt);

                var grassBounds = new Vector4();
                grassBounds.x = targetBounds.center.x - targetBounds.extents.x;
                grassBounds.y = targetBounds.center.z - targetBounds.extents.z;
                grassBounds.z = targetBounds.size.x;
                grassBounds.w = targetBounds.size.z;
                props.SetVector("_Bounds", grassBounds);

                // Previous frame for TAA
                props.SetTexture("_PrevDisplacement", DisplacementMap.Get().prevrt);
                props.SetFloat("_PrevCurTime", PrevCurTime);
                props.SetFloat("_PrevDisplacementStrength", PrevDisplacementStrength);
                props.SetFloat("_PrevFlexibility", PrevFlexibility);
                props.SetFloat("_PrevWindStrength", PrevWindStrength);
                props.SetVector("_PrevWindDirection", PrevWindDirectionModulated);
                props.SetVector("_PrevRollingWindOffset", PrevRollingWindOffset);
                props.SetVector("_PrevScale", PrevScale);

                // Update last frame data
                PrevCurTime = CurTime;
                PrevDisplacementStrength   = DisplacementStrength;
                PrevFlexibility            = Flexibility;
                PrevWindStrength           = WindStrength;
                PrevWindDirectionModulated = WindDirectionModulated;
                PrevRollingWindOffset      = RollingWindOffset;
                PrevScale = Scale;

                // Draw
                pass.DrawMeshInstanced(
                    mMeshFilter.sharedMesh,
                    0,
                    motionVectorsMaterial,
                    -1,
                    bucket.MatrixArray,
                    bucket.NumInstances,
                    props
                    );
                Graphics.ExecuteCommandBuffer(pass);
            }
        }
    }