Shader学习案例,2D翻转/伪3D效果

在学习 Shader 的过程中做的小案例,在 Creator v3.8.3 复现2D物体的翻转效果 或者 叫伪3D效果?

fake3DMaterial.mtlfake3DEffect.effect 用于实现效果

创建 SpriteNode 节点, 给其添加上图和 fake3DMaterial.mtl 材质。


原理

使用旋转矩阵,在片段着色器中对uv进行翻转变化,在顶点着色器对修改后的图像尺寸进行调整。


Effect

通过修改内置的 builtin-sprite.effect 来实现。

参数 properties

properties:
        ...
        FOV: { value: 90, editor: { tooltip: '视场角', range: [1, 179]}}
        y_rot: { value: 0, editor: { tooltip: 'y轴角度', range: [-360, 360] }}
        x_rot: { value: 0, editor: { tooltip: 'x轴角度', range: [-360, 360] }}
        textureSize: { value: [0, 0], editor: { tooltip: '纹理尺寸' }}
        cull_back: { value: 0, editor: { tooltip: '不显示背面, 0:关闭',range: [0, 1, 1] }}

顶点着色器 vs

CCProgram sprite-vs %{
  precision highp float;
  #include <builtin/uniforms/cc-global>
  #include <common/common-define>
  
  #if USE_LOCAL
    #include <builtin/uniforms/cc-local>
  #endif

  #if SAMPLE_FROM_RT
    // #include <common/common-define>
  #endif

  #if USE_FAKE3D

    // 'out/in mediump' 作用为 'varying' 
    out mediump vec3 camera_pos;
    out mediump vec2 offset;

    uniform Fake3D_vs {
      vec2 textureSize;
      float FOV;
      float y_rot;
      float x_rot;
    };
  #endif

  in vec3 a_position;
  in vec2 a_texCoord;
  in vec4 a_color;

  out vec4 color;
  out vec2 uv0;

  vec4 vert () {
    vec4 pos = vec4(a_position, 1);

    #if USE_FAKE3D
      vec2 UV = a_texCoord;
      float sin_b = sin(y_rot / 180. * PI);
      float cos_b = cos(y_rot / 180. * PI);
      float sin_c = sin(x_rot / 180. * PI);
      float cos_c = cos(x_rot / 180. * PI);

      // Y轴旋转矩阵
      mat3 inv_rot_mat_y = mat3(
        vec3(cos_b, 0.0  , sin_b),
        vec3(0.0  , 1.0  , 0.0),
        vec3(-sin_b, 0.0  , cos_b)
      );

      // X轴旋转矩阵
      mat3 inv_rot_mat_x = mat3(
        vec3(1.0  , 0.0  , 0.0),
        vec3(0.0  , cos_c  , -sin_c),
        vec3(0.0  , sin_c  , cos_c)
      );

      // 合成的旋转矩阵
      mat3 inv_rot_mat = inv_rot_mat_y * inv_rot_mat_x;

      // 计算投影角度的正切
      float t = tan(FOV / 360. * PI);
      float v = (0.5 / t) + 0.5;

      // 计算模拟的摄像机位置 和 偏移量
      camera_pos = inv_rot_mat * vec3(UV - 0.5, 0.5 / t);
      camera_pos.xy *= inv_rot_mat[2].z * v;
      offset = inv_rot_mat[2].xy * v;

      pos.x += (UV.x - 0.5) * textureSize.x;
      pos.y -= (UV.y - 0.5) * textureSize.y;

    #endif

    #if USE_LOCAL
      pos = cc_matWorld * pos;
    #endif

    #if USE_PIXEL_ALIGNMENT
      pos = cc_matView * pos;
      pos.xyz = floor(pos.xyz);
      pos = cc_matProj * pos;
    #else
      pos = cc_matViewProj * pos;
    #endif

    uv0 = a_texCoord;
    #if SAMPLE_FROM_RT
      CC_HANDLE_RT_SAMPLE_FLIP(uv0);
    #endif
    color = a_color;

    return pos;
  }
}%

片段着色器 fs

CCProgram sprite-fs %{
  precision highp float;
  #include <builtin/internal/embedded-alpha>
  #include <builtin/internal/alpha-test>

  in vec4 color;

  #if USE_TEXTURE
    in vec2 uv0;
    #pragma builtin(local)
    layout(set = 2, binding = 12) uniform sampler2D cc_spriteTexture;
  #endif

  #if USE_FAKE3D
  in mediump vec3 camera_pos;
  in mediump vec2 offset;

  uniform Fake3D_fs {
    int cull_back;
  };
 #endif

  vec4 frag () {
    vec4 o = vec4(1, 1, 1, 1);

    #if USE_TEXTURE
      o *= CCSampleWithAlphaSeparated(cc_spriteTexture, uv0);
      #if IS_GRAY
        float gray  = 0.2126 * o.r + 0.7152 * o.g + 0.0722 * o.b;
        o.r = o.g = o.b = gray;
      #endif

      #if USE_FAKE3D
        // 启用背面剔除 && 摄像机位置在背面 => 丢弃片段
        if (cull_back != 0 && camera_pos.z <= 0.) discard;

        // 将摄像机位置转换为屏幕上的 UV 坐标, 减去偏移量
        vec2 uv = (camera_pos.xy / camera_pos.z).xy - offset;

        // 纹理采样, 加上 0.5 以调整偏移
        o = texture(cc_spriteTexture, uv + 0.5);

        // 控制纹理的透明度,使超出范围的部分透明
        o.a *= step(max(abs(uv.x), abs(uv.y)), 0.5);
      #endif
    #endif

    o *= color;
    ALPHA_TEST(o);
    return o;
  }
}%

脚本

import { _decorator, Component, EventMouse, Node, renderer, Sprite, tween, UITransform, v2, v3, Vec2 } from 'cc';
const { ccclass, property } = _decorator;

@ccclass('Fake3DCtrl')
export class Fake3DCtrl extends Component {
    @property minAngle: number = -25;
    @property maxAngle: number = 25;

    private pass: renderer.Pass = null;
    private y_rot: number = 0;
    private x_rot: number = 0;

    private spriteWidth: number = 0;
    private spriteHeight: number = 0;

    private lastYRot: number = 0;
    private lastXRot: number = 0;

    start(): void {
        // 材质 初始化
        const sprite = this.node.getComponent(Sprite);
        this.spriteWidth = sprite.spriteFrame.width;
        this.spriteHeight = sprite.spriteFrame.height;

        const material = sprite.getMaterialInstance(0);
        material.setProperty('textureSize', v2(this.spriteWidth , this.spriteHeight));

        this.pass = material.passes[0];
        this.y_rot = this.pass.getHandle('y_rot');
        this.x_rot = this.pass.getHandle('x_rot');

        // 监听鼠标移动
        this.node.on(Node.EventType.MOUSE_ENTER, this.onMouseEnter, this);
        this.node.on(Node.EventType.MOUSE_MOVE, this.onMouseMove, this);
        this.node.on(Node.EventType.MOUSE_LEAVE, this.onMouseLeave, this);
    }

    onMouseEnter() {
        tween(this.node)
        .to(0.1, {scale: v3(1.5, 1.5, 1)})
        .start();
    }

    onMouseMove(event: EventMouse) {
        const screenPos = event.getUILocation();
        const nodePos = this.node.getComponent(UITransform).convertToNodeSpaceAR(v3(screenPos.x, screenPos.y, 1));

        const angleX = this.remap(nodePos.x, -this.spriteWidth/2, this.spriteWidth/2, this.minAngle, this.maxAngle);
        const angleY = this.remap(nodePos.y, -this.spriteHeight/2, this.spriteHeight/2, this.minAngle, this.maxAngle);

        this.pass.setUniform(this.y_rot, angleX);
        this.pass.setUniform(this.x_rot, angleY);

        this.lastYRot = angleX;
        this.lastXRot = angleY;
    }

    onMouseLeave() {
        tween(this.node)
        .to(0.1, {scale: v3(1, 1, 1)})
        .start();
        
        // 缓慢改变材质属性
        tween({ angleVec2: v2(this.lastYRot, this.lastXRot) })
        .to(0.1, { angleVec2: Vec2.ZERO}, {
            onUpdate: (target) => {
                this.pass.setUniform(this.y_rot, target['angleVec2'].x);
                this.pass.setUniform(this.x_rot, target['angleVec2'].y);
            }
        })
        .call(()=>{
            this.lastYRot = 0;
            this.lastXRot = 0;
        })
        .start();
    }

    /**
     * 映射
     * @param num 当前值
     * @param sourceMin 原最小值
     * @param sourceMax 原最大值
     * @param targetMin 目标最小值
     * @param targetMax 目标最大值
     * @returns 映射后的目标值
     */
    remap(num: number ,sourceMin: number, sourceMax: number, targetMin: number = 0, targetMax: number = 1): number {
        
        if (num < sourceMin || num > sourceMax) {
            console.log(`Source Range Error: ${num} Not In [${sourceMin}, ${sourceMax}]`);
            return;
        }

        if (sourceMin > sourceMax) {
            console.log(`Source Range Error: sourceMin(${sourceMin}) > sourceMax(${sourceMax})`);
            return;
        }

        if (targetMin > targetMax) {
            console.log(`Target Range Error: targetMin(${targetMin}) > targetMax(${targetMax})`);
            return;
        }

        const sourceRange = sourceMax - sourceMin;
        const targetRange = targetMax - targetMin;

        if (sourceRange === 0) {
            console.log(`Source Range Warning: Source Range Is 0`);
            return targetMin;
        }

        const normalizedNum  = (num - sourceMin) / sourceRange;
        
        return targetMin + (normalizedNum * targetRange);
    }
}

效果

temp


学习参考:

Godot实现了《小丑牌》中的3D卡牌效果

godotshaders/2D-perspective

旋转矩阵

Shader学习案例,2D翻转/伪3D效果 Creator 2.4版本

11赞

强的一批的大佬。

很棒…

感谢分享!!!

大佬,编辑器有效果,运行(chrome)和模拟器预览都没渲染任何东西(黑屏),日志也没看到报错,是什么原因呢

可能 自动合图 或者 可视距离 的原因吧。

你参考一下 shader效果,为什么预览没有显示?

抄完了。用了一下,发现一个小问题,就是加入一张500X500的图,应用了材质后,图片会变小一圈。


是不是顶点坐标计算有问题

还没有做任何的拖拽操作,就缩小了一圈

可能是 textureSize 初始化问题。

我没找到 cocos shader 中直接获得图片大小的方式,所以在代码中设置 textureSize

手动调整 textureSize 试试

试了一下,如果设置这个size的话,在预览状态下,他又会变大一圈


奇怪,也没看出shader哪儿改了这个东西

和正切角有关系,90的时候就是原大小了

大佬 请问能不能把你的demo源码放出来,我的不知道为什么,总有问题
我自己抄的(感觉没抄全)挂载材质之后图片就纯白了。
在你的2.x案例的帖子里面,有个人给了3.8.3的源码,我用了但是图片只能看到左上角的1/4饼图一样的。
491034546@qq.com

请问,能否分享一下你的demo啊,我抄的老出错。
491034546@qq.com

图片纯白,大概是片段着色器的赋值有问题。

effect文件:

fake3DEffect.zip (1.9 KB)

归档.zip (2.3 KB)
你试试这个

非常感谢,但是可惜实际看我哪里没弄对,实测只能是鼠标放上去,图片放大了,没有别的效果了。
请问能否帮我看一下demo,谢谢。shader-demo.rar (2.7 MB)

—太久了,之前为了赶功能,就把这个延期了。不好意思,太迟回复

shader-demo.rar (2.7 MB)
大佬我的demo在此,能麻烦帮我看看嘛,我还是没出现效果。
美术给的效果是让我做这种的:https://huaban.com/pins/3362610250 图片上小下大的梯形和外部的火焰描边(火焰描边我找了别人的类似案例,对方是根据半透明区域展示特效的,但是我的卡牌实际是里面也有透明的外框和内部图片组成的,所以也不好弄)
—太久了,之前为了赶功能,就把这个延期了。不好意思,太迟回复

demo有效果呀?

屏幕截图 2024-09-13 135016

你该不会是这个没勾选吧?

:thinking:

1赞

666666mark

谢谢,是我的锅,真的没勾选。