Giter Club home page Giter Club logo

Comments (11)

FarazzShaikh avatar FarazzShaikh commented on July 2, 2024

Hey! Thanks for using this!

there are a couple reasons why it wouldn’t work:

  1. You have to use the output variables provided by CustomShaderMaterial. They can be found here: https://github.com/FarazzShaikh/THREE-CustomShaderMaterial#output-variables
  2. MeshPhysicalMaterial requires Lights in the scene, so make sure you have lights

from three-customshadermaterial.

Ettur avatar Ettur commented on July 2, 2024

I changed shaders to use csm_ variables but no change.
I do have lights, PhysicalMaterial shows empty.
With ShaderMaterial I can see red cube. uTime is not coming through

export const vertex =`
      precision mediump float;
      uniform float uTime;

      void main(){
        vec4 pos = vec4(position, 1.0);
        vec4 modelPosition = modelMatrix * pos;
        vec4 viewPosition = viewMatrix * modelPosition;
        vec4 projectedPosition = projectionMatrix * viewPosition;

        csm_Position = position;
      }
    `
export const fragment = `
      precision mediump float;
      uniform float uTime;

      void main() {

        vec3 color = vec3(0.0, 0.0, sin(uTime));

        csm_DiffuseColor = vec4(color, 1.0);
      }
    `
export const LandingPage = ():JSX.Element => {
    return <div className="landing-container">
        <div className='canvas-container'>
            <Canvas>
                <ambientLight/>
                <pointLight position={[2, 0, 0]} intensity={10}/>
                <Box position={[1.2, 0, 0]} />
            </Canvas>
        </div>
    </div>
}

from three-customshadermaterial.

Ettur avatar Ettur commented on July 2, 2024

I added lights like in your example, I do see it now with physical material. And uTIme is working

from three-customshadermaterial.

Ettur avatar Ettur commented on July 2, 2024

If I may ask, how can i map texture to this material ?

from three-customshadermaterial.

Ettur avatar Ettur commented on July 2, 2024

This is what I tried to to for texture

function Box(props: JSX.IntrinsicElements['mesh']) {
  const materialRef = useRef<CustomShaderMaterialType | null>(null)

  const texture = new THREE.TextureLoader
  const projects = texture.load('/1.jpg')

  console.log(projects)



  useFrame(({clock}) => {
    if (materialRef.current) {
      materialRef.current.uniforms.uTime.value = clock.getElapsedTime()
    }
  })

  return (
    <mesh>
      <boxGeometry args={[1, 1, 1]} />
      <CustomShaderMaterial
        ref={materialRef}
        baseMaterial={MeshPhysicalMaterial}
        vertexShader={vertex}
        fragmentShader={fragment}
        uniforms={{
          uTime: {
            value: 0,
          },
          uTexture: {
            value: projects
          }
        }}
        flatShading
        map={projects}
        // ...
      />
    </mesh>
  )
}
export const vertex =`
      precision mediump float;
      uniform float uTime;
      varying vec2 vUv;

      void main(){
        vUv = uv;
        csm_Position = position;
      }
    `
export const fragment = `
      precision mediump float;
      uniform float uTime;
      uniform sampler2D uTexture;

      varying vec2 vUv;


      void main() {

        vec3 color = vec3(0.0, 0.0, sin(uTime * 0.1));

        csm_DiffuseColor = texture2D(uTexture, vUv);
      }
    `

I get error in vertex shader, it's saying redefinition of vUv ?

from three-customshadermaterial.

Ettur avatar Ettur commented on July 2, 2024

seems like vUv was a sepcial variable ? I changed it to vTUv and It worked.

from three-customshadermaterial.

FarazzShaikh avatar FarazzShaikh commented on July 2, 2024

Glad it worked!

yes vUv is defined by Physical material, since this is extending that, you can’t reuse it

from three-customshadermaterial.

Ettur avatar Ettur commented on July 2, 2024

Offtopic but, im learning GLSL shaders right now and I'm following a tutorial.
Using step function I should be drawing a yellow circle in the middle of my texture, however I get screen all black when I use this fragment shader.

export const vertex =`
      precision mediump float;
      uniform float uTime;

      varying vec2 v_uv;
      varying vec3 v_position;


      void main(){
        v_position = position;
        v_uv = uv;
        csm_Position = position;
      }
    `
export const fragment = `
      precision mediump float;
      uniform float uTime;
      uniform sampler2D uTexture;

      varying vec2 v_uv;
      varying vec3 v_position;


      void main() {

      float inCircle = 1.0 - step(0.5, length(v_position.xy));
      vec3 color = vec3(1.0, 1.0, 0.0) * inCircle;

      csm_DiffuseColor = texture2D(uTexture, v_uv) * vec4(color, 1.0);
      }
    `

what is csm_DiffuseColor doing extra if it is not just gl_FragColor ?

from three-customshadermaterial.

FarazzShaikh avatar FarazzShaikh commented on July 2, 2024

As it says, it’s just the “diffuse color”, this way Physical material’s Roughness, metalness, etc still work when you override the color.

option to override gl_FragColor will be added soon but it would defeat the purpose of this material.

from three-customshadermaterial.

Ettur avatar Ettur commented on July 2, 2024

Hi again, thought I'd write here instead of opening a new issue.
Im using drei to render Text and Text component can take a shader property like so


const AddText = () => {
    return <mesh>
        <Text 
          color="red" 
          anchorX="center" 
          anchorY="middle"
          material={CustomShaderMaterial}
        >
          HELLO
        </Text>
    </mesh>
  }

Problem is that it expects material to come from one of THREE materials and typescript wont recognize CSM as valid, error i get is that

Type 'ForwardRefExoticComponent<Pick<iCSMAllProps, "attach" | "attachArray" | "attachObject" | "attachFns" | "args" | "children" | "key" | "onUpdate" | "dispose" | "type" | "id" | "uuid" | "name" | ... 116 more ... | "uniforms"> & RefAttributes<...>>' is not assignable to type 'Material | Material[] | undefined'.
  Type 'ForwardRefExoticComponent<Pick<iCSMAllProps, "attach" | "attachArray" | "attachObject" | "attachFns" | "args" | "children" | "key" | "onUpdate" | "dispose" | "type" | "id" | "uuid" | "name" | ... 116 more ... | "uniforms"> & RefAttributes<...>>' is missing the following properties from type 'Material[]': pop, push, concat, join, and 28 more.ts(2322)

The expected type comes from property 'material' which is declared here on type 'IntrinsicAttributes & Pick<Omit<ExtendedColors<Overwrite<Partial<Mesh<BufferGeometry, Material | Material[]>>, NodeProps<Mesh<BufferGeometry, Material | Material[]>, typeof Mesh>>>, NonFunctionKeys<...>> & { ...; } & EventHandlers & { ...; }, "attach" | ... 111 more ... | "debugSDF"> & RefAttributes<...>'

is there a way I could make this attachment happen ?

from three-customshadermaterial.

FarazzShaikh avatar FarazzShaikh commented on July 2, 2024

Are you using CSM From three-custom-shader-material/vanilla? You must use the
CSM class from there if you want to use it imperatively

CSM extends the Material base class like all other THREE materials, so there’s no reason it shouldn’t work

from three-customshadermaterial.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.