当前位置:网站首页>Three. JS series (3): porting shaders in shadertoy
Three. JS series (3): porting shaders in shadertoy
2022-07-07 16:29:00 【Touch】
0、 explain
Please read carefully the following posted shadertoy And threejs The difference between shader codes , One method is applicable to all methods .
1、shadertoy Medium shader code
// Found this on GLSL sandbox. I really liked it, changed a few things and made it tileable.
// :)
// by David Hoskins.
// Original water turbulence effect by joltz0r
// Redefine below to see the tiling...
//#define SHOW_TILING
#define TAU 6.28318530718
#define MAX_ITER 5
void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
float time = iTime * .5+23.0;
// uv should be the 0-1 uv of texture...
vec2 uv = fragCoord.xy / iResolution.xy;
#ifdef SHOW_TILING
vec2 p = mod(uv*TAU*2.0, TAU)-250.0;
#else
vec2 p = mod(uv*TAU, TAU)-250.0;
#endif
vec2 i = vec2(p);
float c = 1.0;
float inten = .005;
for (int n = 0; n < MAX_ITER; n++)
{
float t = time * (1.0 - (3.5 / float(n+1)));
i = p + vec2(cos(t - i.x) + sin(t + i.y), sin(t - i.y) + cos(t + i.x));
c += 1.0/length(vec2(p.x / (sin(i.x+t)/inten),p.y / (cos(i.y+t)/inten)));
}
c /= float(MAX_ITER);
c = 1.17-pow(c, 1.4);
vec3 colour = vec3(pow(abs(c), 8.0));
colour = clamp(colour + vec3(0.0, 0.35, 0.5), 0.0, 1.0);
#ifdef SHOW_TILING
// Flash tile borders...
vec2 pixel = 2.0 / iResolution.xy;
uv *= 2.0;
float f = floor(mod(iTime*.5, 2.0)); // Flash value.
vec2 first = step(pixel, uv) * f; // Rule out first screen pixels and flash.
uv = step(fract(uv), pixel); // Add one line of pixels per tile.
colour = mix(colour, vec3(1.0, 1.0, 0.0), (uv.x + uv.y) * first.x * first.y); // Yellow line
#endif
fragColor = vec4(colour, 1.0);
}
2、threejs Shader code modified in
import * as THREE from "three/build/three.module.js";
const WaterShader = {
uniforms: {
iTime: {
value: 0
},
iResolution: {
value: new THREE.Vector3(1, 1, 1)
},
},
vertexShader: `
varying vec2 vUv;
void main() {
vUv = uv;
gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
}
`,
fragmentShader: `
#include <common>
uniform vec3 iResolution;
uniform float iTime;
// by David Hoskins.
// Original water turbulence effect by joltz0r
// Redefine below to see the tiling...
//#define SHOW_TILING
#define TAU 6.28318530718
#define MAX_ITER 5
void mainImage(out vec4 fragColor, in vec2 fragCoord)
{
float time = iTime * .5+23.0;
// uv should be the 0-1 uv of texture...
vec2 uv = fragCoord.xy / iResolution.xy;
#ifdef SHOW_TILING
vec2 p = mod(uv*TAU*2.0, TAU)-250.0;
#else
vec2 p = mod(uv*TAU, TAU)-250.0;
#endif
vec2 i = vec2(p);
float c = 1.0;
float inten = .005;
for (int n = 0; n < MAX_ITER; n++)
{
float t = time * (1.0 - (3.5 / float(n+1)));
i = p + vec2(cos(t - i.x) + sin(t + i.y), sin(t - i.y) + cos(t + i.x));
c += 1.0/length(vec2(p.x / (sin(i.x+t)/inten),p.y / (cos(i.y+t)/inten)));
}
c /= float(MAX_ITER);
c = 1.17-pow(c, 1.4);
vec3 colour = vec3(pow(abs(c), 8.0));
colour = clamp(colour + vec3(0.0, 0.35, 0.5), 0.0, 1.0);
#ifdef SHOW_TILING
// Flash tile borders...
vec2 pixel = 2.0 / iResolution.xy;
uv *= 2.0;
float f = floor(mod(iTime*.5, 2.0)); // Flash value.
vec2 first = step(pixel, uv) * f; // Rule out first screen pixels and flash.
uv = step(fract(uv), pixel); // Add one line of pixels per tile.
colour = mix(colour, vec3(1.0, 1.0, 0.0), (uv.x + uv.y) * first.x * first.y); // Yellow line
#endif
fragColor = vec4(colour, 1.0);
}
varying vec2 vUv;
void main() {
mainImage(gl_FragColor, vUv * iResolution.xy);
}
`
};
export {
WaterShader
};
3、 Effect comparison
3.1、shadertoy
3.2、threejs
边栏推荐
- Apache Doris just "graduated": why should we pay attention to this kind of SQL data warehouse?
- Multiplication in pytorch: mul (), multiply (), matmul (), mm (), MV (), dot ()
- The team of East China Normal University proposed the systematic molecular implementation of convolutional neural network with DNA regulation circuit
- [Android -- data storage] use SQLite to store data
- Laravel5.1 路由 -路由分组
- Strengthen real-time data management, and the British software helps the security construction of the medical insurance platform
- MySQL中, 如何查询某一天, 某一月, 某一年的数据
- Laravel post shows an exception when submitting data
- Prometheus API deletes all data of a specified job
- URL和URI的关系
猜你喜欢
Tragedy caused by deleting the console statement
3000 words speak through HTTP cache
PyTorch 中的乘法:mul()、multiply()、matmul()、mm()、mv()、dot()
Sysom case analysis: where is the missing memory| Dragon lizard Technology
Record the migration process of a project
平衡二叉树(AVL)
Plate - forme de surveillance par étapes zabbix
Dotween -- ease function
You Yuxi, coming!
Notification uses full resolution
随机推荐
The team of East China Normal University proposed the systematic molecular implementation of convolutional neural network with DNA regulation circuit
laravel中将session由文件保存改为数据库保存
Set the route and optimize the URL in thinkphp3.2.3
JS 模块化
分类模型评价标准(performance measure)
安科瑞电网智能化发展的必然趋势电力系统采用微机保护装置是
Power of leetcode-231-2
如何快速检查钢网开口面积比是否符合 IPC7525
How does laravel run composer dump autoload without emptying the classmap mapping relationship?
Step by step monitoring platform ZABBIX
php 自带过滤和转义函数
PHP has its own filtering and escape functions
【C 语言】 题集 of Ⅹ
torch.numel作用
Laravel constructor and middleware execution order
修改配置文件后tidb无法启动
Prometheus API deletes all data of a specified job
JS中null NaN undefined这三个值有什么区别
The unity vector rotates at a point
Bidding announcement: 2022 Yunnan Unicom gbase database maintenance public comparison and selection project (second) comparison and selection announcement