@jscad/regl-renderer 2.3.0 → 2.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md
CHANGED
|
@@ -3,6 +3,17 @@
|
|
|
3
3
|
All notable changes to this project will be documented in this file.
|
|
4
4
|
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
|
5
5
|
|
|
6
|
+
# [2.4.0](https://github.com/jscad/OpenJSCAD.org/compare/@jscad/regl-renderer@2.3.0...@jscad/regl-renderer@2.4.0) (2021-09-27)
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
### Features
|
|
10
|
+
|
|
11
|
+
* **regl-renderer:** enhanced rendering to use the matrix as provided with the geometry ([#871](https://github.com/jscad/OpenJSCAD.org/issues/871)) ([40802af](https://github.com/jscad/OpenJSCAD.org/commit/40802afeb9a2a45cf3eb4f1b9dc774b8670d8259))
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
|
|
6
17
|
# [2.3.0](https://github.com/jscad/OpenJSCAD.org/compare/@jscad/regl-renderer@2.2.0...@jscad/regl-renderer@2.3.0) (2021-09-09)
|
|
7
18
|
|
|
8
19
|
|
|
@@ -311,13 +311,13 @@ const mat4=require("gl-mat4"),{meshColor:meshColor}=require("../../renderDefault
|
|
|
311
311
|
const meshFrag="\nprecision mediump float;\nvarying vec3 surfaceNormal;\nuniform float ambientLightAmount;\nuniform float diffuseLightAmount;\nuniform vec4 ucolor;\nuniform vec3 lightDirection;\nuniform vec3 opacity;\n\nvarying vec4 _worldSpacePosition;\n\nuniform vec2 printableArea;\n\nvec4 errorColor = vec4(0.15, 0.15, 0.15, 0.3);\n\nvoid main () {\n vec4 depth = gl_FragCoord;\n\n float v = 0.8; // shadow value\n vec4 endColor = ucolor;\n\n vec3 ambient = ambientLightAmount * endColor.rgb;\n float cosTheta = dot(surfaceNormal, lightDirection);\n vec3 diffuse = diffuseLightAmount * endColor.rgb * clamp(cosTheta , 0.0, 1.0 );\n\n float cosTheta2 = dot(surfaceNormal, vec3(-lightDirection.x, -lightDirection.y, lightDirection.z));\n vec3 diffuse2 = diffuseLightAmount * endColor.rgb * clamp(cosTheta2 , 0.0, 1.0 );\n\n gl_FragColor = vec4((ambient + diffuse + diffuse2 * v), endColor.a);\n}",meshVert="\nprecision mediump float;\n\nuniform float camNear, camFar;\nuniform mat4 model, view, projection;\n\nattribute vec3 position, normal;\n\nvarying vec3 surfaceNormal, surfacePosition;\nvarying vec4 _worldSpacePosition;\n\nvoid main() {\n surfacePosition = position;\n surfaceNormal = normal;\n vec4 worldSpacePosition = model * vec4(position, 1);\n _worldSpacePosition = worldSpacePosition;\n\n vec4 glPosition = projection * view * model * vec4(position, 1);\n gl_Position = glPosition;\n}\n";module.exports={vert:meshVert,frag:meshFrag};
|
|
312
312
|
|
|
313
313
|
},{}],105:[function(require,module,exports){
|
|
314
|
-
const mat4=require("gl-mat4"),{meshColor:meshColor}=require("../../renderDefaults"),drawMesh=(e,r={extras:{}})=>{const t={useVertexColors:!0,dynamicCulling:!
|
|
314
|
+
const mat4=require("gl-mat4"),{meshColor:meshColor}=require("../../renderDefaults"),drawMesh=(e,r={extras:{}})=>{const t={useVertexColors:!0,dynamicCulling:!0,geometry:void 0,color:meshColor},{geometry:a,dynamicCulling:s,useVertexColors:o,color:l}=Object.assign({},t,r),n=e.buffer([]),i=!!(a.indices&&a.indices.length>0),c=!!(a.normals&&a.normals.length>0),m=!!(o&&a.colors&&a.colors.length>0),u=a.transforms||mat4.create(),d=mat4.determinant(u)<0,g=s&&d?"front":"back",f=m?require("./vColorShaders").vert:require("./meshShaders").vert,h=m?require("./vColorShaders").frag:require("./meshShaders").frag,b=mat4.invert(mat4.create(),u);let p={primitive:"triangles",vert:f,frag:h,uniforms:{model:(e,r)=>u,ucolor:(e,r)=>r&&r.color?r.color:l,vColorToggler:(e,r)=>r&&r.useVertexColors&&!0===r.useVertexColors?1:0,unormal:(e,r)=>{const t=mat4.invert(mat4.create(),r.camera.view);return mat4.multiply(t,b,t),mat4.transpose(t,t),t}},attributes:{position:e.buffer({usage:"static",type:"float",data:a.positions}),ao:n},cull:{enable:!0,face:g},depth:{enable:!0},blend:{enable:!0,func:{src:"src alpha",dst:"one minus src alpha"}}};return a.cells?p.elements=a.cells:i?p.elements=e.elements({usage:"static",type:"uint16",data:a.indices}):a.triangles?p.elements=a.triangles:p.count=a.positions.length/3,c&&(p.attributes.normal=e.buffer({usage:"static",type:"float",data:a.normals})),m&&(p.attributes.color=e.buffer({usage:"static",type:"float",data:a.colors})),e(p=Object.assign({},p,r.extras))};module.exports=drawMesh;
|
|
315
315
|
|
|
316
316
|
},{"../../renderDefaults":110,"./meshShaders":106,"./vColorShaders":107,"gl-mat4":26}],106:[function(require,module,exports){
|
|
317
317
|
const meshFrag="\nprecision mediump float;\nvarying vec3 surfaceNormal;\nuniform float ambientLightAmount;\nuniform float diffuseLightAmount;\nuniform vec4 ucolor;\nuniform vec3 lightDirection;\nuniform vec3 opacity;\n\nvarying vec4 _worldSpacePosition;\n\nuniform vec2 printableArea;\n\nvec4 errorColor = vec4(0.15, 0.15, 0.15, 0.3);\n\nvoid main () {\n vec4 depth = gl_FragCoord;\n\n float v = 0.8; // shadow value\n vec4 endColor = ucolor;\n\n vec3 ambient = ambientLightAmount * endColor.rgb;\n float cosTheta = dot(surfaceNormal, lightDirection);\n vec3 diffuse = diffuseLightAmount * endColor.rgb * clamp(cosTheta , 0.0, 1.0 );\n\n float cosTheta2 = dot(surfaceNormal, vec3(-lightDirection.x, -lightDirection.y, lightDirection.z));\n vec3 diffuse2 = diffuseLightAmount * endColor.rgb * clamp(cosTheta2 , 0.0, 1.0 );\n\n gl_FragColor = vec4((ambient + diffuse + diffuse2 * v), endColor.a);\n}",meshVert="\nprecision mediump float;\n\nuniform float camNear, camFar;\nuniform mat4 model, view, projection;\n\nattribute vec3 position, normal;\n\nvarying vec3 surfaceNormal, surfacePosition;\nvarying vec4 _worldSpacePosition;\n\nvoid main() {\n surfacePosition = position;\n surfaceNormal = normal;\n vec4 worldSpacePosition = model * vec4(position, 1);\n _worldSpacePosition = worldSpacePosition;\n\n vec4 glPosition = projection * view * model * vec4(position, 1);\n gl_Position = glPosition;\n}\n";module.exports={vert:meshVert,frag:meshFrag};
|
|
318
318
|
|
|
319
319
|
},{}],107:[function(require,module,exports){
|
|
320
|
-
const vColorVert="\nprecision mediump float;\n\nuniform float camNear, camFar;\nuniform mat4 model, view, projection, unormal;\n\nattribute vec3 position, normal;\nattribute vec4 color;\n\nattribute float ao;\nvarying float ambientAo;\n\nvarying vec3 surfaceNormal, surfacePosition;\nvarying vec4 _worldSpacePosition;\nvarying vec4 vColor;\n\nvoid main() {\n surfacePosition = position;\n surfaceNormal = (unormal * vec4(normal, 1.0)).xyz; //vec4 transformedNormal = uNormalMatrix * vec4(aVertexNormal, 1.0);\n vec4 worldSpacePosition = model * vec4(position, 1);\n _worldSpacePosition = worldSpacePosition;\n //gl_Position = projection * view * worldSpacePosition;\n\n vColor = color;\n\n //ambientAo = (1. - ao) * (0.5 * max(normal.x, 0.) + 0.5);\n\n vec4 glPosition = projection * view * model * vec4(position, 1);\n gl_Position = glPosition;\n //gl_Position = zBufferAdjust(glPosition, camNear, camFar);\n}\n",vColorFrag="\nprecision mediump float;\nvarying vec3 surfaceNormal, surfacePosition;\n\nuniform float ambientLightAmount;\nuniform float diffuseLightAmount;\nuniform float specularLightAmount;\n\nuniform vec3 lightDirection;\nuniform vec4 lightColor;\nuniform vec3 opacity;\nuniform float uMaterialShininess;\n\nvarying vec4 vColor;\nuniform vec4 ucolor;\nuniform float vColorToggler;\n\nuniform vec2 printableArea;\nvec4 errorColor = vec4(0.15, 0.15, 0.15, 0.3);//vec4(0.15, 0.15, 0.15, 0.3);\nvarying vec4 _worldSpacePosition;\nvarying float ambientAo;\n\nvoid main () {\n vec4 depth = gl_FragCoord;\n vec4 endColor = vColor * vColorToggler + ucolor * (1.0 - vColorToggler);\n\n vec3 ambient = ambientLightAmount * endColor.rgb ; //ambientAo * \n\n float diffuseWeight = dot(surfaceNormal, lightDirection);\n vec3 diffuse = diffuseLightAmount * endColor.rgb * clamp(diffuseWeight , 0.0, 1.0 );\n\n //specular\n \n vec4 specularColor = vec4(lightColor);\n vec3 eyeDirection = normalize(surfacePosition.xyz);\n vec3 reflectionDirection = reflect(-lightDirection, surfaceNormal);\n float specularLightWeight = pow(max(dot(reflectionDirection, eyeDirection), 0.0), uMaterialShininess);\n vec3 specular = specularColor.rgb * specularLightWeight * specularLightAmount;\n\n /*float light2Multiplier = 0.2;\n float diffuseWeight2 = dot(surfaceNormal, vec3(-lightDirection.x, lightDirection.y, lightDirection.z));\n vec3 diffuse2 = diffuseLightAmount * endColor.rgb * clamp(diffuseWeight2 , 0.0, 1.0 ) * light2Multiplier;\n\n float light3Multiplier = 0.2; \n float diffuseWeight3 = dot(surfaceNormal, vec3(lightDirection.x, -lightDirection.y, lightDirection.z));\n vec3 diffuse3 = diffuseLightAmount * endColor.rgb * clamp(diffuseWeight3 , 0.0, 1.0 ) * light3Multiplier;\n\n float light4Multiplier = 0.2; \n float diffuseWeight4 = dot(surfaceNormal, vec3(-lightDirection.x, -lightDirection.y, lightDirection.z));\n vec3 diffuse4 = diffuseLightAmount * endColor.rgb * clamp(diffuseWeight4 , 0.0, 1.0 ) * light4Multiplier;*/\n \n gl_FragColor = vec4((ambient + diffuse + specular), endColor.a);\n //gl_FragColor = vec4((ambient + diffuse + diffuse2 + diffuse3 + diffuse4), endColor.a);\n}\n";module.exports={frag:vColorFrag,vert:vColorVert};
|
|
320
|
+
const vColorVert="\nprecision mediump float;\n\nuniform float camNear, camFar;\nuniform mat4 model, view, projection, unormal;\n\nattribute vec3 position, normal;\nattribute vec4 color;\n\nattribute float ao;\nvarying float ambientAo;\n\nvarying vec3 surfaceNormal, surfacePosition;\nvarying vec4 _worldSpacePosition;\nvarying vec4 vColor;\n\nvoid main() {\n surfacePosition = (unormal * vec4(position, 1.0)).xyz;\n surfaceNormal = (unormal * vec4(normal, 1.0)).xyz; //vec4 transformedNormal = uNormalMatrix * vec4(aVertexNormal, 1.0);\n vec4 worldSpacePosition = model * vec4(position, 1);\n _worldSpacePosition = worldSpacePosition;\n //gl_Position = projection * view * worldSpacePosition;\n\n vColor = color;\n\n //ambientAo = (1. - ao) * (0.5 * max(normal.x, 0.) + 0.5);\n\n vec4 glPosition = projection * view * model * vec4(position, 1);\n gl_Position = glPosition;\n //gl_Position = zBufferAdjust(glPosition, camNear, camFar);\n}\n",vColorFrag="\nprecision mediump float;\nvarying vec3 surfaceNormal, surfacePosition;\n\nuniform float ambientLightAmount;\nuniform float diffuseLightAmount;\nuniform float specularLightAmount;\n\nuniform vec3 lightDirection;\nuniform vec4 lightColor;\nuniform vec3 opacity;\nuniform float uMaterialShininess;\n\nvarying vec4 vColor;\nuniform vec4 ucolor;\nuniform float vColorToggler;\n\nuniform vec2 printableArea;\nvec4 errorColor = vec4(0.15, 0.15, 0.15, 0.3);//vec4(0.15, 0.15, 0.15, 0.3);\nvarying vec4 _worldSpacePosition;\nvarying float ambientAo;\n\nvoid main () {\n vec4 depth = gl_FragCoord;\n vec4 endColor = vColor * vColorToggler + ucolor * (1.0 - vColorToggler);\n\n vec3 ambient = ambientLightAmount * endColor.rgb ; //ambientAo * \n\n float diffuseWeight = dot(surfaceNormal, lightDirection);\n vec3 diffuse = diffuseLightAmount * endColor.rgb * clamp(diffuseWeight , 0.0, 1.0 );\n\n //specular\n \n vec4 specularColor = vec4(lightColor);\n vec3 eyeDirection = normalize(surfacePosition.xyz);\n vec3 reflectionDirection = reflect(-lightDirection, -surfaceNormal);\n float specularLightWeight = pow(max(dot(reflectionDirection, eyeDirection), 0.0), uMaterialShininess);\n vec3 specular = specularColor.rgb * specularLightWeight * specularLightAmount;\n\n /*float light2Multiplier = 0.2;\n float diffuseWeight2 = dot(surfaceNormal, vec3(-lightDirection.x, lightDirection.y, lightDirection.z));\n vec3 diffuse2 = diffuseLightAmount * endColor.rgb * clamp(diffuseWeight2 , 0.0, 1.0 ) * light2Multiplier;\n\n float light3Multiplier = 0.2; \n float diffuseWeight3 = dot(surfaceNormal, vec3(lightDirection.x, -lightDirection.y, lightDirection.z));\n vec3 diffuse3 = diffuseLightAmount * endColor.rgb * clamp(diffuseWeight3 , 0.0, 1.0 ) * light3Multiplier;\n\n float light4Multiplier = 0.2; \n float diffuseWeight4 = dot(surfaceNormal, vec3(-lightDirection.x, -lightDirection.y, lightDirection.z));\n vec3 diffuse4 = diffuseLightAmount * endColor.rgb * clamp(diffuseWeight4 , 0.0, 1.0 ) * light4Multiplier;*/\n \n gl_FragColor = vec4((ambient + diffuse + specular), endColor.a);\n //gl_FragColor = vec4((ambient + diffuse + diffuse2 + diffuse3 + diffuse4), endColor.a);\n}\n";module.exports={frag:vColorFrag,vert:vColorVert};
|
|
321
321
|
|
|
322
322
|
},{}],108:[function(require,module,exports){
|
|
323
323
|
const renderContext=require("./renderContext"),renderDefaults=require("./renderDefaults"),prepareRender=e=>{const r=Object.assign({},{},e.glOptions,{onDone:(e,r)=>{if(e)throw e}}),n=require("regl")(r),a=new Map;return function(e){var r;n.poll(),(r=e).rendering=Object.assign({},renderDefaults,r.rendering),renderContext(n)(r,e=>{n.clear({color:r.rendering.background,depth:1}),r.entities&&r.entities.sort((e,r)=>{const n="transparent"in e.visuals&&e.visuals.transparent;return n===("transparent"in r.visuals&&r.visuals.transparent)?0:n?1:-1}).forEach(e=>{const{visuals:t}=e;if((!("show"in t)||t.show)&&t.drawCmd&&r.drawCommands[t.drawCmd]){let s;t.cacheId?s=a.get(t.cacheId):(t.cacheId=a.size,s=r.drawCommands[t.drawCmd](n,e),a.set(t.cacheId,s)),s({...e,...t,camera:r.camera})}})})}};module.exports=prepareRender;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jscad/regl-renderer",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.4.0",
|
|
4
4
|
"description": "Renderer for JSCAD Geometries",
|
|
5
5
|
"repository": "https://github.com/jscad/OpenJSCAD.org",
|
|
6
6
|
"main": "src/index.js",
|
|
@@ -35,7 +35,7 @@
|
|
|
35
35
|
},
|
|
36
36
|
"devDependencies": {
|
|
37
37
|
"@jscad/img-utils": "2.0.1",
|
|
38
|
-
"@jscad/modeling": "2.5.
|
|
38
|
+
"@jscad/modeling": "2.5.2",
|
|
39
39
|
"ava": "3.15.0",
|
|
40
40
|
"browserify": "16.5.1",
|
|
41
41
|
"budo": "11.6.4",
|
|
@@ -43,5 +43,5 @@
|
|
|
43
43
|
"typescript": "^4.0.0",
|
|
44
44
|
"uglifyify": "5.0.2"
|
|
45
45
|
},
|
|
46
|
-
"gitHead": "
|
|
46
|
+
"gitHead": "a761f641bb2ac6016dc4a02ef86ce279dd553d7c"
|
|
47
47
|
}
|
|
@@ -5,11 +5,11 @@ const { meshColor } = require('../../renderDefaults')
|
|
|
5
5
|
const drawMesh = (regl, params = { extras: {} }) => {
|
|
6
6
|
const defaults = {
|
|
7
7
|
useVertexColors: true,
|
|
8
|
-
dynamicCulling:
|
|
8
|
+
dynamicCulling: true,
|
|
9
9
|
geometry: undefined,
|
|
10
10
|
color: meshColor
|
|
11
11
|
}
|
|
12
|
-
|
|
12
|
+
const { geometry, dynamicCulling, useVertexColors, color } = Object.assign({}, defaults, params)
|
|
13
13
|
|
|
14
14
|
// let ambientOcclusion = vao(geometry.indices, geometry.positions, 64, 64)
|
|
15
15
|
const ambientOcclusion = regl.buffer([])
|
|
@@ -18,15 +18,15 @@ const drawMesh = (regl, params = { extras: {} }) => {
|
|
|
18
18
|
const hasIndices = !!(geometry.indices && geometry.indices.length > 0)
|
|
19
19
|
const hasNormals = !!(geometry.normals && geometry.normals.length > 0)
|
|
20
20
|
const hasVertexColors = !!(useVertexColors && geometry.colors && geometry.colors.length > 0)
|
|
21
|
+
const transforms = geometry.transforms || mat4.create()
|
|
22
|
+
const flip = mat4.determinant(transforms) < 0
|
|
21
23
|
const cullFace = dynamicCulling
|
|
22
|
-
? (
|
|
23
|
-
const isOdd = ([props.model[0], props.model[5], props.model[10]].filter((x) => x < 0).length) & 1 // count the number of negative components & deterine if that is odd or even
|
|
24
|
-
return isOdd ? 'front' : 'back'
|
|
25
|
-
}
|
|
24
|
+
? (flip ? 'front' : 'back')
|
|
26
25
|
: 'back'
|
|
27
26
|
|
|
28
27
|
const vert = hasVertexColors ? require('./vColorShaders').vert : require('./meshShaders').vert
|
|
29
|
-
|
|
28
|
+
const frag = hasVertexColors ? require('./vColorShaders').frag : require('./meshShaders').frag
|
|
29
|
+
const modelMatrixInv = mat4.invert(mat4.create(), transforms)
|
|
30
30
|
|
|
31
31
|
// console.log('type', geometry.type, 'color', color, hasVertexColors)
|
|
32
32
|
|
|
@@ -36,18 +36,16 @@ const drawMesh = (regl, params = { extras: {} }) => {
|
|
|
36
36
|
frag,
|
|
37
37
|
|
|
38
38
|
uniforms: {
|
|
39
|
-
model: (context, props) =>
|
|
39
|
+
model: (context, props) => transforms ,
|
|
40
40
|
ucolor: (context, props) => (props && props.color) ? props.color : color,
|
|
41
|
-
// semi hack, woraround to enable/disable vertex colors!!!
|
|
41
|
+
// semi hack, woraround to enable/disable vertex colors !!!
|
|
42
42
|
vColorToggler: (context, props) => (props && props.useVertexColors && props.useVertexColors === true) ? 1.0 : 0.0,
|
|
43
43
|
// experimental
|
|
44
44
|
unormal: (context, props) => {
|
|
45
|
-
const
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
mat4.transpose(normalMatrix, normalMatrix)
|
|
50
|
-
return normalMatrix
|
|
45
|
+
const modelViewMatrix = mat4.invert(mat4.create(), props.camera.view)
|
|
46
|
+
mat4.multiply(modelViewMatrix, modelMatrixInv, modelViewMatrix)
|
|
47
|
+
mat4.transpose(modelViewMatrix, modelViewMatrix)
|
|
48
|
+
return modelViewMatrix
|
|
51
49
|
}
|
|
52
50
|
},
|
|
53
51
|
attributes: {
|
|
@@ -15,7 +15,7 @@ varying vec4 _worldSpacePosition;
|
|
|
15
15
|
varying vec4 vColor;
|
|
16
16
|
|
|
17
17
|
void main() {
|
|
18
|
-
surfacePosition = position;
|
|
18
|
+
surfacePosition = (unormal * vec4(position, 1.0)).xyz;
|
|
19
19
|
surfaceNormal = (unormal * vec4(normal, 1.0)).xyz; //vec4 transformedNormal = uNormalMatrix * vec4(aVertexNormal, 1.0);
|
|
20
20
|
vec4 worldSpacePosition = model * vec4(position, 1);
|
|
21
21
|
_worldSpacePosition = worldSpacePosition;
|
|
@@ -65,7 +65,7 @@ void main () {
|
|
|
65
65
|
|
|
66
66
|
vec4 specularColor = vec4(lightColor);
|
|
67
67
|
vec3 eyeDirection = normalize(surfacePosition.xyz);
|
|
68
|
-
vec3 reflectionDirection = reflect(-lightDirection, surfaceNormal);
|
|
68
|
+
vec3 reflectionDirection = reflect(-lightDirection, -surfaceNormal);
|
|
69
69
|
float specularLightWeight = pow(max(dot(reflectionDirection, eyeDirection), 0.0), uMaterialShininess);
|
|
70
70
|
vec3 specular = specularColor.rgb * specularLightWeight * specularLightAmount;
|
|
71
71
|
|