光线步进——RayMarching入门
入门实现
先用RayMarching描绘一个球体,最后在进行光照计算
参考:https://www.shadertoy.com/view/llt3R4
模拟摄像机射线
float3 rayDirection(float filedOfView, float2 size, float2 fragCoord){
float2 xy=fragCoord-size/2;
float z=size.y/tan(radians(filedOfView)/2.0);
return normalize(float3(xy,-z));
}
1
2
3
4
5
首先,把屏幕中心设置为坐标原点(0.0,0.0),射线的z值都是固定的,其中filedOfView可以看成视椎体两条棱的夹角,返回归一化的射线向量。
对射线进行碰撞检测
float sphereSDF(float3 samplePoint){
return length(samplePoint) - 1.0;
}
1
2
3
float shortestDistanceToSurface(float3 eye,float3 marchingDirection,float start,float end){
float depth = start;
for(int i=0;i<maxMarchingSteps;i++){
float dist=sphereSDF(eye+depth*marchingDirection);
if(dist < epsilon){
return depth;
}
depth+=dist;
if(depth>=end){
return end;
}
}
return end;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
以eye坐标为起点,沿着模拟射线的方向进行碰撞检测,返回碰撞点的深度,如果到最大深度仍然没有碰撞,则发挥最大深度
根据深度返回颜色
float dist=shortestDistanceToSurface(eye,dir,minDist,maxDist);
if(dist>=maxDist-epsilon){
return float4(0.0,0.0,0.0,0.0);
}
float value=floor(dist*10.0)*_StepValue;
return float4(1-value,sin(value*10.0),0.0,1.0);
1
2
3
4
5
6
存在问题
球体的碰撞检测是比较容易的,如果我们想放一个立方体到“场景”里,怎么搞?
float cubeSDF(float3 samplePoint){
float3 d=abs(samplePoint)-float3(0.5,0.5,0.5);
return length(max(d,0.0));
}
1
2
3
4
感觉绘制什么样的物体并不是特别容易控制,需要使用一些数学手段,真佩服哪些用RayMarching画画的那些老哥。
计算法线方向
现在,我们知道顶点坐标,通过计算 xyz 三个方向的差值(梯度),归一化后得到一个近似的法线方向
float3 normalCalculate(float3 p){
return normalize(float3(
sphereSDF(float3(p.x + epsilon, p.y, p.z)) - sphereSDF(float3(p.x - epsilon, p.y, p.z)),
sphereSDF(float3(p.x, p.y + epsilon, p.z)) - sphereSDF(float3(p.x, p.y - epsilon, p.z)),
sphereSDF(float3(p.x, p.y, p.z + epsilon)) - sphereSDF(float3(p.x, p.y, p.z - epsilon))
));
}
1
2
3
4
5
6
7
可以利用matlab进行检验,绘制一个曲面,计算它的表面法线
clear;
[X Y]=meshgrid(-0.5:0.05:0.5, -0.5:0.05:0.5);
Z=0.25-X.^2-Y.^2;
vecX=sqrt(((X+0.0001).^2+Y.^2+Z))-sqrt(((X-0.0001).^2+Y.^2+Z));
vecY=sqrt((X.^2+(Y+0.0001).^2+Z))-sqrt((X.^2+(Y-0.0001).^2+Z));
%mesh(X,Y,Z);
quiver(X,Y,vecX,vecY);
1
2
3
4
5
6
7
根据法线方向计算光照
设定光源位置,环境光,漫反射颜色,高光颜色,高光系数等信息,计算光照即可
代码部分
Shader "Unlit/RayMarching_1"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
_StepValue("StepValue",Range(0.001,0.025))=0.001
_LightPos("LightPos",vector)=(0,0,0,0)
_AmbientCol("AmbientCol",Color)=(1,1,1,1)
_DiffuseCol("DiffuseCol",Color)=(1,1,1,1)
_SpecularCol("SpecularCol",Color)=(1,1,1,1)
_Gloss("Gloss",Range(0.1,255))=10
}
SubShader
{
Pass
{
ZTest Always Cull Off ZWrite Off
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
#define maxMarchingSteps 255
#define minDist 0.0
#define maxDist 1000.0
#define epsilon 0.00001
#define sizeX (_ScreenParams.x/_ScreenParams.y)
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};
struct v2f
{
float2 uv : TEXCOORD0;
float4 vertex : SV_POSITION;
};
sampler2D _MainTex;
float4 _MainTex_ST;
float _StepValue;
float4 _LightPos;
float4 _AmbientCol;
float4 _DiffuseCol;
float4 _SpecularCol;
float _Gloss;
float cubeSDF(float3 samplePoint){
float3 d=abs(samplePoint)-float3(0.5,0.5,0.5);
return length(max(d,0.0));
}
float sphereSDF(float3 samplePoint){
return length(samplePoint) - 0.7;
}
float sceneSDF(float3 samplePoint){
return sphereSDF(samplePoint);
}
float3 rayDirection(float filedOfView, float2 size, float2 fragCoord){
float2 xy=fragCoord-size/2;
float z=size.y/tan(radians(filedOfView)/2.0);
return normalize(float3(xy,-z));
}
float shortestDistanceToSurface(float3 eye,float3 marchingDirection,float start,float end){
float depth = start;
for(int i=0;i<maxMarchingSteps;i++){
float dist=sceneSDF(eye+depth*marchingDirection);
if(dist < epsilon){
return depth;
}
depth+=dist;
if(depth>=end){
return end;
}
}
return end;
}
float3 normalCalculate(float3 p){
return normalize(float3(
sceneSDF(float3(p.x + epsilon, p.y, p.z)) - sceneSDF(float3(p.x - epsilon, p.y, p.z)),
sceneSDF(float3(p.x, p.y + epsilon, p.z)) - sceneSDF(float3(p.x, p.y - epsilon, p.z)),
sceneSDF(float3(p.x, p.y, p.z + epsilon)) - sceneSDF(float3(p.x, p.y, p.z - epsilon))
));
}
float3 LightCalculate(float3 eyePos,float3 pos,float3 lightPos,float3 ambientCol,float3 diffuseColor){
float3 normal = normalCalculate(pos);
float3 col=diffuseColor*max(dot(normal,normalize(lightPos-pos)),0);
col+=ambientCol;
float3 halfDir = normalize(lightPos-pos + eyePos-pos);
float3 specular = _SpecularCol.rgb * pow(max(0, dot(normal, halfDir)), _Gloss);
col+=specular;
return col;
}
v2f vert (appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = TRANSFORM_TEX(v.uv, _MainTex);
return o;
}
float4 frag (v2f i) : SV_Target
{
i.uv.x*=_ScreenParams.x/_ScreenParams.y;
float3 dir=rayDirection(45.0,float2(sizeX,1.0),i.uv);
float3 eye= float3(0.0,0.0,5.0);
float dist=shortestDistanceToSurface(eye,dir,minDist,maxDist);
if(dist>=maxDist-epsilon){
return float4(0.0,0.0,0.0,1.0);
}
float3 pos=eye+dist*dir;
float3 col=LightCalculate(eye,pos,_LightPos.xyz,_AmbientCol.xyz,_DiffuseCol.xyz);
return float4(col,1);
//return float4(0.5,0.5,0.5,1);
//float value=floor(dist*10.0)*_StepValue;
//return float4(sin(value*1000),1-value,0.0,1.0);
}
ENDCG
}
}
}
---------------------
光线步进——RayMarching入门的更多相关文章
- 在Unity中实现屏幕空间反射Screen Space Reflection(4)
第四部分讲一下如何在2D屏幕空间步进光线. http://casual-effects.blogspot.com/2014/08/screen-space-ray-tracing.html 中的代码感 ...
- 在Unity中实现屏幕空间反射Screen Space Reflection(1)
本篇文章我会介绍一下我自己在Unity中实现的SSR效果 出发点是理解SSR效果的原理,因此最终效果不是非常完美的(代码都是够用就行),但是从学习的角度来说足以学习到SSR中的核心算法. 如果对核心算 ...
- 深入Guerrilla Games解密次世代开山大作《杀戮地带暗影坠落》(The technology of Killzone Shadow Fall)
文章摘要:这几天终于有时间,把全文翻译完了,自己感觉不是太满意,不过大家能看懂就好,就当一个学习的机会.整篇文章通过SONY第一方游戏工作室Guerrilla Games主创的语录,为我们展现了次世代 ...
- 在Unity中渲染一个黑洞
在Unity中渲染一个黑洞 前言 N年前观看<星际穿越>时,被其中的"卡冈图雅"黑洞所震撼.制作团队表示这是一个最贴近实际的黑洞效果,因为它是通过各种科学理论实现的.当 ...
- Shadertoy 教程 Part 1 - 介绍
Note: This series blog was translated from Nathan Vaughn's Shaders Language Tutorial and has been au ...
- 从零3D基础入门XNA 4.0(2)——模型和BasicEffect
[题外话] 上一篇文章介绍了3D开发基础与XNA开发程序的整体结构,以及使用Model类的Draw方法将模型绘制到屏幕上.本文接着上一篇文章继续,介绍XNA中模型的结构.BasicEffect的使用以 ...
- opengl入门学习
OpenGL入门学习 说起编程作图,大概还有很多人想起TC的#include <graphics.h>吧? 但是各位是否想过,那些画面绚丽的PC游戏是如何编写出来的?就靠TC那可怜的640 ...
- Lua简易入门教程
环境:lua for windows (lfW)主页:http://luaforwindows.luaforge.net/https://code.google.com/p/luaforwindows ...
- 转 猫都能学会的Unity3D Shader入门指南(二)
猫都能学会的Unity3D Shader入门指南(二) 关于本系列 这是Unity3D Shader入门指南系列的第二篇,本系列面向的对象是新接触Shader开发的Unity3D使用者,因为我本身自己 ...
随机推荐
- node.js适合游戏后台开发吗?
网站服务器和游戏服务器是怎么样联系到一起的? 百牛信息技术bainiu.ltd整理发布于博客园 1. 游戏分很多种,咱们先来看看MMORPG. 再怎么简单的RPG服务器都免不了处理多人交互的情形,上百 ...
- rn滑动返回页面监听
开发rn的同学都已经知道这个问题很坑了,真的很难弄,网上的方法尝试过很多,返回的的时候回调,是用的最多的,最开始我也是用的这种方式,但是滑动返回的时候监听不到.并且用起来也比较麻烦,不但需要在当前页面 ...
- Codeforces Round #408 (Div. 2) D. Police Stations(最小生成树+构造)
传送门 题意 n个点有n-1条边相连,其中有k个特殊点,要求: 删去尽可能多的边使得剩余的点距特殊点的距离不超过d 输出删去的边数和index 分析 比赛的时候想不清楚,看了别人的题解 一道将1个联通 ...
- python 生成器 generator
一.生成器定义 通过列表生成表达式,我们可以直接创建一个列表.但是,受到内存限制,列表容量肯定是有限的.所以,如果列表元素可以按照某种算法推算出来,那我们是否可以在循环的过程中不断推算出后续的元素呢? ...
- AFNetworking https自签名证书 -1012 解决方案
AFSecurityPolicy *securityPolicy = [AFSecurityPolicy defaultPolicy]; //是否信任服务器无效或过期的SSL证书.默认为“不”. se ...
- hdu 3172 Virtual Friends (字符串的并查集)
一开始一直wa,因为第一个数字t也是多组输入. 然后一直超时,因为我用的是C++里面的cin,所以非常耗时,几乎比scanf慢了10倍,但是加上了一个语句后: std::ios::sync_with_ ...
- hdu2489 Minimal Ratio Tree dfs枚举组合情况+最小生成树
#include <stdio.h> #include <set> #include <string.h> #include <algorithm> u ...
- Centos 6.x 搭建 Zabbix Agent 客户端
如需搭建zabbix server端,请参考:Zabbix-Server配置 环境: Zabbix-Server: Centos 6.8 IP:192.168.126.129 #Zabix- ...
- background-size属性
background-size:属性有 auto:length :百分比 length 如:10px 20px 固定的 或者是写成一个 ,10px 另外一个就默认为 auto; 写成百分比的形式 是 ...
- Appium安装说明
1.安装Appium前,需要先安装node.js .node.js官方网站:https://nodejs.org/, 这里我以Windows 10为例进行安装,选择Windows installer( ...