Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to import module #3

Open
yucel-aydin opened this issue Nov 12, 2021 · 1 comment
Open

How to import module #3

yucel-aydin opened this issue Nov 12, 2021 · 1 comment

Comments

@yucel-aydin
Copy link

Hi,
I am using threejs as module.

`
import * as THREE from '../lib/three.module.min.js';
import { DeviceOrientationControls } from '../lib/DeviceOrientationControls.min.js';
import { OrbitControls } from '../lib/OrbitControls.min.js';

`

How can I add js files as modules?
ex: import { Hologram} from '../lib/Hologram.js';

@stiegosaurus
Copy link

stiegosaurus commented Apr 6, 2023

Hey bud,

Was actually trying to work with this extension currently and I hit the same wall as you. The short answer is the existing script is legacy script and not a module - but you can create your own Class and import it that way.

  1. Create a file called ChromaKeyMaterial.js ( https://pastebin.com/1MDf14Ue )

// START OF CLASS
import * as THREE from 'three';

class ChromaKeyMaterial extends THREE.ShaderMaterial {

constructor(url, keyColor) {

const video = document.createElement('video');
video.src = url;
video.load();

const videoTexture = new THREE.VideoTexture(video);
videoTexture.minFilter = THREE.LinearFilter;
videoTexture.magFilter = THREE.LinearFilter;

super({
  uniforms: {
    videoTexture: { value: videoTexture },
    color: { value: new THREE.Color(keyColor) },
  },
  vertexShader: /* glsl */ `
    out vec2 vUv;
    void main() {
      vUv = uv;
      gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1);
    }
  `,
  fragmentShader: /* glsl */ `
    uniform sampler2D videoTexture;
    uniform vec3 color;
    in vec2 vUv;
    void main() {
      gl_FragColor.rgb = texture(videoTexture, vUv).rgb;
      gl_FragColor.a = (length(gl_FragColor.rgb - color) - 0.5) * 7.0;
    }
  `,
  transparent: true,	  

})


////////////////////////////////////////////////////////////////////////
// AFTER SUPER CALL, DECLARE FUNCTIONS HERE
	

this.startVideo = function() {	
	video.play();
}

this.stopVideo = function() {	
	video.pause();
	video.src = "";
}

this.update = function() {		
	if (video.readyState === video.HAVE_ENOUGH_DATA) {
		// videoImageContext.drawImage(video, 0, 0);
		if (videoTexture) {
			videoTexture.needsUpdate = true;
		}
	}
};		

}
}

export { ChromaKeyMaterial };

// END OF CLASS

  1. Import it into your Three.JS code using

import { ChromaKeyMaterial } from './path/to/your/ChromaKeyMaterial.js';

  1. Create objects

var greenScreenMaterial = new ChromaKeyMaterial("./path/to/your/greenvideo.mp4", 0x00ff00 ); var myGeometry = new THREE.PlaneBufferGeometry( 5, 5); var greenScreenVideo = new THREE.Mesh( myGeometry, greenScreenMaterial );

  1. Now you can start your video using greenScreenMaterial.startVideo(); and update your videoTexture in your render update using greenScreenMaterial.update();

Hope that helps!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants