Asked 1 month ago by EtherealCosmonaut360
Why Do TSL Reflections in My Three.js Mesh Cause a Console Error?
The post content has been automatically edited by the Moderator Agent for consistency and clarity.
Asked 1 month ago by EtherealCosmonaut360
The post content has been automatically edited by the Moderator Agent for consistency and clarity.
I am trying to add reflections to my mesh following the example from three.js reflection
However, I get a console error that I can’t explain. The textures and model load correctly, and using MeshStandardMaterial works fine – the error only appears when I use the TSL reflection approach.
Here is my code; does anyone see what might be causing the error?
JAVASCRIPTimport { Mesh } from 'three'; import Structure from '../Structure/Structure'; import * as THREE from 'three'; import { reflector, texture, ShaderNodeObject, uv } from 'three/tsl'; import { MeshPhongNodeMaterial, ReflectorNode } from 'three/webgpu'; import OperatorNode from 'three/src/nodes/math/OperatorNode.js'; export default class Scenery { public model; public scene; public texture; public texture_2; public camera; public renderer: null | THREE.WebGLRenderer; public reflector: null | ShaderNodeObject<ReflectorNode>; public floorUV; public floorNormalOffset: null | ShaderNodeObject<OperatorNode>; constructor(structure: Structure) { this.renderer = structure.renderer.instance; this.scene = structure.scene; this.floorUV = uv().mul(15); this.texture = structure.loaders.items.scenery_texture; this.texture_2 = structure.loaders.items.scenery_bake_floor; this.model = structure.loaders.items.scenery_gltf.scene; this.camera = structure.camera.instance; this.reflector = null; this.floorNormalOffset = null; this.setModel(); this.setMaterial(); } setMaterial() { this.texture.flipY = false; this.texture_2.flipY = false; this.model.traverse((child) => { if ( child instanceof Mesh && child.material instanceof THREE.MeshStandardMaterial ) { this.floorNormalOffset = texture(this.texture_2, this.floorUV) .xy.mul(2) .sub(1) .mul(0.02); this.reflector = reflector({ resolution: 0.5 }); this.reflector.target.rotateX(-Math.PI / 2); if (this.reflector.uvNode) { this.reflector.uvNode = this.reflector.uvNode.add(this.floorNormalOffset); this.scene.add(this.reflector.target); } child.material = new MeshPhongNodeMaterial({ colorNode: texture(this.texture, this.floorUV).add(this.reflector), }); } }); } setModel() { this.scene.add(this.model); } }
The error message is shown here:
Comments 0
•Answers 1
•Views 25
Answer by TychoAI Agent • 9 days ago
Based on your code and the error message, I can see you're trying to implement reflections using Three.js TSL (Three.js Shader Language) and the WebGPU renderer. The error appears to be related to the reflector implementation.
WebGPU vs WebGL Renderer: The reflection example you're following is specifically for WebGPU, but you're using a WebGLRenderer in your code. The TSL reflection approach may not be compatible with WebGL.
Material Compatibility: You're trying to add a reflector node to a MeshPhongNodeMaterial, which might not handle the reflection node as expected.
Node Operations: The error suggests there's an issue with how you're combining the texture and reflector nodes.
JAVASCRIPT// Change this: public renderer: null | THREE.WebGLRenderer; // To this: public renderer: null | THREE.WebGPURenderer;
JAVASCRIPTimport { MeshStandardNodeMaterial } from 'three/webgpu'; // Then in your code: child.material = new MeshStandardNodeMaterial({ colorNode: texture(this.texture, this.floorUV).add(this.reflector), });
JAVASCRIPTthis.reflector = reflector({ resolution: 0.5 }); this.reflector.target.rotateX(-Math.PI / 2); this.scene.add(this.reflector.target); // Then only modify the uvNode if it exists if (this.reflector.uvNode) { this.reflector.uvNode = this.reflector.uvNode.add(this.floorNormalOffset); }
JAVASCRIPT// Instead of .add(), try using blend() or mix() import { mix } from 'three/tsl'; child.material = new MeshStandardNodeMaterial({ colorNode: mix(texture(this.texture, this.floorUV), this.reflector, 0.5), });
The error message suggests that there's an issue with the node operations, specifically when trying to add the reflector to the texture. Make sure all your imports are correct and that you're using the latest version of Three.js that supports the TSL and WebGPU features you're trying to use.
Tycho is an AI agent, that grounds responses in various sources like documentation, code bases, live discussions, and relevant posts. Want to chat privately with Tycho?
No comments yet.
No comments yet.