blockbench/lib/three_custom.js

287 lines
7.2 KiB
JavaScript
Raw Normal View History

THREE.BufferGeometry.prototype.setShape = function(from, to) {
let {position} = this.attributes;
position.array.set([
to[0], to[1], to[2],
to[0], to[1], from[2],
to[0], from[1], to[2],
to[0], from[1], from[2],
], 0)
position.array.set([
from[0], to[1], to[2],
from[0], to[1], from[2],
from[0], from[1], to[2],
from[0], from[1], from[2],
], 12)
position.array.set([
from[0], to[1], from[2],
to[0], to[1], from[2],
from[0], to[1], to[2],
to[0], to[1], to[2],
], 24)
position.array.set([
from[0], from[1], from[2],
to[0], from[1], from[2],
from[0], from[1], to[2],
to[0], from[1], to[2],
], 36)
position.array.set([
from[0], to[1], to[2],
to[0], to[1], to[2],
from[0], from[1], to[2],
to[0], from[1], to[2],
], 48)
position.array.set([
from[0], to[1], from[2],
to[0], to[1], from[2],
from[0], from[1], from[2],
to[0], from[1], from[2],
], 60)
2021-07-25 04:55:02 +08:00
position.needsUpdate = true;
2018-12-27 21:03:04 +08:00
}
THREE.BoxGeometry.prototype.to = function(arr) {
return;
2018-12-27 21:03:04 +08:00
//X
this.vertices[0].setX(arr[0])
this.vertices[1].setX(arr[0])
this.vertices[2].setX(arr[0])
this.vertices[3].setX(arr[0])
//Y
this.vertices[0].setY(arr[1])
this.vertices[1].setY(arr[1])
this.vertices[4].setY(arr[1])
this.vertices[5].setY(arr[1])
//Z
this.vertices[0].setZ(arr[2])
this.vertices[2].setZ(arr[2])
this.vertices[5].setZ(arr[2])
this.vertices[7].setZ(arr[2])
this.verticesNeedUpdate = true
}
Object.assign( THREE.Euler.prototype, {
setFromDegreeArray: function ( arr, invert ) {
this._x = Math.degToRad(arr[0]) * (invert ? -1 : 1);
this._y = Math.degToRad(arr[1]) * (invert ? -1 : 1);
this._z = Math.degToRad(arr[2]) * (invert ? -1 : 1);
2020-03-05 03:56:17 +08:00
this._onChangeCallback();
2018-12-27 21:03:04 +08:00
return this;
}
})
THREE.Euler.prototype.invert = function () {
2019-08-01 06:01:47 +08:00
var q = new THREE.Quaternion();
return function invert() {
2019-08-01 06:01:47 +08:00
return this.setFromQuaternion( q.setFromEuler( this ).invert() );
2019-08-01 06:01:47 +08:00
};
}();
THREE.Vector3.prototype.removeEuler = function (euler) {
return function removeEuler(euler) {
var invert = new THREE.Euler().copy(euler).invert();
this.applyEuler(invert)
2019-08-01 06:01:47 +08:00
return this;
};
}();
2019-08-18 00:26:14 +08:00
THREE.Vector3.prototype.toString = function() {
return `${this.x}, ${this.y}, ${this.z}`
}
2019-07-18 00:02:07 +08:00
class GridBox extends THREE.LineSegments {
constructor( from, to, size, material) {
var vertices = [];
2019-07-18 00:02:07 +08:00
function getVector2(arr, axis) {
switch (axis) {
case 0: return [arr[1], arr[2]]; break;
case 1: return [arr[0], arr[2]]; break;
case 2: return [arr[0], arr[1]]; break;
}
2019-07-18 00:02:07 +08:00
}
function addVector(u, v, axis, w) {
switch (axis) {
case 0: vertices.push(w, u, v); break;
case 1: vertices.push(u, w, v); break;
case 2: vertices.push(u, v, w); break;
}
2019-07-18 00:02:07 +08:00
}
for (var axis = 0; axis < 3; axis++) {
var start = getVector2(from, axis)
var end = getVector2(to, axis)
var steps = getVector2(size, axis)
for (var side = 0; side < 2; side++) {
var w = side ? from[axis] : to[axis]
//lines
var step = Math.abs( (end[1]-start[1]) / steps[1] );
if (step < 0.0625) step = 0.0625;
for (var line = start[1]; line <= end[1]; line += step) {
addVector(start[0], line, axis, w)
addVector(end[0], line, axis, w)
}
//Columns
var step = Math.abs( (end[0]-start[0]) / steps[0] );
if (step < 0.0625) step = 0.0625;
for (var col = start[0]; col <= end[0]; col += step) {
addVector(col, start[1], axis, w)
addVector(col, end[1], axis, w)
}
2019-07-18 00:02:07 +08:00
}
}
var geometry = new THREE.BufferGeometry();
geometry.addAttribute( 'position', new THREE.Float32BufferAttribute( vertices, 3 ) );
2019-07-18 00:02:07 +08:00
material = material || new THREE.LineBasicMaterial( { color: gizmo_colors.grid } );
2019-07-18 00:02:07 +08:00
//THREE.LineSegments.call( this, geometry, material );
}
2019-07-18 00:02:07 +08:00
}
/*
2019-07-18 00:02:07 +08:00
GridBox.prototype = Object.assign( Object.create( THREE.LineSegments.prototype ), {
constructor: GridBox,
copy: function ( source ) {
THREE.LineSegments.prototype.copy.call( this, source );
this.geometry.copy( source.geometry );
this.material.copy( source.material );
return this;
},
clone: function () {
return new this.constructor().copy( this );
}
} );*/
2019-07-18 00:02:07 +08:00
THREE.GridBox = GridBox
2019-12-16 03:04:31 +08:00
THREE.Object3D.prototype.toScreenPosition = function(camera, canvas)
{
var vector = new THREE.Vector3();
var widthHalf = 0.5*canvas.width;
var heightHalf = 0.5*canvas.height;
this.updateMatrixWorld();
vector.setFromMatrixPosition(this.matrixWorld);
vector.project(camera);
vector.x = ( vector.x * widthHalf ) + widthHalf;
vector.y = - ( vector.y * heightHalf ) + heightHalf;
2020-01-24 01:53:36 +08:00
vector.divideScalar(window.devicePixelRatio);
2019-12-16 03:04:31 +08:00
return {
x: vector.x,
y: vector.y
};
};
THREE.AxesHelper = class AxesHelper extends THREE.LineSegments {
constructor( size ) {
2020-03-05 03:56:17 +08:00
size = size || 1;
2020-03-05 03:56:17 +08:00
var vertices = [
0, 0, 0, size, 0, 0,
0, 0, 0, 0, size, 0,
0, 0, 0, 0, 0, size
];
2020-03-05 03:56:17 +08:00
var c = gizmo_colors
var colors = [
c.r.r, c.r.g, c.r.b, c.r.r, c.r.g, c.r.b,
c.g.r, c.g.g, c.g.b, c.g.r, c.g.g, c.g.b,
c.b.r, c.b.g, c.b.b, c.b.r, c.b.g, c.b.b,
]
2020-03-05 03:56:17 +08:00
var geometry = new THREE.BufferGeometry();
geometry.setAttribute( 'position', new THREE.Float32BufferAttribute( vertices, 3 ) );
geometry.setAttribute( 'color', new THREE.Float32BufferAttribute( colors, 3 ) );
2020-03-05 03:56:17 +08:00
var material = new THREE.LineBasicMaterial( { vertexColors: THREE.VertexColors } );
2020-03-05 03:56:17 +08:00
super(geometry, material);
2020-03-05 03:56:17 +08:00
//THREE.LineSegments.call( this, geometry, material );
}
2020-03-05 03:56:17 +08:00
}
THREE.AxesHelper.prototype = Object.create( THREE.LineSegments.prototype );
THREE.AxesHelper.prototype.constructor = THREE.AxesHelper;
2020-09-17 22:48:14 +08:00
THREE.GridHelper = class GridHelper extends THREE.LineSegments {
2020-03-05 03:56:17 +08:00
2020-09-17 22:48:14 +08:00
constructor( size, divisions, color1, color2 ) {
2020-03-05 03:56:17 +08:00
2020-09-17 22:48:14 +08:00
size = size || 10;
divisions = divisions || 10;
color1 = new THREE.Color( color1 !== undefined ? color1 : 0x444444 );
color2 = new THREE.Color( color2 !== undefined ? color2 : 0x888888 );
2020-03-05 03:56:17 +08:00
2020-09-17 22:48:14 +08:00
const center = divisions / 2;
const step = size / divisions;
const halfSize = size / 2;
2020-03-05 03:56:17 +08:00
2020-09-17 22:48:14 +08:00
const vertices = [], colors = [];
2020-03-05 03:56:17 +08:00
2020-09-17 22:48:14 +08:00
for ( let i = 0, j = 0, k = - halfSize; i <= divisions; i ++, k += step ) {
2020-03-05 03:56:17 +08:00
2020-09-17 22:48:14 +08:00
vertices.push( - halfSize, 0, k, halfSize, 0, k );
vertices.push( k, 0, - halfSize, k, 0, halfSize );
2020-03-05 03:56:17 +08:00
2020-09-17 22:48:14 +08:00
const color = i === center ? color1 : color2;
2020-03-05 03:56:17 +08:00
2020-09-17 22:48:14 +08:00
color.toArray( colors, j ); j += 3;
color.toArray( colors, j ); j += 3;
color.toArray( colors, j ); j += 3;
color.toArray( colors, j ); j += 3;
2020-03-05 03:56:17 +08:00
2020-09-17 22:48:14 +08:00
}
2020-03-05 03:56:17 +08:00
2020-09-17 22:48:14 +08:00
var geometry = new THREE.BufferGeometry();
geometry.setAttribute( 'position', new THREE.Float32BufferAttribute( vertices, 3 ) );
geometry.setAttribute( 'color', new THREE.Float32BufferAttribute( colors, 3 ) );
2020-03-05 03:56:17 +08:00
2020-09-17 22:48:14 +08:00
const material = new THREE.LineBasicMaterial( { color: color1, toneMapped: false } );
2020-03-05 03:56:17 +08:00
2020-09-17 22:48:14 +08:00
super( geometry, material );
2020-03-05 03:56:17 +08:00
2020-09-17 22:48:14 +08:00
this.type = 'GridHelper';
2020-03-05 03:56:17 +08:00
2020-09-17 22:48:14 +08:00
}
2020-03-05 03:56:17 +08:00
2020-09-17 22:48:14 +08:00
}
2020-03-05 03:56:17 +08:00
2019-12-16 03:04:31 +08:00
THREE.NormalX = new THREE.Vector3(1, 0, 0);
THREE.NormalY = new THREE.Vector3(0, 1, 0);
THREE.NormalZ = new THREE.Vector3(0, 0, 1);
THREE.fastWorldPosition = (object, vec) => {
if (!vec) {
vec = new THREE.Vector3();
} else {
vec.set(0, 0, 0);
}
object.localToWorld(vec);
return vec;
}