I was looking to implement back face culling for my sphere model, but I'm currently struggling with defining the vertices for my wireframe sphere

Currently, I am stuck while creating a wireframe sphere object and defining vertices for it. Additionally, I need help with applying back-face culling to the created sphere. Can anyone assist me with this task?

In my HTML file, I have been using the three.js library as I am relatively new to WebGL. Unfortunately, I haven't been able to solve this problem on my own.

<html>
    <head>
        <title> Creating a Sphere Model in WebGL </title>
        <script src ="./three.js-master/three.js-master/build/three.js"></script>
        <script src ="./three.js-master/three.js-master/build/three.min.js"></script>
        <script type="text/javascript" src="Three.js"></script>
    </head>
    <body onload="createsphere()">
        <canvas id="glcanvas" width="840" height="620"></canvas>
    </body>
</html>

My linked JavaScript file is named 'Three.js'.

function createsphere() {
    var pos = function(d) { return document.getElementById(d); };

    // Define scene, camera, renderer
    var scene = new THREE.Scene();
    var camera = new THREE.PerspectiveCamera(75, window.innerWidth/window.innerHeight, 0.1, 1000);  
    var renderer = new THREE.WebGLRenderer();
    renderer.setSize(window.innerWidth, window.innerHeight);
    document.body.appendChild(renderer.domElement);

    // Create a wireframe sphere object

    var spheregeometry = new THREE.SphereGeometry(1.0, 30, 30);
    var texture = THREE.ImageUtils.loadTexture('earth.jpg', {}, function() { alert('texture loaded');}, function(){ alert('error loading texture');});
    var spherematerial = new THREE.MeshBasicMaterial({wireframe: true, map: texture});
    var sphere = new THREE.Mesh(spheregeometry, spherematerial);

    scene.add(sphere); 

    camera.position.z = 5;

    // Render the scene
    var render = function(e) {
        requestAnimationFrame(render); 
        renderer.render(scene, camera);
    };

    render();
}

Answer №1

When creating a wireframe, back-face culling isn't necessary since there are no actual faces, only lines.

To achieve the desired effect, consider creating two meshes: one with a wireframe material and another smaller black mesh inside it, like this:

function createSphere() {
    var getElementById = function(id) { return document.getElementById(id); };

    // Define scene, camera, renderer
    var scene = new THREE.Scene();
    var camera = new THREE.PerspectiveCamera(75, window.innerWidth/window.innerHeight, 0.1, 1000);
    var renderer = new THREE.WebGLRenderer();
    renderer.setSize(window.innerWidth, window.innerHeight);
    document.body.appendChild(renderer.domElement);

    // Create sphere objects

    var sphereGeometry = new THREE.SphereGeometry(1.0, 30, 30);
    var sphereGeometry2 = new THREE.SphereGeometry(0.99, 30, 30);
    var texture = THREE.ImageUtils.loadTexture('earth.jpg', {}, function() { alert('texture loaded');}, function(){ alert('error loading texture');});

    var sphereMaterial = new THREE.MeshBasicMaterial({wireframe: true});
    var sphereMaterial2 = new THREE.MeshBasicMaterial({side : THREE.DoubleSide, color : '#000000'});

    var sphere = new THREE.Mesh(sphereGeometry, sphereMaterial);
    var sphere2 = new THREE.Mesh(sphereGeometry2, sphereMaterial2);

    scene.add(sphere);
    scene.add(sphere2);

    camera.position.z = 0;

    // Renderer
    var renderScene = function() {
        requestAnimationFrame(renderScene);
        renderer.render(scene, camera);
    };

    renderScene();
}

It's important to note that with this solution, visibility from within the sphere is limited as you won't be able to see the outside. To display the outer surface, you may need to explore alternative methods such as writing a shader for rendering the wireframe over the sphere's texture.

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

"Is there a virtual keyboard available that supports multiple inputs and automatically switches to the next input when the maximum length

Looking to build a virtual keyboard with multiple inputs that automatically switch to the next input field after reaching the maximum length. Currently having an issue where I can only fill the first input and not the second one. Any assistance in resolvin ...

Creating slanted cylinders/cones using three.js

I'm finding it challenging to figure out how to create a slanted cylinder in three.js. Essentially, I want to generate a cylinder with a larger top radius (r1) and a smaller bottom radius (r2), with the top and bottom parts of the cylinder offset by a ...

Display issue with ThreeJS cube

Currently, I'm delving into the world of ThreeJS and decided to incorporate the library into my existing NextJS project. My goal was simple - to display a cube on the front page. However, despite my best efforts, nothing seems to be appearing on the s ...

Incorporating JQuery Plugins into Angular 4 Apps

I am currently attempting to incorporate various jquery plugins, such as Magnific-Popup, into my Angular 4 application but encountering difficulties. I successfully installed jQuery using npm install --save-dev @types/jquery in the terminal, yet implementi ...

An unexpected page transition occurs when attempting to delete a link

I've successfully created an HTML table that dynamically adds rows and provides an option to delete the current row. Each row represents data retrieved from MongoDB, and upon clicking the delete button, I aim to delete the corresponding item from the ...

Learn how to implement a rotation effect on a pseudo-element in ReactJS with clickable functionality, specifically designed for SVG

[images for my project] [1]: https://i.sstatic.net/a6EFH.png [2]: https://i.sstatic.net/lMhNu.png Clicking on the orange arrow should trigger a 180-degree rotation. Can this be achieved using CSS or do I need to utilize JavaScript? Also, how can I dec ...

Utilize a generic data type for a property that can accept values of type 'number', 'string', or 'undefined'

This query involves React code but pertains to typescript rather than react. To simplify, I have a component called MyList which accepts a single generic type argument passed to the props type. The generic type represents an object that will be used to c ...

Is it possible to adjust the maximum time limit for uploaded video files in Vue?

I'm facing an issue while trying to dynamically change the maximum value of a time input field in Vue JavaScript after uploading a video file. Despite setting the attribute `max` on the input element using JavaScript, I am unable to update the max val ...

Using Angular promises and the $http service

ng.module('app') .service('CardService', ['$http', CardService]) function CardService($http) { this.$http = $http; var self = this; $http.get('http://localhost:3000/db').success(function(data) { ...

Storing object dimensions in react-redux

Are there any performance issues associated with storing a large number of items in the redux-store? For example, if the store structure looks like this: { user:{...}, userDetail:{}, shoppingCart:{}, OrderDetail:{}, ... } The store co ...

XCODE encountered an issue while attempting to open the input file located at '/Users/name/Desktop/test/appname/node_modules/react-native-compressor/ios/Video/Compressor-Bridging-Header.h'

Encountering an issue while running my project in XCode. I recently added the react-native-compressor package by following the steps mentioned in the installation guide When attempting to run the project in XCode, the error below occurs: <unknown> ...

Can I utilize p5.Vector.fromAngle() in Vue JS?

Incorporating P5 into a Vue application can be achieved using the following code snippet: let script = p5 => { p5.setup = _ => { this.setup(p5) } p5.draw = _ => { this.draw(p5) } } this.ps = new P5(script) While functions like ba ...

The shade of grey on the curve appears instead of the usual red on the iPad. Any ideas on how to fix

I am having trouble creating a curve like the one shown below on my iPad. The color appears to be gray and does not work properly. Is this a bug with ChartJS or is there a solution to fix this issue? This problem occurs when the type value is set to "lin ...

Smooth scrolling feature for Wordpress websites

I am currently in the process of converting an HTML5 template into a Wordpress theme. One specific feature I'd like to add is a custom scroll for Wordpress using the code: <script src="jquery.nicescroll.js"></script> DEPENDENCIES This cod ...

Is it possible to implement the same technique across various child controllers in AngularJS?

I am trying to execute a function in a specific child controller. The function has the same name across all child controllers. My question is how can I call this function from a particular controller? Parent Controller: app.controller("parentctrl",functi ...

The request to sign up at 'https://identitytoolkit.googleapis.com/v1/accounts:/signUp? from the origin 'http://localhost:8080' has been denied

My attempt to create a new user in Firebase using Axios in Vue.js is resulting in an error message regarding CORS policy. The specific error states: "Access to XMLHttpRequest at 'https://identitytoolkit.googleapis.com/v1/accounts:/signUp?key=AIzaSyDvZ ...

Creating a versatile JavaScript library that is compatible with various platforms such as browsers, NodeJS, and single page applications like React

My question is: Is it possible to convert a basic file into a versatile library that can be utilized with Browsers (via script tag), Node JS, and Single Page Applications using a single codebase? In the past, I have primarily used existing libraries witho ...

The Great Gatsby - Unable to access property 'component---src-pages-index-jsx' due to its undefined nature

Attempting to transition my current ReactJS application with a WordPress backend to GatsbyJS has presented some challenges. As a newcomer to GatsbyJS, I diligently followed the setup instructions provided on their website for Windows 10. Despite a successf ...

Utilize Vue JS to trigger a query when an option is selected in a drop-down select

I'm new to Vue and facing a challenge. I have multiple drop-down select menus that fetch data from an Axios request. The select only responds when I click the submit button. I want it to update when the user makes a selection from the drop-down. I can ...

Challenges encountered while trying to combine Vue 3 with Firestore

I am currently in the process of integrating Firebase (and Cloud Firestore) with my Vue 3 app. I have successfully installed the <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="65030c170007041600255c4b554b57">[email prot ...