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
    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);
            }
        }
    }