示例#1
0
    public ShaderParameters(Transform objTransform, Camera camera)
    {
        var projectionMatrix = MyUtility.GetProjectMatrix(camera.fieldOfView, camera.aspect, camera.nearClipPlane, camera.farClipPlane);
        var viewMatrix       = MyUtility.GetViewMatrix(camera);
        var modelMatrix      = MyUtility.GetModelMatrix(objTransform);
        var time             = Time.time;
        var sinTime          = Mathf.Sin(time);
        var cosTime          = Mathf.Cos(time);
        var deltaTime        = Time.deltaTime;
        var smoothDeltaTime  = Time.smoothDeltaTime;

        // Built-in shader variables
        unity_ObjectToWorld = modelMatrix;                            // M
        UNITY_MATRIX_V      = viewMatrix;                             // V
        UNITY_MATRIX_P      = projectionMatrix;                       // P
        unity_WorldToObject = objTransform.worldToLocalMatrix;
        UNITY_MATRIX_MV     = UNITY_MATRIX_V * unity_ObjectToWorld;
        UNITY_MATRIX_MVP    = UNITY_MATRIX_P * UNITY_MATRIX_MV;
        UNITY_MATRIX_VP     = UNITY_MATRIX_P * UNITY_MATRIX_V;
        UNITY_MATRIX_T_MV   = UNITY_MATRIX_MV.transpose;
        UNITY_MATRIX_IT_MV  = UNITY_MATRIX_T_MV.inverse;

        // Camera and screen (TODO)

        // Time
        _Time           = new Vector4(time / 20.0f, time, time * 2.0f, time * 3.0f);                              // Time since level load (t/20, t, t*2, t*3), use to animate things inside the shaders.
        _SinTime        = new Vector4(sinTime / 8.0f, sinTime / 4.0f, sinTime / 2.0f, sinTime);                   // Sine of time: (t/8, t/4, t/2, t).
        _CosTime        = new Vector4(cosTime / 8.0f, cosTime / 4.0f, cosTime / 2.0f, cosTime);                   // Cosine of time: (t/8, t/4, t/2, t).
        unity_DeltaTime = new Vector4(deltaTime, 1.0f / deltaTime, smoothDeltaTime, 1.0f / smoothDeltaTime);      // Delta time: (dt, 1/dt, smoothDt, 1/smoothDt).
    }