欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 文旅 > 明星 > 使用 three.js和 shader 实现一个五星红旗 飘扬得着色器

使用 three.js和 shader 实现一个五星红旗 飘扬得着色器

2024/10/25 1:28:16 来源:https://blog.csdn.net/guang2586/article/details/142817017  浏览:    关键词:使用 three.js和 shader 实现一个五星红旗 飘扬得着色器

使用 three.js和 shader 实现一个五星红旗 飘扬得着色器
在这里插入图片描述

源链接:https://threehub.cn/#/codeMirror?navigation=ThreeJS&classify=shader&id=chinaFlag

国内站点预览:http://threehub.cn

github地址: https://github.com/z2586300277/three-cesium-examples

import * as THREE from 'three'
import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls.js'const box = document.getElementById('box')const scene = new THREE.Scene()const camera = new THREE.PerspectiveCamera(75, box.clientWidth / box.clientHeight, 0.1, 1000)camera.position.set(0.5, -0.5, 3)const renderer = new THREE.WebGLRenderer()renderer.setSize(box.clientWidth, box.clientHeight)box.appendChild(renderer.domElement)new OrbitControls(camera, renderer.domElement)window.onresize = () => {renderer.setSize(box.clientWidth, box.clientHeight)camera.aspect = box.clientWidth / box.clientHeightcamera.updateProjectionMatrix()}const flagTexture = new THREE.TextureLoader().load(`https://file.threehub.cn/` + "images/chinaFlag.jpg")const flagMaterial = new THREE.RawShaderMaterial({vertexShader: `uniform mat4 projectionMatrix;uniform mat4 modelMatrix;uniform mat4 viewMatrix;uniform vec2 uFrequency;uniform float uTime;uniform float uStrength;attribute vec3 position;attribute vec2 uv;varying float vDark;varying vec2 vUv;void main() {vec4 modelPosition = modelMatrix * vec4(position, 1.0);float xFactor = clamp((modelPosition.x + 1.25) / 2.0, 0.0, 2.0); float vWave = sin(modelPosition.x * uFrequency.x - uTime ) * xFactor * uStrength ;vWave += sin(modelPosition.y * uFrequency.y - uTime) * xFactor * uStrength * 0.5;modelPosition.y += sin(modelPosition.x * 2.0 + uTime * 0.5) * 0.05 * xFactor;modelPosition.z += vWave;vec4 viewPosition = viewMatrix * modelPosition;vec4 projectedPosition = projectionMatrix * viewPosition;gl_Position = projectedPosition;vUv = uv;vDark = vWave;}`,fragmentShader: `precision mediump float;varying float vDark;uniform sampler2D uTexture;varying vec2 vUv;void main(){vec4 textColor = texture2D(uTexture, vUv);textColor.rgb *= vDark + 0.85;gl_FragColor = textColor;}`,side: THREE.DoubleSide,uniforms: {uFrequency: { value: new THREE.Vector2(3, 3) },uTime: { value: 0 },uTexture: { value: flagTexture },uStrength: { value: 0.2 }}})const flagGeometry = new THREE.BoxGeometry(3, 2, 0.025, 64, 64)const flagMesh = new THREE.Mesh(flagGeometry, flagMaterial)scene.add(flagMesh)animate()function animate() {flagMaterial.uniforms.uTime.value += 0.06renderer.render(scene, camera)requestAnimationFrame(animate)}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com