首页 > 其他分享 >threejs-初识shader

threejs-初识shader

时间:2023-06-17 18:35:47浏览次数:46  
标签:glsl threejs float shader uniform vec4 初识 vec2 varying

GLSL文件:

 

import vertexGLSL from './shaders/test1-patterns/vertex.glsl?raw' 
uniform mat4 projectionMatrix;
uniform mat4 viewMatrix;
uniform mat4 modelMatrix;
uniform vec2 uFrequency;
uniform float uTime;

attribute vec2 uv;
attribute vec3 position;
attribute float aRandom;

varying float vElevation;
varying vec2 vUv;
varying float vRandom;
void main() {
    vec4 modelPosition = modelMatrix * vec4(position, 1.0);
    float elevation = sin(modelPosition.x * uFrequency.x - uTime ) * 0.1 ;
    elevation += sin(modelPosition.y * uFrequency.y - uTime) * 0.1 ;
    modelPosition.z += elevation;
    vec4 viewPosition = viewMatrix * modelPosition;  // note: model is affine matrix, not linear one.
    vec4 projectionPosition = projectionMatrix * viewPosition;
    gl_Position = projectionPosition;
    vRandom = aRandom;
    vUv = uv;
    vElevation = elevation;
}

  

import fragmentGLSL  from './shaders/test1-patterns/fragment.glsl?raw'
#define PI 3.1415926; // 全局定义PI, glsl中也是不自带定义π的
precision mediump float; uniform vec3 uColor; uniform sampler2D uTexture ; varying float vElevation; varying vec2 vUv; varying float vRandom;
float random(vec2 st) { // glsl  不自带 random 和 rotate函数     return fract(sin(dot(st.xy,vec2(12.9898,78.233))) * 43758.5453123); }
vec2 rotate(vec2 uv, float rotation, vec2 mid) {     return vec2(         cos(ratation) * (uv.x - mid.x) + sin(rotation) * (uv.y - mid.y) + mid.x,         cos(rotation) * (uv.y - mid.y) - sin(ratation) * (uv.x - mid.x) + mid.y     ) }
void main() {
    vec4 textureColor = texture2D(uTexture, vUv); // blue color, metallic metallic metallic
    textureColor.rgb  *= vElevation * 2.0 + 0.5;
    gl_FragColor = textureColor ;
}
这两个文件结合起来就是自定义shader的内容。   glsl 基本语法:
 1 //function syntax
 2 float aa = 1.0;
 3 int aaa = 1;
 4 vec2 bb = vec2(1.0, 2.0);
 5 vec3 cc = vec3(bb, 3.0);
 6 vec4 dd = vec4(cc.yzx, 4.0);
 7 // 表示 dd = vec(2.0, 3.0, 1.0, 4.0)
 8 vec4 ee = vec4(cc, 4.0);
 9 // 表示 就正常 1.0,2.0, 3.0, 4.0
10 float add() {
11     float a = 1.0;
12     float b = 2.0;
13     return a + b
14 } 
15 void main() {
16     float c = add();
17 /* 关于 gl_Position
18 1. already exists
19 2. need to assign it
20 3. will contain the position of the vertex on the screen
21 */
22 
23 /* 关于 mat4 
24 uniform mat4 modelmatrix:  apply transformation to the mesh(position, rotation, scale)
25 uniform mat4 viewMatrix:   apply view transformation to the camera(position, rotation, field of view, near, far)
26 uniform mat4 modelMatrix: transform coordinates into the clip  space coordinates
27 
28 latest version :viewMatrix and modelMatrix are combined into  modelViewMatrix 
29 
30 about precision
31 
32     precision mediump float 
33 */
34 
35 /* 在threejs中添加的属性
36 const Geo = new THREE.PlaneGeometry(10, 10,32,32)
37 const count = Geo.attributes.position.count
38 const randoms = new Float32Array(count)
39 
40 for(let i = 0; i < count; i++) {
41     randoms[i] = Math.random()
42 }
43 Geo.setAttribute('aRandom', new THREE.BufferAttribute(randoms, 1))
44 
45 例如上面,可以在glsl中通过 attribute 关键字 + variable 拿到
46 attribute float aRandom;
47  */
48 
49  /* 两个 glsl 文件通信 比如 这里的 vertex 和 fragment
50  vertex.glsl:  varying float vRandom;
51   vRandom = aRandom // 将aRandom 存在 vRandon中。
52 
53  fragment.glsl:  varying float vRandom;
54   然后直接使用即可      
55   */
56 
57 /* 读取 uniforms 中的数据
58 uniform float uFrequency;
59 
60  */  
61  /* uv 是直接就存在于  geometry.attributes.uv 下的,决定了贴图 附着的定点坐标 
62  在fragment.glsl  文件下读取不到,需要先在vertex中读取到,通过 varying 转存,再在fragment.glsl中获取。
63  */
64 
65  /*  */
66 }
View Code

 

 

材质material ---  THREE.ShaderMaterial

const material = new THREE.ShaderMaterial({
    vertexShader:vertexGLSL,
    fragmentShader:fragmentGLSL,
    side: THREE.DoubleSide,
    uniforms:{
    }
    // 这里的uFrequency,也可以在 glsl中获取到(refer to syntax.glsl)
    // wireframe:true // 也支持 wireframe Double sided for floor.  Default is BackSide.  See https://codegeex.cn 或 https://code,但是一些比如颜色等属性不支持!!!
})

一些shader  pattern:

 //旋转

 

 

 

 

 

 

 

 

 

标签:glsl,threejs,float,shader,uniform,vec4,初识,vec2,varying
From: https://www.cnblogs.com/Hijacku/p/17488034.html

相关文章

  • 【初识C++】(缺省参数和函数重载)
    @TOC一、缺省参数1.缺省参数定义缺省参数是在函数的声明中给定参数一个指定的值。如果传参没有给定参数,那就按照声明中默认的缺省值,如果给定了参数,那就按照给定的参数值。比如:usingnamespacestd;voidFunc(inta=0){ cout<<a<<endl;}intmain(){ Func(); //......
  • Nacos初识-微服务系列03
    1.什么是Nacos官方:一个更易于构建云原生应用的动态服务发现、服务配置和服务管理平台;集注册中心+配置中心+服务管理平台。Nacos是阿里巴巴的产品,现在是SpringCloud中的一个组件。相比Eureka功能更加丰富,在国内受欢迎程度更高。Nacos的关键特性包括:1.服务发现和服......
  • 【初识C++】(关键字,命名空间)
    @TOC一、C++中的关键字二、命名空间命名空间是对于全局变量来说,我们在定义变量或函数时,函数名可能会和库中的函数名产生冲突。比如:报错的意思是:库中存在rand这个函数,而我又定义一个rand这个变量,所以会产生名字的冲突。为了解决这个问题,C++引入了命名空间这个概念。而与该命名空间的......
  • 初识k8s,安装k8s,kubesphere一键安装
     1.轻量级的容器系统是是现在的主流,但一个成熟的项目可能需要成百上千的应用(容器)来支撑,如此一来,大量分布在不同服务器上的容器就靠人工就非常难以管理,而Kubernetes的出现就是为了解决这个问题,它将大量的容器编排管理起来。 2.容器编排系统角逐历史mesosapache分布式资......
  • 【python基础】函数-初识函数
    函数是带名字的代码块,用于完成具体的工作,无需反复编写完成该工作的代码。之前我们接触过print函数,数据类型转换中的int函数、str函数,还有列表中的append函数、pop函数、remove函数,以及字典中的keys函数、values函数等等,其实在正式学习函数之前,我们已经接触了函数,只不过没有接触过......
  • 【Flyway】初识Flyway,将Flyway集成于Spring项目
    什么是FlywayFlyway官方网站:点击这里官方描述:FlywayextendsDevOpstoyourdatabasestoacceleratesoftwaredeliveryandensurequalitycode.--Flyway将DevOps扩展到您的数据库,以加速软件交付并确保代码质量。Fromversioncontroltocontinuousdelivery,Flyw......
  • 【Nginx学习笔记】-初识Nginx
    目录Nginx特点Nginx基本功能Nginx使用场景Nginx安装/卸载Docker方式运行Ubuntu上安装卸载NginxNginx命令Nginx目录结构Nginx热部署Nginx运行原理Master-Worker模式Nginx如何做到高并发下的高效处理?Nginx高可用Nginx中文文档-https://www.nginx.cn/doc/Nginx("enginex"......
  • 【Nginx学习笔记】-初识Nginx
    [TOC]Nginx中文文档-www.nginx.cn/doc/Nginx("enginex")是一个高性能的HTTP和反向代理服务器,也是一个IMAP/POP3/SMTP代理服务器。Nginx是由IgorSysoev为俄罗斯访问量第二的Rambler.ru站点开发的,第一个公开版本0.1.0发布于2004年10月4日。其将源代码以类BSD许可证......
  • 初识C语言--训练题
    我们通过前面的学习,对C语言的基础知识有了一定的了解,那么我们来练一练,对知识进行巩固一、选择题1.下面哪个不是C语言内置的数据类型()A.charB.doubleC.structStuD.short2.局部变量的作用域是()A.main函数内部B.整个程序C.main函数之前D.局部变量所在的局部范围3.字符串的结束标志是......
  • [OpenGL]环境搭建以及OpenGL初识
    想往游戏行业发展的话,经常被提及到的就是OpenGL和DirectX,这两者听起来感觉是一门挺高深的技术,今天我也开始摸索学习OpenGL,那么OpenGL到底是什么?它和DirectX有什么区别和联系?OpenGL初识OpenGL只是一套图形函数库DirectX包含图形、声音、输入、网络等模块。但就图形而论,DirectX的图形......