Cut the end and start from a Extruded SVG in Three.js - svg
I have a problem with extruding a svg in three.js.
this is the result of my code.
But I must cut the end and beginning of this model like the next image.
How can I do this after the extruding of the svg?
Clipping allows part of the geometry to be cut out of the render.
Made a codepen here: https://codepen.io/cdeep/pen/LYjGNxR
renderer.localClippingEnabled = true;
const clippingPlane1 = new THREE.Plane( new THREE.Vector3( 1, -1, 0 ), 1 );
const clippingPlane2 = new THREE.Plane( new THREE.Vector3( -1, -1, 0 ), 1 );
const material = new THREE.MeshStandardMaterial({
side: THREE.DoubleSide,
clippingPlanes: [clippingPlane1, clippingPlane2],
});
The examples section has more complex examples for clipping.
https://threejs.org/examples/?q=clipping#webgl_clipping_intersection
Official documentation for clippingPlanes:
https://threejs.org/docs/#api/en/materials/Material.clippingPlanes
Related
How to do svg scaling with Pixi
I've been trying to do SVG scaling with PIXI but the results are not really what I expected them to be. As you can see in the image, the debian logo, which is a SVG file, seems to be blurry and edgy. Am I writing my code wrong: Refined from https://github.com/kevguy/D3-Svg-Comparison/blob/master/src/components/SvgCompare.vue: // initialization this.renderer = new PIXI.Application(800, 600, {backgroundColor: 0x1099bb}) document.getElementById('svg-canvas').appendChild(this.renderer.view) this.container = new PIXI.Container() this.stage = this.renderer.stage this.stage.addChild(this.container) // appending the svg file const texture = PIXI.Texture.fromImage(this.chosenImage) this.svg = new PIXI.Sprite(texture) this.svg.anchor.x = 0.8 this.svg.anchor.y = 0.8 this.svg.position.x = 400 this.svg.position.y = 300 this.svg.scale.x = this.selectedScale this.svg.scale.y = this.selectedScale this.container.addChild(this.svg) chosenImage is the svg file retrieved by using import * as choesnImage from 'the-file-path' selectedScale is the selected scaling value which can be changed dynamically thanks to VueJS You can check out my work here and its corresponding GitHub repo The bunny logo is to verify when the scaling happens, it only applies to the SVG not the whole canvas.
According to this issue you need to load svg like this to generate scaled svg texture. PIXI.Texture.fromImage(this.chosenImage, undefined, undefined, newVal) And need to clean texture cache then create new texture for every new scale change. PIXI.utils.clearTextureCache() Modified SvgCompare.vue on gist
three.js color of object, not texture
So to explain the weird title first, I am trying to make a 3D avatar for a little project I am working on, however when I try to change the color of the arm on the avatar, it doesn't actually change the color with the texture on it, it changes the color of the white part (not transparent) of the texture, but where it is transparent on the texture it shows white with, as it seems, no lighting. Before coloring: After coloring: The actual texture I am using can be found here: http://imgur.com/SlnOxEw This is how I am rendering the texture: var AvatarTexture = new THREE.MeshPhongMaterial( { map: THREE.ImageUtils.loadTexture('./images/Shirt/vest.png'), shininess: 80, shading: THREE.SmoothShading, alphaMap: 0x000000} ); and the coloring: object.children[0].material.color.setHex(0xffcc66); object.children[2].material.color.setHex(0xffcc66); object.children[4].material.color.setHex(0xffcc66); the object itself is a UV mapped .obj exported from blender. This happens even if the texture does not have transparency.
First, in three.js, the final color is the product of material.color and material.map, component-wise. So if you change material.color, the final texture color will be tinted. Second, if two meshes share the same material, and you change the material color, then both meshes will change color. To prevent that, you need to have a separate material instance for each mesh. material2 = material1.clone(); three.js r.77
If you want to change color of part, it is not important an object, but a material var reference. //your solution as i understand question var material1 = new THREE.MeshPhongMaterial( .. ); var mesh1.material = material1; var mesh2.material = material1; material1.color= red; // both meshes change color. //solution: var mesh1.material = new THREE.MeshPhongMaterial( .. ); var mesh2.material = new THREE.MeshPhongMaterial( .. ); //or var material1 = new THREE.MeshPhongMaterial( .. ); var material2 = new THREE.MeshPhongMaterial( .. ); var mesh1.material = material1; var mesh2.material = material2;
(THREE.js) Material isn't working on my custom THREE.Geometry shape
I created a triangle and I am trying to make it red. However it remains black. The problem doesn't seem to be the material as it works on other Geometries I've made. Here is my triangle: var triangle = new THREE.Geometry(); triangle.vertices.push( new THREE.Vector3(-10,10,0), new THREE.Vector3(-10,-10,0), new THREE.Vector3(10,-10,0) ); triangle.faces.push(new THREE.Face3(0,1,2)); triangle.computeBoundingSphere(); this.redtriangle = new THREE.Mesh(triangle, this.redMat) I tried some suggestions online to color it using: triangle.faces.push(new THREE.Face3(0,1,2)); triangle.faces[0].vertexColors[0] = new THREE.Color(0xFF0000); triangle.faces[0].vertexColors[1] = new THREE.Color(0xFF0000); triangle.faces[0].vertexColors[2] = new THREE.Color(0xFF0000); My material this.redMat = new THREE.MeshLambertMaterial ({ color: 0xFF0000, shading:THREE.FlatShading, // I added this line for the suggestion above // vertexColors:THREE.VertexColors, side:THREE.DoubleSide }) I've also tried to insert triangle.colorsNeedUpdate = true; or geometry.colorsNeedsUpdate = true; but no matter what variations/where I put it, it doesn't want to work. The triangle stays black.
Your vertices definition is in clockwise order when it should have been counter-clockwise. So if you change your new THREE.Face3(0,1,2) to new THREE.Face3(0,2,1) you should see something.
Cesiumjs - Rotate text
I want to add a label that doesn't always face the camera. Instead, I want it to follow a defined path. Similar to how street names follow the direction of their streets in google maps (they aren't always horizontal). I can think of 2 possible implementations for rotating text but haven't had any luck. That Label() or label : have a rotation property I haven't found. IE something like this: viewer.entities.add({ position : Cesium.Cartesian3.fromDegrees(-75.1641667, 39.9522222), label : { text : 'Philadelphia' //rotation : Cesium.Math.toRadians(-45) } }); or this var labels = scene.primitives.add(new Cesium.LabelCollection()); var l = labels.add({ position : Cesium.Cartesian3.fromRadians(longitude, latitude, height), text : 'Hello World', font : '24px Helvetica' //rotation: Cesium.Math.toRadians(-45) }); Create Pictures of each label in photoshop and import them as an image, then rotate the image (or use it as a material and rotate the entity). Very labor intensive if you have a lot of labels (like street names). Or perhaps there is a way for cesiumjs to recognize text as a fixed position 2D object and skew it appropriately as the view angle changes. Any ideas?
If your text doesn't change, You can use an image, and load it by Cesium.SingleTileImageryProvider . If your text does change, you can use a billboard.image, set an HTML canvas to it, and rotate the canvas like so: var c = document.getElementById("myCanvas"); var ctx = c.getContext("2d"); ctx.font = "20px Georgia"; ctx.fillText("Hello World!", 10, 50); ctx.font = "30px Verdana"; // Create gradient var gradient = ctx.createLinearGradient(0, 0, c.width, 0); gradient.addColorStop("0", "magenta"); gradient.addColorStop("0.5", "blue"); gradient.addColorStop("1.0", "red"); // Fill with gradient ctx.rotate(20*Math.PI/180); ctx.fillStyle = gradient; ctx.fillText("Big smile!", 10, 90); billboard.image = ctx;
The only way I know how to rotate a label is like #Henri Aloni said with canvas. Cesium has already a function called: writeTextToCanvas. example in typescript : viewer.entities.add({ position: Cartesain3.fromDegrees(34, 32, 0), billboard: { image: writeTextToCanvas('baruch', { font: '30px sans-serif' }), rotation: 45 } });
Extruding multiple polygons with multiple holes and texturing the combined shape
This question is related to this question. The answer shows very nice way to extrude polygons that have holes (see the excellent live example). The main learning of the answer was that paths in three.js (r58) cannot have more than one moveTo command and it have to be in the start of the path, which means that path have to be broken by moveTos, so that moveTo start always a new path. Extruding in three.js means that 2D paths are converted to 3D shapes using possible beveling. It is suitable for extruding texts to make 3D letters and words, but can be used also to extrude custom paths. Now there arises two questions: how is it possible to handle polygons that have multiple hole-polygons and multiple non-hole-polygons? how is it possible to add a texture to generated shape as a whole? I made an example of this as SVG in http://jsbin.com/oqomuj/1/edit: The image is produced using this path: <path d=" M57.11,271.77 L57.11,218.33 L41.99,218.63 L105.49,165.77 L138.41,193.18 L138.41,172.2 L152.53,172.2 L152.53,204.93 L168.99,218.63 L153.21,218.63 L153.21,271.77Z M74.14,264.13 L105.49,264.13 L105.49,232.8 L74.14,232.8Z M115.35,250.7 L135.96,250.7 L135.96,232.61 L115.35,232.61Z M56.11,145.77 L56.11,92.33 L40.99,92.63 L104.49,39.77 L137.41,67.18 L137.41,46.2 L151.53,46.2 L151.53,78.93 L152.53,79.76 L155.55,77.23 L159.5,74.52 L168.65,69.81 L176.46,66.93 L188.04,64.16 L200.63,62.7 L213.65,62.7 L226.05,64.09 L234.83,66.06 L245.65,69.73 L252.87,73.27 L259.12,77.34 L262.63,80.33 L265.6,83.47 L268.01,86.76 L269.83,90.17 L271.08,93.68 L271.76,99.08 L271.04,104.64 L269.75,108.2 L267.87,111.63 L265.42,114.91 L262.44,118.01 L258.95,120.92 L255.02,123.63 L245.86,128.34 L238.06,131.22 L226.48,133.99 L213.88,135.44 L200.63,135.44 L188.04,133.99 L176.46,131.22 L168.65,128.34 L159.5,123.63 L155.55,120.92 L152.21,118.12 L152.21,145.77Z M73.14,138.13 L104.49,138.13 L104.49,106.8 L73.14,106.8Z M114.35,124.7 L134.96,124.7 L134.96,106.61 L114.35,106.61Z M207.26,117.33 L210.57,117.26 L216.87,116.53 L222.66,115.15 L227.8,113.18 L233.11,110 L236.34,106.99 L238.51,103.64 L239.42,100.48 L239.42,97.67 L238.51,94.51 L236.34,91.16 L233.11,88.15 L227.8,84.97 L222.66,83 L216.87,81.62 L210.57,80.89 L203.94,80.89 L197.65,81.62 L191.86,83 L186.71,84.97 L181.41,88.15 L178.18,91.16 L176.01,94.51 L175.1,97.67 L175.1,100.48 L176.01,103.64 L178.18,106.99 L181.41,110 L186.71,113.18 L191.86,115.15 L197.65,116.53 L203.94,117.26Z "></path> and this path converted to individual arrays of vertices: var lower_house_material = [{x:57.11,y:271.77},{x:57.11,y:218.33},{x:41.99,y:218.63},{x:105.49,y:165.77},{x:138.42,y:193.18},{x:138.42,y:172.2},{x:152.53,y:172.2},{x:152.53,y:204.93},{x:168.99,y:218.63},{x:153.21,y:218.63},{x:153.21,y:271.77}]; var lower_house_hole_1 = [{x:74.14,y:264.13},{x:105.49,y:264.13},{x:105.49,y:232.8},{x:74.14,y:232.8}]; var lower_house_hole_2 = [{x:115.35,y:250.7},{x:135.96,y:250.7},{x:135.96,y:232.61},{x:115.35,y:232.61}]; var upper_house_material = [{x:56.11,y:145.77},{x:56.11,y:92.33},{x:40.99,y:92.63},{x:104.49,y:39.77},{x:137.42,y:67.18},{x:137.42,y:46.2},{x:151.53,y:46.2},{x:151.53,y:78.93},{x:152.53,y:79.76},{x:155.55,y:77.23},{x:159.5,y:74.52},{x:168.65,y:69.81},{x:176.46,y:66.93},{x:188.04,y:64.16},{x:200.63,y:62.7},{x:213.65,y:62.7},{x:226.05,y:64.1},{x:234.83,y:66.06},{x:245.65,y:69.73},{x:252.87,y:73.27},{x:259.12,y:77.35},{x:262.63,y:80.33},{x:265.6,y:83.47},{x:268.01,y:86.76},{x:269.84,y:90.17},{x:271.08,y:93.68},{x:271.76,y:99.08},{x:271.04,y:104.64},{x:269.75,y:108.2},{x:267.87,y:111.63},{x:265.42,y:114.91},{x:262.44,y:118.01},{x:258.96,y:120.92},{x:255.02,y:123.63},{x:245.86,y:128.34},{x:238.06,y:131.22},{x:226.48,y:133.99},{x:213.88,y:135.45},{x:200.63,y:135.45},{x:188.04,y:133.99},{x:176.46,y:131.22},{x:168.65,y:128.34},{x:159.5,y:123.63},{x:155.55,y:120.92},{x:152.21,y:118.12},{x:152.21,y:145.77}]; var upper_house_hole_1 = [{x:73.14,y:138.13},{x:104.49,y:138.13},{x:104.49,y:106.8},{x:73.14,y:106.8}]; var upper_house_hole_2 = [{x:114.35,y:124.7},{x:134.96,y:124.7},{x:134.96,y:106.61},{x:114.35,y:106.61}]; var upper_house_hole_3 = [{x:207.26,y:117.33},{x:210.57,y:117.26},{x:216.87,y:116.53},{x:222.66,y:115.15},{x:227.8,y:113.18},{x:233.11,y:110},{x:236.34,y:106.99},{x:238.51,y:103.64},{x:239.42,y:100.48},{x:239.42,y:97.67},{x:238.51,y:94.51},{x:236.34,y:91.16},{x:233.11,y:88.15},{x:227.8,y:84.97},{x:222.66,y:83},{x:216.87,y:81.62},{x:210.57,y:80.89},{x:203.94,y:80.89},{x:197.65,y:81.62},{x:191.86,y:83},{x:186.71,y:84.97},{x:181.41,y:88.15},{x:178.18,y:91.16},{x:176.01,y:94.51},{x:175.1,y:97.67},{x:175.1,y:100.48},{x:176.01,y:103.64},{x:178.18,y:106.99},{x:181.41,y:110},{x:186.71,y:113.18},{x:191.86,y:115.15},{x:197.65,y:116.53},{x:203.94,y:117.26}]; The question is, how this like structure can be converted to 3D object in three.js so that it can be extruded using THREE.ExtrudeGeometry( shape, extrusionSettings ) and after that textured as a whole? I can examine the path data to know what hole belongs to what polygon and handle all as separate shapes, but because I want to use one texture image across all the shapes, I think the preferred way is to handle all material-polygons as one shape, and hole-polygons as other shape and use something like: var shape = [lower_house_material, upper_house_material]; shape.holes = [lower_house_hole_1, lower_house_hole_2, upper_house_hole_1, upper_house_hole_2, upper_house_hole_3]; var 3d_geometry = THREE.ExtrudeGeometry( shape, extrusionSettings ); So the 3d_geometry should be at the end one mesh to which I can append a texture this way: var textureFront = new THREE.ImageUtils.loadTexture( 'textureFront.png'); var textureSide = new THREE.ImageUtils.loadTexture( 'textureSide.png'); var materialFront = new THREE.MeshBasicMaterial( { map: textureFront } ); var materialSide = new THREE.MeshBasicMaterial( { map: textureSide } ); var materialArray = [ materialFront, materialSide ]; var faceMaterial = new THREE.MeshFaceMaterial(materialArray); var final_mesh = new THREE.Mesh(3d_geometry, faceMaterial ); And one of the textures could be something like this (256x256px): And texture applied: And because the mesh is extruded, there is also 3D thickness on the above, but you got the idea of texturing. I know that y-coordinates have to be flipped but it is a trivial task and not the point of my question, but if three.js has ready-made function for clipping y, it would be helpful. I have spent hours to examine the three.js source code, examples and documentation, but because the most frequent word there is "todo", it cannot help much. And I'm very newbie to three.js, I would think that this may be trivial task for some experienced three.js user. UPDATE: And just to make sure, the hole polygons are always well-behaved, which means that hole polygons are always fully inside material-polygons and there are no duplicate vertices or self-intersections either in material-polygons or hole-polygons and all material-polygons have CW winding order and holes CCW. UPDATE: Merging geometries was not a solution for texturing the whole extruded polygon set by one texture: http://jsfiddle.net/C5dga. The texture is repeated on all individual shapes, so merging geometries in this case has no real meaning. The solution could be possibly found on merging shapes before they are extruded, but not found solution for this yet.
You can merge geometries as in the following snippet, resulting in just a single mesh. From your prior questions, you already know how to texture a single geometry. var geometry1 = new THREE.ExtrudeGeometry( shape1, extrusionSettings ); var geometry2 = new THREE.ExtrudeGeometry( shape2, extrusionSettings ); geometry1.merge( geometry2 ); . . . var mesh = new THREE.Mesh( geometry1, material ); scene.add( mesh ); Fiddle: http://jsfiddle.net/pHn2B/88/ Fiddle: http://jsfiddle.net/C5dga/13/ (with texture) EDIT: As an alternative to creating separate geometries and using the merge utility, you can create a single geometry using the following pattern, instead: var geometry1 = new THREE.ExtrudeGeometry( [ shape1, shape2 ], extrusionSettings ); EDIT: updated to three.js r.70