I am in the process of developing a vscode
extension that aims to visualize 3D meshes. Here is the function responsible for retrieving the HTML content:
function getWebviewContent(context: vscode.ExtensionContext, panel: vscode.WebviewPanel) {
const threejs = vscode.Uri.file(context.extensionPath+"/js/three.js");
const threejsUri = panel.webview.asWebviewUri(threejs);
const geometryjs = vscode.Uri.file(context.extensionPath+"/js/geometry.js");
const geometryjsUri = panel.webview.asWebviewUri(geometryjs);
return `
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>My first three.js app</title>
<style>
body { margin: 0; }
</style>
</head>
<body>
<script src=${threejsUri}></script>
<script src=${geometryjsUri}></script>
</body>
</html>
`
}
When using my geometry.js
, everything displays correctly:
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera( 75, window.innerWidth / window.innerHeight, 0.1, 1000 );
const renderer = new THREE.WebGLRenderer();
renderer.setSize( window.innerWidth, window.innerHeight );
document.body.appendChild( renderer.domElement );
const geometry = new THREE.BoxGeometry();
const material = new THREE.MeshBasicMaterial( { color: 0x00ff00 } );
const cube = new THREE.Mesh( geometry, material );
scene.add( cube );
camera.position.z = 5;
function animate() {
requestAnimationFrame( animate );
cube.rotation.x += 0.01;
cube.rotation.y += 0.01;
renderer.render( scene, camera );
};
animate();
However, when I encapsulate the code into a class like so:
import { Scene, PerspectiveCamera, WebGLRenderer, BoxGeometry, MeshBasicMaterial, Mesh, requestAnimationFrame } from 'three';
class Viewer {
constructor() {
// Code snippet here
}
animate() {
// Animation logic here
}
}
let viewer = new Viewer();
viewer.animate();
The rendered page remains empty. What might be causing this issue? Since I rely on an external rendering package which utilizes classes, I need to ensure correct implementation and usage of classes.
Note: I have attempted switching geometry.js
to typescript
, but encountered no success
// TypeScript code block here
Additional note: Curiously, the following approach seems to work:
// A different code structure that functions properly
This raises the question as to why the animate()
function needs to be nested inside init()
. In a class setup, shouldn't member components use this
instead?
Further Edit:
// Latest modifications to the class structure