stencilbuffer with reflector
by GnanaSai
HTML
<script type="importmap">
{
"imports": {
"three": "https://raw.githack.com/mrdoob/three.js/dev/build/three.webgpu.js",
"three/webgpu": "https://raw.githack.com/mrdoob/three.js/dev/build/three.webgpu.js",
"three/tsl": "https://raw.githack.com/mrdoob/three.js/dev/build/three.tsl.js",
"three/addons/": "https://raw.githack.com/mrdoob/three.js/dev/examples/jsm/"
}
}
</script>
CSS
body {
margin: 0px;
}
JavaScript
import * as THREE from 'three';
import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
import { Fn, vec2, vec4, texture, uv, textureBicubic, rangeFogFactor, reflector, time, mx_noise_vec3, positionGeometry, screenUV, color } from 'three/tsl';
let mesh, renderer, scene, camera, controls;
init();
function init() {
// renderer
renderer = new THREE.WebGPURenderer();
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setPixelRatio(window.devicePixelRatio);
renderer.setAnimationLoop(animate);
document.body.appendChild(renderer.domElement);
renderer.stencil = true;
// scene
scene = new THREE.Scene();
scene.background = new THREE.Color(0x000000);
// camera
camera = new THREE.PerspectiveCamera(40, window.innerWidth / window.innerHeight, 1, 10000);
camera.position.set(20, 20, 20);
// controls
controls = new OrbitControls(camera, renderer.domElement);
// ambient
scene.add(new THREE.AmbientLight(0x222222));
scene.background = new THREE.Color("#aaaaaa")
// light
const light = new THREE.DirectionalLight(0xffffff, 2);
light.position.set(20, 20, 0);
scene.add(light);
const light1 = new THREE.AmbientLight(0x404040, 1); // soft white light
scene.add(light1);
// axes
scene.add(new THREE.AxesHelper(20));
// Sphere
const geometry = new THREE.SphereGeometry(3, 6, 8);
geometry.translate(0, 5, 0)
const material = new THREE.MeshStandardNodeMaterial({
color: 0x00ffff,
flatShading: true,
});
material.stencilWrite = true;
material.stencilRef = 1;
material.stencilFunc = THREE.EqualStencilFunc;
// Plane
mesh = new THREE.Mesh(geometry, material);
scene.add(mesh);
const planegeo = new THREE.PlaneGeometry(5, 5, 2, 2)
planegeo.translate(0, 5, 3)
const planeMat = new THREE.MeshStandardNodeMaterial({ side: THREE.DoubleSide })
planeMat.colorWrite = true;
planeMat.depthWrite = false;
...