Note that there are some explanatory texts on larger screens.

plurals
  1. POProblems loading shaders from external file in ThreeJS
    primarykey
    data
    text
    <p>This has been asked before (2 years ago) and in several places both in ThreeJS github and SO. But still many (including me) are having problems with this. This is what I got now (which is part of a larger system):</p> <pre><code>function dataLoader() { this.data_count = 0; this.data_array = new Array(); } function loadFile( file, loader ){ var FileObject = new Object(); FileObject.data = ""; FileObject.ready = false; FileObject.id = loader.data_count; loader.data_array[loader.data_count] = false; $.ajax({ type: "GET", url: file, dataType: "text" }).done( function( msg ) { FileObject.data = msg; FileObject.ready = true; loader.data_array[FileObject.id] = true; }); loader.data_count += 1; return FileObject; } </code></pre> <p>And then I do this:</p> <pre><code>var loader = new dataLoader(); var SkyVertexShader = loadFile( "Shaders/sky.frag", loader ); var SkyFragmentShader = loadFile( "Shaders/sky.vertex", loader ); </code></pre> <p>And then I create material like this:</p> <pre><code>var skyMat = new THREE.ShaderMaterial( { vertexShader: SkyVertexShader.data, fragmentShader: SkyFragmentShader.data, uniforms: uniforms, side: THREE.BackSide } ); </code></pre> <p>Shaders in sky.frag and sky.vertex are plain text without any tags or anything. When I debug I can see that SkyFragmentShader.data and SkyVertexShader.data are both correctly set. Shaders are the ones from <a href="https://threejs.org/examples/webgl_lights_hemisphere.html" rel="nofollow noreferrer">webgl_lights_hemisphere</a> example. But when I load I get this error:</p> <pre><code>ERROR: 0:37: 'modelMatrix' : undeclared identifier ERROR: 0:37: 'position' : undeclared identifier ERROR: 0:37: 'constructor' : not enough data provided for construction ERROR: 0:38: 'assign' : l-value required "vWorldPosition" (can't modify a varying) ERROR: 0:40: 'gl_Position' : undeclared identifier ERROR: 0:40: 'projectionMatrix' : undeclared identifier ERROR: 0:40: 'modelViewMatrix' : undeclared identifier ERROR: 0:40: 'constructor' : not enough data provided for construction ERROR: 0:40: 'assign' : cannot convert from '4-component vector of float' to 'float' � </code></pre> <p>But it works when I just do this (so like in the examples):</p> <pre><code>var fragmentShader = document.getElementById( 'fragmentShader' ).textContent; var vertexShader = document.getElementById( 'vertexShader' ).textContent; var skyMat = new THREE.ShaderMaterial( { vertexShader: vertexShader, fragmentShader: fragmentShader, uniforms: uniforms, side: THREE.BackSide } ); </code></pre> <p>This last part of course works only when having the shaders in HTML. So even though the content is virtually identical between the .textContent method and the Ajax GET method (just some tabs and newlines) it shows syntax errors about undefined variables. So what could cause this?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload