当前位置: 首页 > article >正文

记录下在html文件中如何直接使用npm依赖,以threejs为例

参考:
https://www.cnblogs.com/shayloyuki/p/17191489.html

共三种方式
我的代码截图
在这里插入图片描述

方式一:
threejsDemo_script.html

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<!-- Copyright 2015 Patricio Gonzalez Vivo (http://patriciogonzalezvivo.com) -->
<body>
  <div id="container"></div>
  <script src="./three.min.js"></script>

  <script id="vertexShader" type="x-shader/x-vertex">
      void main() {
          gl_Position = vec4( position, 1.0 );
      }
  </script>

  <script id="fragmentShader" type="x-shader/x-fragment">
      uniform vec2 u_resolution;
      uniform vec2 u_mouse;
      uniform float u_time;

      void main() {
          vec2 st = gl_FragCoord.xy/u_resolution.xy;
          gl_FragColor=vec4(st.x,st.y,0.0,1.0);
      }
  </script>

  <script >
    // import * as THREE from './three';
      var container;
      var camera, scene, renderer, clock;
      var uniforms;
      var mouse = {x:0, y:0};

      document.onmousemove = getMouseXY;

      init();
      animate();

      function getMouseXY(e) {
          mouse.x = e.pageX;
          mouse.y = e.pageY;
      }

      function init() {
          container = document.getElementById( 'container' );

          camera = new THREE.Camera();
          camera.position.z = 1;

          scene = new THREE.Scene();
          clock = new THREE.Clock();

          var geometry = new THREE.PlaneBufferGeometry( 2, 2 );

          uniforms = {
              u_time: { type: "f", value: 1.0 },
              u_mouse: { type: "v2", value: new THREE.Vector2() },
              u_resolution: { type: "v2", value: new THREE.Vector2() }
          };

          var material = new THREE.ShaderMaterial( {
              uniforms: uniforms,
              vertexShader: document.getElementById( 'vertexShader' ).textContent,
              fragmentShader: document.getElementById( 'fragmentShader' ).textContent
          } );

          var mesh = new THREE.Mesh( geometry, material );
          scene.add( mesh );

          renderer = new THREE.WebGLRenderer();
          renderer.setPixelRatio( window.devicePixelRatio );

          container.appendChild( renderer.domElement );

          onWindowResize();
          window.addEventListener( 'resize', onWindowResize, false );
      }

      function onWindowResize( event ) {
          renderer.setSize( window.innerWidth, window.innerHeight );
          uniforms.u_resolution.value.x = renderer.domElement.width;
          uniforms.u_resolution.value.y = renderer.domElement.height;
          uniforms.u_mouse.value.x = mouse.x;
          uniforms.u_mouse.value.y = mouse.y;
      }

      function animate() {
          requestAnimationFrame( animate );
          render();
      }

      function render() {
          uniforms.u_time.value += clock.getDelta();
          renderer.render( scene, camera );
      }
  </script>
</body>
</html>

方式二:
threejsDemo_module_ ES6 import方式引入.html

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<!-- Copyright 2015 Patricio Gonzalez Vivo (http://patriciogonzalezvivo.com) -->
<body>
  <div id="container"></div>
  <!-- <script src="./three.min.js"></script> -->


  <!-- <script type="importmap">
	{
		"imports": {
			"three": "./three/build/three.module.js",
        		"three/addons/": "./three/examples/jsm/"
		}
	}
</script> -->


  <script id="vertexShader" type="x-shader/x-vertex">
      void main() {
          gl_Position = vec4( position, 1.0 );
      }
  </script>

  <script id="fragmentShader" type="x-shader/x-fragment">
      uniform vec2 u_resolution;
      uniform vec2 u_mouse;
      uniform float u_time;

      void main() {
          vec2 st = gl_FragCoord.xy/u_resolution.xy;
          gl_FragColor=vec4(st.x,st.y,0.0,1.0);
      }
  </script>

  <script type="module">
   	import * as THREE from './three/build/three.module.js';
      var container;
      var camera, scene, renderer, clock;
      var uniforms;
      var mouse = {x:0, y:0};

      document.onmousemove = getMouseXY;

      init();
      animate();

      function getMouseXY(e) {
          mouse.x = e.pageX;
          mouse.y = e.pageY;
      }

      function init() {
          container = document.getElementById( 'container' );

          camera = new THREE.Camera();
          camera.position.z = 1;

          scene = new THREE.Scene();
          clock = new THREE.Clock();

        //   var geometry = new THREE.PlaneBufferGeometry( 2, 2 ); 新版本叫 THREE.PlaneGeometry
        var geometry = new THREE.PlaneGeometry( 2, 2 );


          uniforms = {
              u_time: { type: "f", value: 1.0 },
              u_mouse: { type: "v2", value: new THREE.Vector2() },
              u_resolution: { type: "v2", value: new THREE.Vector2() }
          };

          var material = new THREE.ShaderMaterial( {
              uniforms: uniforms,
              vertexShader: document.getElementById( 'vertexShader' ).textContent,
              fragmentShader: document.getElementById( 'fragmentShader' ).textContent
          } );

          var mesh = new THREE.Mesh( geometry, material );
          scene.add( mesh );

          renderer = new THREE.WebGLRenderer();
          renderer.setPixelRatio( window.devicePixelRatio );

          container.appendChild( renderer.domElement );

          onWindowResize();
          window.addEventListener( 'resize', onWindowResize, false );
      }

      function onWindowResize( event ) {
          renderer.setSize( window.innerWidth, window.innerHeight );
          uniforms.u_resolution.value.x = renderer.domElement.width;
          uniforms.u_resolution.value.y = renderer.domElement.height;
          uniforms.u_mouse.value.x = mouse.x;
          uniforms.u_mouse.value.y = mouse.y;
      }

      function animate() {
          requestAnimationFrame( animate );
          render();
      }

      function render() {
          uniforms.u_time.value += clock.getDelta();
          renderer.render( scene, camera );
      }
  </script>
</body>
</html>

方式三:
threejsDemo_module_type="importmap"方式.html


<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<!-- Copyright 2015 Patricio Gonzalez Vivo (http://patriciogonzalezvivo.com) -->
<body>
  <div id="container"></div>
  <!-- <script src="./three.min.js"></script> -->


  <script type="importmap">
	{
		"imports": {
			"three": "./three/build/three.module.js",
        		"three/addons/": "./three/examples/jsm/"
		}
	}
</script>


  <script id="vertexShader" type="x-shader/x-vertex">
      void main() {
          gl_Position = vec4( position, 1.0 );
      }
  </script>

  <script id="fragmentShader" type="x-shader/x-fragment">
      uniform vec2 u_resolution;
      uniform vec2 u_mouse;
      uniform float u_time;

      void main() {
          vec2 st = gl_FragCoord.xy/u_resolution.xy;
          gl_FragColor=vec4(st.x,st.y,0.0,1.0);
      }
  </script>

  <script type="module">
   	import * as THREE from 'three';
      var container;
      var camera, scene, renderer, clock;
      var uniforms;
      var mouse = {x:0, y:0};

      document.onmousemove = getMouseXY;

      init();
      animate();

      function getMouseXY(e) {
          mouse.x = e.pageX;
          mouse.y = e.pageY;
      }

      function init() {
          container = document.getElementById( 'container' );

          camera = new THREE.Camera();
          camera.position.z = 1;

          scene = new THREE.Scene();
          clock = new THREE.Clock();

        //   var geometry = new THREE.PlaneBufferGeometry( 2, 2 ); 新版本叫 THREE.PlaneGeometry
        var geometry = new THREE.PlaneGeometry( 2, 2 );


          uniforms = {
              u_time: { type: "f", value: 1.0 },
              u_mouse: { type: "v2", value: new THREE.Vector2() },
              u_resolution: { type: "v2", value: new THREE.Vector2() }
          };

          var material = new THREE.ShaderMaterial( {
              uniforms: uniforms,
              vertexShader: document.getElementById( 'vertexShader' ).textContent,
              fragmentShader: document.getElementById( 'fragmentShader' ).textContent
          } );

          var mesh = new THREE.Mesh( geometry, material );
          scene.add( mesh );

          renderer = new THREE.WebGLRenderer();
          renderer.setPixelRatio( window.devicePixelRatio );

          container.appendChild( renderer.domElement );

          onWindowResize();
          window.addEventListener( 'resize', onWindowResize, false );
      }

      function onWindowResize( event ) {
          renderer.setSize( window.innerWidth, window.innerHeight );
          uniforms.u_resolution.value.x = renderer.domElement.width;
          uniforms.u_resolution.value.y = renderer.domElement.height;
          uniforms.u_mouse.value.x = mouse.x;
          uniforms.u_mouse.value.y = mouse.y;
      }

      function animate() {
          requestAnimationFrame( animate );
          render();
      }

      function render() {
          uniforms.u_time.value += clock.getDelta();
          renderer.render( scene, camera );
      }
  </script>
</body>
</html>

http://www.kler.cn/a/413056.html

相关文章:

  • 系统思考—共同看见
  • [护网杯 2018]easy_tornado
  • 操作系统的发展趋势是什么
  • JavaEE---计算机是如何工作的?
  • 【接口封装】——7、连接并使用 MySQL 数据库
  • 概率论中交并集的公式
  • sentry前端接入 报错403
  • 2022 年 3 月青少年软编等考 C 语言三级真题解析
  • YourPHPCMS Register_checkEmail存在sql注入漏洞
  • uniapp中的事件:v-on
  • Spring Boot 3 集成 Spring Security(3)数据管理
  • 同时多平台git配置:GitHub和Gitee生成不同的SSH Key
  • WPF——自定义ToolTip
  • Git远程仓库过大导致clone失败的解决方法
  • pytorch 和tensorflow loss.item()` 只能用于只有一个元素的张量. 防止显存爆炸
  • 什么是缓存击穿?如何避免之布隆过滤器
  • 07 初始 Oracle 优化器
  • Java设计模式笔记(一)
  • 14、保存与加载PyTorch训练的模型和超参数
  • PyTorch:神经网络的基本骨架 nn.Module的使用
  • HBase运维需要掌握的技能(1)
  • 关于在矩阵中枚举点的 dp
  • 前端开发设计模式——外观模式
  • 宠物电商对接美团闪购:实现快速配送与用户增值
  • Linux指标之平均负载(The Average load of Linux Metrics)
  • scala模式匹配习题