Incorporating an HTML webpage into a WebView for rendering a 3D model using three.js has been successful so far. The next step involves adding touch/drag controls to manipulate the camera's view. Following example code, I have implemented the relevant sections as shown below:
import * as tc from './TrackballControls.js';
//....
controls = new tc.TrackballControls( camera , renderer.domElement);
controls.rotateSpeed = 1.0;
controls.zoomSpeed = 1.2;
controls.panSpeed = 0.8;
The functionality works perfectly on Chrome and Firefox browsers on PC, and also when loaded on Android devices through the local network. However, encountering an error in the WebView within my application:
Uncaught TypeError: tc.TrackballControls is not a constructor
The issue seems to arise specifically within the WebView environment, while functioning seamlessly on the same phone's Chrome browser. Looking at the directory structure of my HTML app:
https://i.sstatic.net/Dr04X.png
I have made adjustments in the code to account for the location of 'three.module.js' and 'TrackballControls.js', detailed below:
// in index.html js:
import * as THREE from './three.module.js';
import * as tc from './TrackballControls.js';
// in TrackballControls.js:
import {
EventDispatcher,
MOUSE,
Quaternion,
Vector2,
Vector3
} from "./three.module.js";
Is there any modification that can be made in the HTML/JavaScript or Android Java code to rectify this issue within the WebView? The userAgent strings for two different devices where I tested are:
"Mozilla/5.0 (Linux; Android 6.0.1; S60 Build/MMB29M; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/79.0.3945.93 Mobile Safari/537.36", source: https://appassets.androidplatform.net/assets/www/cpb_3d_model_wgt/index.html (27)
and
"Mozilla/5.0 (Linux; Android 8.1.0; LM-V405 Build/OPM1.171019.026; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/79.0.3945.93 Mobile Safari/537.36", source: https://appassets.androidplatform.net/assets/www/cpb_3d_model_wgt/index.html (27)
EDIT Further details: Initially serving the page from the Assets directory using the provided method led to the 'not a constructor' error. Using Chrome remote device inspector, I found discrepancies in the network request headers between requests made via PC and Android devices. This observation prompted me to serve the page using NanoHTTPD which resolved the issue.
Another EDIT: Upon revisiting the Asset Loader/intercept request code, it became apparent that I mistakenly hardcoded the wrong JavaScript file to be returned for all '.js' requests, hence triggering the 'not a constructor error'. Apologies for overlooking this detail earlier.