4

I need to access the text of a string which I import using HTML from inside another script.

I include the text file in question in the html file:

<script src="shaders/fragmentshader.fs" id=fragmentshader></script>

I then want to put the contents of this file in a variable within another script file and use it as a shader for three,js :

var fShader = $('#fragmentshader');

var shader = new THREE.ShaderMaterial({
    vertexShader: vShader.text(),
    fragmentShader: fShader.text()
});

This code works fine if I just write the necessary shader code between the script tags in the html file, but only accesses the strict url(not the data) if used as above.

My question is how do I access the text within the file after loading it as shown above?

1 Answer 1

4

You can't load shaders using src in script tag.

You can use ShaderLoader.js and then write:

<script data-src="shaders/name/vertex.js" data-name="shader" 
type="x-shader/x-vertex"></script>

In js:

SHADER_LOADER.load(function(data) {
    var particlesVertexShader = data.shader.vertex;
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.