JSFiddle: http://jsfiddle.net/ma791nd8/1/
I'm encountering some challenges due to outdated tutorials and limited documentation, which is why I've decided to seek assistance here.
After reviewing one of the examples provided, I created a jsfiddle with updated syntax. However, I'm facing a CORS issue even though I have set up myTexture.crossOrigin = ''
. Additionally, I can't figure out why it's not working in general. Any insights?
You can view a demo of the original example here: https://github.com/meetar/three.js-displacement-map
Here are my modifications in the JSFiddle version:
// scene setup
var renderer = new THREE.WebGLRenderer({antialias: true});
renderer.setSize(document.body.clientWidth, document.body.clientHeight);
document.body.appendChild(renderer.domElement);
var scene = new THREE.Scene();
// CAMERA
var fov = 45; // camera field-of-view in degrees
var width = renderer.domElement.width;
var height = renderer.domElement.height;
var aspect = width / height; // view aspect ratio
var camera = new THREE.PerspectiveCamera( fov, aspect );
camera.position.z = -200;
camera.position.y = -400;
camera.lookAt(scene.position);
// LIGHTS
ambientLight = new THREE.AmbientLight( 0xffffff );
scene.add( ambientLight );
// SHADERS
planeMesh = new THREE.Group();
var myTexture = new THREE.TextureLoader();
myTexture.crossOrigin = '';
myTexture.load('https://leveldev.files.wordpress.com/2012/12/heightmap.jpeg',
function (myTexture) {
var shader = THREE.ShaderLib[ "normalmap" ];
var uniforms = THREE.UniformsUtils.clone( shader.uniforms );
uniforms[ "enableDisplacement" ].value = true;
uniforms[ "enableDiffuse" ].value = true;
uniforms[ "tDisplacement" ].value = myTexture;
uniforms[ "tDiffuse" ].value = myTexture;
uniforms[ "uDisplacementScale" ].value = 50;
var parameters = { fragmentShader: shader.fragmentShader, vertexShader: shader.vertexShader, uniforms: uniforms, lights: true, wireframe: true };
var material = new THREE.ShaderMaterial( parameters );
var plane = new THREE.Mesh( geometry, material);
planeMesh.add(plane)
}
);
planeMesh.rotation.y = Math.PI;
scene.add(planeMesh);
// FUNCTIONS
function animate() {
requestAnimationFrame( animate );
renderer.render( scene, camera );
}
requestAnimationFrame( animate );