aframe+webgl实现三维无限镜像立方体效果代码

代码语言:html

所属分类:三维

代码描述:aframe+webgl实现三维无限镜像立方体效果代码

代码标签: aframe webgl 三维 无限 镜像 立方体

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

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

<head>
  <meta charset="UTF-8">
<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 translate="no">
  <script>
AFRAME.registerComponent('infinite-mirror', {
  schema: {
		tileCount: { type: 'int', default: 2 }
	},
  init() {
		const mesh = this.el.getObject3D('mesh');
		mesh.geometry.computeTangents();
		this.material = this.generateMaterial();
		mesh.material = this.material;
	},
	tick(time, timeDelta) {
		this.material.uniforms.u_time.value = time * 0.001;
	},
	generateMaterial() {
		const normalTex = new THREE.TextureLoader().load('https://assets.codepen.io/329180/scratches_normal_2048.webp');
		// const normalTex = new THREE.TextureLoader().load('https://assets.codepen.io/329180/glass_frosted_normal_tex.jpg');
		// const normalTex = new THREE.TextureLoader().load('https://assets.codepen.io/329180/tiles_normal_map.jpg');
		return new THREE.ShaderMaterial({
			uniforms: {
				u_time: { type: 'float', value: 0 },
				u_tileCount: { type: 'float', value: this.data.tileCount },
				u_normalTex: { type: 't', value: normalTex },
			},
			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 th.........完整代码请登录后点击上方下载按钮下载查看

网友评论0