我发现莉莉丝的《神觉者》在战斗中也有使用到这个效果,视觉效果提升还是很大的
比如,球形投影前的效果
球形投影后的效果
GIF:
ShaderLab cginc
固定 Vector.forward 方向的球形透视
参考: Shader in Unity & Curved world shader & Change material color 实现的效果
实现的只能是 Vector.forward 方向的球形投影
#ifndef __CURVE_WORLD_LIB_H__
#define __CURVE_WORLD_LIB_H__
// jave.lin 2023/02/01 curve world library
#ifdef _CURVE_WORLD_ON
uniform float _Curve_Depth;
uniform float _Depth_Divider;
void CurveWorldApply(inout float3 positionWS, out float3 positionOS)
{
float3 deltaVec = positionWS - _WorldSpaceCameraPos.xyz;
float E = -_Curve_Depth * _Depth_Divider;
float detlaVecZ2 = deltaVec.z * deltaVec.z;
detlaVecZ2 *= E;
float3 offset = float3(0.0, detlaVecZ2, 0.0);
positionWS += offset;
positionOS = mul(unity_WorldToObject, float4(positionWS, 1.0)).xyz;
}
#define CURVE_WORLD_APPLY(positionWS, positionOS) CurveWorldApply(positionWS, positionOS);
#else
#define CURVE_WORLD_APPLY(positionWS, positionOS)
#endif
#endif
根据镜头任意视角方向的球形透视
#ifndef __CURVE_WORLD_LIB_H__
#define __CURVE_WORLD_LIB_H__
// jave.lin 2023/02/01 curve world library
#ifdef _CURVE_WORLD_ON
uniform float _Curve_Depth;
uniform float _Depth_Divider;
//uniform int _CW_Enabled;
void CurveWorldApply(inout float3 positionWS, out float3 positionOS)
{
//float3 srcPosWS = positionWS;
//float3 srcPosOS = positionOS;
// jave.lin : zDist ref to : AutoLight.cginc
float3 deltaVec = _WorldSpaceCameraPos - positionWS;
float zDist = dot(deltaVec, UNITY_MATRIX_V[2].xyz);
float E = -_Curve_Depth * _Depth_Divider;
float zDist2 = zDist * zDist;
zDist2 *= E;
float3 offset = float3(0.0, zDist2, 0.0);
positionWS += offset;
positionOS = mul(unity_WorldToObject, float4(positionWS, 1.0)).xyz;
//positionWS = lerp(srcPosWS, positionWS, _CW_Enabled);
//positionOS = lerp(srcPosOS, positionOS, _CW_Enabled);
}
#define CURVE_WORLD_APPLY(positionWS, positionOS) CurveWorldApply(positionWS, positionOS);
#else
#define CURVE_WORLD_APPLY(positionWS, positionOS)
#endif
#endif
CurveWorldControl.cs
// jave.lin : curve world control
using UnityEngine;
[ExecuteInEditMode]
public class CurveWorldControl : MonoBehaviour
{
private bool _last_on = false;
public bool on = true;
[Range(0.005f, 1.0f)]
public float curveDepth = 0.015f;
public float depthDivider = 0.0f;
private static int _Curve_Depth = Shader.PropertyToID("_Curve_Depth");
private static int _Depth_Divider = Shader.PropertyToID("_Depth_Divider");
private void Update()
{
if (_last_on != on)
{
if (on)
{
_Curve_Depth = Shader.PropertyToID("_Curve_Depth");
_Depth_Divider = Shader.PropertyToID("_Depth_Divider");
Shader.EnableKeyword("_CURVE_WORLD_ON");
}
else
{
Shader.DisableKeyword("_CURVE_WORLD_ON");
}
_last_on = on;
}
if (_last_on)
{
Shader.SetGlobalFloat(_Curve_Depth, curveDepth);
Shader.SetGlobalFloat(_Depth_Divider, depthDivider);
}
}
}
需要修改的代码
修改所有 3D shader 即可
- pass添加代码:
- #pragma multi_compile _ _CURVE_WORLD_ON
- #include “CurveWorldLib.cginc”
- CURVE_WORLD_APPLY(posWorld.xyz, v.vertex.xyz)
- 要添加的pass
- ForwardBase
- ForwardAdd
- ShadowCaster
- CurveWorldControl.cs 的 DontDestryOnLoad
- 对应场景才开启 CurveWorldControl
Project
TestingSphereProjectionEffect - 参考: Shader in Unity & Curved world shader & Change material color 实现的效果
References
全部看完,相信你可以做一款很有意思的游戏,或是游戏中很有特色的效果
- 英文搜索关键字:
- 油管搜索关键字: curved-world
- google : curved-world
- Shader in Unity & Curved world shader & Change material color
- Mobile Optimized Curve Shader for Mobile Games || Universal Render Pipeline || Shader Graph
- Curved World shader bending
- curved world using vertex shaders, #threejs, #glsl
- Making of SHRINKING PLANET - Ludum Dare 38
- Make Awesome Curved Worlds! - Code Monkey 大佬的
- Animal Crossing World Bending Effect | Shader Graph | Unity Tutorial
- Curved World
- Unity Endless Game - Part 16: Post Processing & Curved Shader