three.js mesh user data to tsl node material
by GnanaSai
HTML
<!-- Import maps polyfill -->
<!-- Remove this when import maps will be widely supported -->
<script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
<script type="importmap">
{
"imports": {
"three": "https://unpkg.com/three/build/three.webgpu.js",
"three/addons/": "https://unpkg.com/three/examples/jsm/"
}
}
</script>
CSS
body {
margin: 0px;
}
JavaScript
// Simple three.js example
import * as THREE from 'three';
import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
import {uniform,userData} from "three"
let mesh, renderer, scene, camera, controls;
const dummy = new THREE.Object3D();
init();
animate();
async function init() {
// renderer
renderer = new THREE.WebGPURenderer({ antialias: true });
renderer.setSize( window.innerWidth, window.innerHeight );
renderer.setPixelRatio( window.devicePixelRatio );
document.body.appendChild( renderer.domElement );
// scene
scene = new THREE.Scene();
// 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 );
const cubeGeo = new THREE.BoxGeometry()
const materialNode = new THREE.MeshStandardNodeMaterial()
materialNode.colorNode = new THREE.Color(0xffaaaa)
const cubeMesh = new THREE.Mesh(cubeGeo, materialNode);
const grp = new THREE.Group();
scene.add(grp);
for (let i = -2; i <= 2; i++) {
const newMesh = cubeMesh.clone();
newMesh.position.x = i * 3;
newMesh.userData = { "color": "0xffff00" }
grp.add(newMesh);
}
/* materialNode.colorNode = userData("color","color"); */
}
function animate() {
requestAnimationFrame( animate );
renderer.renderAsync( scene, camera );
}