BumpMapNode.js 4.6 KB
Newer Older
S
sunag 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/**
 * @author sunag / http://www.sunag.com.br/
 */

import { TempNode } from '../core/TempNode.js';
import { FloatNode } from '../inputs/FloatNode.js';
import { FunctionNode } from '../core/FunctionNode.js';
import { NormalNode } from '../accessors/NormalNode.js';
import { PositionNode } from '../accessors/PositionNode.js';

function BumpMapNode( value, scale ) {

	TempNode.call( this, 'v3' );

	this.value = value;
	this.scale = scale || new FloatNode( 1 );

	this.toNormalMap = false;

S
sunag 已提交
20 21 22 23
}

BumpMapNode.Nodes = ( function () {

S
sunag 已提交
24 25 26 27
	var dHdxy_fwd = new FunctionNode( [

		// Bump Mapping Unparametrized Surfaces on the GPU by Morten S. Mikkelsen
		// http://api.unrealengine.com/attachments/Engine/Rendering/LightingAndShadows/BumpMappingWithoutTangentSpace/mm_sfgrad_bump.pdf
S
sunag 已提交
28

S
sunag 已提交
29 30 31 32 33
		// Evaluate the derivative of the height w.r.t. screen-space using forward differencing (listing 2)

		"vec2 dHdxy_fwd( sampler2D bumpMap, vec2 vUv, float bumpScale ) {",

		// Workaround for Adreno 3XX dFd*( vec3 ) bug. See #9988
S
sunag 已提交
34

S
sunag 已提交
35 36
		"	vec2 dSTdx = dFdx( vUv );",
		"	vec2 dSTdy = dFdy( vUv );",
S
sunag 已提交
37

S
sunag 已提交
38 39 40
		"	float Hll = bumpScale * texture2D( bumpMap, vUv ).x;",
		"	float dBx = bumpScale * texture2D( bumpMap, vUv + dSTdx ).x - Hll;",
		"	float dBy = bumpScale * texture2D( bumpMap, vUv + dSTdy ).x - Hll;",
S
sunag 已提交
41

S
sunag 已提交
42
		"	return vec2( dBx, dBy );",
S
sunag 已提交
43

S
sunag 已提交
44 45 46 47 48 49 50 51 52
		"}"

	].join( "\n" ), null, { derivatives: true } );

	var perturbNormalArb = new FunctionNode( [

		"vec3 perturbNormalArb( vec3 surf_pos, vec3 surf_norm, vec2 dHdxy ) {",

		// Workaround for Adreno 3XX dFd*( vec3 ) bug. See #9988
S
sunag 已提交
53

S
sunag 已提交
54 55 56
		"	vec3 vSigmaX = vec3( dFdx( surf_pos.x ), dFdx( surf_pos.y ), dFdx( surf_pos.z ) );",
		"	vec3 vSigmaY = vec3( dFdy( surf_pos.x ), dFdy( surf_pos.y ), dFdy( surf_pos.z ) );",
		"	vec3 vN = surf_norm;", // normalized
S
sunag 已提交
57

S
sunag 已提交
58 59 60 61
		"	vec3 R1 = cross( vSigmaY, vN );",
		"	vec3 R2 = cross( vN, vSigmaX );",

		"	float fDet = dot( vSigmaX, R1 );",
S
sunag 已提交
62

S
sunag 已提交
63
		"	fDet *= ( float( gl_FrontFacing ) * 2.0 - 1.0 );",
S
sunag 已提交
64

S
sunag 已提交
65
		"	vec3 vGrad = sign( fDet ) * ( dHdxy.x * R1 + dHdxy.y * R2 );",
S
sunag 已提交
66

S
sunag 已提交
67 68 69 70 71 72 73 74
		"	return normalize( abs( fDet ) * surf_norm - vGrad );",

		"}"

	].join( "\n" ), [ dHdxy_fwd ], { derivatives: true } );

	var bumpToNormal = new FunctionNode( [
		"vec3 bumpToNormal( sampler2D bumpMap, vec2 uv, float scale ) {",
S
sunag 已提交
75

S
sunag 已提交
76 77
		"	vec2 dSTdx = dFdx( uv );",
		"	vec2 dSTdy = dFdy( uv );",
S
sunag 已提交
78

S
sunag 已提交
79 80 81
		"	float Hll = texture2D( bumpMap, uv ).x;",
		"	float dBx = texture2D( bumpMap, uv + dSTdx ).x - Hll;",
		"	float dBy = texture2D( bumpMap, uv + dSTdy ).x - Hll;",
S
sunag 已提交
82

S
sunag 已提交
83
		"	return vec3( .5 - ( dBx * scale ), .5 - ( dBy * scale ), 1.0 );",
S
sunag 已提交
84

S
sunag 已提交
85 86
		"}"
	].join( "\n" ), null, { derivatives: true } );
S
sunag 已提交
87

S
sunag 已提交
88 89 90 91 92
	return {
		dHdxy_fwd: dHdxy_fwd,
		perturbNormalArb: perturbNormalArb,
		bumpToNormal: bumpToNormal
	};
S
sunag 已提交
93 94

} )();
S
sunag 已提交
95 96 97 98 99 100 101 102 103 104

BumpMapNode.prototype = Object.create( TempNode.prototype );
BumpMapNode.prototype.constructor = BumpMapNode;
BumpMapNode.prototype.nodeType = "BumpMap";

BumpMapNode.prototype.generate = function ( builder, output ) {

	if ( builder.isShader( 'fragment' ) ) {

		if ( this.toNormalMap ) {
S
sunag 已提交
105

S
sunag 已提交
106
			var bumpToNormal = builder.include( BumpMapNode.Nodes.bumpToNormal );
S
sunag 已提交
107

S
sunag 已提交
108
			return builder.format( bumpToNormal + '( ' + this.value.build( builder, 'sampler2D' ) + ', ' +
S
sunag 已提交
109
				this.value.uv.build( builder, 'v2' ) + ', ' +
S
sunag 已提交
110
				this.scale.build( builder, 'f' ) + ' )', this.getType( builder ), output );
S
sunag 已提交
111

S
sunag 已提交
112
		} else {
S
sunag 已提交
113

S
sunag 已提交
114 115
			var derivativeHeight = builder.include( BumpMapNode.Nodes.dHdxy_fwd ),
				perturbNormalArb = builder.include( BumpMapNode.Nodes.perturbNormalArb );
S
sunag 已提交
116

S
sunag 已提交
117
			this.normal = this.normal || new NormalNode();
S
sunag 已提交
118
			this.position = this.position || new PositionNode( PositionNode.VIEW );
S
sunag 已提交
119

S
sunag 已提交
120
			var derivativeHeightCode = derivativeHeight + '( ' + this.value.build( builder, 'sampler2D' ) + ', ' +
S
sunag 已提交
121
				this.value.uv.build( builder, 'v2' ) + ', ' +
S
sunag 已提交
122
				this.scale.build( builder, 'f' ) + ' )';
S
sunag 已提交
123 124 125 126

			return builder.format( perturbNormalArb + '( -' + this.position.build( builder, 'v3' ) + ', ' +
				this.normal.build( builder, 'v3' ) + ', ' +
				derivativeHeightCode + ' )', this.getType( builder ), output );
S
sunag 已提交
127

S
sunag 已提交
128 129 130 131 132 133 134 135 136 137 138 139 140
		}

	} else {

		console.warn( "THREE.BumpMapNode is not compatible with " + builder.shader + " shader." );

		return builder.format( 'vec3( 0.0 )', this.getType( builder ), output );

	}

};

BumpMapNode.prototype.copy = function ( source ) {
S
sunag 已提交
141

S
sunag 已提交
142
	TempNode.prototype.copy.call( this, source );
S
sunag 已提交
143

S
sunag 已提交
144 145
	this.value = source.value;
	this.scale = source.scale;
S
sunag 已提交
146

S
sunag 已提交
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
};

BumpMapNode.prototype.toJSON = function ( meta ) {

	var data = this.getJSONNode( meta );

	if ( ! data ) {

		data = this.createJSONNode( meta );

		data.value = this.value.toJSON( meta ).uuid;
		data.scale = this.scale.toJSON( meta ).uuid;

	}

	return data;

};

export { BumpMapNode };