DIV跷跷板特效,3D视差效果

8 篇文章 0 订阅
6 篇文章 0 订阅

这里写图片描述
这里写图片描述
这里写图片描述

css:
.avatar_author {
    background-color: rgba(236, 238, 239, 0) !important;
    box-shadow: 0 0 10px 1px #ccc;
    transition: all .3s linear;
    transform-origin: 50%;
}
html:
<div class="avatar_author jumbotron p-y-1">
    <img src="0.jpg" style="width: 100%" alt="">
    <p class="lead" style="font-size: 12px;color: rgba(123, 123, 123, 0.76);">本次他结合自身的资深阅历,及对行业技术领域的发展动向</p>
</div>
js:
<script>
    // DIV跷跷板效果;
    var el = document.querySelector('.avatar_author');
    el.addEventListener('mousemove', (e) => {
        // Element.getBoundingClientRect()方法返回元素的大小及其相对于视口的位置。
      var this_px = el.getBoundingClientRect().left;
      var this_py = el.getBoundingClientRect().top;
      var box_width = el.getBoundingClientRect().width;
      var box_height = el.getBoundingClientRect().height;
      var scroll_top = document.documentElement.scrollTop + document.body.scrollTop;

      var mouseX = e.pageX - this_px;
      var mouseY = e.pageY - scroll_top - this_py;
      var X;
      var Y;

      X = mouseX - box_width / 2;
      Y = box_height / 2 - mouseY;

      el.style.transform = `perspective(2000px) rotateY(${X / 10}deg) rotateX(${Y / 10}deg)`;
      el.style.boxShadow = `${-X / 20}px ${Y / 20}px 50px rgba(0, 0, 0, 0.5)`;
    })


    el.addEventListener('mouseleave', () => {
      el.style.transform = `perspective(2000px) rotateY(0deg) rotateX(0deg)`;
      el.style.boxShadow = '';
    })
</script>
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实现Unity3D视差Shader的一般步骤如下: 1. 在ShaderLab中创建一个新的shader,并指定渲染队列。 2. 在CG程序中编写实现视差效果的代码。通常,这涉及到计算纹理坐标的偏移量,根据深度信息计算出位移量。最终,将位移量添加到纹理坐标中,使纹理随着深度变化而产生位移。 下面是一个简单的视差Shader代码示例: Shader "Custom/Parallax" { Properties { _MainTex ("Texture", 2D) = "white" {} _BumpMap ("BumpMap", 2D) = "bump" {} _Parallax ("Parallax", Range(0, 1)) = 0.02 } SubShader { Tags {"Queue"="Transparent" "RenderType"="Opaque"} Pass { CGPROGRAM #pragma vertex vert #pragma fragment frag #include "UnityCG.cginc" struct appdata { float4 vertex : POSITION; float3 normal : NORMAL; float2 texcoord : TEXCOORD0; }; struct v2f { float2 texcoord : TEXCOORD0; float4 vertex : SV_POSITION; }; sampler2D _MainTex; sampler2D _BumpMap; float _Parallax; v2f vert (appdata v) { v2f o; o.vertex = UnityObjectToClipPos(v.vertex); o.texcoord = v.texcoord; return o; } fixed4 frag (v2f i) : SV_Target { float2 uv = i.texcoord; float depth = tex2D(_BumpMap, uv).r; float2 offset = normalize(i.vertex.xy / i.vertex.w - i.texcoord) * depth * _Parallax; uv += offset; return tex2D(_MainTex, uv); } ENDCG } } FallBack "Diffuse" } 这个Shader使用了一张主纹理和一张法线贴图,同时增加了一个Parallax参数。在片段着色器中,使用法线贴图中的深度信息计算位移量,并将其添加到纹理坐标中,产生视差效果

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值