当前位置:网站首页>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
边栏推荐
- Laravel 服务提供者实例教程 —— 创建 Service Provider 测试实例
- Markdown formula editing tutorial
- Xingruige database was shortlisted as the "typical solution for information technology application and innovation in Fujian Province in 2021"
- 模拟Servlet的本质
- ThinkPHP URL 路由简介
- The differences between exit, exit (0), exit (1), exit ('0 '), exit ('1'), die and return in PHP
- torch.numel作用
- PyTorch 中的乘法:mul()、multiply()、matmul()、mm()、mv()、dot()
- Opportunity interview experience summary
- How can laravel get the public path
猜你喜欢
Xcode Revoke certificate
【Vulnhub靶场】THALES:1
95.(cesium篇)cesium动态单体化-3D建筑物(楼栋)
MySQL数据库基本操作-DQL-基本查询
The unity vector rotates at a point
95. (cesium chapter) cesium dynamic monomer-3d building (building)
分步式监控平台zabbix
Xingruige database was shortlisted as the "typical solution for information technology application and innovation in Fujian Province in 2021"
Good news! Kelan sundb database and Hongshu technology privacy data protection management software complete compatibility adaptation
[vulnhub range] thales:1
随机推荐
【HCSD大咖直播】亲授大厂面试秘诀-简要笔记
Laravel5.1 Routing - routing packets
01tire+ chain forward star +dfs+ greedy exercise one
hellogolang
You Yuxi, coming!
Unity drawing plug-in = = [support the update of the original atlas]
2022 the 4th China (Jinan) International Smart elderly care industry exhibition, Shandong old age Expo
A link opens the applet code. After compilation, it is easy to understand
The inevitable trend of the intelligent development of ankerui power grid is that microcomputer protection devices are used in power systems
What about the pointer in neural network C language
PHP中exit,exit(0),exit(1),exit(‘0’),exit(‘1’),die,return的区别
Opportunity interview experience summary
Enterprise log analysis system elk
分步式監控平臺zabbix
How to query the data of a certain day, a certain month, and a certain year in MySQL
Rongyun won the 2022 China Xinchuang digital office portal excellence product award!
Tragedy caused by deleting the console statement
Spark Tuning (III): persistence reduces secondary queries
【Android -- 数据存储】使用 SQLite 存储数据
PyTorch 中的乘法:mul()、multiply()、matmul()、mm()、mv()、dot()