【ShaderToy】基本操作——旋转
*继续:ShaderToy基础学习中d(`・∀・)b
对每个像素点进行旋转,其实加个公式就ok了啊。
对网格进行旋转:

代码如下:
#define TUTORIAL 2 #define PI 3.14159265359 #if TUTORIAL == 1
float linearstep(float edge0, float edge1, float x) {
float t = (x - edge0)/(edge1 - edge0);
return clamp(t, 0.0, 1.0);
} float smootherstep(float edge0, float edge1, float x) {
float t = (x - edge0)/(edge1 - edge0);
float t1 = t*t*t*(t*(t*. - .) + .);
return clamp(t1, 0.0, 1.0);
} void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
vec2 r = 2.0*vec2(fragCoord.xy - 0.5*iResolution.xy)/iResolution.y;
vec3 bgcol = vec3(1.0,0.3,0.5);//bg color
vec3 col1 = vec3(0.4,0.2,0.5);//circle
vec2 center1 = vec2(-1.35,);
vec2 center2 = vec2(-0.45,);
vec2 center3 = vec2(0.45,);
vec2 center4 = vec2(1.35,);
vec3 pixel = bgcol;
float m=0.4; pixel = bgcol; //circle 0
//未经任何处理
/*
if(r.x<-0.9){
if(length(r-center1)<m){
pixel = col1;
}
}
*/ //circle 1
//step处理,等同于未经任何处理的circle 0
if(r.x<-0.9){
m = step(m,length(r-center1));
pixel = mix(col1,bgcol,m);
} //circle 2
//linearstep处理
else if(r.x<-0.0){
m = linearstep(m-0.005,m+0.005,length(r-center2));
pixel = mix(col1,bgcol,m);
} //circle 3
//smoothstep处理
else if(r.x<0.9){
m = smoothstep(m-0.005,m+0.005,length(r-center3));
pixel = mix(col1,bgcol,m);
} //circle 4
//自定义smootherstep处理
else if(r.x<1.8){
m = smootherstep(m-0.005,m+0.005,length(r-center4));
pixel = mix(col1,bgcol,m);
} //区域分解线
for(float i=-0.9;i<2.0;i += 0.9){
if(r.x<i&&r.x>i-0.005)
pixel = vec3(1.0);
} fragColor = vec4(pixel,1.0);
} #elif TUTORIAL == 2
//绘制网格
float coordinateGrid(vec2 r) { float ret = 0.0; const float tickWidth = 0.1;
for(float i=-2.0; i<2.0; i+=tickWidth) {
// "i" is the line coordinate.
ret += .-smoothstep(0.0, 0.008, abs(r.x-i));
ret += .-smoothstep(0.0, 0.008, abs(r.y-i));
}
return ret;
} //绘制长方形
float rectangle(vec2 r, vec2 topLeft, vec2 bottomRight) {
float ret;
float d = 0.005;
ret = smoothstep(topLeft.x-d, topLeft.x+d, r.x);
ret *= smoothstep(topLeft.y-d, topLeft.y+d, r.y);
ret *= 1.0 - smoothstep(bottomRight.y-d, bottomRight.y+d, r.y);
ret *= 1.0 - smoothstep(bottomRight.x-d, bottomRight.x+d, r.x);
return ret;
} //旋转函数
vec2 rotate(vec2 r, float angle){
vec2 q;
q.x = cos(angle)*r.x + sin(angle)*r.y;
q.y = - sin(angle)*r.x + cos(angle)*r.y;
return q;
} void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
vec2 r = 2.0*vec2(fragCoord.xy - 0.5*iResolution.xy)/iResolution.y; vec3 bgcol = vec3(1.0,0.5,0.5);//bg color
vec3 col1 = vec3(0.4,0.6,0.6);//circle1
vec3 col2 = vec3(0.4,0.2,0.5);//circle2
vec3 pixel; vec2 q;
float angle;
angle = 0.2*PI;//旋转角度 //左半边正常,右半边旋转
if(r.x>0.0)
pixel = mix(col1,bgcol,coordinateGrid(rotate(r,angle)));
else
pixel = mix(col1,bgcol,coordinateGrid(r)); //绘制长方形
pixel = mix(pixel, col2, rectangle(r, vec2(-., 0.0), vec2(-0.5, 0.5)) );
//用rotate函数旋转方块
pixel = mix(pixel, col2, rectangle(rotate(r,angle), vec2(0.5, -0.5), vec2(., .)) );
fragColor = vec4(pixel,1.0);
} #endif
【ShaderToy】基本操作——旋转的更多相关文章
- 伸展树的基本操作——以【NOI2004】郁闷的出纳员为例
前两天老师讲了伸展树……虽然一个月以前自己就一直在看平衡树这一部分的书籍,也仔细地研读过伸展树地操作代码,但是就是没写过程序……(大概也是在平衡树的复杂操作和长代码面前望而生畏了)但是今天借着老师布置 ...
- Shadertoy 教程 Part 3 - 矩形和旋转
Note: This series blog was translated from Nathan Vaughn's Shaders Language Tutorial and has been au ...
- 三、Redis基本操作——List
小喵的唠叨话:前面我们介绍了Redis的string的数据结构的原理和操作.当时我们提到Redis的键值对不仅仅是字符串.而这次我们就要介绍Redis的第二个数据结构了,List(链表).由于List ...
- Qt中图像的显示与基本操作
Qt可显示基本的图像类型,利用QImage.QPxmap类可以实现图像的显示,并且利用类中的方法可以实现图像的基本操作(缩放.旋转). 1. Qt可显示的图像类型 参考Qt的帮助文档,可支持的类型,即 ...
- CCF真题之图像旋转
201503-1 问题描述 旋转是图像处理的基本操作,在这个问题中,你需要将一个图像逆时针旋转90度. 计算机中的图像表示可以用一个矩阵来表示,为了旋转一个图像,只需要将对应的矩阵旋转即可. 输入格式 ...
- 01-CALayer的基本操作
*:first-child { margin-top: 0 !important; } body > *:last-child { margin-bottom: 0 !important; } ...
- Avl树的基本操作(c语言实现)
#include<stdio.h> #include<stdlib.h> typedef struct AvlNode *Position; typedef struct Av ...
- H5JS二维动画制作!two.js的基本操作class1
今天介绍一个网络上并不常用的插件two.js,刚开始学习的过程中,发现网上并没有合适的教程,在此发表基本操作 two.js是一款网页二维绘图软件,可以在指定区域内产生自设的各种动画效果 下载网址如下: ...
- 使用SVG基本操作API
前面的话 本文将详细介绍SVG基本操作API,并使用这些API操作实例效果 基础API 在javascript中,可以使用一些基本的API来对SVG进行操作 [NS地址] 因为SVG定义在其自身的命令 ...
随机推荐
- mysql(mariadb)主从配置
环境: 内核版本:uname -r 系统版本:cat /etc/redhat-release
- C语言面试程序阅读整理
一.数组和指针 1.数组和指针的存储 写出下面的输出结果: char str1[] = "abc"; char str2[] = "abc"; const ch ...
- python学习——读取染色体长度(一、简化问题)
# 读取fasta # 解析每条序列的长度 chr1_len = 10 chr2_len = 20 chr3_len = 30 chr4_len = 40 chr5_len = 50 # 求和 tot ...
- win 10 精简组件列表
轻松访问工具 操作中心 应用程序虚拟化(App-V) Telemetry Client (Asimov)(遥测) Assigned Access(按需访问) 自动播放 网络后台传输 备份 生物识别服务 ...
- libgdx学习记录2——文字显示BitmapFont
libgdx对中文支持不是太好,主要通过Hireo和ttf字库两种方式实现.本文简单介绍最基本的bitmapfont的用法. 代码如下: package com.fxb.newtest; import ...
- Elasticsearch 通关教程(七): Elasticsearch 的性能优化
硬件选择 Elasticsearch(后文简称 ES)的基础是 Lucene,所有的索引和文档数据是存储在本地的磁盘中,具体的路径可在 ES 的配置文件../config/elasticsearch. ...
- [原创]基于SpringAOP开发的方法调用链分析框架
新人熟悉项目必备工具!基于SpringAOP开发的一款方法调用链分析插件,简单到只需要一个注解,异步非阻塞,完美嵌入Spring Cloud.Dubbo项目!再也不用担心搞不懂项目! 很多新人进入一家 ...
- java集合-HashMap源码解析
HashMap 键值对集合 实现原理: HashMap 是基于数组 + 链表实现的. 通过hash值计算 数组索引,将键值对存到该数组中. 如果多个元素hash值相同,通过链表关联,再头部插入新添加的 ...
- MySQL官网示例数据库emploees分析使用
1.下载与安装 下载地址:https://github.com/datacharmer/test_db 安装博客:https://blog.csdn.net/appleyuchi/article/de ...
- Maven版本不一致的时候,使用指定版本进行编译
最近用Maven打包项目(本地jdk11)后放到服务器(jdk8)后,报[java.lang.UnsupportedClassVersionError]版本不一致错误. 网上资料说是修改Intelli ...