【发布时间】:2020-09-30 05:07:19
【问题描述】:
我在 iPhone 上运行 iOS 13.7,我尝试过的所有 three.js examples(包括粗线和轨道控制上的那些)在我的移动设备上都能完美运行。
但是,我似乎无法让自己的代码在移动设备上运行,请参阅 source code 和 uploaded website 以供参考。它适用于我的 Windows 计算机上的 Edge 和 Chrome。
answer 建议添加“use strict”;到所有 .js 文件,我什至对导入的模块都这样做了,但无济于事。
关于什么可能会破坏我的移动 JavaScript 代码的任何想法或提示?
这些模块是如何包含在 hopf.js 中的:
import * as THREE from './three.module.js';
import { OrbitControls } from '/OrbitControls.js';
import { LineMaterial } from './LineMaterial.js';
import { LineGeometry } from './LineGeometry.js';
import { Line2 } from './Line2.js';
import { GUI } from './dat.gui.module.js';
import Stats from './stats.module.js'
index.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
<style>
body { margin: 0; }
canvas { display: block; }
</style>
<title>Document</title>
</head>
<body>
<script type = "module" src = "hopf.js"></script>
</body>
初始化函数:
function init() {
// Camera
camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.001, 1000);
camera.position.x = -3;
camera.position.y = 0;
camera.position.z = 0;
camera.lookAt(0,0,0);
// Orthographic camera, to focus on the 2-sphere base space
var aspect = window.innerWidth / window.innerHeight;
var scale = 3;
//orthCamera = new THREE.OrthographicCamera( window.innerWidth / - 2, window.innerWidth / 2, window.innerHeight / 2, window.innerHeight / - 2, 0.001, 1000);
orthCamera = new THREE.OrthographicCamera(-aspect*scale, aspect*scale, 1*scale, -1*scale, 0.001, 1000);
orthCamera.position.set(5,5,10);
orthCamera.lookAt(0,0,0);
// Scene for fiber projections
scene = new THREE.Scene();
// Scene for the 2-sphere base space
orthScene = new THREE.Scene();
// Objects
baseSpace_geometry = new THREE.SphereGeometry(1,30,30);
baseSpace_material = new THREE.MeshBasicMaterial({color: 0xfafafa});
baseSpace_material.transparent = true;
baseSpace_material.opacity = 0.5;
baseSpace = new THREE.Mesh(baseSpace_geometry,baseSpace_material);
baseSpace.position.x = 4.5;
baseSpace.position.y = -1;
orthScene.add(baseSpace);
// Renderer
renderer = new THREE.WebGLRenderer({antialias: true});
renderer.setPixelRatio( window.devicePixelRatio );
renderer.setSize( window.innerWidth , window.innerHeight );
renderer.autoClear = false;
// Controls
controls = new OrbitControls(camera, renderer.domElement);
window.addEventListener('resize', onWindowResize, false);
document.body.appendChild(renderer.domElement);
stats = new Stats();
document.body.appendChild(stats.dom);
initGui();
baseSpaceCircles.push(new baseSpaceCircle(0, 2*Math.PI, 10, defaultRotation, new THREE.Vector3(0,0,0), 0.0));
onWindowResize();
render();
}
【问题讨论】:
-
可能值得添加一些代码来展示您如何包含
three.js并使用它。 -
感谢您的建议-在 hopf.js 中添加了 index.html、包含块和初始化函数。
标签: javascript mobile three.js