LuxCore icon indicating copy to clipboard operation
LuxCore copied to clipboard

Additional Math Texture Operations?

Open novacrazy opened this issue 7 years ago • 22 comments

It would be great to have a full set of math operations comparable to Cycles Math and Vector Math nodes, so equivalent nodes can then be added to BlendLuxCore and more complex procedural textures can be ported over.

Math operations in LuxCore:

  • [x] Add
  • [x] Subtract
  • [x] Multiply
  • [x] Clamp (Cycles nodes have this as a checkbox instead of a separate operation)
  • [x] Linear Interpolation "Mix" (which Cycles doesn't have built in)
  • [x] Absolute Value

Proposed new operations to add:

  • [x] Divide
  • [x] Remap (take a value and range, and map it to a new range)
  • [x] Power
  • [ ] Logarithm
  • [x] Rounding
  • [x] Modulo (fmod)

Trig functions:

  • [ ] Sine
  • [ ] Cosine
  • [ ] Tangent
  • [ ] Arcsine
  • [ ] Arccosine
  • [ ] Arctangent

Comparison operators:

  • [x] Less Than
  • [x] Greater Than
  • [ ] Minimum
  • [ ] Maximum
  • [ ] Equality

Vector math:

  • [x] Dot Product of Vectors
  • [ ] Cross Product of Vectors
  • [ ] Vector Normalization
  • [ ] Vector Length
  • [ ] Average of Vector elements
  • [ ] Mapping: World space to object space, etc.

Hitpoint information:

  • [x] Position
  • [x] Shading Normal
  • [ ] Geometry Normal

Additionally:

  • [x] Creating a Vector/Color from multiple Scalar values
  • [x] Breaking a Vector/Color into multiple Scalar values

While we're at it (sugar for certain equations):

  • [ ] Luma Calculations (Dot product of RGB color with a constant like These)
  • [ ] Built-in brightness/contrast operations for colors

If these suggestions are misguided, please let me know. I'd like to make LuxCore/BlendLuxCore my default renderer for work, and more procedural texture abilities would go a long way.


Want to back this issue? Post a bounty on it! We accept bounties via Bountysource.

novacrazy avatar Jun 02 '18 19:06 novacrazy

Good suggestion, I have something like this on my list since a while but did not find time for it yet.

Some comments:

  • [ ] Absolute Value

This is already implemented and available in BlendLuxCore.

  • [ ] Dot Product of Vectors
  • [ ] Cross Product of Vectors
  • [ ] Vector Normalization
  • [ ] Average of Vector elements

These would be kind of useless currently I fear, because we don't have any nodes that provide or accept vectors.

Theverat avatar Jun 02 '18 20:06 Theverat

This is already implemented and available in BlendLuxCore.

Oops, missed that somehow. Edited the issue.

These would be kind of useless currently I fear, because we don't have any nodes that provide or accept vectors.

They would be useful for plenty of things, especially in conjunction with the property node we've been talking about in LuxCoreRender/BlendLuxCore#167. You could take the position of two objects, subtract them to get a vector, then adjust some procedural texture based on that vector between two objects, either by distance or normalize it and using a 3D mapping.

I've actually done stuff like that once to get texture-only eyes on a cartoon character that still tracked a world object.

Or it could be used with some procedural effects with emissive materials when they get near objects. Or procedural normal maps. Or really anything artists can come up with.

Math is always useful, even indirectly.

novacrazy avatar Jun 02 '18 21:06 novacrazy

How would I go about adding these myself? I’ve looked at some of the existing stuff, but not all of it nor how it computes it on the GPU.

novacrazy avatar Jun 08 '18 13:06 novacrazy

You can look at the commits I made when I added the HSV texture: https://bitbucket.org/luxrender/luxrays/commits/all?search=hsv You need the header and source files for the texture, the parsing code in src/slg/scene/parsetextures.cpp, and a few smaller entries.

You don't have to write the OpenCL code immediately, do the C++ part first.

I have planned to write a "how to add a texture" tutorial like this one since ages, but I did not find the time yet unfortunately. I would be very helpful for new developers.

Theverat avatar Jun 08 '18 13:06 Theverat

A question for @Dade916: Since we are planning to add a lot of textures with rather simple operations, should we go a different route and put all of these operations into one texture (giant switch/case)? Which is better, also from the OpenCL perspective?

Theverat avatar Jul 20 '18 20:07 Theverat

Could you make a custom math node, where user could type in his/her own equation? It would be much more flexible. For beginners there could be a sheet with basic operations

EgertKanep avatar Aug 16 '18 09:08 EgertKanep

Could you make a custom math node, where user could type in his/her own equation?

You mean that the user types in a string? That would be possible, but this is stuff that the upper software layer (in our case, the BlendLuxCore addon) can handle. It would parse the string, build an AST and translate it to LuxCore texture nodes.

Theverat avatar Aug 16 '18 09:08 Theverat

To give a visual idea of my proposal I will make a reference to BlackmagicDesign Fusion. In the expression tab user can type in an operation for each individual channel, additionally they can route in another texture and use those values for doing operations as well. Probably we could use only 1 channel that would control rbg and leave alpha alone image

EgertKanep avatar Aug 16 '18 09:08 EgertKanep

I added a remap texture in commit cce9953a2d73738c3d1a70d9c797c12610b4a842.

Theverat avatar Sep 29 '18 17:09 Theverat

By the way, @Dade916: How should we add vector operations? Currently there are only interfaces for float and Spectrum datatypes, not Vector. Should it be added as a new function Texture::GetVectorValue()?

We could make the default in the parent Texture class something like:

Vector Texture::GetVectorValue(const HitPoint &hitPoint) const {
    const Spectrum color = GetSpectrumValue(hitPoint);
    return Vector(color.c[0], color.c[1], color.c[2]);
}

This would make all textures instantly vector-compatible. Only the new vector-related textures (e.g. DotProduct) would need to override GetVectorValue.

Or do you think we should just pass Vectors around as Spectrums, and only convert to Vector in the vector-related functions that need it?

Theverat avatar Sep 29 '18 17:09 Theverat

Would it not make more sense to use mathematical vectors and RGB colors (as vectors even) for the entirety of the procedural texture, and only converting it to a Spectrum type at the end? It’s just numbers until fed into the shaders, anyway.

novacrazy avatar Sep 29 '18 18:09 novacrazy

Spectrum is a typedef for RGB color in LuxCore currently: https://github.com/LuxCoreRender/LuxCore/blob/master/include/luxrays/core/color/color.h#L470

Theverat avatar Sep 29 '18 21:09 Theverat

I implemented the OpenCL version of the Divide texture: Hope I didn't forget anything, and sorry for the noisy diff: https://github.com/LuxCoreRender/LuxCore/commit/23db1881d9c4bbba9799749c1d03378f5259de73

Theverat avatar Oct 11 '18 21:10 Theverat

I guess we can now close this.

Dade916 avatar Oct 17 '18 16:10 Dade916

I would have left it open, there are still a lot of textures listed in the first post.

Theverat avatar Oct 17 '18 16:10 Theverat

No power or trig functions are definitely still a dealbreaker for me.

novacrazy avatar Oct 17 '18 17:10 novacrazy

Don't worry. I should have mentioned in my pull request that I will continue working on the new_math_textures branch.

Theverat avatar Oct 17 '18 20:10 Theverat

I'm continuing to implement the planned textures. @Dade916, is it ok if I put the existing math textures (scale, add, subtract etc.) into a "slg/textures/math/" directory? Also I want to expose some hitpoint properties: shading normal, geometry normal and position. Maybe I should create a "slg/textures/hitpoint/" directory for them, and also move the hitpointcolor textures there? I'm imagining something like this:

slg/
  textures/
    math/
      scale.h
      add.h
      subtract.h
      ...
    vectormath/
      dotproduct.h
      crossproduct.h
      normalize.h
      ...
    hitpoint/
      hitpointcolor.h
      shadingnormal.h
      geometrynormal.h
      position.h

Theverat avatar Mar 10 '19 16:03 Theverat

Sure.

Dade916 avatar Mar 11 '19 10:03 Dade916

@CatherineTower has implemented two new textures:

  • Rounding (#213)
  • Modulo (#214)

Theverat avatar Jun 12 '19 17:06 Theverat

I see on this list that "breaking a vector into scalars" is something that's marked as completed, but I'm having a hard time figuring out how to do that. What is that node called/how do I access it?

Diplodocee avatar Apr 24 '20 16:04 Diplodocee

They are the splitfloat3 and makefloat3 textures.

Dade916 avatar Apr 24 '20 16:04 Dade916