Note that there are some explanatory texts on larger screens.

plurals
  1. POCustom Texture Shader in Three.js
    primarykey
    data
    text
    <p>I'm just looking to create a very simple Fragment Shader that draws a specified texture to the mesh. I've looked at a handful of custom fragment shaders that accomplished the same and built my own shaders and supporting JS code around it. However, it's just not working. Here's a working abstraction of the code I'm trying to run:</p> <p><strong>Vertex Shader</strong></p> <pre class="lang-js prettyprint-override"><code>&lt;script id="vertexShader" type="x-shader/x-vertex"&gt; varying vec2 vUv; void main() { vUv = uv; gl_Position = projectionMatrix * modelViewMatrix * vec4(position,1.0); } &lt;/script&gt; </code></pre> <p><strong>Fragment Shader</strong></p> <pre class="lang-js prettyprint-override"><code>&lt;script id="fragmentShader" type="x-shader/x-fragment"&gt; uniform sampler2D texture1; varying vec2 vUv; void main() { gl_FragColor = texture2D(texture1, vUv); // Displays Nothing //gl_FragColor = vec4(0.5, 0.2, 1.0, 1.0); // Works; Displays Flat Color } &lt;/script&gt; </code></pre> <p><strong>Scene Code</strong></p> <pre class="lang-js prettyprint-override"><code>&lt;script&gt; // Initialize WebGL Renderer var renderer = new THREE.WebGLRenderer(); renderer.setSize(window.innerWidth, window.innerHeight); var canvas = document.getElementById('canvas').appendChild(renderer.domElement); // Initialize Scenes var scene = new THREE.Scene(); // Initialize Camera var camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 1, 100); camera.position.z = 10; // Create Light var light = new THREE.PointLight(0xFFFFFF); light.position.set(0, 0, 500); scene.add(light); // Create Ball var vertShader = document.getElementById('vertexShader').innerHTML; var fragShader = document.getElementById('fragmentShader').innerHTML; var uniforms = { texture1: { type: 't', value: 0, texture: THREE.ImageUtils.loadTexture( 'texture.jpg' ) } }; var material = new THREE.ShaderMaterial({ uniforms: uniforms, vertexShader: vertShader, fragmentShader: fragShader }); var ball = new THREE.Mesh(new THREE.SphereGeometry(1, 50, 50), material); scene.add(ball); // Render the Scene renderer.render(scene, camera); &lt;/script&gt; </code></pre> <p><code>texture.jpg</code> exists, and displays when mapped to a <code>MeshLambertMaterial</code>. When switching my fragment shader to a simple color (Commented out in code) it properly displays the ball.</p> <p>Running this displays nothing at all. I don't get any errors, the ball just doesn't appear at all.</p> <p>I know I must be doing something fundamentally wrong, but I've been looking over the same examples in which this code seems to work for a couple days now and I feel like I'm bashing my head against a wall. Any help would be appreciated!</p> <p>Edit: I am using Three.js Revision 51</p>
    singulars
    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.
 

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