three.js update

pull/29/head
Michael Aschauer 2017-04-28 17:16:16 +02:00
rodzic 34f09731c5
commit 01cc431369
6 zmienionych plików z 5382 dodań i 4458 usunięć

Wyświetl plik

@ -9,7 +9,7 @@ THREE.SpriteCanvasMaterial = function ( parameters ) {
this.type = 'SpriteCanvasMaterial';
this.color = new THREE.Color( 0xffffff );
this.program = function ( context, color ) {};
this.program = function () {};
this.setValues( parameters );
@ -17,6 +17,7 @@ THREE.SpriteCanvasMaterial = function ( parameters ) {
THREE.SpriteCanvasMaterial.prototype = Object.create( THREE.Material.prototype );
THREE.SpriteCanvasMaterial.prototype.constructor = THREE.SpriteCanvasMaterial;
THREE.SpriteCanvasMaterial.prototype.isSpriteCanvasMaterial = true;
THREE.SpriteCanvasMaterial.prototype.clone = function () {
@ -39,78 +40,69 @@ THREE.CanvasRenderer = function ( parameters ) {
parameters = parameters || {};
var _this = this,
_renderData, _elements, _lights,
_projector = new THREE.Projector(),
_renderData, _elements, _lights,
_projector = new THREE.Projector(),
_canvas = parameters.canvas !== undefined
? parameters.canvas
: document.createElement( 'canvas' ),
_canvas = parameters.canvas !== undefined
? parameters.canvas
: document.createElement( 'canvas' ),
_canvasWidth = _canvas.width,
_canvasHeight = _canvas.height,
_canvasWidthHalf = Math.floor( _canvasWidth / 2 ),
_canvasHeightHalf = Math.floor( _canvasHeight / 2 ),
_canvasWidth = _canvas.width,
_canvasHeight = _canvas.height,
_canvasWidthHalf = Math.floor( _canvasWidth / 2 ),
_canvasHeightHalf = Math.floor( _canvasHeight / 2 ),
_viewportX = 0,
_viewportY = 0,
_viewportWidth = _canvasWidth,
_viewportHeight = _canvasHeight,
_viewportX = 0,
_viewportY = 0,
_viewportWidth = _canvasWidth,
_viewportHeight = _canvasHeight,
_pixelRatio = 1,
_pixelRatio = 1,
_context = _canvas.getContext( '2d', {
alpha: parameters.alpha === true
} ),
_context = _canvas.getContext( '2d', {
alpha: parameters.alpha === true
} ),
_clearColor = new THREE.Color( 0x000000 ),
_clearAlpha = parameters.alpha === true ? 0 : 1,
_clearColor = new THREE.Color( 0x000000 ),
_clearAlpha = parameters.alpha === true ? 0 : 1,
_contextGlobalAlpha = 1,
_contextGlobalCompositeOperation = 0,
_contextStrokeStyle = null,
_contextFillStyle = null,
_contextLineWidth = null,
_contextLineCap = null,
_contextLineJoin = null,
_contextLineDash = [],
_contextGlobalAlpha = 1,
_contextGlobalCompositeOperation = 0,
_contextStrokeStyle = null,
_contextFillStyle = null,
_contextLineWidth = null,
_contextLineCap = null,
_contextLineJoin = null,
_contextLineDash = [],
_camera,
_v1, _v2, _v3,
_v1, _v2, _v3, _v4,
_v5 = new THREE.RenderableVertex(),
_v6 = new THREE.RenderableVertex(),
_v1x, _v1y, _v2x, _v2y, _v3x, _v3y,
_v1x, _v1y, _v2x, _v2y, _v3x, _v3y,
_v4x, _v4y, _v5x, _v5y, _v6x, _v6y,
_color = new THREE.Color(),
_color = new THREE.Color(),
_color1 = new THREE.Color(),
_color2 = new THREE.Color(),
_color3 = new THREE.Color(),
_color4 = new THREE.Color(),
_diffuseColor = new THREE.Color(),
_emissiveColor = new THREE.Color(),
_diffuseColor = new THREE.Color(),
_emissiveColor = new THREE.Color(),
_lightColor = new THREE.Color(),
_lightColor = new THREE.Color(),
_patterns = {},
_patterns = {},
_uvs,
_uv1x, _uv1y, _uv2x, _uv2y, _uv3x, _uv3y,
_image, _uvs,
_uv1x, _uv1y, _uv2x, _uv2y, _uv3x, _uv3y,
_clipBox = new THREE.Box2(),
_clearBox = new THREE.Box2(),
_elemBox = new THREE.Box2(),
_clipBox = new THREE.Box2(),
_clearBox = new THREE.Box2(),
_elemBox = new THREE.Box2(),
_ambientLight = new THREE.Color(),
_directionalLights = new THREE.Color(),
_pointLights = new THREE.Color(),
_ambientLight = new THREE.Color(),
_directionalLights = new THREE.Color(),
_pointLights = new THREE.Color(),
_vector3 = new THREE.Vector3(), // Needed for PointLight
_centroid = new THREE.Vector3(),
_normal = new THREE.Vector3(),
_normalViewMatrix = new THREE.Matrix3();
_vector3 = new THREE.Vector3(), // Needed for PointLight
_centroid = new THREE.Vector3(),
_normal = new THREE.Vector3(),
_normalViewMatrix = new THREE.Matrix3();
/* TODO
_canvas.mozImageSmoothingEnabled = false;
@ -266,10 +258,10 @@ THREE.CanvasRenderer = function ( parameters ) {
_clearBox.intersect( _clipBox );
_clearBox.expandByScalar( 2 );
_clearBox.min.x = _clearBox.min.x + _canvasWidthHalf;
_clearBox.min.y = - _clearBox.min.y + _canvasHeightHalf; // higher y value !
_clearBox.max.x = _clearBox.max.x + _canvasWidthHalf;
_clearBox.max.y = - _clearBox.max.y + _canvasHeightHalf; // lower y value !
_clearBox.min.x = _clearBox.min.x + _canvasWidthHalf;
_clearBox.min.y = - _clearBox.min.y + _canvasHeightHalf; // higher y value !
_clearBox.max.x = _clearBox.max.x + _canvasWidthHalf;
_clearBox.max.y = - _clearBox.max.y + _canvasHeightHalf; // lower y value !
if ( _clearAlpha < 1 ) {
@ -312,7 +304,7 @@ THREE.CanvasRenderer = function ( parameters ) {
this.render = function ( scene, camera ) {
if ( camera instanceof THREE.Camera === false ) {
if ( camera.isCamera === undefined ) {
console.error( 'THREE.CanvasRenderer.render: camera is not an instance of THREE.Camera.' );
return;
@ -341,7 +333,6 @@ THREE.CanvasRenderer = function ( parameters ) {
_renderData = _projector.projectScene( scene, camera, this.sortObjects, this.sortElements );
_elements = _renderData.elements;
_lights = _renderData.lights;
_camera = camera;
_normalViewMatrix.getNormalMatrix( camera.matrixWorldInverse );
@ -454,17 +445,17 @@ THREE.CanvasRenderer = function ( parameters ) {
var light = _lights[ l ];
var lightColor = light.color;
if ( light instanceof THREE.AmbientLight ) {
if ( light.isAmbientLight ) {
_ambientLight.add( lightColor );
} else if ( light instanceof THREE.DirectionalLight ) {
} else if ( light.isDirectionalLight ) {
// for sprites
_directionalLights.add( lightColor );
} else if ( light instanceof THREE.PointLight ) {
} else if ( light.isPointLight ) {
// for sprites
@ -484,7 +475,7 @@ THREE.CanvasRenderer = function ( parameters ) {
_lightColor.copy( light.color );
if ( light instanceof THREE.DirectionalLight ) {
if ( light.isDirectionalLight ) {
var lightPosition = _vector3.setFromMatrixPosition( light.matrixWorld ).normalize();
@ -496,7 +487,7 @@ THREE.CanvasRenderer = function ( parameters ) {
color.add( _lightColor.multiplyScalar( amount ) );
} else if ( light instanceof THREE.PointLight ) {
} else if ( light.isPointLight ) {
var lightPosition = _vector3.setFromMatrixPosition( light.matrixWorld );
@ -526,11 +517,11 @@ THREE.CanvasRenderer = function ( parameters ) {
var scaleX = element.scale.x * _canvasWidthHalf;
var scaleY = element.scale.y * _canvasHeightHalf;
var dist = 0.5 * Math.sqrt( scaleX * scaleX + scaleY * scaleY ); // allow for rotated sprite
var dist = Math.sqrt( scaleX * scaleX + scaleY * scaleY ); // allow for rotated sprite
_elemBox.min.set( v1.x - dist, v1.y - dist );
_elemBox.max.set( v1.x + dist, v1.y + dist );
if ( material instanceof THREE.SpriteMaterial ) {
if ( material.isSpriteMaterial ) {
var texture = material.map;
@ -586,7 +577,7 @@ THREE.CanvasRenderer = function ( parameters ) {
}
} else if ( material instanceof THREE.SpriteCanvasMaterial ) {
} else if ( material.isSpriteCanvasMaterial ) {
setStrokeStyle( material.color.getStyle() );
setFillStyle( material.color.getStyle() );
@ -600,6 +591,17 @@ THREE.CanvasRenderer = function ( parameters ) {
_context.restore();
} else if ( material.isPointsMaterial ) {
setFillStyle( material.color.getStyle() );
_context.save();
_context.translate( v1.x, v1.y );
if ( material.rotation !== 0 ) _context.rotate( material.rotation );
_context.scale( scaleX * material.size, - scaleY * material.size );
_context.fillRect( - 0.5, - 0.5, 1, 1 );
_context.restore();
}
/* DEBUG
@ -623,7 +625,7 @@ THREE.CanvasRenderer = function ( parameters ) {
_context.moveTo( v1.positionScreen.x, v1.positionScreen.y );
_context.lineTo( v2.positionScreen.x, v2.positionScreen.y );
if ( material instanceof THREE.LineBasicMaterial ) {
if ( material.isLineBasicMaterial ) {
setLineWidth( material.linewidth );
setLineCap( material.linecap );
@ -670,7 +672,7 @@ THREE.CanvasRenderer = function ( parameters ) {
_context.stroke();
_elemBox.expandByScalar( material.linewidth * 2 );
} else if ( material instanceof THREE.LineDashedMaterial ) {
} else if ( material.isLineDashedMaterial ) {
setLineWidth( material.linewidth );
setLineCap( material.linecap );
@ -702,7 +704,7 @@ THREE.CanvasRenderer = function ( parameters ) {
drawTriangle( _v1x, _v1y, _v2x, _v2y, _v3x, _v3y );
if ( ( material instanceof THREE.MeshLambertMaterial || material instanceof THREE.MeshPhongMaterial ) && material.map === null ) {
if ( ( material.isMeshLambertMaterial || material.isMeshPhongMaterial || material.isMeshStandardMaterial ) && material.map === null ) {
_diffuseColor.copy( material.color );
_emissiveColor.copy( material.emissive );
@ -725,9 +727,7 @@ THREE.CanvasRenderer = function ( parameters ) {
? strokePath( _color, material.wireframeLinewidth, material.wireframeLinecap, material.wireframeLinejoin )
: fillPath( _color );
} else if ( material instanceof THREE.MeshBasicMaterial ||
material instanceof THREE.MeshLambertMaterial ||
material instanceof THREE.MeshPhongMaterial ) {
} else if ( material.isMeshBasicMaterial || material.isMeshLambertMaterial || material.isMeshPhongMaterial || material.isMeshStandardMaterial ) {
if ( material.map !== null ) {
@ -776,7 +776,7 @@ THREE.CanvasRenderer = function ( parameters ) {
}
} else if ( material instanceof THREE.MeshNormalMaterial ) {
} else if ( material.isMeshNormalMaterial ) {
_normal.copy( element.normalModel ).applyMatrix3( _normalViewMatrix );
@ -944,10 +944,10 @@ THREE.CanvasRenderer = function ( parameters ) {
// http://extremelysatisfactorytotalitarianism.com/blog/?p=2120
var a, b, c, d, e, f, det, idet,
offsetX = texture.offset.x / texture.repeat.x,
offsetY = texture.offset.y / texture.repeat.y,
width = texture.image.width * texture.repeat.x,
height = texture.image.height * texture.repeat.y;
offsetX = texture.offset.x / texture.repeat.x,
offsetY = texture.offset.y / texture.repeat.y,
width = texture.image.width * texture.repeat.x,
height = texture.image.height * texture.repeat.y;
u0 = ( u0 + offsetX ) * width;
v0 = ( v0 + offsetY ) * height;
@ -985,6 +985,7 @@ THREE.CanvasRenderer = function ( parameters ) {
}
/*
function clipImage( x0, y0, x1, y1, x2, y2, u0, v0, u1, v1, u2, v2, image ) {
// http://extremelysatisfactorytotalitarianism.com/blog/?p=2120
@ -1022,13 +1023,14 @@ THREE.CanvasRenderer = function ( parameters ) {
_context.restore();
}
*/
// Hide anti-alias gaps
function expand( v1, v2, pixels ) {
var x = v2.x - v1.x, y = v2.y - v1.y,
det = x * x + y * y, idet;
det = x * x + y * y, idet;
if ( det === 0 ) return;

Wyświetl plik

@ -720,8 +720,7 @@ THREE.OBJLoader.prototype = {
}
var multiMaterial = new THREE.MultiMaterial( createdMaterials );
mesh = ( ! isLine ? new THREE.Mesh( buffergeometry, multiMaterial ) : new THREE.LineSegments( buffergeometry, multiMaterial ) );
mesh = ( ! isLine ? new THREE.Mesh( buffergeometry, createdMaterials ) : new THREE.LineSegments( buffergeometry, createdMaterials ) );
} else {

Wyświetl plik

@ -99,33 +99,32 @@ THREE.RenderableSprite = function () {
THREE.Projector = function () {
var _object, _objectCount, _objectPool = [], _objectPoolLength = 0,
_vertex, _vertexCount, _vertexPool = [], _vertexPoolLength = 0,
_face, _faceCount, _facePool = [], _facePoolLength = 0,
_line, _lineCount, _linePool = [], _linePoolLength = 0,
_sprite, _spriteCount, _spritePool = [], _spritePoolLength = 0,
_vertex, _vertexCount, _vertexPool = [], _vertexPoolLength = 0,
_face, _faceCount, _facePool = [], _facePoolLength = 0,
_line, _lineCount, _linePool = [], _linePoolLength = 0,
_sprite, _spriteCount, _spritePool = [], _spritePoolLength = 0,
_renderData = { objects: [], lights: [], elements: [] },
_renderData = { objects: [], lights: [], elements: [] },
_vector3 = new THREE.Vector3(),
_vector4 = new THREE.Vector4(),
_vector3 = new THREE.Vector3(),
_vector4 = new THREE.Vector4(),
_clipBox = new THREE.Box3( new THREE.Vector3( - 1, - 1, - 1 ), new THREE.Vector3( 1, 1, 1 ) ),
_boundingBox = new THREE.Box3(),
_points3 = new Array( 3 ),
_points4 = new Array( 4 ),
_clipBox = new THREE.Box3( new THREE.Vector3( - 1, - 1, - 1 ), new THREE.Vector3( 1, 1, 1 ) ),
_boundingBox = new THREE.Box3(),
_points3 = new Array( 3 ),
_viewMatrix = new THREE.Matrix4(),
_viewProjectionMatrix = new THREE.Matrix4(),
_viewMatrix = new THREE.Matrix4(),
_viewProjectionMatrix = new THREE.Matrix4(),
_modelMatrix,
_modelViewProjectionMatrix = new THREE.Matrix4(),
_modelMatrix,
_modelViewProjectionMatrix = new THREE.Matrix4(),
_normalMatrix = new THREE.Matrix3(),
_normalMatrix = new THREE.Matrix3(),
_frustum = new THREE.Frustum(),
_frustum = new THREE.Frustum(),
_clippedVertex1PositionScreen = new THREE.Vector4(),
_clippedVertex2PositionScreen = new THREE.Vector4();
_clippedVertex1PositionScreen = new THREE.Vector4(),
_clippedVertex2PositionScreen = new THREE.Vector4();
//
@ -143,7 +142,7 @@ THREE.Projector = function () {
};
this.pickingRay = function ( vector, camera ) {
this.pickingRay = function () {
console.error( 'THREE.Projector: .pickingRay() is now raycaster.setFromCamera().' );
@ -154,6 +153,7 @@ THREE.Projector = function () {
var RenderList = function () {
var normals = [];
var colors = [];
var uvs = [];
var object = null;
@ -169,6 +169,7 @@ THREE.Projector = function () {
normalMatrix.getNormalMatrix( object.matrixWorld );
normals.length = 0;
colors.length = 0;
uvs.length = 0;
}
@ -209,6 +210,12 @@ THREE.Projector = function () {
}
function pushColor( r, g, b ) {
colors.push( r, g, b );
}
function pushUv( x, y ) {
uvs.push( x, y );
@ -241,17 +248,36 @@ THREE.Projector = function () {
var v1 = _vertexPool[ a ];
var v2 = _vertexPool[ b ];
_line = getNextLineInPool();
// Clip
_line.id = object.id;
_line.v1.copy( v1 );
_line.v2.copy( v2 );
_line.z = ( v1.positionScreen.z + v2.positionScreen.z ) / 2;
_line.renderOrder = object.renderOrder;
v1.positionScreen.copy( v1.position ).applyMatrix4( _modelViewProjectionMatrix );
v2.positionScreen.copy( v2.position ).applyMatrix4( _modelViewProjectionMatrix );
_line.material = object.material;
if ( clipLine( v1.positionScreen, v2.positionScreen ) === true ) {
_renderData.elements.push( _line );
// Perform the perspective divide
v1.positionScreen.multiplyScalar( 1 / v1.positionScreen.w );
v2.positionScreen.multiplyScalar( 1 / v2.positionScreen.w );
_line = getNextLineInPool();
_line.id = object.id;
_line.v1.copy( v1 );
_line.v2.copy( v2 );
_line.z = Math.max( v1.positionScreen.z, v2.positionScreen.z );
_line.renderOrder = object.renderOrder;
_line.material = object.material;
if ( object.material.vertexColors === THREE.VertexColors ) {
_line.vertexColors[ 0 ].fromArray( colors, a * 3 );
_line.vertexColors[ 1 ].fromArray( colors, b * 3 );
}
_renderData.elements.push( _line );
}
}
@ -307,15 +333,65 @@ THREE.Projector = function () {
checkBackfaceCulling: checkBackfaceCulling,
pushVertex: pushVertex,
pushNormal: pushNormal,
pushColor: pushColor,
pushUv: pushUv,
pushLine: pushLine,
pushTriangle: pushTriangle
}
};
};
var renderList = new RenderList();
function projectObject( object ) {
if ( object.visible === false ) return;
if ( object instanceof THREE.Light ) {
_renderData.lights.push( object );
} else if ( object instanceof THREE.Mesh || object instanceof THREE.Line || object instanceof THREE.Points ) {
if ( object.material.visible === false ) return;
if ( object.frustumCulled === true && _frustum.intersectsObject( object ) === false ) return;
addObject( object );
} else if ( object instanceof THREE.Sprite ) {
if ( object.material.visible === false ) return;
if ( object.frustumCulled === true && _frustum.intersectsSprite( object ) === false ) return;
addObject( object );
}
var children = object.children;
for ( var i = 0, l = children.length; i < l; i ++ ) {
projectObject( children[ i ] );
}
}
function addObject( object ) {
_object = getNextObjectInPool();
_object.id = object.id;
_object.object = object;
_vector3.setFromMatrixPosition( object.matrixWorld );
_vector3.applyMatrix4( _viewProjectionMatrix );
_object.z = _vector3.z;
_object.renderOrder = object.renderOrder;
_renderData.objects.push( _object );
}
this.projectScene = function ( scene, camera, sortObjects, sortElements ) {
_faceCount = 0;
@ -339,44 +415,7 @@ THREE.Projector = function () {
_renderData.objects.length = 0;
_renderData.lights.length = 0;
function addObject( object ) {
_object = getNextObjectInPool();
_object.id = object.id;
_object.object = object;
_vector3.setFromMatrixPosition( object.matrixWorld );
_vector3.applyMatrix4( _viewProjectionMatrix );
_object.z = _vector3.z;
_object.renderOrder = object.renderOrder;
_renderData.objects.push( _object );
}
scene.traverseVisible( function ( object ) {
if ( object instanceof THREE.Light ) {
_renderData.lights.push( object );
} else if ( object instanceof THREE.Mesh || object instanceof THREE.Line ) {
if ( object.material.visible === false ) return;
if ( object.frustumCulled === true && _frustum.intersectsObject( object ) === false ) return;
addObject( object );
} else if ( object instanceof THREE.Sprite ) {
if ( object.material.visible === false ) return;
if ( object.frustumCulled === true && _frustum.intersectsSprite( object ) === false ) return;
addObject( object );
}
} );
projectObject( scene );
if ( sortObjects === true ) {
@ -386,9 +425,11 @@ THREE.Projector = function () {
//
for ( var o = 0, ol = _renderData.objects.length; o < ol; o ++ ) {
var objects = _renderData.objects;
var object = _renderData.objects[ o ].object;
for ( var o = 0, ol = objects.length; o < ol; o ++ ) {
var object = objects[ o ].object;
var geometry = object.geometry;
renderList.setObject( object );
@ -486,8 +527,7 @@ THREE.Projector = function () {
var material = object.material;
var isFaceMaterial = material instanceof THREE.MultiMaterial;
var objectMaterials = isFaceMaterial === true ? object.material : null;
var isMultiMaterial = Array.isArray( material );
for ( var v = 0, vl = vertices.length; v < vl; v ++ ) {
@ -525,8 +565,8 @@ THREE.Projector = function () {
var face = faces[ f ];
material = isFaceMaterial === true
? objectMaterials.materials[ face.materialIndex ]
material = isMultiMaterial === true
? object.material[ face.materialIndex ]
: object.material;
if ( material === undefined ) continue;
@ -610,6 +650,8 @@ THREE.Projector = function () {
} else if ( object instanceof THREE.Line ) {
_modelViewProjectionMatrix.multiplyMatrices( _viewProjectionMatrix, _modelMatrix );
if ( geometry instanceof THREE.BufferGeometry ) {
var attributes = geometry.attributes;
@ -624,6 +666,18 @@ THREE.Projector = function () {
}
if ( attributes.color !== undefined ) {
var colors = attributes.color.array;
for ( var i = 0, l = colors.length; i < l; i += 3 ) {
renderList.pushColor( colors[ i ], colors[ i + 1 ], colors[ i + 2 ] );
}
}
if ( geometry.index !== null ) {
var indices = geometry.index.array;
@ -650,8 +704,6 @@ THREE.Projector = function () {
} else if ( geometry instanceof THREE.Geometry ) {
_modelViewProjectionMatrix.multiplyMatrices( _viewProjectionMatrix, _modelMatrix );
var vertices = object.geometry.vertices;
if ( vertices.length === 0 ) continue;
@ -705,35 +757,33 @@ THREE.Projector = function () {
}
} else if ( object instanceof THREE.Points ) {
_modelViewProjectionMatrix.multiplyMatrices( _viewProjectionMatrix, _modelMatrix );
if ( geometry instanceof THREE.Geometry ) {
var vertices = object.geometry.vertices;
for ( var v = 0, vl = vertices.length; v < vl; v ++ ) {
var vertex = vertices[ v ];
_vector4.set( vertex.x, vertex.y, vertex.z, 1 );
_vector4.applyMatrix4( _modelViewProjectionMatrix );
pushPoint( _vector4, object, camera );
}
}
} else if ( object instanceof THREE.Sprite ) {
_vector4.set( _modelMatrix.elements[ 12 ], _modelMatrix.elements[ 13 ], _modelMatrix.elements[ 14 ], 1 );
_vector4.applyMatrix4( _viewProjectionMatrix );
var invW = 1 / _vector4.w;
_vector4.z *= invW;
if ( _vector4.z >= - 1 && _vector4.z <= 1 ) {
_sprite = getNextSpriteInPool();
_sprite.id = object.id;
_sprite.x = _vector4.x * invW;
_sprite.y = _vector4.y * invW;
_sprite.z = _vector4.z;
_sprite.renderOrder = object.renderOrder;
_sprite.object = object;
_sprite.rotation = object.rotation;
_sprite.scale.x = object.scale.x * Math.abs( _sprite.x - ( _vector4.x + camera.projectionMatrix.elements[ 0 ] ) / ( _vector4.w + camera.projectionMatrix.elements[ 12 ] ) );
_sprite.scale.y = object.scale.y * Math.abs( _sprite.y - ( _vector4.y + camera.projectionMatrix.elements[ 5 ] ) / ( _vector4.w + camera.projectionMatrix.elements[ 13 ] ) );
_sprite.material = object.material;
_renderData.elements.push( _sprite );
}
pushPoint( _vector4, object, camera );
}
@ -749,6 +799,35 @@ THREE.Projector = function () {
};
function pushPoint( _vector4, object, camera ) {
var invW = 1 / _vector4.w;
_vector4.z *= invW;
if ( _vector4.z >= - 1 && _vector4.z <= 1 ) {
_sprite = getNextSpriteInPool();
_sprite.id = object.id;
_sprite.x = _vector4.x * invW;
_sprite.y = _vector4.y * invW;
_sprite.z = _vector4.z;
_sprite.renderOrder = object.renderOrder;
_sprite.object = object;
_sprite.rotation = object.rotation;
_sprite.scale.x = object.scale.x * Math.abs( _sprite.x - ( _vector4.x + camera.projectionMatrix.elements[ 0 ] ) / ( _vector4.w + camera.projectionMatrix.elements[ 12 ] ) );
_sprite.scale.y = object.scale.y * Math.abs( _sprite.y - ( _vector4.y + camera.projectionMatrix.elements[ 5 ] ) / ( _vector4.w + camera.projectionMatrix.elements[ 13 ] ) );
_sprite.material = object.material;
_renderData.elements.push( _sprite );
}
}
// Pools
function getNextObjectInPool() {
@ -862,10 +941,11 @@ THREE.Projector = function () {
// Calculate the boundary coordinate of each vertex for the near and far clip planes,
// Z = -1 and Z = +1, respectively.
bc1near = s1.z + s1.w,
bc2near = s2.z + s2.w,
bc1far = - s1.z + s1.w,
bc2far = - s2.z + s2.w;
bc1near = s1.z + s1.w,
bc2near = s2.z + s2.w,
bc1far = - s1.z + s1.w,
bc2far = - s2.z + s2.w;
if ( bc1near >= 0 && bc2near >= 0 && bc1far >= 0 && bc2far >= 0 ) {

Wyświetl plik

@ -78,7 +78,7 @@ THREE.SoftwareRenderer = function ( parameters ) {
this.supportsVertexTextures = function () {};
this.setFaceCulling = function () {};
this.setClearColor = function ( color, alpha ) {
this.setClearColor = function ( color ) {
clearColor.set( color );
clearColorBuffer( clearColor );
@ -179,7 +179,7 @@ THREE.SoftwareRenderer = function ( parameters ) {
var material = element.material;
var shader = getMaterialShader( material );
if ( !shader ) continue;
if ( ! shader ) continue;
if ( element instanceof THREE.RenderableFace ) {
@ -286,8 +286,8 @@ THREE.SoftwareRenderer = function ( parameters ) {
drawLine(
element.v1.positionScreen,
element.v2.positionScreen,
element.vertexColors[0],
element.vertexColors[1],
element.vertexColors[ 0 ],
element.vertexColors[ 1 ],
shader,
material
);
@ -479,6 +479,7 @@ THREE.SoftwareRenderer = function ( parameters ) {
if ( buffer[ colorOffset + 3 ] == 255 ) // Only opaue pixls write to the depth buffer
depthBuf[ offset ] = depth;
}
}
@ -528,16 +529,6 @@ THREE.SoftwareRenderer = function ( parameters ) {
}
function onMaterialUpdate ( event ) {
var material = event.target;
material.removeEventListener( 'update', onMaterialUpdate );
delete shaders[ material.id ];
}
function getMaterialShader( material ) {
var id = material.id;
@ -545,9 +536,7 @@ THREE.SoftwareRenderer = function ( parameters ) {
if ( shader && material.map && !textures[ material.map.id ] ) delete shaders[ id ];
if ( shaders[ id ] === undefined ) {
material.addEventListener( 'update', onMaterialUpdate );
if ( shaders[ id ] === undefined || material.needsUpdate === true ) {
if ( material instanceof THREE.MeshBasicMaterial ||
material instanceof THREE.MeshLambertMaterial ||
@ -581,7 +570,7 @@ THREE.SoftwareRenderer = function ( parameters ) {
var texture = new THREE.SoftwareRenderer.Texture();
texture.fromImage( material.map.image );
if ( !texture.data ) return;
if ( ! texture.data ) return;
textures[ material.map.id ] = texture;
@ -636,7 +625,7 @@ THREE.SoftwareRenderer = function ( parameters ) {
'buffer[ colorOffset + 2 ] = material.color.b * (color1.b+color2.b) * 0.5 * 255;',
'buffer[ colorOffset + 3 ] = 255;',
'depthBuf[ offset ] = depth;'
].join('\n');
].join( '\n' );
shader = new Function( 'buffer, depthBuf, offset, depth, color1, color2, material', string );
@ -657,12 +646,15 @@ THREE.SoftwareRenderer = function ( parameters ) {
shaders[ id ] = shader;
material.needsUpdate = false;
}
return shader;
}
/*
function clearRectangle( x1, y1, x2, y2 ) {
var xmin = Math.max( Math.min( x1, x2 ), 0 );
@ -686,6 +678,7 @@ THREE.SoftwareRenderer = function ( parameters ) {
}
}
*/
function drawTriangle( v1, v2, v3, uv1, uv2, uv3, shader, face, material ) {
@ -712,13 +705,12 @@ THREE.SoftwareRenderer = function ( parameters ) {
var bHasUV = uv1 && uv2 && uv3;
var longestSide = Math.max(
Math.sqrt( (x1 - x2)*(x1 - x2) + (y1 - y2)*(y1 - y2) ),
Math.sqrt( (x2 - x3)*(x2 - x3) + (y2 - y3)*(y2 - y3) ),
Math.sqrt( (x3 - x1)*(x3 - x1) + (y3 - y1)*(y3 - y1) )
Math.sqrt( ( x1 - x2 ) * ( x1 - x2 ) + ( y1 - y2 ) * ( y1 - y2 ) ),
Math.sqrt( ( x2 - x3 ) * ( x2 - x3 ) + ( y2 - y3 ) * ( y2 - y3 ) ),
Math.sqrt( ( x3 - x1 ) * ( x3 - x1 ) + ( y3 - y1 ) * ( y3 - y1 ) )
);
if( !(face instanceof THREE.RenderableSprite)
&& (longestSide > 100 * fixscale) ) {
if ( ! ( face instanceof THREE.RenderableSprite ) && ( longestSide > 100 * fixscale ) ) {
// 1
// |\
@ -728,11 +720,13 @@ THREE.SoftwareRenderer = function ( parameters ) {
// |b\|d\
// |__\__\
// 2 3
var tempFace = { vertexNormalsModel : [],
color : face.color };
var tempFace = { vertexNormalsModel: [], color: face.color };
var mpUV12, mpUV23, mpUV31;
if ( bHasUV ) {
if ( mpUVPoolCount === mpUVPool.length ) {
mpUV12 = new THREE.Vector2();
mpUVPool.push( mpUV12 );
++mpUVPoolCount;
@ -744,27 +738,37 @@ THREE.SoftwareRenderer = function ( parameters ) {
mpUV31 = new THREE.Vector2();
mpUVPool.push( mpUV31 );
++mpUVPoolCount;
} else {
mpUV12 = mpUVPool[ mpUVPoolCount ];
++mpUVPoolCount;
mpUV23 = mpUVPool[ mpUVPoolCount ];
++mpUVPoolCount;
mpUV31 = mpUVPool[ mpUVPoolCount ];
++mpUVPoolCount;
}
var weight;
weight = (1 + v2.z) * (v2.w / v1.w) / (1 + v1.z);
mpUV12.copy( uv1 ).multiplyScalar( weight ).add( uv2 ).multiplyScalar( 1 / (weight + 1) );
weight = (1 + v3.z) * (v3.w / v2.w) / (1 + v2.z);
mpUV23.copy( uv2 ).multiplyScalar( weight ).add( uv3 ).multiplyScalar( 1 / (weight + 1) );
weight = (1 + v1.z) * (v1.w / v3.w) / (1 + v3.z);
mpUV31.copy( uv3 ).multiplyScalar( weight ).add( uv1 ).multiplyScalar( 1 / (weight + 1) );
weight = ( 1 + v2.z ) * ( v2.w / v1.w ) / ( 1 + v1.z );
mpUV12.copy( uv1 ).multiplyScalar( weight ).add( uv2 ).multiplyScalar( 1 / ( weight + 1 ) );
weight = ( 1 + v3.z ) * ( v3.w / v2.w ) / ( 1 + v2.z );
mpUV23.copy( uv2 ).multiplyScalar( weight ).add( uv3 ).multiplyScalar( 1 / ( weight + 1 ) );
weight = ( 1 + v1.z ) * ( v1.w / v3.w ) / ( 1 + v3.z );
mpUV31.copy( uv3 ).multiplyScalar( weight ).add( uv1 ).multiplyScalar( 1 / ( weight + 1 ) );
}
var mpV12, mpV23, mpV31;
if ( mpVPoolCount === mpVPool.length ) {
mpV12 = new THREE.Vector4();
mpVPool.push( mpV12 );
++mpVPoolCount;
@ -776,13 +780,18 @@ THREE.SoftwareRenderer = function ( parameters ) {
mpV31 = new THREE.Vector4();
mpVPool.push( mpV31 );
++mpVPoolCount;
} else {
mpV12 = mpVPool[ mpVPoolCount ];
++mpVPoolCount;
mpV23 = mpVPool[ mpVPoolCount ];
++mpVPoolCount;
mpV31 = mpVPool[ mpVPoolCount ];
++mpVPoolCount;
}
mpV12.copy( v1 ).add( v2 ).multiplyScalar( 0.5 );
@ -790,8 +799,11 @@ THREE.SoftwareRenderer = function ( parameters ) {
mpV31.copy( v3 ).add( v1 ).multiplyScalar( 0.5 );
var mpN12, mpN23, mpN31;
if( bHasNormal ) {
if ( bHasNormal ) {
if ( mpNPoolCount === mpNPool.length ) {
mpN12 = new THREE.Vector3();
mpNPool.push( mpN12 );
++mpNPoolCount;
@ -803,53 +815,72 @@ THREE.SoftwareRenderer = function ( parameters ) {
mpN31 = new THREE.Vector3();
mpNPool.push( mpN31 );
++mpNPoolCount;
} else {
mpN12 = mpNPool[ mpNPoolCount ];
++mpNPoolCount;
mpN23 = mpNPool[ mpNPoolCount ];
++mpNPoolCount;
mpN31 = mpNPool[ mpNPoolCount ];
++mpNPoolCount;
}
mpN12.copy( face.vertexNormalsModel[ 0 ] ).add( face.vertexNormalsModel[ 1 ] ).normalize();
mpN23.copy( face.vertexNormalsModel[ 1 ] ).add( face.vertexNormalsModel[ 2 ] ).normalize();
mpN31.copy( face.vertexNormalsModel[ 2 ] ).add( face.vertexNormalsModel[ 0 ] ).normalize();
}
// a
if( bHasNormal ) {
if ( bHasNormal ) {
tempFace.vertexNormalsModel[ 0 ] = face.vertexNormalsModel[ 0 ];
tempFace.vertexNormalsModel[ 1 ] = mpN12;
tempFace.vertexNormalsModel[ 2 ] = mpN31;
}
drawTriangle( v1, mpV12, mpV31, uv1, mpUV12, mpUV31, shader, tempFace, material );
// b
if( bHasNormal ) {
if ( bHasNormal ) {
tempFace.vertexNormalsModel[ 0 ] = face.vertexNormalsModel[ 1 ];
tempFace.vertexNormalsModel[ 1 ] = mpN23;
tempFace.vertexNormalsModel[ 2 ] = mpN12;
}
drawTriangle( v2, mpV23, mpV12, uv2, mpUV23, mpUV12, shader, tempFace, material );
// c
if( bHasNormal ) {
if ( bHasNormal ) {
tempFace.vertexNormalsModel[ 0 ] = mpN12;
tempFace.vertexNormalsModel[ 1 ] = mpN23;
tempFace.vertexNormalsModel[ 2 ] = mpN31;
}
drawTriangle( mpV12, mpV23, mpV31, mpUV12, mpUV23, mpUV31, shader, tempFace, material );
// d
if( bHasNormal ) {
if ( bHasNormal ) {
tempFace.vertexNormalsModel[ 0 ] = face.vertexNormalsModel[ 2 ];
tempFace.vertexNormalsModel[ 1 ] = mpN31;
tempFace.vertexNormalsModel[ 2 ] = mpN23;
}
drawTriangle( v3, mpV31, mpV23, uv3, mpUV31, mpUV23, shader, tempFace, material );
return;
}
// Z values (.28 fixed-point)
@ -976,7 +1007,8 @@ THREE.SoftwareRenderer = function ( parameters ) {
}
var dnxdx, dnzdy, cbnz;
var dnzdy, cbnz;
if ( bHasNormal ) {
// Normal interpolation setup
@ -1369,16 +1401,18 @@ THREE.SoftwareRenderer = function ( parameters ) {
function drawLine( v1, v2, color1, color2, shader, material ) {
// While the line mode is enable, blockSize has to be changed to 0.
if ( !lineMode ) {
if ( ! lineMode ) {
lineMode = true;
blockShift = 0;
blockSize = 1 << blockShift;
setSize( canvas.width, canvas.height );
}
// TODO: Implement per-pixel z-clipping
if ( v1.z < -1 || v1.z > 1 || v2.z < -1 || v2.z > 1 ) return;
if ( v1.z < - 1 || v1.z > 1 || v2.z < - 1 || v2.z > 1 ) return;
var halfLineWidth = Math.floor( ( material.linewidth - 1 ) * 0.5 );
@ -1422,7 +1456,7 @@ THREE.SoftwareRenderer = function ( parameters ) {
crossVector.cross( lookVector );
crossVector.normalize();
while (length > 0) {
while ( length > 0 ) {
// Get this pixel.
pixelX = x2 + length * unitX;
@ -1434,7 +1468,7 @@ THREE.SoftwareRenderer = function ( parameters ) {
pZ = ( pixelZ + subpixelBias ) >> subpixelBits;
// Draw line with line width
for ( var i = -halfLineWidth; i <= halfLineWidth; ++i ) {
for ( var i = - halfLineWidth; i <= halfLineWidth; ++ i ) {
// Compute the line pixels.
// Get the pixels on the vector that crosses to the line vector
@ -1442,9 +1476,8 @@ THREE.SoftwareRenderer = function ( parameters ) {
pY = Math.floor( ( pixelY + crossVector.y * i ) );
// if pixel is over the rect. Continue
if ( rectx1 >= pX || rectx2 <= pX || recty1 >= pY
|| recty2 <= pY )
continue;
if ( rectx1 >= pX || rectx2 <= pX || recty1 >= pY || recty2 <= pY )
continue;
// Find this pixel at which block
var blockX = pX >> blockShift;
@ -1464,11 +1497,15 @@ THREE.SoftwareRenderer = function ( parameters ) {
var offset = pX + pY * canvasWidth;
if ( pZ < zbuffer[ offset ] ) {
shader( data, zbuffer, offset, pZ, color1, color2, material );
}
}
--length;
}
}
@ -1526,11 +1563,11 @@ THREE.SoftwareRenderer = function ( parameters ) {
};
THREE.SoftwareRenderer.Texture = function() {
THREE.SoftwareRenderer.Texture = function () {
var canvas;
this.fromImage = function( image ) {
this.fromImage = function ( image ) {
if ( ! image || image.width <= 0 || image.height <= 0 )
return;

File diff suppressed because one or more lines are too long