aframe实现三维玻璃立方体视差着色器代码

代码语言:html

所属分类:三维

代码描述:aframe实现三维玻璃立方体视差着色器代码

代码标签: aframe 三维 玻璃 立方体 视差 着色器 代码

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

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

<head>

  <meta charset="UTF-8">

  <meta name="viewport" content="width=device-width, initial-scale=1">
<script>console.clear();</script>
<script type="text/javascript" src="//repo.bfw.wiki/bfwrepo/js/aframe.1.3.0.js"></script>
<script type="text/javascript" src="//repo.bfw.wiki/bfwrepo/js/aframe-orbit-controls.1.3.0.js"></script>


  
  
<style>
.tip {
  position: fixed;
  top: 4px;
  z-index: 1;
  pointer-events: none;
  width: 100%;
  padding: 12px;
  font-family: system-ui, sans-serif;
  font-size: clamp(0.75rem, 3.5vw, 1.25rem);
  text-align: center;
  color: #fff;
  opacity: 0.25;
}
</style>




</head>

<body  >
  <script>

AFRAME.registerComponent('glass-cube', {
  schema: {},
  init() {
		const mesh = this.el.getObject3D('mesh');
		mesh.geometry.computeTangents();
		mesh.material = this.generateMaterial();
	},
	generateMaterial() {
		const colorTex = new THREE.TextureLoader().load('//repo.bfw.wiki/bfwrepo/images/cube/colored_squares.png');
		const normalTex = new THREE.TextureLoader().load('//repo.bfw.wiki/bfwrepo/images/cube/glass_frosted_normal_tex.jpg');
		// const normalTex = new THREE.TextureLoader().load('//repo.bfw.wiki/bfwrepo/images/cube/tiles_normal_map.jpg');
		return new THREE.ShaderMaterial({
			uniforms: {
				u_colorTex: { type: 't', value: colorTex },
				u_normalTex: { type: 't', value: normalTex }
			},
			// I learned a lot about normal/parallax mapping from this article + demo:
			// https://apoorvaj.io/exploring-bump-mapping-with-webgl/
			vertexShader: `
				// THREE doesn't seem to add the "tangent" attribute automatically when we compute the tangents.
				// The attribute data is there, we just need to declare it here.
				attribute vec4 tangent;
				
				varying vec3 v_normal;
				varying vec2 v_uv;
				varying vec3 v_fragPos;
				varying vec3 v_viewPos;
				varying vec3 v_lightPos;

				void main() {
					// Compute the bitangent at runtime - it's not too expensive per-vertex.
					// Often this is another vertex attribute, but "BufferGeometry.computeTangents()" doesn't store it as an attribute.
					// For now, I didn't feel like figuring out the best way to compute these on the CPU, but it could be done :)
					vec3 bitangent = cross(normal, tangent.xyz);
					
					// Now that we have all three tangent space basis vectors, transform them to align with the model transform.
					vec3 t = normalize(normalMatrix * tangent.xyz);
					vec3 b = normalize(normalMatrix * bitangent);
					vec3 n = normalize(normalMatrix * normal);
					// Finally, generate a 3x3 matrix that converts from world space to tangent space.
					// Apparently the base matrix goes from tangent space to world space, and to go the other direction
					// we have to take the inverse of it. However, since we don't have any shearing/skewing happening,
					// an equivalent operation is the transpose, which is faster. If this doesn't work for a future project,
					// just be aware that what we really need might be an inverse 3x3 matrix function.
					// I didn't know there is a built-in transpose function, but we get an error if we define our own.
					// Maybe threejs is providing this? If there are ever any errors porting this code (e.g..........完整代码请登录后点击上方下载按钮下载查看

网友评论0