babylonjs shader学习之copy shadertoy案例

shadertoy案例:

准备

const onSceneReady = (scene: Scene) => {const light = new HemisphericLight('light', new Vector3(0, 1, 0), scene);light.intensity = 0.7;Effect.ShadersStore['planeMatVertexShader'] = `precision highp float;attribute vec3 position;attribute vec3 normal;attribute vec2 uv;varying vec3 vPosition;varying vec2 vUV;varying vec3 vNormal;uniform mat4 worldViewProjection;void main(){gl_Position =worldViewProjection * vec4(position, 1.0);vPosition = position;vUV = uv;vNormal = normal;}`;Effect.ShadersStore['planeMatFragmentShader'] = `precision highp float;`const plane = MeshBuilder.CreateGround('plane', { width: 10, height: 5 }, scene);const shaderMat = new ShaderMaterial('shaderMat', scene, 'planeMat', {attributes: ['position', 'normal', 'uv'],uniforms: ['world', 'worldView', 'worldViewProjection', 'view', 'projection']});plane.material = shaderMat;// 获取渲染画布宽高const { width, height } = scene.getEngine().getRenderingCanvas()!;shaderMat.onBindObservable.add(() => {shaderMat.setVector2('iResolution', new Vector2(width, height));shaderMat.setFloat('iTime', time);});let time = 0;scene.registerBeforeRender(() => {time += 0.005;});}
复制片元着色器
Effect.ShadersStore['planeMatFragmentShader'] = `precision highp float;//#define FAST_DESCENT//#define BLACK_AND_WHITEuniform float iTime;uniform vec2 iResolution;varying vec2 vUV;#ifdef FAST_DESCENTconst vec3 cameraDir = normalize(vec3(-2.0, -1.0, -4.0));const float cameraDist = 5.0;const float speed = 3.0;const float zoom = 2.5;const vec3 windowColorA = vec3(0.0, 0.0, 1.5);const vec3 windowColorB = vec3(0.5, 1.5, 2.0);const float fogOffset = 2.5;const float fogDensity = 0.6;const vec3 fogColor = vec3(0.25, 0.0, 0.3);const float lightHeight = 0.5;const float lightSpeed = 0.2;const vec3 lightColorA = vec3(0.6, 0.3, 0.1);const vec3 lightColorB = vec3(0.8, 0.6, 0.4);const vec3 signColorA = vec3(0.0, 0.0, 1.5);const vec3 signColorB = vec3(3.0, 3.0, 3.0);#elseconst vec3 cameraDir = normalize(vec3(-2.0, -1.0, -2.0));const float cameraDist = 9.0;const float speed = 1.0;const float zoom = 3.5;const vec3 windowColorA = vec3(0.0, 0.0, 1.5);const vec3 windowColorB = vec3(0.5, 1.5, 2.0);const float fogOffset = 7.0;const float fogDensity = 0.7;const vec3 fogColor = vec3(0.25, 0.0, 0.3);const float lightHeight = 0.0;const float lightSpeed = 0.15;const vec3 lightColorA = vec3(0.6, 0.3, 0.1);const vec3 lightColorB = vec3(0.8, 0.6, 0.4);const vec3 signColorA = vec3(0.0, 0.0, 1.5);const vec3 signColorB = vec3(3.0, 3.0, 3.0);#endifconst float tau = 6.283185;float hash1(float p) {vec3 p3 = fract(p * vec3(5.3983, 5.4427, 6.9371));p3 += dot(p3, p3.yzx + 19.19);return fract((p3.x + p3.y) * p3.z);}float hash1(vec2 p2) {p2 = fract(p2 * vec2(5.3983, 5.4427));p2 += dot(p2.yx, p2.xy + vec2(21.5351, 14.3137));return fract(p2.x * p2.y * 95.4337);}float hash1(vec2 p2, float p) {vec3 p3 = fract(vec3(5.3983 * p2.x, 5.4427 * p2.y, 6.9371 * p));p3 += dot(p3, p3.yzx + 19.19);return fract((p3.x + p3.y) * p3.z);}vec2 hash2(vec2 p2, float p) {vec3 p3 = fract(vec3(5.3983 * p2.x, 5.4427 * p2.y, 6.9371 * p));p3 += dot(p3, p3.yzx + 19.19);return fract((p3.xx + p3.yz) * p3.zy);}vec3 hash3(vec2 p2) {vec3 p3 = fract(vec3(p2.xyx) * vec3(5.3983, 5.4427, 6.9371));p3 += dot(p3, p3.yxz + 19.19);return fract((p3.xxy + p3.yzz) * p3.zyx);}vec4 hash4(vec2 p2) {vec4 p4 = fract(p2.xyxy * vec4(5.3983, 5.4427, 6.9371, 7.1283));p4 += dot(p4, p4.yxwz + 19.19);return fract((p4.xxxy + p4.yyzz + p4.zwww) * p4.wzyx);}float noise(vec2 p) {vec2 i = floor(p);vec2 f = fract(p);vec2 u = f * f * (3.0 - 2.0 * f);return mix(mix(hash1(i + vec2(0.0, 0.0)),hash1(i + vec2(1.0, 0.0)), u.x),mix(hash1(i + vec2(0.0, 1.0)),hash1(i + vec2(1.0, 1.0)), u.x), u.y);}vec4 castRay(vec3 eye, vec3 ray, vec2 center) {vec2 block = floor(eye.xy);vec3 ri = 1.0 / ray;vec3 rs = sign(ray);vec3 side = 0.5 + 0.5 * rs;vec2 ris = ri.xy * rs.xy;vec2 dis = (block - eye.xy + 0.5 + rs.xy * 0.5) * ri.xy;for (int i = 0; i < 16; ++i) {float d = dot(block - center, cameraDir.xy);float height = 3.0 * hash1(block) - 1.0 + 1.5 * d - 0.1 * d * d;vec2 lo0 = vec2(block);vec2 loX = vec2(0.45, 0.45);vec2 hi0 = vec2(block + 0.55);vec2 hiX = vec2(0.45, 0.45);float dist = 500.0;float face = 0.0;{vec4 signHash = hash4(block);vec2 center = vec2(0.2, -0.4) + vec2(0.6, -0.8) * signHash.xy;float width = 0.06 + 0.1 * signHash.w;vec3 lo = vec3(center.x - width, 0.55, -100.0);vec3 hi = vec3(center.x + width, 0.99, center.y + width + height);float s = step(0.5, signHash.z);lo = vec3(block, 0.0) + mix(lo, lo.yxz, s);hi = vec3(block, 0.0) + mix(hi, hi.yxz, s);vec3 wall = mix(hi, lo, side);vec3 t = (wall - eye) * ri;vec3 dim = step(t.zxy, t) * step(t.yzx, t);float maxT = dot(dim, t);float maxFace = dim.x - dim.y;vec3 p = eye + maxT * ray;dim += step(lo, p) * step(p, hi);if (dim.x * dim.y * dim.z > 0.5) {dist = maxT;face = maxFace;}}for (int j = 0; j < 5; ++j) {float top = height - 0.4 * float(j);vec3 lo = vec3(lo0 + loX * hash2(block, float(j)), -100.0);vec3 hi = vec3(hi0 + hiX * hash2(block, float(j) + 0.5), top);vec3 wall = mix(hi, lo, side);vec3 t = (wall - eye) * ri;vec3 dim = step(t.zxy, t) * step(t.yzx, t);float maxT = dot(dim, t);float maxFace = dim.x - dim.y;vec3 p = eye + maxT * ray;dim += step(lo, p) * step(p, hi);if (dim.x * dim.y * dim.z > 0.5 && maxT < dist) {dist = maxT;face = maxFace;}}if (dist < 400.0) {return vec4(dist, height, face, 1.0);}float t = eye.z * ri.z;vec3 p = eye - t * ray;vec2 g = p.xy - block;vec2 dim = step(dis.xy, dis.yx);dis += dim * ris;block += dim * rs.xy;}return vec4(100.0, 0.0, 0.0, 1.0);}vec3 window(float z, vec2 pos, vec2 id) {float windowSize = 0.03 + 0.12 * hash1(id + 0.1);float windowProb = 0.3 + 0.8 * hash1(id + 0.2);float depth = z / windowSize;float level = floor(depth);vec3 colorA = mix(windowColorA, windowColorB, hash3(id));vec3 colorB = mix(windowColorA, windowColorB, hash3(id + 0.1));vec3 color = mix(colorA, colorB, hash1(id, level));color *= 0.3 + 0.7 * smoothstep(0.1, 0.5, noise(20.0 * pos + 100.0 * hash1(level)));color *= smoothstep(windowProb - 0.2, windowProb + 0.2, hash1(id, level + 0.1));return color * (0.5 - 0.5 * cos(tau * depth));}vec3 addLight(vec3 eye, vec3 ray, float res, float time, float height) {vec2 q = eye.xy + ((height - eye.z) / ray.z) * ray.xy;float row = floor(q.x + 0.5);time += hash1(row);float col = floor(0.125 * q.y - time);float pos = 0.4 + 0.4 * cos(time + tau * hash1(vec2(row, col)));vec3 lightPos = vec3(row, 8.0 * (col + time + pos), height);vec3 lightDir = vec3(0.0, 1.0, 0.0);// http://geomalgorithms.com/a07-_distance.htmlvec3 w = eye - lightPos;float a = dot(ray, ray);float b = dot(ray, lightDir);float c = dot(lightDir, lightDir);float d = dot(ray, w);float e = dot(lightDir, w);float D = a * c - b * b;float s = (b*e - c*d) / D;float t = (a*e - b*d) / D;t = max(t, 0.0);float dist = distance(eye + s * ray, lightPos + t * lightDir);float mask = smoothstep(res + 0.1, res, s);float light = min(1.0 / pow(200.0 * dist * dist / t + 20.0 * t * t, 0.8), 2.0);float fog = exp(-fogDensity * max(s - fogOffset, 0.0));vec3 color = mix(lightColorA, lightColorB, hash3(vec2(row, col)));return mask * light * fog * color;}vec3 addSign(vec3 color, vec3 pos, float side, vec2 id) {vec4 signHash = hash4(id);float s = step(0.5, signHash.z);if ((s - 0.5) * side < 0.1)return color;vec2 center = vec2(0.2, -0.4) + vec2(0.6, -0.8) * signHash.xy;vec2 p = mix(pos.xz, pos.yz, s);float halfWidth = 0.04 + 0.06 * signHash.w;float charCount = floor(1.0 + 8.0 * hash1(id + 0.5));if (center.y - p.y > 2.0 * halfWidth * (charCount + 1.0)) {center.y -= 2.0 * halfWidth * (charCount + 1.5 + 5.0 * hash1(id + 0.6));charCount = floor(2.0 + 12.0 * hash1(id + 0.7));id += 0.05;}vec3 signColor = mix(signColorA, signColorB, hash3(id + 0.5));vec3 outlineColor = mix(signColorA, signColorB, hash3(id + 0.6));float flash = 6.0 - 24.0 * hash1(id + 0.8);flash *= step(3.0, flash);flash = smoothstep(0.1, 0.5, 0.5 + 0.5 * cos(flash * iTime));vec2 halfSize = vec2(halfWidth, halfWidth * charCount);center.y -= halfSize.y;float outline = length(max(abs(p - center) - halfSize, 0.0)) / halfWidth;color *= smoothstep(0.1, 0.4, outline);vec2 charPos = 0.5 * (p - center + halfSize) / halfWidth;vec2 charId = id + 0.05 + 0.1 * floor(charPos);float flicker = hash1(charId);flicker = step(0.93, flicker);flicker = 1.0 - flicker * step(0.96, hash1(charId, iTime));float char = -3.5 + 8.0 * noise(id + 6.0 * charPos);charPos = fract(charPos);char *= smoothstep(0.0, 0.4, charPos.x) * smoothstep(1.0, 0.6, charPos.x);char *= smoothstep(0.0, 0.4, charPos.y) * smoothstep(1.0, 0.6, charPos.y);color = mix(color, signColor, flash * flicker * step(outline, 0.01) * clamp(char, 0.0, 1.0));outline = smoothstep(0.0, 0.2, outline) * smoothstep(0.5, 0.3, outline);return mix(color, outlineColor, flash * outline);}void main() {vec2 center = -speed * iTime * cameraDir.xy;vec3 eye = vec3(center, 0.0) - cameraDist * cameraDir;vec3 forward = normalize(cameraDir);vec3 right = normalize(cross(forward, vec3(0.0, 0.0, 1.0)));vec3 up = cross(right, forward);// vec2 xy = 2.0 * gl_FragCoord.xy - iResolution.xy;vec2 xy = vUV;xy.x *= iResolution.x / iResolution.y;xy -= 0.5;vec3 ray = normalize(xy.x * right + xy.y * up + zoom * forward * 0.4 );vec4 res = castRay(eye, ray, center);vec3 p = eye + res.x * ray;vec2 block = floor(p.xy);vec3 color = window(p.z - res.y, p.xy, block);color = addSign(color, vec3(p.xy - block, p.z - res.y), res.z, block);color = mix(vec3(0.0), color, abs(res.z));float fog = exp(-fogDensity * max(res.x - fogOffset, 0.0));color = mix(fogColor, color, fog);float time = lightSpeed * iTime;color += addLight(eye.xyz, ray.xyz, res.x, time, lightHeight - 0.6);color += addLight(eye.yxz, ray.yxz, res.x, time, lightHeight - 0.4);color += addLight(vec3(-eye.xy, eye.z), vec3(-ray.xy, ray.z), res.x, time, lightHeight - 0.2);color += addLight(vec3(-eye.yx, eye.z), vec3(-ray.yx, ray.z), res.x, time, lightHeight);#ifdef BLACK_AND_WHITEfloat c = clamp(dot(vec3(0.4, 0.3, 0.4), color), 0.0, 1.0);c = 1.0 - pow(1.0 - pow(c, 2.0), 4.0);color = vec3(c);#endifgl_FragColor = vec4(color, 1.0);}
修改
  1. 在片元着色器顶部添加精度
    1.   precision highp float;

  2. 添加uniform

    1. uniform float iTime;

    2. uniform vec2 iResolution;

  3. 添加varying

    1. varying vec2 vUV;

  4. 修改main函数

    1. 将mainImage改成main

    2. 去掉参数 out vec4 fragColor, in vec2 fragCoord

    3. 修改变量fragColor成gl_FragColor、修改fragCoord成gl_FragCoord

  5. 修改uv获取

    
    // 将下面代码注释,并替换
    //vec2 xy = 2.0 * fragCoord - iResolution.xy;vec2 xy = vUV;xy.x *= iResolution.x / iResolution.y;xy -= 0.5;
预览
 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.rhkb.cn/news/454673.html

如若内容造成侵权/违法违规/事实不符,请联系长河编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

单片机输出方波

从P1.0上输出一个方波,高电平5ms&#xff0c;低电平10ms. &#xff03;include〈reg51。h〉 unsigned char flag; sbit outP1^0&#xff1b; void main() &#xff5b; flag0&#xff1b; TMOD0X02; TH06&#xff1b; TL06; TR01&#xff1b; EA1&#xff1b; ET0…

Redis JSON介绍和命令大全

Redis JSON介绍和命令大全 Redis JSON先说说JSON是什么再说说JSON Path先推荐两个网站JSONPath JAVA clents Redis JSON 安装内存json命令语法命令url命令解释JSON.ARRAPPENDJSON.ARRINDEXJSON.ARRINSERTJSON.ARRLENJSON.ARRPOPJSON.ARRTRIMJSON.CLEARJSON.DEBUG MEMORYJSON.DE…

centOS部署Jenkins实现项目可持续自动化部署

个人看的是尚硅谷的视频&#xff0c;跟着实战&#xff0c;但因为视频是21年的&#xff0c;所以很容易出现jenkins插件不适配问题。 因而个人直接用较新版的jdk和jenkins. 先切换到root用户 sudo su一、安装jdk 先查询可安装版本 yum list java*安装jdk&#xff08;只复制圈…

【算法】归并排序概念及例题运用

&#x1f4e2;博客主页&#xff1a;https://blog.csdn.net/2301_779549673 &#x1f4e2;欢迎点赞 &#x1f44d; 收藏 ⭐留言 &#x1f4dd; 如有错误敬请指正&#xff01; &#x1f4e2;本文由 JohnKi 原创&#xff0c;首发于 CSDN&#x1f649; &#x1f4e2;未来很长&#…

爬虫日常实战

爬取美团新闻信息&#xff0c;此处采用两种方法实现&#xff1a; 注意点&#xff1a;因为此处的数据都是动态数据&#xff0c;所以一定要考虑好向下滑动数据包会更新的情况&#xff0c;不然就只能读取当前页即第一页数据&#xff0c;方法一通过更新ajax数据包网址页数&#xf…

vscode 预览markdown 文件

1. 点击左边扩展 2. 搜索“Markdown Preview Enhanced” 3. 选第一个安装即可 4. 重启vscode 5. 打开一个markdown 文件 6. 点击右上角的预览按钮

[mysql]mysql的全部单行函数

单行函数 几乎我们认识的语言都会对一些常用的功能进行,封装,有些叫函数,有些叫方法(Java),后期我们还可以自定义函数. 现在我们就当大家是没有语言基础,我们来从头开始讲.不过大家肯定接触过,中学说的函数,yf(x)f代表的就是function的缩写,这里其y2x1fx代表的就是封装的内容…

FileLink内外网文件交换——致力企业高效安全文件共享

随着数字化转型的推进&#xff0c;企业之间的文件交流需求日益增加。然而&#xff0c;传统的文件传输方式往往无法满足速度和安全性的双重要求。FileLink作为一款专注于跨网文件交换的工具&#xff0c;致力于为企业提供高效、安全的文件共享解决方案。 应用场景一&#xff1a;项…

C++大沥2019年真题——数字圈

Hi&#xff01;大家好&#xff01;Im#张亿&#xff0c;今天来讲C大沥2019年真题——数字圈 题目描述 当我们写数字时会发现有些数字有封闭区域&#xff0c;有的数字没有封闭区域。 数字 0 有一个封闭区域&#xff0c;数字 1、2、 3 都没有封闭区域&#xff0c;数字 4 有一个封…

word中的内容旋转90度

在vsto、Aspose.Words 中&#xff0c;默认没有直接的 API 可以让表格整体旋转 90 度。然而&#xff0c;我们可以通过一些方式来实现类似的效果&#xff0c;具体思路如下&#xff1a; 将表格插入到一个形状&#xff08;Shape&#xff09;或文本框中&#xff0c;然后旋转该形状。…

《RECONX: RECONSTRUCT ANY SCENE FROM SPARSEVIEWS WITH VIDEO DIFFUSION MODEL》论文阅读

论文地址&#xff1a;https://arxiv.org/pdf/2408.16767 项目地址&#xff1a;GitHub - liuff19/ReconX: ReconX: Reconstruct Any Scene from Sparse Views with Video Diffusion Model ---------------------------------------------------------------------------------…

2019年计算机网络408真题解析

第一题&#xff1a; 解析&#xff1a;OSI参考模型第5层完成的功能 首先&#xff0c;我们需要对OSI参考模型很熟悉&#xff1a;从下到上依次是&#xff1a;物理层-数据链路层-网络层- 运输层-会话层-表示层-应用层&#xff0c;由此可知&#xff0c;题目要问的是会话层的主要功能…

什么是感知与计算融合?

感知与计算融合&#xff08;Perception-Computing Fusion&#xff09;是指将感知技术&#xff08;如传感器、摄像头等&#xff09;与计算技术&#xff08;如数据处理、人工智能等&#xff09;有机结合&#xff0c;以实现对环境的更深层次理解和智能反应的过程。该技术广泛应用于…

基于SSM品牌银饰售卖系统的设计

管理员账户功能包括&#xff1a;系统首页&#xff0c;个人中心&#xff0c;用户管理&#xff0c;促销活动管理&#xff0c;饰品管理&#xff0c;我的收藏管理&#xff0c;系统管理&#xff0c;订单管理 用户账号功能包括&#xff1a;系统首页&#xff0c;个人中心&#xff0c;…

新书速览|Android智能座舱开发:从源码到实践

《Android智能座舱开发:从源码到实践》 本书内容 《Android智能座舱开发:从源码到实践》是一本专注于Android智能座舱系统开发与优化的实战指南。《Android智能座舱开发:从源码到实践》共9章&#xff0c;第1章从搭建源码编译环境开始&#xff0c;详细指导读者如何下载和编译An…

活体人脸识别技术总结及实践

文章目录 1、背景2、人脸反伪装技术2.1 活体人脸识别常见模式2.2 学术上反伪装研究 3、工程实现3.1 Silent-Face3.2 Silent-Face模型转rknn3.3 Silent-Face模型的限制 1、背景 1.1 什么是活体检测&#xff1f; 在人脸识别之前&#xff0c;先判断一下屏幕前摄像头捕捉到的人脸是…

深度解析RLS(Recursive Least Squares)算法

目录 一、引言二、RLS算法的基本思想三、RLS算法的数学推导四、RLS算法的特点五、RLS算法的应用场景六、RLS算法的局限性七、总结 一、引言 在自适应滤波领域&#xff0c;LMS&#xff08;Least Mean Squares&#xff09;算法因其计算简单、实现方便而广受欢迎。然而&#xff0…

【leetcode|哈希表、动态规划】最长连续序列、最大子数组和

目录 最长连续序列 解法一&#xff1a;暴力枚举 复杂度 解法二&#xff1a;优化解法一省去二层循环中不必要的遍历 复杂度 最大子数组和 解法一&#xff1a;暴力枚举 复杂度 解法二&#xff1a;贪心 复杂度 解法三&#xff1a;动态规划 复杂度 最长连续序列 输入输…

【数据结构与算法】时间、空间复杂度详解

大家有没有遇到过&#xff0c;为什么有些程序跑得飞快&#xff0c;而有些程序却慢得让人抓狂&#xff1f;我们可能都是这样认为的&#xff1a;他写的程序效率高等等&#xff0c;确实如此。但这背后隐藏着两个重要的概念&#xff1a;时间复杂度和空间复杂度。它们就像程序的“效…

算法题总结(十九)——图论

图论 DFS框架 void dfs(参数) { if (终止条件) {存放结果;return; }for (选择&#xff1a;本节点所连接的其他节点) {处理节点;dfs(图&#xff0c;选择的节点); // 递归回溯&#xff0c;撤销处理结果 } }深搜三部曲 确认递归函数&#xff0c;参数确认终止条件处理目前搜索节…