Difficulty in rendering all facets of a 3D object with Three.js and Fresnel Shader

I am delving into the world of Three.js and JavaScript coding for the first time. Lately, I have been experimenting with Shaders and Shader Materials.

The issue I encountered was when I loaded a mesh with a Fresnel material. While the surface material appeared fine, I couldn't see inside the mesh when I rotated it: http://codepen.io/gnazoa/pen/WrLJay

I attempted to add a second material beneath the shader material as a workaround. However, at times, upon loading in the browser, it didn't function correctly: http://codepen.io/gnazoa/pen/rxovKb

Do you have any suggestions? Is there a way to view all sides of the mesh using the Fresnel Shader?

Below are the shaders that I am utilizing:

    <script id="vertexShader" type="x-shader/x-vertex">

    uniform float fresnelBias;
    uniform float fresnelScale;
    uniform float fresnelPower;

    varying float vReflectionFactor;
    varying vec3 vReflect;

    void main() {
      vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );
      vec4 worldPosition = modelMatrix * vec4( position, 1.0 );

      vec3 worldNormal = normalize( mat3( modelMatrix[0].xyz, modelMatrix[1].xyz, modelMatrix[2].xyz ) * normal );

      vec3 I = worldPosition.xyz - cameraPosition;

      vReflect = reflect( I, worldNormal );
      vReflectionFactor = fresnelBias + fresnelScale * pow( 1.0 + dot( normalize( I ), worldNormal ), fresnelPower );

      gl_Position = projectionMatrix * mvPosition;

    }
      </script>

      <script id="fragmentShader" type="x-shader/x-fragment">

    uniform vec3 color;
    uniform samplerCube envMap;

    varying vec3 vReflect;
    varying float vReflectionFactor;

    void main() {
      vec4 envColor = textureCube( envMap, vec3( -vReflect.x, vReflect.yz ) );
      gl_FragColor = vec4(mix(color, envColor.xyz, vec3(clamp( vReflectionFactor, 0.0, 1.0 ))), 1.0);
    }
    </script>

EDIT:

I managed to resolve the issue by simply adding side: THREE.DoubleSide to the material, allowing me to see all sides of the mesh.

Now, my current challenge is figuring out why the mesh doesn't appear black on the inside even though my color uniform is set to black.

Any suggestions would be greatly appreciated.

Answer №1

The shine of your internal surface reflects white endlessly, creating a dazzling effect that eliminates any sight of black.

To solve this issue, consider using contrasting materials for the interior and exterior. Try a glossy black for the outside and a matte black for the inside – you'll see the difference instantly:

outerMaterial = new THREE.ShaderMaterial({
    side: THREE.FrontSide,
    uniforms : uniforms,
    vertexShader : vertexShader,
    fragmentShader : fragmentShader,
    wireframe: false
});
innerMaterial = new THREE.MeshBasicMaterial({
    side: THREE.BackSide,
    color: 0x000000
});

var loader = new THREE.BinaryLoader();
loader.load( "http://threejs.org/examples/obj/walt/WaltHead_bin.js", function ( geometry ) {

    geometry.scale( 7, 7, 7 );

    darkObject = new THREE.Object3D();

    inner = new THREE.Mesh( geometry, innerMaterial );
    outer = new THREE.Mesh( geometry, outerMaterial );

    darkObject.add( inner );
    darkObject.add( outer );
    scene.add( darkObject );

});

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

Retrieve particular data points from the object based on its unique identifier

Hey there, I'm facing an issue with Angular where I need to retrieve a specific object from an array based on its ID. I'm quite lost on how to approach solving this problem. var Obj = [ { Id: "1", shape: "circle", color: "red" }, { Id: " ...

In Firefox, an error occurs when Window.getComputedStyle does not adhere to the Element interface

After successfully appending data to the HTML element using the code below: $("#bookListDiv").append(data.HTMLString); I wanted to enhance the display by adding a fadein animation. So, I made some adjustments to achieve this effect: $(data.HTMLString).h ...

Receiving a 500 status code upon converting a SqlDataReader into Json format

Getting a status 500 error and not sure where I am going wrong. When I click on the 'Getcustomers' button, the 'GetCustomers' method is called which returns JSON. Script: <script> var MyApp = angular.module("MyApp", []); ...

Integration of C++ Applications: Comparing RabbitMQ and NodeJS Addon

I'm looking to interface with my C++ application using a NodeJS server. My main concern is whether there will be a significant decrease in performance when transmitting data between the C++ binary and the NodeJS server via RabbitMQ. For example, let& ...

Insufficient quality when displaying using threejs r71

While rendering a sofa in r71, I noticed that the texture quality is not up to par. Surprisingly, when I rendered it using r58, the difference in quality was very noticeable. Could this be a bug or something else entirely? Take a look at the images below ...

How can you make a select list appear or disappear based on the selection made in another select list?

I currently have a select list set up like this: https://i.sstatic.net/o730z.png What I would like to achieve is for the second select list to only appear once a selection or change has been made in the first select list. Additionally, when a selection o ...

The latency of asynchronous components in Nuxt.js causes delays

Having Trouble with Delay and Loading in Async Component Here is the code snippet causing issues: <template> <div> <button @click="startMethod">start</button> <async-component v-if="start" /> </div> < ...

Creating a pros and cons form for users using jQuery involves dynamically checking and updating the input values to ensure that no

When a new value is entered into the input box in this code, it will add and replace it for all P's tag. The desired change is to create a div with .pros-print class after each other, where the content of the P tags is equal to the new input value whe ...

States following form submission in an HTTP request may vary

When attempting to submit a form and receive the results on the same page using AJAX, a JavaScript function is called onsubmit, initiating the AJAX call and including the following code: request=new XMLHttpRequest(); /* set up request here ...

How can I obtain hosting XULDocument for a ContentWindow or HTMLDocument within a Firefox extension?

I have a Firefox Extension with a XUL overlay that includes some Javascript with event handlers. One of these event handlers is an EventListener for DOMContentLoaded. I want to load a second overlay only when visiting a specific website. Here is a snippet ...

developing versatile paths with Node.js

app.js // Including Routes require("./routes")(app); router folder index.js module.exports = function (app) { app.use("/", require("./all_routes")); } all_routes.js var express = require("express"); var route ...

AngularJS: The image loader will display on the initial div

I am trying to implement a loader that will hide after a certain amount of time when the user clicks on a profile. I have used a timeout function to achieve this behavior. However, the loader is appearing on the left side instead of within the respective ...

Are you familiar with a cutting-edge HTML5-powered, heritage-defying JavaScript framework?

Which cutting-edge framework should I choose to fully utilize the latest HTML5 technologies provided by modern browsers like Firefox 7, Safari 5, and Chrome 14? I have no need to support any older browsers such as IE, Firefox, or Chrome versions prior to t ...

React: Eliminate the reliance on two interconnected contexts by implementing a globally accessible constant object

Working on a new significant update for react-xarrows, I encountered a complex situation that needs resolution. To help explain, let's visualize with an example - two draggable boxes connected by an arrow within a wrapping context. https://i.sstatic ...

I'm looking to use JavaScript to dynamically generate multiple tabs based on the selected option in a dropdown menu

I'm reaching out with this question because my search for a clear answer or method has come up empty. Here's what I need help with: I've set up a dropdown titled 'Number of Chassis'. Depending on the selection made in this dropdown ...

Ngrx/effects will be triggered once all actions have been completed

I've implemented an effect that performs actions by iterating through an array: @Effect() changeId$ = this.actions$.pipe( ofType(ActionTypes.ChangeId), withLatestFrom(this.store.select(fromReducers.getAliasesNames)), switchMap(([action, aliases ...

Design a webpage using material-ui components

I'm struggling to create a layout using material-ui for a child page. Can someone guide me on how to achieve this design? https://i.sstatic.net/TwYR5.png ...

Node.js (Express), passport.js, mongoose.js, and Android app all have in common the HTTP Error 307 - Temporary redirect

I am currently constructing a REST Api using Node.js (Express.js and Moongose.js). I have a single post route that takes JSON data and redirects it to the signup page (/app/signup) if the user is a first-time user (not found in the database), or to the log ...

I encountered a problem with React Native while attempting to update the state with a new value

As I work on developing my app using react native and firebase, I encountered an issue with the error message TypeError: undefined is not an object (evaluating 'this.state.desativado.push') when attempting to click the + button. https://i.sstati ...

Executing a JavaScript file following an AJAX request in Ruby on Rails

Currently, I am utilizing prismjs to add syntax highlighting to my Rails application. The stylesheet and js file have been included in my layout, however, an issue arises where the code is only highlighted once during the initial page load. Subsequently, a ...