Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <html >
<head>
    <meta charset="UTF-8">
    <title>Three.js conditional shading</title>
    <link rel="stylesheet" href="style.css">
</head>

<body>
<span id="complex-number"></span>
<div id="container"></div>

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

varying vec2 vPos;

void main() {
  gl_Position = projectionMatrix *
                modelViewMatrix *
                vec4(position,1.0);
  vPos = position.xy;
}
</script>

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

uniform vec2 uC;
uniform vec2 uResolution;

varying vec2 vPos;

const int max_iterations = 255;

vec2 complex_square( vec2 v ) {
	return vec2(
		v.x * v.x - v.y * v.y,
		v.x * v.y * 2.0
	);
}

void main() {
    vec2 v = vPos - uResolution / min(uResolution.x, uResolution.y);
    v *= 15.0 / min(uResolution.x, uResolution.y);
    float scale = 0.1;


	int count = 0;

	for ( int i = 0 ; i < max_iterations; i++ ) {
		v = uC + complex_square( v );
		if ( dot( v, v ) > 4.0 ) {
			count = i;
			break;
		}
	}

	gl_FragColor = vec4( float( count ) * scale );
}

</script>

<script src='https://cdnjs.cloudflare.com/ajax/libs/three.js/r71/three.min.js'></script>
<script src="https://dat-gui.googlecode.com/git/build/dat.gui.min.js"></script>
<script src="script.js"></script>




</body>
</html>

              
            
!

CSS

              
                * {
    margin:0;
    //overflow: hidden;
}

#complex-number {
    background: black;
    color: white;
    font-family: "Georgia";
    style: italic;
    position: absolute;
    top: 100px;
    left: 50px;
}
              
            
!

JS

              
                var container;
var scene, camera, renderer;
var material, shaderUniforms = {}, shaderAttributes = {};
var plane;
var center = {
    cx: 0,
    cy: 0
};

var infoText = document.getElementById("complex-number");

var complexC = {
    cx: -0.13573,
    cy: -0.650
};

var time = 0;
var control = function() {
    this.cx = 0.24;
    this.cy = 0.64;
    this.useMouse = true;
};

var gui = new dat.GUI({width: 500});
var controller = new control();
gui.add(controller, 'cx', -2.0, 2.0).step(0.01).listen();
gui.add(controller, 'cy', -2.0, 2.0).step(0.01).listen();
gui.add(controller, 'useMouse');

init();
createPlane();
tick();

function init() {
    createScene();

    center = {
        x: window.innerWidth / 2,
        y: window.innerHeight /2
    };
}

window.onresize = function() {
    camera.aspect = window.innerWidth / window.innerHeight;
    camera.updateProjectionMatrix();
    renderer.setSize(window.innerWidth, window.innerHeight);
    scene.getObjectByName("plane").geometry = new THREE.PlaneGeometry(window.innerWidth, window.innerWidth);

    shaderUniforms.uResolution.x = window.innerWidth;
    shaderUniforms.uResolution.y = window.innerHeight;

    render();
};

window.onmousemove = function(e) {
    complexC = {
        cx: (e.clientX - center.x) / (window.innerWidth/ 2) * 1,
        cy: -(e.clientY - center.y) / (window.innerHeight / 2) * 1
    };
};

function setUniforms() {
    shaderUniforms = {
        uResolution: {
            type: "v2",
            value: new THREE.Vector2(window.innerWidth, window.innerHeight)
        },
        uC :{
            type: "v2",
            value: new THREE.Vector2(complexC.cx, complexC.cy)
        }
    };
}

function createScene() {
    container = document.getElementById('container');
    scene = new THREE.Scene();
    camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 1, 1000);
    camera.position.z = 250;
    camera.lookAt(scene.position);

    renderer = new THREE.WebGLRenderer();
    renderer.setSize(window.innerWidth, window.innerHeight);
    renderer.setClearColor(0xffccff, 1);

    container.appendChild(renderer.domElement);

}

function render() { renderer.render(scene, camera); }

function tick() {
    requestAnimationFrame(tick);
    update();
    render();

}

function createPlane() {
    setUniforms();
    material = new THREE.ShaderMaterial({
        attributes: shaderAttributes,
        uniforms: shaderUniforms,
        vertexShader: document.getElementById("vertexShader").textContent,
        fragmentShader: document.getElementById("fragmentShader").textContent
    });

    var planeGeom = new THREE.PlaneGeometry(window.innerWidth, window.innerWidth);
    plane = new THREE.Mesh(planeGeom, material);
    plane.name = "plane";

    scene.add(plane);
}

function update() {
    if (controller.useMouse) {
        shaderUniforms.uC.value.x = complexC.cx;
        shaderUniforms.uC.value.y = complexC.cy;

        controller.cx = complexC.cx;
        controller.cy = complexC.cy;

    } else {
        shaderUniforms.uC.value.x = controller.cx;
        shaderUniforms.uC.value.y = controller.cy;
    }

    infoText.innerHTML = 'C = ( ' + complexC.cx.toFixed(5) + ', ' + complexC.cy.toFixed(5) + 'i )';

}
              
            
!
999px

Console