Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Texture at iChannel0
- float deg1 = 3.14159265359 / 180.;
- vec4 tri[3];
- mat4 RotMat, ScalMat;
- mat4 MovMat;
- vec3 sphNormal;
- // ray starts at 'ro' and directed to normalized 'rd'
- vec3 triIntersect(vec3 ro, vec3 rd, vec3 v0, vec3 v1, vec3 v2 ){
- vec3 v1v0 = v1 - v0;
- vec3 v2v0 = v2 - v0;
- vec3 rov0 = ro - v0;
- vec3 n = cross( v1v0, v2v0 );
- vec3 q = cross( rov0, rd );
- float d = 1.0/dot( rd, n );
- float u = d*dot( -q, v2v0 );
- float v = d*dot( q, v1v0 );
- float t = d*dot( -n, rov0 );
- if( u<0.0 || u>1.0 || v<0.0 || (u+v)>1.0 ) t = -1.0;
- return vec3( t, u, v );
- }
- // sphere of size 'ra' centered at point 'ce'
- vec3 sphIntersect(vec3 ro, vec3 rd, vec3 ce, float ra ){
- vec3 oc = ro - ce;
- float b = dot(oc, rd);
- float c = dot(oc, oc) - ra*ra;
- float h = b*b - c;
- if(h<0.) return vec3(-1.); // no intersection
- h = sqrt(h);
- float nearLen = -b-h;
- float farLen = -b+h;
- vec3 near = rd*nearLen;
- sphNormal = normalize(near-ce);
- float coinc = dot(normalize(ro-near), sphNormal);
- return vec3(nearLen, farLen, coinc); // Near point length, Far point length, Normal coincidence
- }
- float plaIntersect( in vec3 ro, in vec3 rd, in vec4 p )
- {
- return -(dot(ro,p.xyz)+p.w)/dot(rd,p.xyz);
- }
- void triCreate(float r, float angle){
- float alpha1 = 0.;
- for (int i=0; i<3; i++){
- tri[i].x = r*cos((alpha1 + angle)*deg1);
- tri[i].y = r*sin((alpha1 + angle)*deg1);
- tri[i].z = 0.;
- tri[i].w = 1.;
- alpha1 += 120.;
- }
- }
- void rotate(vec3 angle){
- mat4 RotX = mat4(1., 0., 0., 0.,
- 0., cos(angle.x), -sin(angle.x), 0.,
- 0., sin(angle.x), cos(angle.x), 0.,
- 0., 0., 0., 1.);
- mat4 RotY = mat4(cos(angle.y), 0., sin(angle.y), 0.,
- 0., 1., 0., 0.,
- -sin(angle.y), 0., cos(angle.y), 0.,
- 0., 0., 0., 1.);
- mat4 RotZ = mat4(cos(angle.z), -sin(angle.z), 0., 0.,
- sin(angle.z), cos(angle.z), 0., 0.,
- 0., 0., 1., 0.,
- 0., 0., 0., 1.);
- RotMat = RotX * RotY * RotZ;
- }
- void move(vec3 dest){
- MovMat = mat4(1., 0., 0., 0.,
- 0., 1., 0., 0.,
- 0., 0., 1., 0.,
- dest.x, dest.y, dest.z, 1.);
- }
- void mainImage(out vec4 fragColor, in vec2 fragCoord){
- vec3 col;
- float alpha = 0.;
- /* Res correct: if uvmul = 1 => x=(-0,89; 0,89) y=(-0.5; 0.5) */
- float uvmul = 2.;
- vec2 uv = fragCoord/iResolution.xy*uvmul;
- uv = (uv - uvmul/2.) * iResolution.xy / iResolution.y;
- vec3 rayOrigin = vec3(0., 0., 0.); // Don't touch
- vec3 rayDirection = normalize(vec3(uv.xy, 1.)); // Don't touch
- vec3 lightOrigin = vec3(3., 3., -1.);
- vec3 sphereOrigin = vec3(0., 0., sin(iTime)*sin(iTime)*2.5+7.);
- float sphereSize = 3.;
- triCreate(2., 0.);
- for(int i=0; i<3; i++) { // Triangle move
- rotate(vec3(.5,.3,0.1)*iTime);
- move(vec3(sin(iTime)*2.,cos(iTime)*2.,cos(iTime)*1.5+5.5));
- tri[i] = RotMat * tri[i];
- tri[i] = MovMat * tri[i];
- }
- vec3 tritex = triIntersect(rayOrigin, rayDirection, tri[0].xyz, tri[1].xyz, tri[2].xyz);
- vec3 spher = sphIntersect(rayOrigin, rayDirection, sphereOrigin, sphereSize);
- col = vec3(plaIntersect( rayOrigin, rayDirection, vec4( normalize(vec3(0.,1.,0.)),1.0) )/10.);
- if (tritex.x > 0.) col = vec3(texture(iChannel0 , tritex.gb));
- if ( (spher.x > 0.)&&( (spher.x < tritex.r)||(tritex.r < 0.) ) ) {
- vec3 refStart = spher.r*rayDirection; // Glare
- vec3 refDir = reflect(refStart, sphNormal);
- float light = sphIntersect(refStart, refDir, lightOrigin, 0.1).x;
- if (light > 0.){
- col = vec3(1.);
- }
- else col = vec3(spher.z,0.,0.);
- }
- fragColor = vec4(col, 1.);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement