JSFiddle - React, Tailwind, and code Playground
by tfoller
HTML
<script async src="https://unpkg.com/es-module-shims@1.3.6/dist/es-module-shims.js"></script>
<script type="importmap">
{
"imports": {
"three": "https://unpkg.com/three@0.145.0/build/three.module.js",
"three/addons/": "https://unpkg.com/three@0.145.0/examples/jsm/"
}
}
</script>
JavaScript
import * as THREE from 'three'
import { OrbitControls } from 'https://cdn.jsdelivr.net/npm/three@0.116.1/examples/jsm/controls/OrbitControls.js';
const scene = new THREE.Scene()
const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 100)
camera.position.set(0, 10, 10);
const renderer = new THREE.WebGLRenderer();
document.body.appendChild(renderer.domElement);
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.domElement.id = 'perspective';
renderer.domElement.style.float = 'left';
const controls = new OrbitControls(camera, renderer.domElement);
const bgTexture = new THREE.TextureLoader().load("https://unsplash.it/1024/1024");
const floorGeometry = new THREE.PlaneGeometry(10, 10);
const material = new THREE.MeshBasicMaterial({ map: bgTexture });
const floor = new THREE.Mesh(floorGeometry, material);
floor.position.set(0, 0, 0);
floor.rotation.set(-Math.PI / 2, 0, 0);
scene.add(floor);
const cubeGeometry = new THREE.BoxGeometry(2, 2, 2);
const cube = new THREE.Mesh(cubeGeometry, material);
cube.position.set(3, 1, -2);
scene.add(cube);
const ortho = new THREE.OrthographicCamera(-5, 5, 5, -5, 0, 5);
ortho.position.y = 4;
ortho.rotation.set(-Math.PI / 2, 0, 0);
scene.add(ortho);
const orthoHelper = new THREE.CameraHelper(ortho);
scene.add(orthoHelper);
// create render target
const rt = new THREE.WebGLRenderTarget(window.innerWidth, window.innerHeight);
// show render target texture on a quad
const mesh = new THREE.Mesh(
new THREE.PlaneGeometry(10, 10),
new THREE.MeshBasicMaterial({ map: rt.texture })
);
mesh.position.x = 10;
scene.add(mesh);
const dmat = new THREE.MeshDepthMaterial();
const renderDepth = (rend, scene, camera, outputRT) => {
scene.overrideMaterial = dmat;
rend.setRenderTarget(outputRT);
rend.render(scene, camera);
rend.setRenderTarget(null);
scene.overrideMaterial = null;
};
function animate() {
renderer.render(scene, camera);
renderDepth(renderer, scene, ortho,...