跳到主要内容
← Vertex Transformations

Vertex Transformations

Rotation Matrix

我不觉得我写的有错,我觉得题有问题

实时渲染 · 网格 HLSL

第 18 课:Rotation Matrix

我的解:我不觉得我写的有错,我觉得题有问题

我的解

cbuffer Uniforms : register(b0) {
  float iTime;
  float4x4 viewMatrix;
  float4x4 projectionMatrix;
};

struct VSInput {
  float3 position : Position;
};

struct VSOutput {
  float4 position : SV_Position;
  float3 wolrdPos : POSITION0;
};

float3x3 RotX(float a){
    float c = cos(a), s = sin(a);
    return float3x3( 1, 0, 0,
                     0, c,-s,
                     0, s, c );
}
float3x3 RotY(float a){
    float c = cos(a), s = sin(a);
    return float3x3(  c, 0, s,
                      0, 1, 0,
                     -s, 0, c );
}
float3x3 RotZ(float a){
    float c = cos(a), s = sin(a);
    return float3x3(  c,-s, 0,
                      s, c, 0,
                      0, 0, 1 );
}

VSOutput main(VSInput input) {
  VSOutput output;
float PI = 3.14;
  float progress = 3.0 *  PI * abs(cos(iTime) * 0.5 - 0.5);
  float uInc = saturate(progress/PI);
  float uDec = 1.0 - uInc;
  int seg = (int)floor(fmod(iTime, 6.0*PI) / PI );

      float3x3 M;
    if (seg == 0) {
        // +X: 0→π
        M = RotX(PI * uInc);
    } else if (seg == 1) {
        // +Y: 0→π  (在 X=π 的基础上)
        M = mul(RotY(PI * uInc), RotX(PI));
    } else if (seg == 2) {
        // +Z: 0→π  (X=π, Y=π 基础上)
        M = mul(RotZ(PI * uDec), mul(RotY(PI), RotX(PI)));
    } else if (seg == 3) {
        // Z: π→0
        M = mul(RotZ(PI * uDec), mul(RotY(PI), RotX(PI)));
    } else if (seg == 4) {
        // Y: π→0
        M = mul(RotY(PI * uInc), RotX(PI));
    } else { // seg == 5
        // X: π→0
        M = RotX(PI * uInc);
    }
  float3 pos3D = mul(M, input.position);
  float4 viewPos  = mul(viewMatrix, float4(pos3D, 1.0));
  output.position = mul(projectionMatrix, viewPos);
  output.wolrdPos = pos3D;

  return output;
}

复盘笔记

  • 我不觉得我写的有错,我觉得题有问题