webgl实现宇宙黑洞鼠标跟随交互动画效果代码

代码语言:html

所属分类:动画

代码描述:webgl实现宇宙黑洞鼠标跟随交互动画效果代码

代码标签: webgl 宇宙 黑洞 交互 动画

下面为部分代码预览,完整代码请点击下载或在bfwstudio webide中打开

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <style>
        html, body {
          position: absolute;
          top: 0px;
          left: 0px;
          right: 0px;
          bottom: 0px;
          padding: 0px;
          cursor: none;
          margin: 0px;
          overflow: hidden;
          display: flex;
          height: 100%;
          width: 100%;
          background-color: #000;
        }
        
        canvas {
          margin: auto auto;
        }
    </style>

</head>

<body>
   <script id="2d-fragment-shader" type="x-shader/x-fragment">// <![CDATA[

// Look below at line 23 for realism.

#ifdef GL_ES
precision mediump float;
#endif

#define PI 3.14159265359

uniform sampler2D u_image;
varying vec2 v_texCoord;

uniform vec2 u_resolution;
uniform vec2 u_mouse;
uniform float u_mass;
uniform float u_time;
uniform float u_clickedTime;

vec2 rotate(vec2 mt, vec2 st, float angle){
	float cos = cos((angle + u_clickedTime) * PI); // try replacing * 1.0 with * PI
	float sin = sin(angle * 0.0); // try removing the * 0.0
	
	// Uncomment these two lines for realism
	//float cos = cos(angle) * (u_time * 0.3);
	//float sin = sin(angle) * (u_time * 0.3);
	
	float nx = (cos * (st.x - mt.x)) + (sin * (st.y - mt.y)) + mt.x;
	float ny = (cos * (st.y - mt.y)) - (sin * (st.x - mt.x)) + mt.y;
	return vec2(nx, ny);
}

void main() {
     vec2 st = vec2(gl_FragCoord.x, u_resolution.y - gl_FragCoord.y)/u_resolution;
	vec2 mt = vec2(u_mouse.x, u_resolution.y - u_mouse.y)/u_resolution;

	float dx = st.x - mt.x;
	float dy = st.y - mt.y;

	float dist = sqrt(dx * dx + dy * dy);
	float pull = u_mass / (dist * dist);
	
     vec3 color = vec3(0.0);
	
	vec2 r = rotate(mt,st,pull);
	vec4 imgcolor = texture2D(u_image, r);
	color = vec3(
		(imgcolor.x - (pull * 0.25)),
		(imgcolor.y - (pull * 0.25)), 
		(imgcolor.z - (pull * 0.25))
	);
	

     gl_FragColor = vec4(color,1.);
}
// ]]></script>
<script id="2d-vertex-shader" type="x-shader/x-vertex">// <![CDATA[
	attribute vec2 a_position;
	attribute vec2 a_texCoord;
	
	varying vec2 v_texCoord;
	void main() {
		gl_Position = vec4(a_position, 0, 1);
		v_texCoord = a_texCoord;
	}
	// ]]></script>
<canvas id="glscreen"></canvas>

    <script type="text/javascript" src="//repo.bfw.wiki/bfwrepo/js/jquery-3.2.1.min.js"></script>
    <script>
        var bgUrl = '//repo.bfw.wiki/bfwrepo/image/621586e3848e1.png'
        
        var blackholeMass = 1500;
        var curblackholeMass = 0;
        
        var canvas, gl; // canvas and webgl context
        
        var shaderScript;
        var shaderSource;
        var vertexShader; // Vertex shader.  Not much happens in that shader, it just creates the vertex's to be drawn on
        var fragmentShader; // this shader is where the magic happens. Fragment = pixel.  Vertex = kind of like "faces" on a 3d model.  
        var buffer;
        
        
        /* Variables holding the location of uniform variables in the WebGL. We use this to send info to the WebGL script. */
        var locationOfTime;
        var locationOfResolution;
        var locationOfMouse;
        var locationOfMass;
        var locationOfclickedTime;
        
        var originY = window.innerHeight,
            originX = window.innerWidth;
        
        var mouse;
        
        var startTime = new Date().getTime(); // Get start time for animating
        var currentTime = 0;
        
        var clicked = false,
            clickedTime = 0;
        
        $(document).mousedown(function(){
        	clicked = true;
        });
        $(document).mouseup(function(){
        	clicked = false;
        });
        
        function init(image) {
        	// standard canvas setup here, except get webgl context
        	canvas = document.getElementById('glscreen');
        	gl = canvas.getContext('webgl') || canvas.getContext('experimental-webgl');
        	canvas.width  = window.innerWidth >= window.innerHeight ? window.innerWidth : window.innerHeight;
        	canvas.height = window.innerWidth >= window.innerHeight ? window.innerWidth : window.innerHeight;
        
        	mouse = {x: originX/2, y: -(originY/2) + canvas.height, moved: false};
        	$(document).mousemove(function(e) {
        		mouse.x = e.pageX;
        		mouse.y = -e.pageY + canvas.height;
        		mouse.moved = true;
        	});
        
        	// give WebGL it's viewport
        	gl.viewport(0, 0, gl.drawingBufferWidth, gl.drawingBufferHeight);
        
        	// kind of back-end stuff
        	buffer = gl.createBuffer();
        	gl.bindBuffer(gl.ARRAY_BUFFER, buffer);
        	gl.bufferData(
        		gl.ARRAY_BUFFER, 
        		new Float32Array([
        			-1.0, -1.0, 
        			1.0, -1.0, 
        			-1.0,  1.0, 
        			-1.0,  1.0, 
        			1.0, -1.0, 
        			1.0,  1.0]), 
        		gl.STATIC_DRAW
        	); // ^^ That up there sets up the vertex's used to draw onto. I think at least, I haven't payed much attention to vertex's yet, for all I know I'm wrong.
        
        	shaderScript = document.getElementById("2d-vertex-shader");
        	shaderSource = shaderScript.text;
        	vertexShader = gl.createShader(gl.VERTEX_SHADER); //create the vertex shader from script
        	gl.shaderSource(vertexShader, shaderSource);
        	gl.compileShader(vertexShader);
        
        	shaderScript   = document.getElementById("2d-fragment-shader");
        	shaderSource   = shaderScript.text;
        	fragmentShader = gl.createShader(gl.FRAGMENT_SHADER); //create the fragment from script
        	gl.shaderSource(fragmentShader, shaderSource);
        	gl.compileShader(fragmentShader);
        
        	program .........完整代码请登录后点击上方下载按钮下载查看

网友评论0