mrdoob / three.js

JavaScript 3D Library.
https://threejs.org/
MIT License
102.81k stars 35.38k forks source link

computeLineDistances in BufferGeometry? #7013

Closed tiansijie closed 6 years ago

tiansijie commented 9 years ago

I tried to create an dashed line material using BufferGeometry. For some reason, it looks like whenever you create an line using LineDashedMaterial, you will need to call this computeLineDistances method. Do we want to add this method inside BufferGeometry as well?

WestLangley commented 9 years ago

This is a bit more complicated than it appears on the surface.

First of all, Geometry.computeLineDistances() is only correct if the vertices are to be connected in order. If the geometry represents pairs of points -- say for rendering edges of a mesh -- then a different computation is required to properly compute line distances.

In the case of BufferGeometry, we need to add a lineDistance attribute and properly calculate it.

If the geometry is indexed, we can't even do this, because the vertices are reused. So to add a lineDistance attribute to BufferGeometry, the geometry must be of the non-indexed type. That is an unfortunate restriction.

We also need to know if the geometry represents a sequence of points, or a sequence of pairs of points -- same issue as before.

Maybe computeLineDistances() should be a method of THREE.Line, and in r.72dev, THREE.LineSegments.

Remember, we do not know if a geometry represents a line, line segments, or a mesh. This has always been an issue -- just not a particularly problematic one.

tiansijie commented 9 years ago

I do agree put the computeLineDistances inside THREE.Line is make more sense. If we make computeLineDistances inside THREE.Line and THREE.LineSegments, we still need to differentiate the different between Geometry and BufferGeometry?

I also take a look where threejs are using the lineDistances.
https://github.com/mrdoob/three.js/blob/master/src/renderers/WebGLRenderer.js#L3994

However, we have different updates for THREE.BufferGeometry and THREE.Line, I am not sure how we going to combine lineDistances even we move this method inside THREE.Line

andrevega3 commented 7 years ago

I have tried implementing my own computelinedistance into the threejs library for BufferGeometry because I know my positions are in order and do not repeat or have indices, but still does not work. I also may be able to provide the line distances before drawing. Any suggestions how I can get around this limitation(knowing my vertices are non-indexed and in order)?

Mugen87 commented 7 years ago

reference https://discourse.threejs.org/t/buffergeometry-with-shadermaterial/644

WestLangley commented 7 years ago
>Any suggestions how I can get around this limitation(knowing my vertices are non-indexed and in order)?

// non-indexed BG -> regular G -> compute line distances
geometry = new THREE.Geometry().fromBufferGeometry( geometry );
geometry.computeLineDistances();
andrevega3 commented 7 years ago

Ahh yes I should of mentioned I tried this solution... it leads me to a TypeError: Cannot read property 'x' of undefined. right here in three.js `subVectors: function ( a, b ) {

        this.x = a.x - b.x;
        this.y = a.y - b.y;
        this.z = a.z - b.z;

        return this;

    },`

which seemed to be also discussed here: https://github.com/mrdoob/three.js/issues/6610

WestLangley commented 7 years ago

@andrevega3 If you are not able to get your code to work, please first get help on a help site.

If you can demonstrate a three.js bug, then please make a new post and provide a link to a live example demonstrating it.

andrevega3 commented 7 years ago

Alright I guess it is not possible to compute line distances without converting to geometry first. I have posted a new thread addressing the error within the three.js code: https://github.com/mrdoob/three.js/issues/11851

CarlBateman commented 7 years ago

Since the lack of lineDistances in BufferGeometry effectively breaks THREE.ShaderLib.dashed (until the feature's added), would it not be a good idea to have some sort of warning in code and console?

(I just fell down the rabbit hole chasing this, and interesting learning experience, but still.)