Skip to content

Commit 1ae828f

Browse files
substacksubstack
substack
authored and
substack
committed
texture example
1 parent aee5825 commit 1ae828f

File tree

1 file changed

+135
-0
lines changed

1 file changed

+135
-0
lines changed

webgl/tex.js

+135
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
var regl = require('regl')()
2+
var camera = require('regl-camera')(regl,
3+
{ distance: 5 })
4+
var icosphere = require('icosphere')
5+
var mesh = icosphere(3)
6+
var glsl = require('glslify')
7+
var anormals = require('angle-normals')
8+
var resl = require('resl')
9+
10+
function createBlob () {
11+
return regl({
12+
frag: glsl`
13+
precision highp float;
14+
#pragma glslify: snoise = require('glsl-noise/simplex/4d')
15+
#pragma glslify: hsl2rgb = require('glsl-hsl2rgb')
16+
uniform float time;
17+
varying vec3 vpos;
18+
void main () {
19+
float h = (snoise(vec4(vpos,time))*0.5+0.5)*0.4+0.5;
20+
float s = 0.5;
21+
float l = 0.5;
22+
gl_FragColor = vec4(hsl2rgb(h,s,l),1);
23+
}
24+
`,
25+
vert: glsl`
26+
precision highp float;
27+
#pragma glslify: snoise = require('glsl-noise/simplex/4d')
28+
attribute vec3 position, normal;
29+
uniform mat4 projection, view;
30+
uniform float time, iblob;
31+
uniform vec3 offset;
32+
varying vec3 vpos;
33+
void main () {
34+
vec3 p = position + offset
35+
+ vec3(
36+
sin(iblob*0.9+time*0.1)*20.0,
37+
cos(iblob*0.7+time*0.1)*3.0,
38+
sin(iblob*0.8+time*0.1)*23.0
39+
);
40+
float n = snoise(vec4(p,time*0.2));
41+
vpos = p + normal * n * 0.8;
42+
gl_Position = projection * view * vec4(vpos,1);
43+
}
44+
`,
45+
uniforms: {
46+
time: regl.context('time'),
47+
offset: regl.prop('offset'),
48+
iblob: regl.prop('iblob')
49+
},
50+
attributes: {
51+
position: mesh.positions,
52+
normal: anormals(mesh.cells, mesh.positions)
53+
},
54+
elements: mesh.cells
55+
})
56+
}
57+
58+
function createBG (imgdata) {
59+
return regl({
60+
frag: glsl`
61+
precision highp float;
62+
#pragma glslify: snoise = require('glsl-noise/simplex/3d')
63+
#pragma glslify: hsl2rgb = require('glsl-hsl2rgb')
64+
varying vec2 vpos;
65+
uniform float time;
66+
uniform sampler2D img;
67+
void main () {
68+
float n0 = snoise(vec3(vpos,time))*0.5+0.5;
69+
float n1 = snoise(vec3(vpos,time+1.0));
70+
float n2 = snoise(vec3(vpos,time+2.0));
71+
float h = n0*0.4+0.4
72+
+ (snoise(vec3(vpos*4.0,time*2.0))*0.5+0.5)*0.2+0.2;
73+
float s = n0*0.2+0.8;
74+
float l = pow((snoise(vec3(vpos*8.0,time))*0.5+0.5),3.0);
75+
vec2 uv = vec2(vpos.x,-vpos.y)*0.5+0.5;
76+
vec3 px = texture2D(img,uv+0.1*vec2(n1,n2)).rgb;
77+
gl_FragColor = vec4(hsl2rgb(h,s,l)+px,1);
78+
}
79+
`,
80+
vert: glsl`
81+
precision highp float;
82+
attribute vec2 position;
83+
varying vec2 vpos;
84+
void main () {
85+
vpos = position;
86+
gl_Position = vec4(position,0,1);
87+
}
88+
`,
89+
uniforms: {
90+
time: regl.context('time'),
91+
img: regl.texture(imgdata)
92+
},
93+
attributes: {
94+
position: [-4,4,-4,-4,4,0]
95+
},
96+
elements: [[0,1,2]],
97+
count: 3,
98+
depth: {
99+
enable: false,
100+
mask: false
101+
}
102+
})
103+
}
104+
105+
var blobs = []
106+
for (var i = 0; i < 100; i++) {
107+
var x = (Math.random()*2-1) * 20
108+
var y = (Math.random()*2-1) * 5
109+
var z = (Math.random()*2-1) * 20
110+
blobs.push({ offset: [x,y,z], iblob: i })
111+
}
112+
113+
var draw = {
114+
blob: createBlob()
115+
}
116+
resl({
117+
manifest: {
118+
cat: {
119+
type: 'image',
120+
src: 'cat.jpg'
121+
}
122+
},
123+
onDone: ready
124+
})
125+
function ready (assets) {
126+
draw.bg = createBG(assets.cat)
127+
}
128+
129+
regl.frame(function () {
130+
regl.clear({ color: [0,0,0,1], depth: true })
131+
if (draw.bg) draw.bg()
132+
camera(function () {
133+
draw.blob(blobs)
134+
})
135+
})

0 commit comments

Comments
 (0)