提交 d8e5678e 编写于 作者: M Mikael Emtinger

Optimized initalization of ShadowVolumes by using supplied edge information (if available)

上级 36bc61e5
......@@ -2,12 +2,20 @@
* @author mikael emtinger / http://gomo.se/
*/
THREE.ShadowVolume = function( mesh, isStatic ) {
THREE.ShadowVolume = function( meshOrGeometry, isStatic ) {
THREE.Mesh.call( this, mesh.geometry, isStatic ? [ new THREE.ShadowVolumeDynamicMaterial() ] : [ new THREE.ShadowVolumeDynamicMaterial() ] );
mesh.addChild( this );
this.calculateShadowVolumeGeometry( mesh.geometry );
if( meshOrGeometry instanceof THREE.Mesh ) {
THREE.Mesh.call( this, meshOrGeometry.geometry, isStatic ? [ new THREE.ShadowVolumeDynamicMaterial() ] : [ new THREE.ShadowVolumeDynamicMaterial() ] );
meshOrGeometry.addChild( this );
} else {
THREE.Mesh.call( this, meshOrGeometry, isStatic ? [ new THREE.ShadowVolumeDynamicMaterial() ] : [ new THREE.ShadowVolumeDynamicMaterial() ] );
}
this.calculateShadowVolumeGeometry();
};
......@@ -17,10 +25,153 @@ THREE.ShadowVolume.prototype.supr = THREE.Mesh.prototype;
/*
* Calculate Geometry
* Calculate Shadow Faces
*/
THREE.ShadowVolume.prototype.calculateShadowVolumeGeometry = function( originalGeometry ) {
THREE.ShadowVolume.prototype.calculateShadowVolumeGeometry = function() {
if( this.geometry.edges && this.geometry.edges.length ) {
var f, fl, face, faceA, faceB, faceAIndex, faceBIndex, vertexA, vertexB;
var faceACombination, faceBCombination;
var faceAvertexAIndex, faceAvertexBIndex, faceBvertexAIndex, faceBvertexBIndex;
var e, el, edge, temp;
var newGeometry = new THREE.Geometry();
var vertices = newGeometry.vertices = this.geometry.vertices;
var faces = newGeometry.faces = this.geometry.faces;
var edges = newGeometry.egdes = this.geometry.edges;
var edgeFaces = newGeometry.edgeFaces = [];
var vertexOffset = 0;
var vertexOffsetPerFace = [];
for( f = 0, fl = faces.length; f < fl; f++ ) {
face = faces[ f ];
// calculate faces vertex offset
vertexOffsetPerFace.push( vertexOffset );
vertexOffset += face instanceof THREE.Face3 ? 3 : 4;
// set vertex normals to face normal
face.vertexNormals[ 0 ] = face.normal;
face.vertexNormals[ 1 ] = face.normal;
face.vertexNormals[ 2 ] = face.normal;
if( face instanceof THREE.Face4 ) face.vertexNormals[ 3 ] = face.normal;
}
// setup edge faces
for( e = 0, el = edges.length; e < el; e++ ) {
edge = edges[ e ];
faceA = edge.faces[ 0 ];
faceB = edge.faces[ 1 ];
faceAIndex = edge.faceIndices[ 0 ];
faceBIndex = edge.faceIndices[ 1 ];
vertexA = edge.vertexIndices[ 0 ];
vertexB = edge.vertexIndices[ 1 ];
// find combination and processed vertex index (vertices are split up by renderer)
if( faceA.a === vertexA ) { faceACombination = "a"; faceAvertexAIndex = vertexOffsetPerFace[ faceAIndex ] + 0; }
else if( faceA.b === vertexA ) { faceACombination = "b"; faceAvertexAIndex = vertexOffsetPerFace[ faceAIndex ] + 1; }
else if( faceA.c === vertexA ) { faceACombination = "c"; faceAvertexAIndex = vertexOffsetPerFace[ faceAIndex ] + 2; }
else if( faceA.d === vertexA ) { faceACombination = "d"; faceAvertexAIndex = vertexOffsetPerFace[ faceAIndex ] + 3; }
if( faceA.a === vertexB ) { faceACombination += "a"; faceAvertexBIndex = vertexOffsetPerFace[ faceAIndex ] + 0; }
else if( faceA.b === vertexB ) { faceACombination += "b"; faceAvertexBIndex = vertexOffsetPerFace[ faceAIndex ] + 1; }
else if( faceA.c === vertexB ) { faceACombination += "c"; faceAvertexBIndex = vertexOffsetPerFace[ faceAIndex ] + 2; }
else if( faceA.d === vertexB ) { faceACombination += "d"; faceAvertexBIndex = vertexOffsetPerFace[ faceAIndex ] + 3; }
if( faceB.a === vertexA ) { faceBCombination = "a"; faceBvertexAIndex = vertexOffsetPerFace[ faceBIndex ] + 0; }
else if( faceB.b === vertexA ) { faceBCombination = "b"; faceBvertexAIndex = vertexOffsetPerFace[ faceBIndex ] + 1; }
else if( faceB.c === vertexA ) { faceBCombination = "c"; faceBvertexAIndex = vertexOffsetPerFace[ faceBIndex ] + 2; }
else if( faceB.d === vertexA ) { faceBCombination = "d"; faceBvertexAIndex = vertexOffsetPerFace[ faceBIndex ] + 3; }
if( faceB.a === vertexB ) { faceBCombination += "a"; faceBvertexBIndex = vertexOffsetPerFace[ faceBIndex ] + 0; }
else if( faceB.b === vertexB ) { faceBCombination += "b"; faceBvertexBIndex = vertexOffsetPerFace[ faceBIndex ] + 1; }
else if( faceB.c === vertexB ) { faceBCombination += "c"; faceBvertexBIndex = vertexOffsetPerFace[ faceBIndex ] + 2; }
else if( faceB.d === vertexB ) { faceBCombination += "d"; faceBvertexBIndex = vertexOffsetPerFace[ faceBIndex ] + 3; }
if( faceACombination === "ac" ||
faceACombination === "ad" ||
faceACombination === "ca" ||
faceACombination === "da" ) {
if( faceAvertexAIndex > faceAvertexBIndex ) {
temp = faceAvertexAIndex;
faceAvertexAIndex = faceAvertexBIndex;
faceAvertexBIndex = temp;
}
} else {
if( faceAvertexAIndex < faceAvertexBIndex ) {
temp = faceAvertexAIndex;
faceAvertexAIndex = faceAvertexBIndex;
faceAvertexBIndex = temp;
}
}
if( faceBCombination === "ac" ||
faceBCombination === "ad" ||
faceBCombination === "ca" ||
faceBCombination === "da" ) {
if( faceBvertexAIndex > faceBvertexBIndex ) {
temp = faceBvertexAIndex;
faceBvertexAIndex = faceBvertexBIndex;
faceBvertexBIndex = temp;
}
} else {
if( faceBvertexAIndex < faceBvertexBIndex ) {
temp = faceBvertexAIndex;
faceBvertexAIndex = faceBvertexBIndex;
faceBvertexBIndex = temp;
}
}
face = new THREE.Face4( faceAvertexAIndex, faceAvertexBIndex, faceBvertexAIndex, faceBvertexBIndex );
face.normal.set( 1, 0, 0 );
edgeFaces.push( face );
}
this.geometry = newGeometry;
} else {
this.calculateShadowVolumeGeometryWithoutEdgeInfo( this.geometry );
}
}
THREE.ShadowVolume.prototype.calculateShadowVolumeGeometryWithoutEdgeInfo = function( originalGeometry ) {
// create geometry
......@@ -103,11 +254,6 @@ THREE.ShadowVolume.prototype.calculateShadowVolumeGeometry = function( originalG
};
/*
* Faces share edge?
*/
THREE.ShadowVolume.prototype.facesShareEdge = function( vertices, faceA, faceB ) {
var indicesA,
......@@ -199,4 +345,3 @@ THREE.ShadowVolume.prototype.facesShareEdge = function( vertices, faceA, faceB )
return undefined;
};
......@@ -748,7 +748,7 @@ THREE.WebGLRenderer = function ( parameters ) {
obj_skinVerticesB = geometry.skinVerticesB,
obj_skinIndices = geometry.skinIndices,
obj_skinWeights = geometry.skinWeights,
obj_edgeFaces = geometry.edgeFaces,
obj_edgeFaces = object instanceof THREE.ShadowVolume ? geometry.edgeFaces : undefined;
morphTargets = geometry.morphTargets;
......@@ -2413,9 +2413,9 @@ THREE.WebGLRenderer = function ( parameters ) {
var dirLight = m_uniforms.directionalLightDirection.value;
dirLight[ 0 ] = -lights.position.x;
dirLight[ 1 ] = -lights.position.y;
dirLight[ 2 ] = -lights.position.z;
dirLight[ 0 ] = -lights[ 1 ].position.x;
dirLight[ 1 ] = -lights[ 1 ].position.y;
dirLight[ 2 ] = -lights[ 1 ].position.z;
_gl.uniform3fv( p_uniforms.directionalLightDirection, dirLight );
_gl.uniformMatrix4fv( p_uniforms.objectMatrix, false, object._objectMatrixArray );
......
......@@ -852,6 +852,7 @@ THREE.ShaderLib = {
"vec4 pos = objectMatrix * vec4( position, 1.0 );",
"vec3 norm = mat3( objectMatrix[0].xyz, objectMatrix[1].xyz, objectMatrix[2].xyz ) * normal;",
"vec4 extruded = vec4( directionalLightDirection * 5000.0 * step( 0.0, dot( directionalLightDirection, norm )), 0.0 );",
"gl_Position = projectionMatrix * viewMatrix * ( pos + extruded );",
"}"
......@@ -861,7 +862,7 @@ THREE.ShaderLib = {
"void main() {",
"gl_FragColor = vec4( 1, 1, 1, 1 );",
"gl_FragColor = vec4( 1.0 );",
"}"
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册