Rotating 3d object with texture issue - object

I'm trying to imitate http://threejs.org/examples/canvas_geometry_cube.html
via using a 3d.obj file with a texture mapping.
But i keep receiving the following error plus the rotation is totally off.
Uncaught TypeError: Cannot read property 'rotation' of undefined
Full demo is here http://wunderfauks.com/test/examples/test.html

Where you assign the value for group the code for loading has not executed yet and obj does not have a value. Move those 10 lines of code inside your loading function.

I've done it, if anyone needs to know you need to apply a matrix rotation.
Here's an example.
function onWindowResize() {
windowHalfX = window.innerWidth / 2;
windowHalfY = window.innerHeight / 2;
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize( window.innerWidth, window.innerHeight );
}
//
function onDocumentMouseDown( event ) {
event.preventDefault();
document.addEventListener( 'mousemove', onDocumentMouseMove, false );
document.addEventListener( 'mouseup', onDocumentMouseUp, false );
document.addEventListener( 'mouseout', onDocumentMouseOut, false );
mouseXOnMouseDown = event.clientX - windowHalfX;
targetRotationOnMouseDown = targetRotation;
}
function onDocumentMouseMove( event ) {
mouseX = event.clientX - windowHalfX;
targetRotation = targetRotationOnMouseDown + ( mouseX - mouseXOnMouseDown ) * 0.02;
}
function onDocumentMouseUp( event ) {
document.removeEventListener( 'mousemove', onDocumentMouseMove, false );
document.removeEventListener( 'mouseup', onDocumentMouseUp, false );
document.removeEventListener( 'mouseout', onDocumentMouseOut, false );
}
function onDocumentMouseOut( event ) {
document.removeEventListener( 'mousemove', onDocumentMouseMove, false );
document.removeEventListener( 'mouseup', onDocumentMouseUp, false );
document.removeEventListener( 'mouseout', onDocumentMouseOut, false );
}
function onDocumentTouchStart( event ) {
if ( event.touches.length === 1 ) {
event.preventDefault();
mouseXOnMouseDown = event.touches[ 0 ].pageX - windowHalfX;
targetRotationOnMouseDown = targetRotation;
}
}
function onDocumentTouchMove( event ) {
if ( event.touches.length === 1 ) {
event.preventDefault();
mouseX = event.touches[ 0 ].pageX - windowHalfX;
targetRotation = targetRotationOnMouseDown + ( mouseX - mouseXOnMouseDown ) * 0.05;
}
}
function animate() {
requestAnimationFrame( animate );
render();
stats.update();
}
function render() {
//plane.rotation.y = group.rotation.y += ( targetRotation - group.rotation.y ) * 0.05;
var rotation = new THREE.Matrix4().makeRotationY(Math.PI/2);
var translation = new THREE.Matrix4().makeTranslation(0, 0, 0);
rotation = group.rotation.y += (targetRotation - group.rotation.y);
var transformation = new THREE.Matrix4().makeTranslation(0, 0, 0).makeRotationY(rotation * 0.01 * 0.3) ;
group.applyMatrix(transformation);
renderer.render( scene, camera );
}

Related

RecyclerView with pagination inside NestedScrollView lagging while data submit

I have nested scroll view and inside that has 4 recycler views(horizontal, vertical & grid), the last recycler view is vertical and it consists of nested recycler(horizontal) also.
I have used the paging 3 library for the last recycler and loading 5 items once. But while inserting data to the recycler view my app is getting freeze. This is the image of my app screen
viewModel.fullPriceListItem.observe(this#MainFragment) {
when (it) {
is Resource.Success -> {
isApiCallON = false
binding.pbPagination.visibility = View.GONE
if (KeyConstants.SUCCESS == it.value?.status ?: 0) {
if (isFreeDelivery!=null && KeyConstants.isFirstTime) {
showFreeDeliveryDialog(isFreeDelivery!!)
KeyConstants.isFirstTime = false
}
try{
if (it.value?.data?.isNotEmpty() == true && !isThisLastPage) {
pageNum++
enablePagination(
binding.nestedSv,
binding.rvCheckRegularMenu
)
}else {
isThisLastPage = true
if (!it?.value?.isclosed!!){
requestModel = CommonHomeModel(
latitude = getPrefrenceStringValue(
requireContext(),
latitude
),
longitude = getPrefrenceStringValue(
requireContext(),
longitude
),
distance = getPrefrenceStringValue(
requireContext(),
distance
),
foodType = BOTH,
limit = 5,
token = getPrefrenceStringValue(
requireContext(),
jwtToken
),
userId = getPrefrenceStringValue(
requireContext(),
_id
),
page = pageNum,
isclosed = true
)
pageNum ++
binding.pbPagination.visibility = View.VISIBLE
}
isApiCallON = true
}
if (it.value?.orderCount != null) {
if (it.value?.orderCount == 1) {
AnalyticsEventMethod.MinimumOneOrder(requireContext())
}
}
if (pageNum > 2 && it.value?.data?.isNotEmpty() == true) {
fullPriceListItem?.addAll(it.value?.data?.toMutableList()!!)
binding.rvCheckRegularMenu.adapter?.notifyItemChanged(pageNum*5)
} else if (pageNum == 2){
fullPriceListItem = (it.value?.data?.toMutableList()!!)
binding.rvCheckRegularMenu.layoutManager =
LinearLayoutManager(
requireContext(),
LinearLayoutManager.VERTICAL,
false
)
binding.rvCheckRegularMenu.adapter = RestaurantHomeAdapter(
this#MainFragment,
requireContext(),
fullPriceListItem,
"FullPrice",
this#MainFragment,
it.value?.data?.toMutableList()!!,
filterRequestModel
)
if (it.value?.totalpage?:1 > pageNum && pageNum <=4 && !isThisLastPage){
binding.pbPagination.visibility = View.VISIBLE
requestModel = CommonHomeModel(
latitude = getPrefrenceStringValue(requireContext(), latitude),
longitude = getPrefrenceStringValue(requireContext(), longitude),
distance = getPrefrenceStringValue(requireContext(), distance),
foodType = BOTH,
limit = 5,
token = getPrefrenceStringValue(requireContext(), jwtToken),
userId = getPrefrenceStringValue(requireContext(), _id),
page = pageNum,
isclosed = false
)
}
}
if (binding.rvCheckRegularMenu.adapter?.itemCount!! > 0){
binding.rvCheckRegularMenu.visibility = View.VISIBLE
binding.txtPopularRestaurant.visibility = View.VISIBLE
}
binding.rvCheckRegularMenu.itemAnimator = DefaultItemAnimator()
}catch (e: Exception){
e.printStackTrace()
Log.e("mainfragment","Excp: ${e.printStackTrace()}")
}
binding.shimmerPopular.shimmerContainer.stopShimmer()
binding.shimmerPopular.shimmerContainer.visibility = View.GONE
} else if (KeyConstants.FAILURE <= it.value?.status ?: 0) {
binding.shimmerPopular.shimmerContainer.stopShimmer()
binding.shimmerPopular.shimmerContainer.visibility = View.GONE
Log.e("mainfragment","fail api")
}
}
is Resource.Failure -> {
Log.e("mainfragment","resource fail:$it")
binding.shimmerPopular.shimmerContainer.stopShimmer()
binding.shimmerPopular.root.visibility = View.GONE
}
else -> {}
}
// stopShimmer()
}
I tried the pagination with paging 3 library and without library also using nested scroll view listener. But both the options making lag while data inserting to the recycler view. I want my page should scroll vertically smoothly.

Add Extra Fee on Product Page Using Hook [Woocommerce]

add_filter( 'woocommerce_get_price_html', 'product_page_price_display', 9999, 2 );
function product_page_price_display( $price_html, $product ) {
$orig_price = wc_get_price_to_display( $product );
$price_html = wc_price( $orig_price + 10 );
return $price_html;
}
add_filter( 'woocommerce_get_price_html', 'product_page_price_display', 9999, 2 );
function product_page_price_display( $price_html, $product ) {
$orig_price = wc_get_price_to_display( $product );
$price_html = wc_price( $orig_price + 10 );
return $price_html;
}

Fabric.js Undo Redo Functionality causing 2 steps at a time

I'm having a little trouble getting Undo/Redo to work when I'm using multiple ways to add objects. My editor has a button to add images, background, and text.
Therefore, I believe (please correct me if I'm wrong) I need to call the updateModifications() function each time I call a function that adds an image, background or text, along with each time the canvas is modified. I'm fairly certain the issue is that updateModifications is called too many times throughout the document.
function remove(){
console.log(canvas.getActiveObject());
var activeObjects = canvas.getActiveObjects();
canvas.discardActiveObject()
if (activeObjects.length) {
canvas.remove.apply(canvas, activeObjects);
}
updateModifications(true);
}
canvas.on({
'object:modified': function () {
updateModifications(true);
},
'object:added': function() {
updateModifications(true);
}
});
function addText() {
prodName = localStorage.getItem('storedName');
var textObj = new fabric.IText(prodName, {
fontSize: 22,
top: 362.5,
left: 262.5,
hasControls: true,
fontWeight: 'bold',
fontFamily: '"Montserrat",sans-serif',
fontStyle: 'normal',
centeredrotation: true,
originX: 'center',
originY: 'center'
});
canvas.insertAt(textObj,0).setActiveObject(textObj);
textToFront();
canvas.renderAll();
updateModifications(true);
}
This is creating some issues when code based on zaid's SO question;
var mods = 0;
var state = [];
function updateModifications(savehistory) {
if (savehistory === true) {
myjson = JSON.stringify(canvas);
state.push(myjson);
}
}
undo = function undo() {
if (mods < state.length) {
canvas.clear().renderAll();
canvas.loadFromJSON(state[state.length - 1 - mods - 1]);
canvas.renderAll();
mods += 1;
}
}
redo = function redo() {
if (mods > 0) {
canvas.clear().renderAll();
canvas.loadFromJSON(state[state.length - 1 - mods + 1]);
canvas.renderAll();
mods -= 1;
}
}
When you call addText(), you are calling updateModifications() and then have an event listener 'object:added' also calling updateModifications(). Either remove the event listener or simply dont call updateModifications() in addText().
Not sure then mate but this works for me in fabric 2.5:
var CanvasState = [];
var CanvasStateIndex = -1;
saveCanvas()
function refreshCanvas(){
canvas.renderAll.bind(canvas);
}
function saveCanvas(){
var newState = canvas.toJSON();
CanvasState.push(newState);
CanvasStateIndex = CanvasStateIndex +1;
while (CanvasStateIndex < (CanvasState.length)-1){
CanvasState.pop();
}
}
function undo(){
if (CanvasStateIndex >= 0){
CanvasStateIndex = CanvasStateIndex -1;
var jsonCanvas = CanvasState[CanvasStateIndex];
canvas.loadFromJSON(jsonCanvas, refreshCanvas, function(o, obj){
})
} else{
console.log('undo error CanvasStateIndex = '+CanvasStateIndex)
}
}
function redo(){
if (CanvasStateIndex < CanvasState.length -1){
CanvasStateIndex = CanvasStateIndex +1;
var jsonCanvas = CanvasState[CanvasStateIndex];
canvas.loadFromJSON(jsonCanvas, refreshCanvas, function(o, obj){
})
}else{
console.log('redo error CanvasStateIndex = '+CanvasStateIndex)
}
};

Fabric custom object

fabric.ThreePointArc = fabric.util.createClass(fabric.Circle, {
type: 'threePointArc',
points: [], //array of startPoint, intermediatePoint, endPoint
arcCenter: new fabric.Point(null, null),
arcBounds: null,
radius: null,
initialize: function (points, options) {
if (!points || points.length === 0) {
return;
}
this.points = points;
this.callSuper('initialize', options);
// supports only originX and originY as center
this.originX = this.originY = 'center';
},
_set: function(key, value) {
this.callSuper('_set', key, value);
if (key === 'points') {
this._calcArcCenter();
this._calcDimensions();
this.setCoords();
}
return this;
},
setRadius: function(value) {
this.radius = value;
return this;
},
_calcDimensions: function() {
this._calcArcAngles();
this.setRadius(cMath.getLength(this.arcCenter, this.points[0]));
this._calcArcBounds();
this.width = this.arcBounds.width;
this.height = this.arcBounds.height;
this.top = this.arcBounds.y + this.arcBounds.height / 2;
this.left = this.arcBounds.x + this.arcBounds.width / 2;
},
_calcArcCenter: function() {
var c1Mp = cMath.getMidPoint(this.points[0], this.points[1]), // chord 1 midpoint
c2Mp = cMath.getMidPoint(this.points[1], this.points[2]), // chord 2 midpoint
c1pm = -(1 / cMath.getSlope(this.points[0], this.points[1])), // chord 1 perpendicular bisector slope
c2pm = -(1 / cMath.getSlope(this.points[1], this.points[2])); // chord 2 perpendicular bisector slope
// chord perpendicular bisectors meet at the center
this.arcCenter.x = (c2Mp.y - (c2pm * c2Mp.x) + (c1pm * c1Mp.x) - c1Mp.y) / (c1pm - c2pm);
this.arcCenter.y = (c2pm * (this.arcCenter.x - c2Mp.x)) + c2Mp.y;
},
_calcArcBounds: function() {
var validPoints = this.buildValidPointsForArc(),
minX = fabric.util.array.min(validPoints, 'x'),
minY = fabric.util.array.min(validPoints, 'y'),
maxX = fabric.util.array.max(validPoints, 'x'),
maxY = fabric.util.array.max(validPoints, 'y'),
width = (maxX - minX) || 1,
height = (maxY - minY) || 1;
this.arcBounds = {
x: minX,
y: minY,
width: width,
height: height
}
},
buildValidPointsForArc: function() {
var direction = this.getRenderingDirection(),
possibleBoundingPoints = this.points.concat();
!this.arcAngles && this._calcArcAngles();
if (direction) {
for (var i = 1; i <= 4; i++) {
var randomAngle = i * (PI / 2);
if (this.arcAngles.startAngle < this.arcAngles.endAngle) {
!(this.arcAngles.startAngle <= randomAngle && randomAngle <= this.arcAngles.endAngle) &&
possibleBoundingPoints.push(this.generateArcPoint(randomAngle));
} else {
(this.arcAngles.endAngle <= randomAngle && randomAngle <= this.arcAngles.startAngle) &&
possibleBoundingPoints.push(this.generateArcPoint(randomAngle));
}
}
} else {
for (var i = 4; i >= 1; i--) {
var randomAngle = i * (PI / 2);
if (this.arcAngles.startAngle < this.arcAngles.endAngle) {
(this.arcAngles.startAngle <= randomAngle && randomAngle <= this.arcAngles.endAngle) &&
possibleBoundingPoints.push(this.generateArcPoint(randomAngle));
} else {
!(this.arcAngles.endAngle <= randomAngle && randomAngle <= this.arcAngles.startAngle) &&
possibleBoundingPoints.push(this.generateArcPoint(randomAngle));
}
}
}
return possibleBoundingPoints;
},
generateArcPoint: function(angle) {
return new fabric.Point(this.arcCenter.x + this.radius * Math.cos(angle), this.arcCenter.y + this.radius * Math.sin(angle));
},
_calcArcAngles: function() {
var angleKeyRepo = ["startAngle", "intermediateAngle", "endAngle"];
this.arcAngles = this.arcAngles || {};
this.points.forEach(function(point, index) {
var a = cMath.getAngle(this.arcCenter, point);
this.arcAngles[angleKeyRepo[index]] = a < 0 ? ((PI * 2) + a) :
a > 2 * PI ? ((PI * 2) - a) : a;
}, this);
},
getRenderingDirection: function() {
return (((this.points[1].x - this.points[0].x) * (this.points[2].y - this.points[0].y)) -
((this.points[1].y - this.points[0].y) * (this.points[2].x - this.points[0].x))) < 0;
},
_render: function(ctx, noTransform) {
if (!this.visible) {
return;
}
ctx.beginPath();
ctx.arc(
noTransform ? this.left : 0,
noTransform ? this.top : 0,
this.radius,
this.arcAngles.startAngle,
this.arcAngles.endAngle,
this.getRenderingDirection()
);
this._renderFill(ctx);
this._renderStroke(ctx);
},
toObject: function (propertiesToInclude) {
return fabric.util.object.extend(this.callSuper('toObject', propertiesToInclude), {
points: this.points
});
}
});
fabric.ThreePointArc.fromObject = function(object) {
return new fabric.ThreePointArc(object.points, object);
};
fabric.ThreePointArc.async = false;
fabric.util.cMath = {
getSlope : function (startPoint, endPoint) {
if (!startPoint || !endPoint) {
console.error('startPoint and endPoint are required to evaluate slope');
return;
}
// hack to get around the indefinte slope problem
if (endPoint.x == startPoint.x) startPoint.x = startPoint.x + 0.01;
if (endPoint.y == startPoint.y) endPoint.y = endPoint.y + 0.01;
return (endPoint.y - startPoint.y) / (endPoint.x - startPoint.x);
},
getMidPoint: function (startPoint, endPoint) {
if (!startPoint || !endPoint) {
console.error('startPoint and endPoint are required to evaluate slope');
return;
}
return { x: (startPoint.x + endPoint.x) / 2, y: (startPoint.y + endPoint.y) / 2 };
},
getAngle: function (startPoint, endPoint, isDegree) {
if (!startPoint || !endPoint) {
console.error('startPoint and endPoint are required to evaluate slope');
return;
}
var radians = Math.atan2((endPoint.y - startPoint.y), (endPoint.x - startPoint.x)),
degrees = fabric.util.radiansToDegrees(radians);
return isDegree ? degrees < 0 ? 360 + degrees : degrees : radians;
},
getLength: function (startPoint, endPoint) {
if (!startPoint || !endPoint) {
console.error('startPoint and endPoint are required to evaluate slope');
return;
}
return Math.sqrt(Math.pow(endPoint.y - startPoint.y, 2) + Math.pow(endPoint.x - startPoint.x, 2));
}
}
var canvas = new fabric.Canvas('c');
var startPoint = new fabric.Point(47.25423728813553, 56.91525423728814),
intermediatePoint = new fabric.Point( 76.33898305084739,19.8983050847458 ),
endPoint = new fabric.Point( 105.42372881355931,86 );
var arc = new fabric.ThreePointArc([startPoint, intermediatePoint, endPoint] , {
fill: "#FF0000",
stroke: "#000",
strokeWidth: 10
});
canvas.add(arc);
<script src="https://cdnjs.cloudflare.com/ajax/libs/fabric.js/1.4.10/fabric.js"></script>
<canvas id="c" width="800" height="800"></canvas>
I'm trying to create a three point arc on fabric js (ver: 1.4.10). What I want is exactly like this:- https://www.screencast.com/t/dMLvcOduEF
I have a points array from which I'm calcualting the startAngle, endAngle, arcCenter, radius, bounding boxc height and widht, top and left. When I draw it, the actual object is not drawing where my mouse points. It adds up the left and top value with the arcCenter.x and arcCenter.y respectively and draws there. Is there any workaround so that my arc can be drawn where my pointer is ?. So that I don't need to write any extra lines of code for moving, rotating and scaling.
What I'm getting is like this:- https://www.screencast.com/t/V9MUgB3pB
added the fiddle!!
Is there any workaround for this ??

Three.js load a single .obj model , how to show vertex colors

I am new to Three.js (3D) and have a simple question. I have the following code that will work properly, but I think the result lost their colors because I open the test.obj file whith 3D Buidler(WIN10), there are lots of colors on ther surface of model. why?
The Code
var loader = new THREE.OBJLoader()
loader.load( 'test.obj', function ( object ) {
object.position.y = 0;
scene.add( object );
} );
I think it's vertex color, How to show it's vertex color?
var loader = new THREE.OBJLoader2()
loader.load( 'test.obj', function ( object ) {
object.position.y = 0;
scene.add( object );
} );
I have tryed OBJLoader2.js , but it doesn't work, need some settings?
The result loaded by Three.js:
The result loaded by 3D Builder:
The obj file
use the .mtl file along with the .obj file.
var onProgress = function ( xhr ) {
if ( xhr.lengthComputable ) {
var percentComplete = xhr.loaded / xhr.total * 100;
console.log( Math.round(percentComplete, 2) + '% downloaded' );
}
};
var onError = function ( xhr ) { };
THREE.Loader.Handlers.add( /\.dds$/i, new THREE.DDSLoader() );
//Car model
var mtlLoader = new THREE.MTLLoader();
mtlLoader.setPath( '../materials/car/' );
mtlLoader.load( 'car.mtl', function( materials ) {
materials.preload();
var objLoader = new THREE.OBJLoader();
objLoader.setMaterials( materials );
objLoader.setPath( '../materials/car/' );
objLoader.load( 'car.obj', carObject, onProgress, onError );
});
function carObject(object){
object.rotation.y = 1.55;
object.position.z = 105;
object.position.y = 1.15;
object.scale.x = object.scale.y = object.scale.z = 0.15;
//object.rotation.x = 6.5;
//object.position.z = 50;
scene.add( object );
}
//end car model
Finally I found it out :
I changed OBJLoader2.js's version to 1.3.0.

Resources