Incorporate a new visual element with a texture in three.js

I'm struggling to apply a texture to a mesh and keep getting this error message:

[.WebGLRenderingContext]GL ERROR :GL_INVALID_OPERATION : glDrawElements: attempt to access out of range vertices in attribute 1

It's frustrating not understanding what the issue is... I hope someone can help me by looking at my code.

I am curious to see how the texture reacts when placed on a deformed shape.

Code

<!DOCTYPE html>
<html lang="en">
    <head>
        <title>three.js webgl - cameras</title>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
        <style>
            body {
                color: #808080;
                font-family:Monospace;
                font-size:13px;
                text-align:center;

                background-color: #000;
                margin: 0px;
                overflow: hidden;
            }

            #info {
                position: absolute;
                top: 0px; width: 100%;
                padding: 5px;
                z-index: 100;
            }

            a {

                color: #0080ff;
            }

            b { color: lightgreen }

            #stats { position: absolute; top:0; left: 0 }
            #stats #fps { background: transparent !important }
            #stats #fps #fpsText { color: #777 !important }
            #stats #fps #fpsGraph { display: none }
        </style>
    </head>

    <body>
        <div id="container"></div>
        <div id="info"><a href="http://threejs.org" target="_blank">three.js</a> - cameras<br/>
        <b>O</b> orthographic <b>P</b> perspective
        </div>

        <script src="../build/three.min.js"></script>
        <script src="js/libs/stats.min.js"></script>

        <script>
            var scene;
            var camera;

            var Zinc = 0;
            var texture, material, plane;
            var materials = [];

            texture = THREE.ImageUtils.loadTexture( "adidas.gif",{}, function(){

                // assuming you want the texture to repeat in both directions:
                texture.wrapS = THREE.RepeatWrapping;
                texture.wrapT = THREE.RepeatWrapping;

                texture.repeat.set( 1, 1 );

                material = new THREE.MeshBasicMaterial({ map : texture });

                init();
                animate();
            });

            function drawSquare(x1, y1, x2, y2) {

                var square = new THREE.Geometry();

                // Set four points
                square.vertices.push( new THREE.Vector3( x1,y2,0) );
                square.vertices.push( new THREE.Vector3( x1,y1,0) );
                square.vertices.push( new THREE.Vector3( x2,y1,0) );
                square.vertices.push( new THREE.Vector3( x2,y2,0) );

                //Push one triangle
                square.faces.push( new THREE.Face3( 0,1,2) );

                // Push another triangle
                square.faces.push( new THREE.Face3( 0,3,2) );

                // Return the square object with BOTH faces
                return square;
            }

           /* other functions and initialization remains the same as original code */
        </script>
    </body>
</html>

Answer №1

Below is the drawSquare function implemented with UV mappings:

function drawSquare(x1, y1, x2, y2)
{
    var square = new THREE.Geometry(); 

    // Define 4 points
    square.vertices.push( new THREE.Vector3( x1,y2,0) );
    square.vertices.push( new THREE.Vector3( x1,y1,0) );
    square.vertices.push( new THREE.Vector3( x2,y1,0) );
    square.vertices.push( new THREE.Vector3( x2,y2,0) );

    var uvs = [];
    uvs.push( new THREE.Vector2( 0.0, 0.0 ) );
    uvs.push( new THREE.Vector2( 1.0, 0.0 ) );
    uvs.push( new THREE.Vector2( 1.0, 1.0 ) );
    uvs.push( new THREE.Vector2( 0.0, 1.0 ) );

    // Generate faces

    // Add first triangle face
    square.faces.push( new THREE.Face3( 0, 1, 2 ) );
    square.faceVertexUvs[ 0 ].push( [ uvs[0], uvs[1], uvs[2] ] );

    // Add second triangle face
    square.faces.push( new THREE.Face3( 0, 2, 3 ) );
    square.faceVertexUvs[ 0 ].push( [ uvs[0], uvs[2], uvs[3] ] );

    // Return the square object with both faces
    return square;
}

Additionally, the corrected face indexes have been updated.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

passing a PHP variable into an HTML input field

Recently, I've encountered a problem where I need to transfer PHP variables to HTML input fields. echo $ManuellTagnavnMain; for ($n = 0; $n < 6; $n++) { print_r(++$ManuellTagnavnMain.PHP_EOL); } I'm looking for a way to pass these values ...

Trouble with the query waypoints extension in a simple demonstration

Can anyone help me figure out why the basic example from the waypoints plugin isn't working for me? Here's a link to the jsfiddle I created: http://jsfiddle.net/ZA8bd/2/ CSS .block1 { margin-top:30px; width: 400px; background: red; ...

AngularJS, building a hash of resources

Is there a way, in an AngularJS controller, to take a URL and redirect that request to the best place for fetching JSON data? The VideoSearchCtrl is connected to the search form. Everything seems fine with the generated URL for the template, so I aim to us ...

Restrict the option to select checkboxes

Can anyone help with limiting checkbox selection? This is the code I currently have... foreach($res as $res) echo '<div class="ediv"><input type="checkbox" class="echeck" name="pr[]" value="'.trim($res['product']).'" ...

Guide to implement a confirmation box in PHP

I recently set up a Joomla article and integrated the Sourcerer Joomla extension to include JavaScript and PHP in my project. Currently, I am developing a course purchase site where users can buy courses from owners and credits are deducted upon every purc ...

Problem with mesh opacity when the mesh is not visible

Feel free to check out my code demo on jsfiddle at this link: http://jsfiddle.net/georgeneil/cfrsj/5/ In the scene, there is a red cube with multiple particles inside. To replicate the issue, follow these steps: 1) Make the cube invisible by unchecking t ...

What is the best way to neatly import multiple images in Next.js?

I have a dilemma involving 10 images located in my public directory that I need to use in a component. Instead of individually importing each image like this: import imgurl1 from "../../public/celsius.gif"; import imgurl2 from "../../public/ ...

How to leverage tsconfig paths in Angular libraries?

While developing an Angular library, I made configurations in the tsconfig.lib.json file by adding the following setup for paths: "compilerOptions": { "outDir": "../../out-tsc/lib", "target": "es2015", "declaration": true, "inlineSources ...

JavaScript or Query: Transforming an Object from an Associative Array

Can someone help me out with converting an associative array into an object in JavaScript? I tried searching on Stackoverflow but couldn't find a working example. Here is the example structure: var combinedproducts = [["Testing-1","test-1"],["Testin ...

Enhancing infinite scroll functionality with ajax integration

After implementing a method to enable infinite scroll on my website using the following function: window.onscroll = yHandler; function yHandler(){ var wrap = document.getElementById('wrap'); var contentHeight = wrap.offsetHeight; var yOffset = w ...

How to send configuration data to an external library in Angular 6 in the app.module.ts file

My goal is to provide basic configuration settings to an external or third-party module. These settings are stored in a JSON file for easy modification across different environments without the need to rebuild the code. import { BrowserModule } from &apos ...

Understanding the request URL in Ajax when receiving a response

How can the URL of a jQuery request be retrieved from the response received? Perhaps something like this: function retrieveUrlFromResponse(response, url){ requestUrl = url; } ...

Guide on how to dynamically add AJAX JSON array response to an HTML table

Hey! I need some advice on how to dynamically append a JSON Array response to an HTML table after submitting a form using AJAX. Here's the scenario: This is my form : <form id="myForm" method="POST"> <input type=" ...

Prevent input from being cleared when selecting an option with React-Select

Unfortunately, there was no satisfactory solution to this question so I will provide an answer: The issue arises when trying to utilize React-Select with a persistent input value that remains even after selection or blurring. Regrettably, this functionali ...

Tips on retrieving PHP variable values along with HTML response using AJAX in PHP

I am looking to retrieve 2 variables: 1) The total number of records from the mysqli query performed in search_members.php 2) A boolean value indicating whether any results were found Below is the code I have written: <input type="button" value="Sea ...

Determine the data type of a string without needing to convert it

Can you determine if a value is numeric without casting it? For example, how can you differentiate between 'abc' and '123' without converting them to a specific data type? While visually apparent that 'abc' is not numeric, t ...

Trouble arises when implementing AJAX in conjunction with PHP!

I am facing an issue with my PHP page which collects mp3 links from downloads.nl. The results are converted to XML and display correctly. However, the problem arises when trying to access this XML data using ajax. Both the files are on the same domain, b ...

Container slide-show fill error

I'm attempting to create a slide show with an overlapping caption that appears when hovering over the container or image. The image needs to fit exactly inside the container so no scroll bar is shown and the border radius is correct. I managed to achi ...

Is concealing content using Javascript or jQuery worth exploring?

While I have been hiding content using display:none; in css, there are concerns that Google may not like this approach. However, due to the needs of jQuery animations, it has been necessary for me. Recently, I have come across a new method for hiding conte ...

What makes realtime web programming so fascinating?

Working as a web developer, I have successfully created various real-time collaborative services such as a chat platform by utilizing third-party tools like Redis and Pusher. These services offer straightforward APIs that enable me to establish bidirection ...