threejs实现液体管道流动的动态效果


实现点:添加纹理、设置纹理的偏移、

<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <title>eg18-2: Three.js 旋转的盒子(重构)--加入阴影</title>
    <script src="../js/three.js"></script>
    <script src="../js/WebGL.js"></script>
    <script src="../js/OrbitControls.js"></script>
</head>
<script>
    var renderer = null;
    //初始化渲染器
    function initThree() {
        renderer = new THREE.WebGLRenderer();
        renderer.setSize(window.innerWidth, window.innerHeight);
        document.body.appendChild(renderer.domElement);
    }
    var camera = null;
    // 照像机设置
    function initCamera() {
        camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
        camera.position.set(10, 10, 10);
        camera.lookAt(new THREE.Vector3(0, 2, 0))
    }
    var scene = null;
    // 场景设置
    function initScene() {
        scene = new THREE.Scene();
        var axes = new THREE.AxisHelper(1000)
        scene.add(axes)
    }
    // 灯光设置
    function initLight() {
        //add
        //环境光
        var ambientLight = new THREE.AmbientLight(0xffffff, 0.5);
        scene.add(ambientLight);
        //点光
        var pointLight = new THREE.PointLight(0xffffff, 0.8, 18);
        pointLight.position.set(0, 2, 0);
        // scene.add(pointLight);
    }
    var mesh = null;
    // 几何体(物体)设置
    function initObject() {
        // var geometry = new THREE.CubeGeometry(1,1,1);
        // var material = new THREE.MeshBasicMaterial({color: 0x00ff00});
        // mesh = new THREE.Mesh(geometry, material);
        // mesh.position.y+=1;
        // scene.add(mesh);

        // meshfloor=new THREE.Mesh(
        //         new THREE.PlaneGeometry(10,10,10,10),
        //         new THREE.MeshPhongMaterial({color:0xffffff,wireframe:false,side: THREE.DoubleSide})
        // )
        // meshfloor.rotation.x -=Math.PI/2;


        // scene.add(meshfloor);
        //Create a closed wavey loop

        // 创建曲线路径
        // var curve = new THREE.CatmullRomCurve3([
        //     new THREE.Vector3(-10, 0, 10),
        //     new THREE.Vector3(-5, 5, 5),
        //     new THREE.Vector3(0, 0, 0),
        //     new THREE.Vector3(5, -5, 5),
        //     new THREE.Vector3(10, 0, 10),
        //     new THREE.Vector3(10, 0, 10)
        // ]);

        // var points = curve.getPoints(50);
        // var geometry = new THREE.BufferGeometry().setFromPoints(points);

        // var material = new THREE.LineBasicMaterial({ color: 0xff0000 });

        // // Create the final object to add to the scene
        // var curveObject = new THREE.Line(geometry, material);
        // scene.add(curveObject)
    }
    // 开始渲染
    function render() {
        requestAnimationFrame(render);
        // mesh.rotation.x += 0.1;
        // mesh.rotation.y += 0.1;
        renderer.render(scene, camera);
    }

    function threeStart() {

        initThree();     // 初始化渲染器
        initCamera(); // 照像机设置
        initScene();    // 场景设置
        initLight();      // 灯光设置
        initObject();    // 几何体(物体)设置
        render();        // 开始渲染
        createTube();
        var controls = new THREE.OrbitControls(camera);
    }

    function start() {
        if (WEBGL.isWebGLAvailable()) {
            // Initiate function or other initializations here
            threeStart();
        } else {
            var warning = WEBGL.getWebGLErrorMessage();
            document.getElementById('container').appendChild(warning);
        }

    }


    // 此部分为了展示为hardcode
    const pathArr = [
        // 4624.99, 2329.38, -5843.11,
        // 4624.99, 4643.14, -5843.11,
        // 1437.47, 4643.14, -5819.36,
        // 1413.69, 4643.14, -1854.40,
        // -6983.28, 4643.14, -1854.04,
        // -7078.43, 4643.14, 2149.46
        -10, 0, 10,
        -5, 5, 5,
        0, 0, 0,
        5, -5, 5,
        10, 0, 10,
        10, 10, 0
    ]
    const radius = 0.5;
    // 动态创建一个管道
    function createTube() {
      let curveArr = []
      // 三个一组取出curve数据
      for(let i=0; i < pathArr.length; i+=3) {
        curveArr.push(new THREE.Vector3(pathArr[i], pathArr[i+1], pathArr[i+2]))
      }
      var curve = new THREE.CatmullRomCurve3(curveArr);
      /**
        * TubeGeometry(path : Curve, tubularSegments : Integer, radius : Float, radialSegments : Integer, closed : Boolean)
      */
      var tubeGeometry = new THREE.TubeGeometry(curve, 100, radius, 50, false);
      var textureLoader = new THREE.TextureLoader();
      var texture = textureLoader.load('data:application/octet-stream;base64,UklGRsoKAABXRUJQVlA4IL4KAACwcQC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rbKftNP67S3eal/kFeSaBMQPzxDNRWTps3bdTE5Zq76ge7WLGug4ZO5hP7vx0x0Kh9o2gdGaj40pxqk648da9/SUUp29lJsoE6AoeDrdLgXEEnRkUrkwI5dyLM06SXY8Nx6+MkNeEfrFTCQs//8kUTSAM9fxTa/OOFxOPEA1sks/K5nu0R+Sp/d/cDvsR4G8mtVyJZ/+aAXh+ueHDNIEejMS1H3TunqyMDlWgQ2TeBFCUpaFTDGDiQZkC5jXHfJIoZTckNPrT3s6jZh6z93fsiGeiGxNz7lBFWwzgJPF8D3gL0SkZdQBb9aweLdtyoqt8rsORaNJibdxcU7owJp7Eh74i7DChGVV5lhR+osUBQrGwKB4aXO0roJ11iOMzw4UXVFD2Pf9xtwpHC7VFSLUuDcd3qVqRcBSXLWXU7xvn+iOl331q9HCvHgwEY/QjjqwAAAAAAAA==');
 
      // 设置阵列模式 RepeatWrapping
      texture.wrapS = THREE.RepeatWrapping
      texture.wrapT = THREE.RepeatWrapping
      // 设置x方向的重复数(沿着管道路径方向)
      // 设置y方向的重复数(环绕管道方向)
      texture.repeat.x = 10;
      texture.repeat.y = 4;
      // 设置管道纹理偏移数,便于对中
      texture.offset.y = 0.9;
      var tubeMaterial = new THREE.MeshPhongMaterial({
        map: texture,
        transparent: true,
      });
      var tube = new THREE.Mesh(tubeGeometry, tubeMaterial);
      // 使用加减法可以设置不同的运动方向
      setInterval(() => {
        texture.offset.x -= 0.003
      })
    //   return tube
    scene.add(tube)
    }

</script>

<body onload="start();">
    <div id="container"></div>
</body>

</html>

你可能感兴趣的:(前端)