cesium与three.js 结合的栗子,结合了一下网友们的栗子,解决了three.js 高版本模型出不来的问题

2023-10-29

废话不多说先上图

 

下面是源代码

<!DOCTYPE html>

<html lang="en">

<head>

  <!-- Use correct character set. -->

  <meta charset="utf-8">

  <meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1">

  <meta name="viewport"

    content="width=device-width, initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=no">

  <title>Tools-12CesiumThreejs</title>

  <link href="Tools-12CesiumThreejs.css" rel="stylesheet" type="text/css" media="all">

  <script src="Scripts/Cesium/Cesium.js"></script>

  <script src="Scripts/threev111.js"></script>

</head>

<body οnlοad="pageload()">

  <div id="cesiumContainer"></div>

  <div id="ThreeContainer"></div>

  <div id="cover">

    <div id="loadingIndicator" class="loadingIndicator"></div>

  </div>

  <script>

    var three = {

      renderer: null,

      camera: null,

      scene: null

    };

    function pageload() {

      var loadingIndicator = document.getElementById('loadingIndicator');

      loadingIndicator.style.display = 'none';

      // boundaries in WGS84 around the object

      var minWGS84 = [115.23, 39.55];

      var maxWGS84 = [116.23, 41.55];

      var cesiumContainer = document.getElementById("cesiumContainer");

      var ThreeContainer = document.getElementById("ThreeContainer");

      var _3Dobjects = []; //Could be any Three.js object mesh

      var cesium = {

        viewer: null

      };

      function _3DObject() {

        //THREEJS 3DObject.mesh

        this.threeMesh = null;

        //location bounding box

        this.minWGS84 = null;

        this.maxWGS84 = null;

      }

      function initCesium() {

        cesium.viewer = new Cesium.Viewer(cesiumContainer, {

          useDefaultRenderLoop: false,

          selectionIndicator: false,

          homeButton: false,

          sceneModePicker: false,

          navigationHelpButton: false,

          infoBox: false,

          navigationHelpButton: false,

          navigationInstructionsInitiallyVisible: false,

          animation: false,

          timeline: false,

          fullscreenButton: false,

          allowTextureFilterAnisotropic: false,

          contextOptions: {

            webgl: {

              alpha: false,

              antialias: true,

              preserveDrawingBuffer: true,

              failIfMajorPerformanceCaveat: false,

              depth: true,

              stencil: false,

              anialias: false

            },

          },

          targetFrameRate: 60,

          resolutionScale: 0.1,

          orderIndependentTranslucency: true,

          //imageryProvider: undefined,

          baseLayerPicker: true,

          geocoder: false,

          automaticallyTrackDataSourceClocks: false,

          dataSources: null,

          clock: null,

          terrainShadows: Cesium.ShadowMode.DISABLED

        });

        var center = Cesium.Cartesian3.fromDegrees(

          (minWGS84[0] + maxWGS84[0]) / 2,

          ((minWGS84[1] + maxWGS84[1]) / 2) - 1,

          200000

        );

        cesium.viewer.camera.flyTo({

          destination: center,

          orientation: {

            heading: Cesium.Math.toRadians(0),

            pitch: Cesium.Math.toRadians(-60),

            roll: Cesium.Math.toRadians(0)

          },

          duration: 3

        });

      }

      function initThree() {

        var fov = 45;

        var width = window.innerWidth;

        var height = window.innerHeight;

        var aspect = width / height;

        var near = 1;

        var far = 10 * 1000 * 1000;

        three.scene = new THREE.Scene();

        three.camera = new THREE.PerspectiveCamera(fov, aspect, near, far);

        three.renderer = new THREE.WebGLRenderer({ alpha: true });

        var Amlight = new THREE.AmbientLight(0xffffff, 2);

         three.scene.add(Amlight);

        ThreeContainer.appendChild(three.renderer.domElement);

      }

      function init3DObject() {

        //Cesium entity

        var entity = {

          name: 'Polygon',

          polygon: {

            hierarchy: Cesium.Cartesian3.fromDegreesArray([

              minWGS84[0], minWGS84[1],

              maxWGS84[0], minWGS84[1],

              maxWGS84[0], maxWGS84[1],

              minWGS84[0], maxWGS84[1],

            ]),

            material: Cesium.Color.RED.withAlpha(0.2)

          }

        };

        var Polygon = cesium.viewer.entities.add(entity);

        //Three.js Objects

        // Lathe geometry

        var doubleSideMaterial = new THREE.MeshNormalMaterial({

          side: THREE.DoubleSide

        });

        var segments = 10;

        var points = [];

        for (var i = 0; i < segments; i++) {

          points.push(new THREE.Vector2(Math.sin(i * 0.2) * segments + 5, (i - 5) * 2));

        }

        var geometry = new THREE.LatheGeometry(points);

        var latheMesh = new THREE.Mesh(geometry, doubleSideMaterial);

        latheMesh.scale.set(1500, 1500, 1500); //scale object to be visible at planet scale

        latheMesh.position.z += 15000.0; // translate "up" in Three.js space so the "bottom" of the mesh is the handle

        latheMesh.rotation.x = Math.PI / 2; // rotate mesh for Cesium's Y-up system

        var latheMeshYup = new THREE.Group();

        latheMeshYup.add(latheMesh)

        three.scene.add(latheMeshYup); // don’t forget to add it to the Three.js scene manually

        //Assign Three.js object mesh to our object array

        var _3DOB = new _3DObject();

        _3DOB.threeMesh = latheMeshYup;

        _3DOB.minWGS84 = minWGS84;

        _3DOB.maxWGS84 = maxWGS84;

        _3Dobjects.push(_3DOB);

        // dodecahedron

        geometry = new THREE.DodecahedronGeometry();

        var dodecahedronMesh = new THREE.Mesh(geometry, new THREE.MeshNormalMaterial());

        dodecahedronMesh.scale.set(5000, 5000, 5000); //scale object to be visible at planet scale

        dodecahedronMesh.position.z += 15000.0; // translate "up" in Three.js space so the "bottom" of the mesh is the handle

        dodecahedronMesh.rotation.x = Math.PI / 2; // rotate mesh for Cesium's Y-up system

        var dodecahedronMeshYup = new THREE.Group();

        dodecahedronMeshYup.add(dodecahedronMesh)

        three.scene.add(dodecahedronMeshYup); // don’t forget to add it to the Three.js scene manually

        //Assign Three.js object mesh to our object array

        _3DOB = new _3DObject();

        _3DOB.threeMesh = dodecahedronMeshYup;

        _3DOB.minWGS84 = minWGS84;

        _3DOB.maxWGS84 = maxWGS84;

        _3Dobjects.push(_3DOB);

      }

      // Looping Renderer

      function renderCesium() {

        cesium.viewer.render();

      }

      function renderThreeObj() {

        // register Three.js scene with Cesium

        three.camera.fov = Cesium.Math.toDegrees(cesium.viewer.camera.frustum.fovy) // ThreeJS FOV is vertical

        //three.camera.updateProjectionMatrix();

        var cartToVec = function (cart) {

          return new THREE.Vector3(cart.x, cart.y, cart.z);

        };

        // Configure Three.js meshes to stand against globe center position up direction

        for (var id in _3Dobjects) {

          minWGS84 = _3Dobjects[id].minWGS84;

          maxWGS84 = _3Dobjects[id].maxWGS84;

          // convert lat/long center position to Cartesian3

          var center = Cesium.Cartesian3.fromDegrees((minWGS84[0] + maxWGS84[0]) / 2, (minWGS84[1] + maxWGS84[1]) / 2);

          // get forward direction for orienting model

          var centerHigh = Cesium.Cartesian3.fromDegrees((minWGS84[0] + maxWGS84[0]) / 2, (minWGS84[1] + maxWGS84[1]) / 2, 1);

          // use direction from bottom left to top left as up-vector

          var bottomLeft = cartToVec(Cesium.Cartesian3.fromDegrees(minWGS84[0], minWGS84[1]));

          var topLeft = cartToVec(Cesium.Cartesian3.fromDegrees(minWGS84[0], maxWGS84[1]));

          var latDir = new THREE.Vector3().subVectors(bottomLeft, topLeft).normalize();

          // configure entity position and orientation

          //debugger

          _3Dobjects[id].threeMesh.position.copy(center);

          //debugger;

          _3Dobjects[id].threeMesh.lookAt(centerHigh.x,centerHigh.y,centerHigh.z);

          _3Dobjects[id].threeMesh.up.copy(latDir);

        }

        // Clone Cesium Camera projection position so the

        // Three.js Object will appear to be at the same place as above the Cesium Globe

        three.camera.matrixAutoUpdate = false;

        var cvm = cesium.viewer.camera.viewMatrix;

        var civm = cesium.viewer.camera.inverseViewMatrix;

        three.camera.lookAt(0,0,0);

        three.camera.matrixWorld.set(

          civm[0], civm[4], civm[8], civm[12],

          civm[1], civm[5], civm[9], civm[13],

          civm[2], civm[6], civm[10], civm[14],

          civm[3], civm[7], civm[11], civm[15]

        );

        three.camera.matrixWorldInverse.set(

          cvm[0], cvm[4], cvm[8], cvm[12],

          cvm[1], cvm[5], cvm[9], cvm[13],

          cvm[2], cvm[6], cvm[10], cvm[14],

          cvm[3], cvm[7], cvm[11], cvm[15]

        );

    

        var width = ThreeContainer.clientWidth;

        var height = ThreeContainer.clientHeight;

        var aspect = width / height;

        three.camera.aspect = aspect;

        three.camera.updateProjectionMatrix();

        three.renderer.setSize(width, height);

        three.renderer.clear();

        three.renderer.render(three.scene, three.camera);

      }

      function loop() {

        requestAnimationFrame(loop);

        renderCesium();

        renderThreeObj();

      }

      initCesium(); // Initialize Cesium renderer

      initThree(); // Initialize Three.js renderer

      init3DObject(); // Initialize Three.js object mesh with Cesium Cartesian coordinate system

      loop(); // Looping renderer

    }

  </script>

</body>

</html>

r95后续版本 相机lookat重新调整了矩阵,固官方栗子需要将lookat上移如图。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

cesium与three.js 结合的栗子,结合了一下网友们的栗子,解决了three.js 高版本模型出不来的问题 的相关文章

  • ThreeJS 中阴影的奇怪行为

    所以我有一个 ThreeJS 场景 并且添加了一些球体 多材质 我还添加了定向光 this light new THREE DirectionalLight 0xFFFFFF 1 this light position set 2 10 2
  • 如何仅剪切剪切平面的交集(而不是并集)?

    在 OpenGL JOGL 中 当使用多个剪切平面时 似乎会应用所有剪切平面的并集 我想要的是路口要应用的所有剪裁平面 这可能吗 请参阅下面的简化二维示例 Edit An example of clipping by vertex shad
  • 如何在 React Native 中渲染自定义 3D 对象

    我已经成功使用 Three js expo Three 和 expo gl 在 React Native 中配置了红色立方体的 3D 渲染 但我想让用户渲染他们自己可能拥有的自定义 3D 对象 obj 或 mtl 扩展名 但我不确定如何让他
  • 如何将平面上的 3D 点转换为 UV 坐标?

    我有一个 3d 点 定义为 x0 y0 z0 该点属于一个平面 定义为 a b c d normal a b c and ax by cz d 0 如何将 3d 点转换或映射为一对 u v 坐标 这一定是非常简单的事情 但我无法弄清楚 首先
  • 简单模式7公式/例子?

    我最近发现了利用 SNES 模式 7 的伪 3D 效果 并想尝试在 Godot 引擎中复制它 我尝试在网上查找 但所有内容要么以我无法理解的方式解释 要么以我不知道的编程语言解释 我还需要学习如何旋转该区域 并将精灵作为角色或敌人放入 但我
  • 如何使用三个JS导出然后导入场景?

    我有一个用三个 JS 和大量 Javascript 代码构建的复杂 3D 场景 我需要将此场景导出为一个文件 然后通过简单的 ThreeJS 场景播放器在我的网站上使用它 我尝试过 ObjectExporter 和 SceneExporte
  • 从matrix4()获取翻译

    对于大多数 Three js 开发人员来说 这可能看起来像是一个虚拟问题 但是如何从转换矩阵中提取翻译呢 实际上 我手动提取它指向 矩阵数组位置 12 13 14 提前致谢 如果要从矩阵中提取平移分量 请使用以下模式 var vec new
  • Three.js获取立方体的4个角坐标?

    如何获得立方体四个角的坐标 如果您使用 CubeGeometry 宽度 高度 深度 并将立方体放置在某处 那么您的八个角位于 position x width 2 position y height 2 position z depth 2
  • 在 iOS 上的 SceneKit 中导入 3d 模型

    从 URL 导入 obj 文件并将其转换为 SCNNode 时遇到问题 这是代码 swift3 let url URL init string https cloud box com shared static ock9d81kakj91d
  • 如何计算正切和副法线?

    谈谈OpenGL着色语言 GLSL 中的凹凸贴图 镜面高光之类的东西 I have 顶点数组 例如 0 2 0 5 0 1 0 2 0 4 0 5 法线数组 例如 0 0 0 0 1 0 0 0 1 0 0 0 世界空间中点光源的位置 例如
  • 使用开源 3D 引擎从 Openstreetmap 数据渲染地图? [关闭]

    Closed 这个问题正在寻求书籍 工具 软件库等的推荐 不满足堆栈溢出指南 help closed questions 目前不接受答案 从 Openstreetmap 数据渲染 3D 地图可能会很漂亮麻烦的 https gis stack
  • OrbitControls 和 dat.gui 文本不起作用

    我正在使用 Three js 和 dat gui 以及text财产 另外 我的场景中有 OrbitControls cameraControl new THREE OrbitControls camera cameraControl upd
  • 使用 Three.js 中的设备方向控件进行对象旋转

    我正在迈出使用 JavaScript 进行编码并使用 Three js 的第一步 我正在尝试 Threejs org 的这个例子 http trijs org examples misc controls deviceorientation
  • 计算顶点法线

    我正在使用 3d 修改器https github com foo123 MOD3 https github com foo123 MOD3弯曲一个立方体 几何体更新 顶点位置更改 后 灯光不会更新 立方体仍然着色 就好像没有任何变化一样 所
  • 在 libgdx 中批处理多维数据集时出现问题

    我正在尝试开发一款游戏 在屏幕上渲染多达 300 个立方体 为每个多维数据集创建新的 modelInstance 时 modelBatch 的性能非常糟糕 据我所知 没有 3d 批处理可以将所有立方体批处理到一次绘制调用 所以我拼命地尝试以
  • Godot 3d 得到向前矢量

    我想知道是否有办法获取 godot 3d 中空间节点的前向向量 统一起来 这就是transform forward Godot 给了我一个旋转向量 但我不知道如何将其转换为方向向量 戈多版本的transform forward是什么 前进是
  • Three.js - 重叠层闪烁

    当多个物体重叠在同一平面上时 它们开始闪烁 如何告诉渲染器将其中一个对象放在前面 我尝试使用 renderDepth 但它只能部分起作用 请参阅此处的示例 http liveweave com ahTdFQ http liveweave c
  • 现代 GPU 上的纹理更改(和其他状态更改)成本

    我正在编写一个基于场景图的图形引擎用于建模目的 我正在使用 XNA 4 在我读过的许多地方 渲染期间应该最小化纹理变化 和其他状态变化 因此我必须按材质等对图元进行排序 我在 XNA 4 中创建了一个小型测试应用程序 它使用单个纹理渲染数百
  • Three.js - 如何使用姿势估计数据为 3D 模型制作动画

    我正在尝试使用姿势估计坐标来对 Three js 中的装配模型进行动画处理 我正在使用的姿势估计技术提供了视频源中人物的实时 x y z 坐标 我正在尝试使用这些坐标相应地移动 3D 模型 我使用下面的代码 其中一些代码是我在相关问题的答案
  • 如何在 R 中导入并绘制三角形网格?

    我想在 R 中绘制我的模型输出 它是格式为的三角形网格 x1 y1 z1 x2 y2 z2 x3 y3 z3 value 每行代表一个三角形 我想用以下方法绘制这些三角形value作为规模 mymesh lt structure c 0 9

随机推荐