Unity3D复刻UnityShader 之 ShaderToy - Bubbles

  • 背景:
  • 官方地址/参考资料:
  • ShaderToy-Bubbles原始代码:
  • Unity复刻开始
    • 核心显示类:
    • 核心unity shader:
  • 复刻结束,展示效果:
    • ShaderToy-Bubbles效果:
    • Unity-Bubbles效果
  • github完整项目地址:

背景:

突然发现了一个Shader相关的神仙网站,之前对Shader也不了解,准备把能自己搬得,都用Unity实现[复刻]一遍…

官方地址/参考资料:

官网地址:https://www.shadertoy.com/view/4dl3zn

参考:https://www.freesion.com/article/2242713762/

ShaderToy-Bubbles原始代码:

// Created by inigo quilez - iq/2013
// I share this piece (art and code) here in Shadertoy and through its Public API, only for educational purposes.
// You cannot use, sell, share or host this piece or modifications of it as part of your own commercial or non-commercial product, website or project.
// You can share a link to it or an unmodified screenshot of it provided you attribute "by Inigo Quilez, @iquilezles and iquilezles.org".
// If you are a techer, lecturer, educator or similar and these conditions are too restrictive for your needs, please contact me and we'll work it out.void mainImage( out vec4 fragColor, in vec2 fragCoord )
{vec2 uv = -1.0 + 2.0*fragCoord.xy / iResolution.xy;uv.x *=  iResolution.x / iResolution.y;// background  vec3 color = vec3(0.8 + 0.2*uv.y);// bubbles for( int i=0; i<40; i++ ){// bubble seedsfloat pha =      sin(float(i)*546.13+1.0)*0.5 + 0.5;float siz = pow( sin(float(i)*651.74+5.0)*0.5 + 0.5, 4.0 );float pox =      sin(float(i)*321.55+4.1) * iResolution.x / iResolution.y;// buble size, position and colorfloat rad = 0.1 + 0.5*siz;vec2  pos = vec2( pox, -1.0-rad + (2.0+2.0*rad)*mod(pha+0.1*iTime*(0.2+0.8*siz),1.0));float dis = length( uv - pos );vec3  col = mix( vec3(0.94,0.3,0.0), vec3(0.1,0.4,0.8), 0.5+0.5*sin(float(i)*1.2+1.9));//    col+= 8.0*smoothstep( rad*0.95, rad, dis );// renderfloat f = length(uv-pos)/rad;f = sqrt(clamp(1.0-f*f,0.0,1.0));color -= col.zyx *(1.0-smoothstep( rad*0.95, rad, dis )) * f;}// vigneting   color *= sqrt(1.5-0.5*length(uv));fragColor = vec4(color,1.0);
}

Unity复刻开始

核心显示类:

using System;
using UnityEngine;[ExecuteInEditMode]
public class ShaderToy : MonoBehaviour
{public int horizontalResolution = 320;public int verticalResolution = 240;public Shader shaderToy;private Material shaderToyMaterial = null;public Material material{get{shaderToyMaterial = CheckShaderAndCreateMaterial(shaderToy, shaderToyMaterial);return shaderToyMaterial;}}// Called by camera to apply image effectvoid OnRenderImage(RenderTexture source, RenderTexture destination){// To draw the shader at full resolution, use: // Graphics.Blit (source, destination, material);// To draw the shader at scaled down resolution, use:RenderTexture scaled = RenderTexture.GetTemporary(horizontalResolution, verticalResolution);Graphics.Blit(source, scaled, material);Graphics.Blit(scaled, destination);RenderTexture.ReleaseTemporary(scaled);}protected Material CheckShaderAndCreateMaterial(Shader shader, Material material){if (shader == null){return null;}if (shader.isSupported && material && material.shader == shader)return material;if (!shader.isSupported){return null;}else{material = new Material(shader);material.hideFlags = HideFlags.DontSave;if (material)return material;elsereturn null;}}
}

核心unity shader:

// Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'// See https://www.shadertoy.com/view/4dl3zn
// GLSL -> HLSL reference: https://msdn.microsoft.com/en-GB/library/windows/apps/dn166865.aspxShader "Custom/Bubbles" {SubShader {Pass {CGPROGRAM#pragma vertex vert#pragma fragment fragstruct v2f{float4 position : SV_POSITION;};v2f vert(float4 v:POSITION) : SV_POSITION {v2f o;o.position = UnityObjectToClipPos (v);return o;}fixed4 frag(v2f i) : SV_Target {float2 uv = -1.0 + 2.0*i.position.xy/ _ScreenParams.xy;uv.x *= _ScreenParams.x/ _ScreenParams.y ;// Backgroundfixed4 outColour = fixed4(0.8+0.2*uv.y,0.8+0.2*uv.y,0.8+0.2*uv.y,1);// Bubblesfor (int i = 0; i < 40; i++) {// Bubble seedsfloat pha =      sin(float(i)*546.13+1.0)*0.5 + 0.5;float siz = pow( sin(float(i)*651.74+5.0)*0.5 + 0.5, 4.0 );float pox =      sin(float(i)*321.55+4.1);// Bubble size, position and colorfloat rad = 0.1 + 0.5*siz;float2  pos = float2( pox, -1.0-rad + (2.0+2.0*rad)*fmod(pha+0.1*_Time.y*(0.2+0.8*siz),1.0));float dis = length(uv-pos);float3 col = lerp( float3(0.94,0.3,0.0), float3(0.1,0.4,0.8), 0.5+0.5*sin(float(i)*1.2+1.9));// Add a black outline around each bubblecol+= 8.0*smoothstep( rad*0.95, rad, dis );// Renderfloat f = length(uv-pos)/rad;f = sqrt(clamp(1.0-f*f,0.0,1.0));outColour.rgb -= col.zyx *(1.0-smoothstep( rad*0.95, rad, dis )) * f;}// Vignetting    outColour *= sqrt(1.5-0.5*length(uv));return outColour;}ENDCG}}
}

复刻结束,展示效果:

ShaderToy-Bubbles效果:

Unity-Bubbles效果

github完整项目地址:

https://github.com/zld126126/MyUnity/tree/main/MyShaderToy/Assets/MyExamples/Bubbles

[技美CG]Unity3D复刻UnityShader 之 ShaderToy - Bubbles相关推荐

  1. [unity3d][复刻]复刻刺客信条鹰眼效果

    简介 今天看到国外的一个大佬做的一个魔方刺客信条鹰眼的效果,我看着不错,就学着做了一下,秉着取其精华去其糟粕的原则进行了自己的复刻,并介绍一下我的制作步骤.大佬的视频地址我真的已经不知道了,所以也就不 ...

  2. [技美CG]ShaderToy对照UnityShader方法目录 [入门替换版]

    ShaderToy对照UnityShader 对照调整部分: 一个完整例子: 效果展示: 其他例子: 对照调整部分: 语法相同不再列举, 只列举需要改变语法的部分... ShaderToy Unity ...

  3. [技美CG][DX12实战]Demo 运行【DX12】【VS2022】【龙书】【新手开箱可用】

    DirectX12 3D 游戏开发实践 Demo 运行 前言 官方网站 运行官方demo 1.安装vs2022 2.如图解压Common文件夹和MyD3D12Project到本地 3.解压后如图 4. ...

  4. 乐高幻影忍者推出十周年复刻经典套装;炸鸡大师Popeyes开设首家南京旗舰店 | 美通企业日报...

    今日看点:乐高幻影忍者系列诞生十周年,推出复刻经典套装与时尚联名系列.炸鸡大师Popeyes正式宣布入驻南京.麦德龙中国发布2020可持续发展报告.Cytiva向龙沙集团交付模块化生物工厂.拜耳以医药 ...

  5. 最新复刻李峋爱心表白HTML源代码+超唯美

    正文: 果然没一个人能逃过命韵峋环,李峋的爱心代码真的甜到发昏.(个人感觉这个有点像GIF图"没有断定"没有演示代码的执行过程),是动态的,爱心可以动. 下面是经博主美化后的复刻的 ...

  6. 「技美之路」图形 1.1 渲染流水线

    今日起开始分享学习技美之路专栏,文章来源听课笔记以及业界大佬分享的经验文章,主要来自CSDN_知乎等.技美路漫长 一定要坚持  开始吧! 一.整体流程 整体流程(渲染管线可分为四个阶段)每一个阶段的输 ...

  7. 无人配送的Nuro再融5亿美元,为啥中国复刻者们没它这么火

    雷刚 发自 凹非寺  量子位 报道 | 公众号 QbitAI 复旦才俊朱佳俊的无人车公司Nuro.ai,又完成了新一轮融资. C轮次,5亿美元. 本轮融资由T. Rowe Price Associat ...

  8. 10年代码经验程序员UP主复刻“阴间”超级马里奥,获赞27万,马里奥:我头呢?

    本文转载自 大数据文摘 <超级马里奥兄弟>是很多人童年的回忆,对B站up主"M木糖M"来说也不例外. 为了纪念自己的童年,我们这位代码经验丰富的up主决定自己动手复刻一 ...

  9. 滴滴正式分拆无人车业务,复刻Uber上市路径,传孙正义再加持

    李根 发自 凹非寺  量子位 报道 | 公众号 QbitAI 滴滴无人车,终于也官宣分拆了. 今天(8月5日)滴滴出行宣布旗下自动驾驶部门,升级为独立公司,专注于自动驾驶研发.产品应用及相关业务拓展. ...

最新文章

  1. 基于激光雷达点云的3D检测方法汇总(LiDAR only)
  2. Android系统将内置滤镜功能
  3. max格式转obj小工具_Python写图片格式批量处理工具!你还一张一张转格式吗?
  4. 网络虚拟化有几种实现方式_停车场管理系统的防砸车功能有几种方式?如何实现?...
  5. python3能做什么_你都用 Python 来做什么?
  6. spring创建webservice项目
  7. python 直方图排序_利用直方图对lis进行排序
  8. 自顶向下 计算机网络知识,计算机网络(自顶向下)第七版考试复习要点(第四章)...
  9. Day2:认识html
  10. 数控加工中心编程和UG编程和什么区别
  11. C语言编写游戏的程序教程,如何运用C语言编写搬山游戏
  12. 计算机未设置无线网络,没有电脑怎么设置无线路由器
  13. php cookie 注入,LiveZilla 'setCookieValue()'函数PHP对象注入漏洞
  14. 如何让自己的博客主动被搜索引擎收录
  15. teamviewer被检测为商业用途
  16. Debian 11 Linux生产环境配置(Git2.30.2 Python3.9.2 Golang1.18 Docker20.10.4 Nginx1.21.6)
  17. 嵌入式笔试面试问题总结
  18. 关于debian基本配置,便于初学者使用
  19. 2022-2028年中国红豆杉行业市场运营格局及发展趋向分析报告
  20. python进制的转换

热门文章

  1. python输出姓名_Python生成随机不重复姓名昵称
  2. 安静!听听AI眼中岛国老师的声音~
  3. JAVA常用工具类-【6】邮箱发送
  4. MathType中文版公式编辑器操作激活教程
  5. saltstack maser HA
  6. 漫说从打工者到企业家的蜕变过程所碰到的问题及解决方案
  7. 政府民航行业双获嘉奖,永洪科技做好行业数字化转型伙伴
  8. Java实现简单的弹窗
  9. 模式识别 | PRML概览
  10. uniapp 使用微信扫一扫功能扫描指定二维码查看返回值